-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathPullToRefresh.swift
More file actions
260 lines (222 loc) · 8.13 KB
/
PullToRefresh.swift
File metadata and controls
260 lines (222 loc) · 8.13 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// pulltorefresh.swift
// PullToRefresh
//
// Created by András Samu on 2019. 09. 15..
// Updated by Veit Progl on 29.01.20.
// Copyright © 2019. András Samu. All rights reserved.
//
import SwiftUI
@available(iOS 13.0, macOS 10.15, *)
class RefreshData: ObservableObject {
@Binding var isDone: Bool
@Published var showText: String
@Published var showRefreshView: Bool {
didSet {
self.showText = "Loading"
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
if self.showRefreshView {
self.showRefreshView = false
self.showDone = true
self.showText = "Done"
}
}
}
}
@Published var pullStatus: CGFloat
@Published var showDone: Bool {
didSet {
if self.showDone && self.isDone {
self.showDone = false
self.showText = "Pull to refresh"
}
print(self.isDone)
}
}
init(isDone:Binding<Bool>) {
self._isDone = isDone
self.showText = "Pull to refresh"
self.showRefreshView = false
self.pullStatus = 0
self.showDone = false
}
}
@available(iOS 13.0, macOS 10.15, *)
public struct RefreshableNavigationView<Content: View>: View {
let content: () -> Content
let action: () -> Void
let navigationBarTitleDisplayMode: NavigationBarItem.TitleDisplayMode
private var title: String
@Binding var isDone: Bool
@ObservedObject var data: RefreshData
public init(
title:String,
action: @escaping () -> Void,
isDone: Binding<Bool>,
navigationBarTitleDisplayMode: NavigationBarItem.TitleDisplayMode,
@ViewBuilder content: @escaping () -> Content) {
self.title = title
self.action = action
self.navigationBarTitleDisplayMode = navigationBarTitleDisplayMode
self.content = content
self._isDone = isDone
self.data = RefreshData(isDone: isDone)
}
// public init<leadingItem: View>(title:String, action: @escaping () -> Void ,@ViewBuilder content: @escaping () -> Content, @ViewBuilder leadingItem: @escaping () -> leadingItem) {
// self.title = title
// self.action = action
// self.content = content
// self.leadingItem = leadingItem
// }
public var body: some View {
NavigationView{
RefreshableList(data: data, action: self.action) {
self.content()
}.navigationBarTitle(Text(title), displayMode: self.navigationBarTitileDisplayMode)
}
}
}
@available(iOS 13.0, macOS 10.15, *)
public struct RefreshableNavigationViewWithItem<Content: View, LeadingItem: View, TrailingItem: View>: View {
let content: () -> Content
let leadingItem: () -> LeadingItem
let trailingItem: () -> TrailingItem
let action: () -> Void
private var title: String
@Binding var isDone: Bool
@ObservedObject var data: RefreshData
// public init(title:String, action: @escaping () -> Void ,@ViewBuilder content: @escaping () -> Content) {
// self.title = title
// self.action = action
// self.content = content
// }
public init(title:String, action: @escaping () -> Void, isDone: Binding<Bool> ,@ViewBuilder leadingItem: @escaping () -> LeadingItem, @ViewBuilder trailingItem: @escaping () -> TrailingItem, @ViewBuilder content: @escaping () -> Content) {
self.title = title
self.action = action
self.content = content
self.leadingItem = leadingItem
self.trailingItem = trailingItem
self._isDone = isDone
self.data = RefreshData(isDone: isDone)
}
public var body: some View {
NavigationView{
RefreshableList(data: data, action: self.action) {
self.content()
}.navigationBarTitle(title)
.navigationBarItems(leading: self.leadingItem(), trailing: self.trailingItem())
}
}
}
@available(iOS 13.0, macOS 10.15, *)
public struct RefreshableList<Content: View>: View {
@ObservedObject var data: RefreshData
let action: () -> Void
let content: () -> Content
init(data: RefreshData, action: @escaping () -> Void, @ViewBuilder content: @escaping () -> Content) {
self.data = data
self.action = action
self.content = content
UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear
}
public var body: some View {
List{
Section(header: PullToRefreshView(data: self.data)) {
content()
}
}
.offset(y: -40)
.onPreferenceChange(RefreshableKeyTypes.PrefKey.self) { values in
guard let bounds = values.first?.bounds else { return }
self.data.pullStatus = CGFloat((bounds.origin.y - 106) / 80)
self.refresh(offset: bounds.origin.y)
}
}
func refresh(offset: CGFloat) {
if offset > 185 && !self.data.showRefreshView && !self.data.showDone {
self.data.showRefreshView = true
DispatchQueue.main.async {
self.action()
}
}
}
}
@available(iOS 13.0, macOS 10.15, *)
struct Spinner: View {
@Binding var percentage: CGFloat
var body: some View {
GeometryReader{ geometry in
ForEach(1...10, id: \.self) { i in
Rectangle()
.fill(Color.gray)
.cornerRadius(1)
.frame(width: 2.5, height: 8)
.opacity(self.percentage * 10 >= CGFloat(i) ? Double(i)/10.0 : 0)
.offset(x: 0, y: -8)
.rotationEffect(.degrees(Double(36 * i)), anchor: .bottom)
}.offset(x: 20, y: 12)
}.frame(width: 40, height: 40)
}
}
@available(iOS 13.0, macOS 10.15, *)
struct RefreshView: View {
@ObservedObject var data: RefreshData
var body: some View {
HStack() {
VStack(alignment: .center){
if self.data.showDone {
Image(systemName: "checkmark.circle")
.foregroundColor(Color.green)
.imageScale(.large)
} else if (!data.showRefreshView) {
Spinner(percentage: self.$data.pullStatus)
} else {
ActivityIndicator(isAnimating: .constant(true), style: .large)
}
Text(self.data.showText).font(.caption)
}
}
}
}
@available(iOS 13.0, macOS 10.15, *)
struct PullToRefreshView: View {
@ObservedObject var data: RefreshData
var body: some View {
GeometryReader{ geometry in
RefreshView(data: self.data)
.opacity(Double((geometry.frame(in: CoordinateSpace.global).origin.y - 106) / 80)).preference(key: RefreshableKeyTypes.PrefKey.self, value: [RefreshableKeyTypes.PrefData(bounds: geometry.frame(in: CoordinateSpace.global))])
.offset(y: -70)
}
.offset(y: -20)
}
}
@available(iOS 13.0, macOS 10.15, *)
struct ActivityIndicator: UIViewRepresentable {
@Binding var isAnimating: Bool
let style: UIActivityIndicatorView.Style
func makeUIView(context: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
return UIActivityIndicatorView(style: style)
}
func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext<ActivityIndicator>) {
isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
}
}
@available(iOS 13.0, macOS 10.15, *)
struct RefreshableKeyTypes {
struct PrefData: Equatable {
let bounds: CGRect
}
struct PrefKey: PreferenceKey {
static var defaultValue: [PrefData] = []
static func reduce(value: inout [PrefData], nextValue: () -> [PrefData]) {
value.append(contentsOf: nextValue())
}
typealias Value = [PrefData]
}
}
@available(iOS 13.0, macOS 10.15, *)
struct Spinner_Previews: PreviewProvider {
static var previews: some View {
Spinner(percentage: .constant(1))
}
}