@@ -27,15 +27,24 @@ import SwiftUI
27
27
28
28
public struct AxisTabView < SelectionValue, Background, Content> : View where SelectionValue : Hashable , Background : View , Content : View {
29
29
30
- private let viewModel : ATViewModel < SelectionValue >
31
30
@StateObject private var stateViewModel : ATStateViewModel < SelectionValue > = . init( )
32
-
31
+ private let viewModel : ATViewModel < SelectionValue >
32
+ private var selection : Binding < SelectionValue > { Binding (
33
+ get: { self . viewModel. selection } ,
34
+ set: {
35
+ self . onTapReceive ? ( $0)
36
+ self . viewModel. selection = $0
37
+ }
38
+ )
39
+ }
40
+
33
41
/// Defines the settings for the tab view.
34
42
private let constant : ATConstant
35
43
36
44
/// The style of the background view.
37
45
public var background : ( ( ATTabState ) -> Background )
38
46
public var content : ( ) -> Content
47
+ public var onTapReceive : ( ( SelectionValue ) -> Void ) ?
39
48
40
49
public var body : some View {
41
50
GeometryReader { proxy in
@@ -47,15 +56,15 @@ public struct AxisTabView<SelectionValue, Background, Content> : View where Sele
47
56
}
48
57
. overlayPreferenceValue ( ATTabItemPreferenceKey . self) { items in
49
58
let items = items. prefix ( getLimitItemCount ( size: proxy. size, itemCount: items. count) )
50
- let state = ATTabState ( constant: constant, itemCount: items. count, previousIndex: stateViewModel. previousIndex, currentIndex: stateViewModel. indexOfTag ( viewModel . selection) , size: proxy. size, safeAreaInsets: proxy. safeAreaInsets)
59
+ let state = ATTabState ( constant: constant, itemCount: items. count, previousIndex: stateViewModel. previousIndex, currentIndex: stateViewModel. indexOfTag ( selection. wrappedValue ) , size: proxy. size, safeAreaInsets: proxy. safeAreaInsets)
51
60
VStack ( spacing: 0 ) {
52
61
if constant. axisMode == . bottom {
53
62
Spacer ( )
54
63
}
55
64
getTabContent ( Array ( items) )
56
65
. frame ( width: proxy. size. width, height: constant. tab. normalSize. height)
57
66
. padding ( edgeSet, getSafeArea ( proxy) )
58
- . animation ( constant. tab. animation ?? . none, value: viewModel . selection)
67
+ . animation ( constant. tab. animation ?? . none, value: self . selection. wrappedValue )
59
68
. background ( background ( state) )
60
69
if constant. axisMode == . top {
61
70
Spacer ( )
@@ -76,7 +85,7 @@ public struct AxisTabView<SelectionValue, Background, Content> : View where Sele
76
85
77
86
//MARK: - Methods
78
87
private func getItemWidth( tag: SelectionValue ) -> CGFloat {
79
- if tag == self . viewModel . selection {
88
+ if tag == self . selection. wrappedValue {
80
89
if constant. tab. selectWidth > 0 {
81
90
return constant. tab. selectWidth
82
91
}
@@ -89,7 +98,7 @@ public struct AxisTabView<SelectionValue, Background, Content> : View where Sele
89
98
ForEach ( Array ( items. enumerated ( ) ) , id: \. offset) { index, item in
90
99
if constant. tab. spacingMode == . center {
91
100
ZStack {
92
- if item. tag as! SelectionValue == viewModel . selection {
101
+ if item. tag as! SelectionValue == self . selection. wrappedValue {
93
102
item. select
94
103
. transition ( constant. tab. transition)
95
104
} else {
@@ -101,7 +110,7 @@ public struct AxisTabView<SelectionValue, Background, Content> : View where Sele
101
110
height: constant. tab. normalSize. height)
102
111
. onTapGesture {
103
112
if let tag = item. tag as? SelectionValue {
104
- self . viewModel . selection = tag
113
+ self . selection. wrappedValue = tag
105
114
if constant. tab. activeVibration { vibration ( ) }
106
115
}
107
116
}
@@ -111,7 +120,7 @@ public struct AxisTabView<SelectionValue, Background, Content> : View where Sele
111
120
} else {
112
121
Spacer ( )
113
122
ZStack {
114
- if item. tag as! SelectionValue == viewModel . selection {
123
+ if item. tag as! SelectionValue == self . selection. wrappedValue {
115
124
item. select
116
125
. transition ( constant. tab. transition)
117
126
} else {
@@ -123,7 +132,7 @@ public struct AxisTabView<SelectionValue, Background, Content> : View where Sele
123
132
height: constant. tab. normalSize. height)
124
133
. onTapGesture {
125
134
if let tag = item. tag as? SelectionValue {
126
- self . viewModel . selection = tag
135
+ self . selection. wrappedValue = tag
127
136
if constant. tab. activeVibration { vibration ( ) }
128
137
}
129
138
}
@@ -175,11 +184,13 @@ public extension AxisTabView where SelectionValue: Hashable, Background: View, C
175
184
/// - constant: Defines the settings for the tab view.
176
185
/// - background: The style of the background view.
177
186
/// - content: Content views with tab items applied.
178
- init ( selection: Binding < SelectionValue > , constant: ATConstant = . init( ) , @ViewBuilder background: @escaping ( ATTabState ) -> Background , @ViewBuilder content: @escaping ( ) -> Content ) {
187
+ /// - onTapReceive: Method that treats the currently selected tab as imperative syntax.
188
+ init ( selection: Binding < SelectionValue > , constant: ATConstant = . init( ) , @ViewBuilder background: @escaping ( ATTabState ) -> Background , @ViewBuilder content: @escaping ( ) -> Content , onTapReceive: ( ( SelectionValue ) -> Void ) ? = nil ) {
179
189
self . viewModel = ATViewModel ( selection: selection, constant: constant)
180
190
self . background = background
181
191
self . constant = constant
182
192
self . content = content
193
+ self . onTapReceive = onTapReceive
183
194
}
184
195
}
185
196
0 commit comments