@@ -12,7 +12,7 @@ public enum MenuType {
1212public enum SwipeRole {
1313 case destructive /// for removing element
1414 case cancel
15- case defaults
15+ case `default`
1616}
1717
1818/// For opened cells auto-hiding during swiping anothers
@@ -27,8 +27,10 @@ private enum VisibleButton: Equatable {
2727 case right( UUID )
2828}
2929
30- struct SwipeAction < V1: View , V2: View > : ViewModifier {
30+ public struct SwipeAction < V1: View , V2: View > : ViewModifier {
3131
32+ @Environment ( \. layoutDirection) var layoutDirection
33+
3234 @Binding private var state : SwipeState
3335 @State private var offset : CGFloat = 0
3436 @State private var oldOffset : CGFloat = 0
@@ -64,7 +66,7 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
6466 init (
6567 menu: MenuType ,
6668 allowsFullSwipe: Bool = false ,
67- fullSwipeRole: SwipeRole = . defaults ,
69+ fullSwipeRole: SwipeRole = . default ,
6870 swipeColor: Color ? = nil ,
6971 state: Binding < SwipeState > ,
7072 @ViewBuilder _ content: @escaping ( ) -> TupleView < ( Leading < V1 > , Trailing < V2 > ) > ,
@@ -83,7 +85,7 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
8385 init (
8486 menu: MenuType ,
8587 allowsFullSwipe: Bool = false ,
86- fullSwipeRole: SwipeRole = . defaults ,
88+ fullSwipeRole: SwipeRole = . default ,
8789 swipeColor: Color ? = nil ,
8890 state: Binding < SwipeState > ,
8991 @ViewBuilder leading: @escaping ( ) -> V1 ,
@@ -102,7 +104,7 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
102104 init (
103105 menu: MenuType ,
104106 allowsFullSwipe: Bool = false ,
105- fullSwipeRole: SwipeRole = . defaults ,
107+ fullSwipeRole: SwipeRole = . default ,
106108 swipeColor: Color ? = nil ,
107109 state: Binding < SwipeState > ,
108110 @ViewBuilder trailing: @escaping ( ) -> V2 ,
@@ -118,13 +120,13 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
118120 self . action = action
119121 }
120122
121- func reset( ) {
123+ private func reset( ) {
122124 visibleButton = . none
123125 offset = 0
124126 oldOffset = 0
125127 }
126128
127- var leadingView : some View {
129+ private var leadingView : some View {
128130 leadingSwipeView
129131 . measureSize {
130132 if !maxLeadingOffsetIsCounted {
@@ -141,7 +143,7 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
141143 }
142144 }
143145
144- var trailingView : some View {
146+ private var trailingView : some View {
145147 trailingSwipeView
146148 . measureSize {
147149 if !minTrailingOffsetIsCounted {
@@ -158,7 +160,7 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
158160 }
159161 }
160162
161- var swipedMenu : some View {
163+ private var swipedMenu : some View {
162164 HStack ( spacing: 0 ) {
163165 leadingView
164166 Spacer ( )
@@ -167,7 +169,7 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
167169 }
168170 }
169171
170- var slidedMenu : some View {
172+ private var slidedMenu : some View {
171173 HStack ( spacing: 0 ) {
172174 leadingView
173175 . offset ( x: ( - 1 * maxLeadingOffset) + offset)
@@ -177,24 +179,28 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
177179 }
178180 }
179181
180- func gesturedContent( content: Content ) -> some View {
181-
182+ private func gesturedContent( content: Content ) -> some View {
182183 content
183184 . id ( id)
184185 . contentShape ( Rectangle ( ) ) ///otherwise swipe won't work in vacant area
185186 . offset ( x: offset)
186187 . measureSize {
187188 contentWidth = $0. width
188189 }
189- . gesture (
190- // DragGesture(minimumDistance: 15, coordinateSpace: .local)
190+ . gesture (
191191 DragGesture ( minimumDistance: 15 , coordinateSpace: . global)
192192 . updating ( $dragGestureActive) { value, state, transaction in
193193 state = true
194194 }
195195 . onChanged { value in
196- let totalSlide = value . translation . width + oldOffset
196+ let totalSlide : Double
197197
198+ switch layoutDirection {
199+ case . rightToLeft:
200+ totalSlide = - value. translation. width - oldOffset
201+ default :
202+ totalSlide = value. translation. width + oldOffset
203+ }
198204 if allowsFullSwipe && ... 0 ~= Int ( totalSlide) {
199205 withAnimation {
200206 offset = totalSlide
@@ -205,17 +211,26 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
205211 }
206212 }
207213 } . onEnded { value in
214+ let translationWidth : Double
215+
216+ switch layoutDirection {
217+ case . rightToLeft:
218+ translationWidth = - value. translation. width
219+ default :
220+ translationWidth = value. translation. width
221+ }
222+
208223 withAnimation {
209224 if
210225 ///user dismisses right buttons
211226 visibleButton == . left( id) ,
212- value . translation . width < - 20
227+ translationWidth < - 20
213228 {
214229 reset ( )
215230 } else if
216231 ///user dismisses right buttons
217232 visibleButton == . right( id) ,
218- value . translation . width > 20
233+ translationWidth > 20
219234 {
220235 reset ( )
221236 } else if
@@ -239,7 +254,7 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
239254
240255 if
241256 allowsFullSwipe,
242- value . translation . width < - ( contentWidth * 0.7 )
257+ translationWidth < - ( contentWidth * 0.7 )
243258 {
244259 withAnimation ( . linear( duration: 0.3 ) ) {
245260 offset = - contentWidth
@@ -260,9 +275,10 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
260275
261276 action ? ( )
262277 }
263- } )
264- . valueChanged ( of: dragGestureActive) { dragActive in
265- print ( " id: \( id) state: \( state) visibleButton: \( visibleButton) offset: \( offset) " )
278+ }
279+ )
280+ . valueChanged ( of: dragGestureActive) { _ in
281+ //print("id: \(id) state: \(state) visibleButton: \(visibleButton) offset: \(offset)")
266282 }
267283 . valueChanged ( of: state) { value in
268284 switch value {
@@ -338,3 +354,4 @@ struct SwipeAction<V1: View, V2: View>: ViewModifier {
338354 }
339355 }
340356}
357+
0 commit comments