Skip to content

Commit c2cc52b

Browse files
committed
Add isSelected to segment and optimize
1 parent 5f00426 commit c2cc52b

7 files changed

Lines changed: 30 additions & 14 deletions

File tree

.DS_Store

6 KB
Binary file not shown.

Example/.DS_Store

6 KB
Binary file not shown.
6 KB
Binary file not shown.

Example/CustomizableSegmentedControlExample/CustomizableSegmentedControlExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/CustomizableSegmentedControlExample/CustomizableSegmentedControlExample/CustomizableSegmentedControlExampleView.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct CustomizableSegmentedControlExampleView: View {
3636
selection: $selection,
3737
options: options,
3838
selectionView: selectionView(),
39-
segmentContent: { option, isPressed in
39+
segmentContent: { option, _, isPressed in
4040
segmentView(title: option.title, imageName: option.imageName, isPressed: isPressed)
4141
.colorMultiply(selection == option ? Color.black : .white)
4242
.animation(.default, value: selection)
@@ -60,7 +60,7 @@ struct CustomizableSegmentedControlExampleView: View {
6060
selection: $selection,
6161
options: options,
6262
selectionView: selectionView(),
63-
segmentContent: { option, isPressed in
63+
segmentContent: { option, _, isPressed in
6464
segmentView(title: option.title, imageName: option.imageName, isPressed: isPressed)
6565
}
6666
)
@@ -117,7 +117,7 @@ struct CustomizableSegmentedControlExampleView: View {
117117
selection: $animationSelection,
118118
options: animationOptions,
119119
selectionView: selectionView(color: .purple),
120-
segmentContent: { option, isPressed in
120+
segmentContent: { option, _, isPressed in
121121
segmentView(title: option.title, imageName: nil, isPressed: isPressed)
122122
}
123123
)
@@ -177,3 +177,8 @@ private extension CustomizableSegmentedControlExampleView {
177177
}
178178

179179
}
180+
181+
@available(iOS 17.0, *)
182+
#Preview {
183+
CustomizableSegmentedControlExampleView()
184+
}

Sources/.DS_Store

6 KB
Binary file not shown.

Sources/CustomizableSegmentedControl/CustomizableSegmentedControl.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct CustomizableSegmentedControl<Option: Hashable & Identifiable, Sele
1414
@Binding private var selection: Option
1515
private let options: [Option]
1616
private let selectionView: () -> SelectionView
17-
private let segmentContent: (Option, Bool) -> SegmentContent
17+
private let segmentContent: (_ option: Option, _ isSelected: Bool, _ isPressed: Bool) -> SegmentContent
1818

1919
@State private var optionIsPressed: [Option.ID: Bool] = [:]
2020

@@ -41,13 +41,12 @@ public struct CustomizableSegmentedControl<Option: Hashable & Identifiable, Sele
4141
selection: Binding<Option>,
4242
options: [Option],
4343
selectionView: @escaping () -> SelectionView,
44-
@ViewBuilder segmentContent: @escaping (Option, Bool) -> SegmentContent
44+
@ViewBuilder segmentContent: @escaping (_ option: Option, _ isSelected: Bool, _ isPressed: Bool) -> SegmentContent
4545
) {
4646
self._selection = selection
4747
self.options = options
4848
self.selectionView = selectionView
4949
self.segmentContent = segmentContent
50-
self.optionIsPressed = Dictionary(uniqueKeysWithValues: options.lazy.map { ($0.id, false) })
5150
}
5251

5352
// MARK: - UI
@@ -56,17 +55,14 @@ public struct CustomizableSegmentedControl<Option: Hashable & Identifiable, Sele
5655
HStack(spacing: interSegmentSpacing) {
5756
ForEach(Array(zip(options.indices, options)), id: \.1.id) { index, option in
5857
Segment(
59-
content: segmentContent(option, optionIsPressed[option.id, default: false]),
58+
content: segmentContent(option, option.id == selection.id, optionIsPressed[option.id, default: false]),
6059
selectionView: selectionView(),
6160
isSelected: selection == option,
6261
animation: slidingAnimation,
6362
contentBlendMode: contentStyle.contentBlendMode,
6463
firstLevelOverlayBlendMode: contentStyle.firstLevelOverlayBlendMode,
6564
highestLevelOverlayBlendMode: contentStyle.highestLevelOverlayBlendMode,
66-
isPressed: .init(
67-
get: { optionIsPressed[option.id, default: false] },
68-
set: { optionIsPressed[option.id] = $0 }
69-
),
65+
isPressed: $optionIsPressed[option.id],
7066
backgroundID: buttonBackgroundID,
7167
namespaceID: namespaceID,
7268
accessibiltyValue: segmentAccessibilityValueCompletion(index + 1, options.count),
@@ -95,7 +91,7 @@ extension CustomizableSegmentedControl {
9591
let contentBlendMode: BlendMode?
9692
let firstLevelOverlayBlendMode: BlendMode?
9793
let highestLevelOverlayBlendMode: BlendMode?
98-
@Binding var isPressed: Bool
94+
@Binding var isPressed: Bool?
9995
let backgroundID: String
10096
let namespaceID: Namespace.ID
10197
let accessibiltyValue: String
@@ -159,7 +155,7 @@ extension CustomizableSegmentedControl {
159155
selection: Binding<Option>,
160156
options: [Option],
161157
selectionView: SelectionView,
162-
@ViewBuilder segmentContent: @escaping (Option, Bool) -> SegmentContent
158+
@ViewBuilder segmentContent: @escaping (_ option: Option, _ isSelected: Bool, _ isPressed: Bool) -> SegmentContent
163159
) {
164160
self.init(
165161
selection: selection,
@@ -177,7 +173,7 @@ extension CustomizableSegmentedControl.Segment {
177173

178174
private struct SegmentButtonStyle: ButtonStyle {
179175

180-
@Binding var isPressed: Bool
176+
@Binding var isPressed: Bool?
181177

182178
func makeBody(configuration: Configuration) -> some View {
183179
configuration.label

0 commit comments

Comments
 (0)