|
9 | 9 | import ListPagination |
10 | 10 | import SwiftUI |
11 | 11 |
|
| 12 | +#if !targetEnvironment(macCatalyst) |
12 | 13 | public struct AdvancedList<EmptyStateView: View, ErrorStateView: View, LoadingStateView: View, PaginationErrorView: View, PaginationLoadingView: View> : View { |
13 | | - #if !targetEnvironment(macCatalyst) |
14 | 14 | @ObservedObject private var listService: ListService |
15 | 15 | @ObservedObject private var pagination: AdvancedListPagination<PaginationErrorView, PaginationLoadingView> |
16 | | - #endif |
| 16 | + private let emptyStateView: () -> EmptyStateView |
| 17 | + private let errorStateView: (Error) -> ErrorStateView |
| 18 | + private let loadingStateView: () -> LoadingStateView |
| 19 | + @State private var isLastItem: Bool = false |
17 | 20 |
|
| 21 | + public init(listService: ListService, @ViewBuilder emptyStateView: @escaping () -> EmptyStateView, @ViewBuilder errorStateView: @escaping (Error) -> ErrorStateView, @ViewBuilder loadingStateView: @escaping () -> LoadingStateView, pagination: AdvancedListPagination<PaginationErrorView, PaginationLoadingView>) { |
| 22 | + self.listService = listService |
| 23 | + self.emptyStateView = emptyStateView |
| 24 | + self.errorStateView = errorStateView |
| 25 | + self.loadingStateView = loadingStateView |
| 26 | + self.pagination = pagination |
| 27 | + } |
| 28 | +} |
| 29 | +#else |
| 30 | +public struct AdvancedList<EmptyStateView: View, ErrorStateView: View, LoadingStateView: View> : View { |
18 | 31 | private let emptyStateView: () -> EmptyStateView |
19 | 32 | private let errorStateView: (Error) -> ErrorStateView |
20 | 33 | private let loadingStateView: () -> LoadingStateView |
21 | 34 | @State private var isLastItem: Bool = false |
22 | 35 |
|
23 | | - #if targetEnvironment(macCatalyst) |
24 | 36 | @EnvironmentObject var listService: ListService |
25 | | - @EnvironmentObject var pagination: AdvancedListPagination<PaginationErrorView, PaginationLoadingView> |
26 | | - #endif |
| 37 | + /* |
| 38 | + We have to erase the types of the pagination error and loading view |
| 39 | + because currently there is no way to specify the types |
| 40 | + */ |
| 41 | + @EnvironmentObject var pagination: AdvancedListPagination<AnyView, AnyView> |
27 | 42 |
|
| 43 | + public init(@ViewBuilder emptyStateView: @escaping () -> EmptyStateView, @ViewBuilder errorStateView: @escaping (Error) -> ErrorStateView, @ViewBuilder loadingStateView: @escaping () -> LoadingStateView) { |
| 44 | + self.emptyStateView = emptyStateView |
| 45 | + self.errorStateView = errorStateView |
| 46 | + self.loadingStateView = loadingStateView |
| 47 | + } |
| 48 | +} |
| 49 | +#endif |
| 50 | + |
| 51 | +extension AdvancedList { |
28 | 52 | public var body: AnyView { |
29 | 53 | switch listService.listState { |
30 | 54 | case .error(let error): |
@@ -62,22 +86,6 @@ public struct AdvancedList<EmptyStateView: View, ErrorStateView: View, LoadingSt |
62 | 86 | ) |
63 | 87 | } |
64 | 88 | } |
65 | | - |
66 | | - #if !targetEnvironment(macCatalyst) |
67 | | - public init(listService: ListService, @ViewBuilder emptyStateView: @escaping () -> EmptyStateView, @ViewBuilder errorStateView: @escaping (Error) -> ErrorStateView, @ViewBuilder loadingStateView: @escaping () -> LoadingStateView, pagination: AdvancedListPagination<PaginationErrorView, PaginationLoadingView>) { |
68 | | - self.listService = listService |
69 | | - self.emptyStateView = emptyStateView |
70 | | - self.errorStateView = errorStateView |
71 | | - self.loadingStateView = loadingStateView |
72 | | - self.pagination = pagination |
73 | | - } |
74 | | - #else |
75 | | - public init(@ViewBuilder emptyStateView: @escaping () -> EmptyStateView, @ViewBuilder errorStateView: @escaping (Error) -> ErrorStateView, @ViewBuilder loadingStateView: @escaping () -> LoadingStateView) { |
76 | | - self.emptyStateView = emptyStateView |
77 | | - self.errorStateView = errorStateView |
78 | | - self.loadingStateView = loadingStateView |
79 | | - } |
80 | | - #endif |
81 | 89 | } |
82 | 90 |
|
83 | 91 | extension AdvancedList { |
@@ -117,47 +125,51 @@ extension AdvancedList { |
117 | 125 | struct AdvancedList_Previews : PreviewProvider { |
118 | 126 | private static let listService = ListService() |
119 | 127 |
|
| 128 | + #if targetEnvironment(macCatalyst) |
120 | 129 | static var previews: some View { |
121 | 130 | NavigationView { |
122 | | - #if targetEnvironment(macCatalyst) |
123 | | - AdvancedList(emptyStateView: { |
124 | | - Text("No data") |
125 | | - }, errorStateView: { error in |
126 | | - VStack { |
127 | | - Text(error.localizedDescription) |
128 | | - .lineLimit(nil) |
129 | | - |
130 | | - Button(action: { |
131 | | - // do something |
132 | | - }) { |
133 | | - Text("Retry") |
134 | | - } |
| 131 | + AdvancedList(emptyStateView: { |
| 132 | + Text("No data") |
| 133 | + }, errorStateView: { error in |
| 134 | + VStack { |
| 135 | + Text(error.localizedDescription) |
| 136 | + .lineLimit(nil) |
| 137 | + |
| 138 | + Button(action: { |
| 139 | + // do something |
| 140 | + }) { |
| 141 | + Text("Retry") |
135 | 142 | } |
136 | | - }, loadingStateView: { |
137 | | - Text("Loading ...") |
138 | | - }) |
139 | | - .environmentObject(listService) |
140 | | - .environmentObject(.noPagination) |
141 | | - #else |
142 | | - AdvancedList(listService: listService, emptyStateView: { |
143 | | - Text("No data") |
144 | | - }, errorStateView: { error in |
145 | | - VStack { |
146 | | - Text(error.localizedDescription) |
147 | | - .lineLimit(nil) |
148 | | - |
149 | | - Button(action: { |
150 | | - // do something |
151 | | - }) { |
152 | | - Text("Retry") |
153 | | - } |
| 143 | + } |
| 144 | + }, loadingStateView: { |
| 145 | + Text("Loading ...") |
| 146 | + }) |
| 147 | + .environmentObject(listService) |
| 148 | + .environmentObject(AdvancedListPagination.noPagination) |
| 149 | + } |
| 150 | + } |
| 151 | + #else |
| 152 | + static var previews: some View { |
| 153 | + NavigationView { |
| 154 | + AdvancedList(listService: listService, emptyStateView: { |
| 155 | + Text("No data") |
| 156 | + }, errorStateView: { error in |
| 157 | + VStack { |
| 158 | + Text(error.localizedDescription) |
| 159 | + .lineLimit(nil) |
| 160 | + |
| 161 | + Button(action: { |
| 162 | + // do something |
| 163 | + }) { |
| 164 | + Text("Retry") |
154 | 165 | } |
155 | | - }, loadingStateView: { |
156 | | - Text("Loading ...") |
157 | | - }, pagination: .noPagination) |
158 | | -// .navigationBarTitle(Text("List of Items")) |
159 | | - #endif |
| 166 | + } |
| 167 | + }, loadingStateView: { |
| 168 | + Text("Loading ...") |
| 169 | + }, pagination: .noPagination) |
| 170 | +// .navigationBarTitle(Text("List of Items")) |
160 | 171 | } |
161 | 172 | } |
| 173 | + #endif |
162 | 174 | } |
163 | 175 | #endif |
0 commit comments