Skip to content

Commit

Permalink
fix(SegmentSelector): Fix segment selection after theme changes (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmpg93 committed Sep 6, 2021
1 parent 211eb4d commit 27afa2e
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Mistica/Source/Components/Checkbox/Checkbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private extension Checkbox {
imageView.layer.opacity = 1
imageView.layer.transform = CATransform3DIdentity
borderView.layer.borderColor = UIColor.controlActivated.cgColor
borderView.layer.borderWidth = intrinsicContentSize.width / 2.0
borderView.layer.borderWidth = max(bounds.width, intrinsicContentSize.width) / 2.0
} else {
imageView.layer.opacity = 0
imageView.layer.transform = CATransform3DScale(imageView.layer.transform, 0.01, 0.01, 0.01)
Expand Down
10 changes: 9 additions & 1 deletion Mistica/Source/Components/SegmentSelector/SegmentSelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public class SegmentSelector: UIView {
themeDidChange()
}
}

override public var intrinsicContentSize: CGSize {
collectionView.intrinsicContentSize
}
}

// MARK: Public API
Expand Down Expand Up @@ -166,7 +170,7 @@ extension SegmentSelector: UICollectionViewDataSource {
// MARK: UICollectionViewDelegate

extension SegmentSelector: UICollectionViewDelegate {
public func collectionView(_: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
public func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
// We allow selection only when the cell is not already selected.
indexPath.item != selectedSegmentIndex
}
Expand Down Expand Up @@ -326,7 +330,11 @@ private extension SegmentSelector {
}

func reloadContent() {
let selectedSegment = self.selectedSegment
collectionView.reloadData()
if let selectedSegment = selectedSegment {
select(selectedSegment)
}
scrollToFirstItemIfNeeded()
}

Expand Down
43 changes: 43 additions & 0 deletions Mistica/Tests/MisticaTests/UI/SegmentSelectorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// SegmentSelectorTests.swift
//
// Made with ❤️ by Novum
//
// Copyright © Telefonica. All rights reserved.
//

@testable import Mistica
import SnapshotTesting
import XCTest

final class SegmentSelectorTests: XCTestCase {
override func setUp() {
super.setUp()
UIView.setAnimationsEnabled(false)

isRecording = false
}

func testSegmentsInSegmentSelector() {
assertSnapshotForAllBrandsAndStyles(
as: .image(size: CGSize(width: 320, height: 49)),
viewBuilder: makeSegmentSelector(segments: 5, hasSelectedItem: false)
)
}

func testSelectedSegmentsInSegmentSelector() {
assertSnapshotForAllBrandsAndStyles(
as: .image(size: CGSize(width: 320, height: 49)),
viewBuilder: makeSegmentSelector(segments: 5, hasSelectedItem: true)
)
}

func makeSegmentSelector(segments: Int, hasSelectedItem: Bool) -> SegmentSelector {
let segments = (0 ..< segments).map { Segment(id: "\($0)", title: "Segment") }
let segmentSelector = SegmentSelector(segments: segments)
if hasSelectedItem, let segment = segments.first {
segmentSelector.select(segment)
}
return segmentSelector
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 27afa2e

Please sign in to comment.