@@ -13,27 +13,28 @@ public struct AdvancedList<Data: RandomAccessCollection, Content: View, EmptySta
1313 public typealias OnMoveAction = Optional < ( IndexSet , Int ) -> Void >
1414 public typealias OnDeleteAction = Optional < ( IndexSet ) -> Void >
1515
16+ private typealias Configuration = ( AnyDynamicViewContent ) -> AnyDynamicViewContent
17+
1618 @ObservedObject private var pagination : AdvancedListPagination < PaginationErrorView , PaginationLoadingView >
1719 private var data : Data
1820 private var content : ( Data . Element ) -> Content
1921 private var listState : Binding < ListState >
20- private var onMoveAction : OnMoveAction = nil
21- private var onDeleteAction : OnDeleteAction = nil
2222 private let emptyStateView : ( ) -> EmptyStateView
2323 private let errorStateView : ( Error ) -> ErrorStateView
2424 private let loadingStateView : ( ) -> LoadingStateView
2525 @State private var isLastItem : Bool = false
2626
27- public init ( _ data: Data , @ViewBuilder content: @escaping ( Data . Element ) -> Content , listState: Binding < ListState > , onMoveAction: OnMoveAction = nil , onDeleteAction: OnDeleteAction = nil , @ViewBuilder emptyStateView: @escaping ( ) -> EmptyStateView , @ViewBuilder errorStateView: @escaping ( Error ) -> ErrorStateView , @ViewBuilder loadingStateView: @escaping ( ) -> LoadingStateView , pagination: AdvancedListPagination < PaginationErrorView , PaginationLoadingView > ) {
27+ private var configurations : [ Configuration ]
28+
29+ public init ( _ data: Data , @ViewBuilder content: @escaping ( Data . Element ) -> Content , listState: Binding < ListState > , @ViewBuilder emptyStateView: @escaping ( ) -> EmptyStateView , @ViewBuilder errorStateView: @escaping ( Error ) -> ErrorStateView , @ViewBuilder loadingStateView: @escaping ( ) -> LoadingStateView , pagination: AdvancedListPagination < PaginationErrorView , PaginationLoadingView > ) {
2830 self . data = data
2931 self . content = content
3032 self . listState = listState
31- self . onMoveAction = onMoveAction
32- self . onDeleteAction = onDeleteAction
3333 self . emptyStateView = emptyStateView
3434 self . errorStateView = errorStateView
3535 self . loadingStateView = loadingStateView
3636 self . pagination = pagination
37+ configurations = [ ]
3738 }
3839}
3940
@@ -62,13 +63,31 @@ extension AdvancedList {
6263 }
6364}
6465
66+ // MARK: - View modifiers
67+ extension AdvancedList {
68+ public func onMove( perform action: OnMoveAction ) -> Self {
69+ configure { AnyDynamicViewContent ( $0. onMove ( perform: action) ) }
70+ }
71+
72+ public func onDelete( perform action: OnDeleteAction ) -> Self {
73+ configure { AnyDynamicViewContent ( $0. onDelete ( perform: action) ) }
74+ }
75+ }
76+
77+ // MARK: - Private helper
6578extension AdvancedList {
79+ private func configure( _ configuration: @escaping Configuration ) -> Self {
80+ var result = self
81+ result. configurations. append ( configuration)
82+ return result
83+ }
84+
6685 private func getListView( ) -> some View {
6786 List {
68- ForEach ( data ) { item in
69- self . getItemView ( item )
70- } . onMove ( perform : self . onMoveAction )
71- . onDelete ( perform : self . onDeleteAction )
87+ configurations
88+ . reduce ( AnyDynamicViewContent ( ForEach ( data ) { item in
89+ self . getItemView ( item )
90+ } ) ) { ( currentView , configuration ) in configuration ( currentView ) }
7291 }
7392 }
7493
@@ -121,7 +140,7 @@ struct AdvancedList_Previews : PreviewProvider {
121140 let id : String = UUID ( ) . uuidString
122141 }
123142
124- private static let items : [ MockItem ] = [ ]
143+ private static let items : [ MockItem ] = Array ( 0 ... 5 ) . map { _ in MockItem ( ) }
125144 @State private static var listState : ListState = . items
126145
127146 static var previews : some View {
0 commit comments