-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPagingView.swift
More file actions
48 lines (46 loc) · 1.63 KB
/
PagingView.swift
File metadata and controls
48 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import SwiftUI
import KotlinSharedUI
struct PagingView<T: AnyObject, EmptyContent: View, ErrorContent: View, LoadingContent: View, SuccessContent: View>: View {
let data: PagingState<T>
@ViewBuilder
let emptyContent: () -> EmptyContent
@ViewBuilder
let errorContent: (KotlinThrowable) -> ErrorContent
@ViewBuilder
let loadingContent: () -> LoadingContent
let loadingCount = 5
@ViewBuilder
let successContent: (T) -> SuccessContent
var body: some View {
switch onEnum(of: data) {
case .empty: emptyContent()
case .error(let error): errorContent(error.error)
case .loading: ForEach(0..<5) { index in
loadingContent()
}
case .success(let success):
ForEach(0..<success.itemCount, id: \.self) { index in
if let item = success.peek(index: index) {
successContent(item)
.onAppear {
_ = success.get(index: index)
}
} else {
loadingContent()
.onAppear {
_ = success.get(index: index)
}
}
}
}
}
}
extension PagingView {
init(
data: PagingState<T>,
@ViewBuilder
successContent: @escaping (T) -> SuccessContent
) where ErrorContent == EmptyView, LoadingContent == EmptyView, EmptyContent == EmptyView {
self.init(data: data, emptyContent: { EmptyView() }, errorContent: {_ in EmptyView()}, loadingContent: {EmptyView()}, successContent: successContent)
}
}