-
Notifications
You must be signed in to change notification settings - Fork 12
/
ContentView.swift
222 lines (205 loc) · 6.88 KB
/
ContentView.swift
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
import SwiftUI
import Refresher
struct ContentView: View {
@State var refreshed = 0
@State var searchText = ""
var body: some View {
NavigationView {
ScrollView {
if #available(iOS 15.0, *) {
Text("Searching for \(searchText)")
.searchable(text: $searchText)
.navigationTitle("Searchable")
}
VStack {
Text("Hello, world!")
Text("Refreshed: \(refreshed)")
}
NavigationLink(destination: DetailsView(style: .default)) {
Text("Go to details")
.padding()
}
NavigationLink(destination: DetailsOverlayView()) {
Text("Go to details in overlay mode")
.padding()
}
NavigationLink(destination: DetailsCustom()) {
Text("Go to details with a custom spinner")
.padding()
}
NavigationLink(destination: DetailsView(style: .system)) {
Text("Go to details with system style animation")
.padding()
}
NavigationLink(destination: DetailsView(style: .system, useImage: false)) {
Text("Go to details with system style - no image")
.padding()
}
NavigationLink(destination: DetailsSearch()) {
Text("Go to details with system style and search bar in header")
.padding()
}
ForEach((1...100), id: \.self) { _ in
Text("asdf")
}
}
.refresher {
await Task.sleep(seconds: 2)
refreshed += 1
}
.navigationTitle("Refresher")
}
}
}
struct DetailsSearch: View {
@State var refreshed = 0
@State var searchText = ""
var body: some View {
ScrollView {
VStack {
if #available(iOS 15.0, *) {
Text("Searching for \(searchText)")
.searchable(text: $searchText)
.navigationTitle("Searchable")
}
Text("Details!")
Text("Refreshed: \(refreshed)")
}
}
.refresher(style: .system) {
await Task.sleep(seconds: 2)
refreshed += 1
}
.navigationBarTitle("", displayMode: .inline)
}
}
struct DetailsView: View {
@State var refreshed = 0
var style: Style
@State var useImage = true
let rectangles = (0..<50).map { _ in CGFloat.random(in: 10..<50) }
var body: some View {
ScrollView {
VStack {
if useImage {
Image("photo")
.resizable()
.aspectRatio(contentMode: .fill)
}
Text("Details!")
Text("Refreshed: \(refreshed)")
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 8) {
ForEach(0..<10) { _ in
Rectangle()
.frame(width: 100, height: 100)
}
}
.padding(.horizontal)
}
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 8), count: 3)) {
ForEach(rectangles, id: \.self) { size in
VStack {
Rectangle()
.frame(width: size, height: size)
Text("text")
}
}
}
}
}
.refresher(style: style) {
await Task.sleep(seconds: 2)
refreshed += 1
}
.navigationBarTitle("", displayMode: .inline)
}
}
struct DetailsOverlayView: View {
@State var refreshed = 0
var body: some View {
ScrollView {
VStack {
GeometryReader { geometry in
Image("photo")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: geometry.size.width, height: geometry.size.height)
.offset(y: -geometry.frame(in: .global).minY)
}
.frame(height: 180)
Text("Details!")
Text("Refreshed: \(refreshed)")
}
}
.refresher(style: .overlay) {
await Task.sleep(seconds: 2)
refreshed += 1
}
.navigationBarTitle("", displayMode: .inline)
}
}
struct DetailsCustom: View {
@State var refreshed = 0
var body: some View {
ScrollView {
VStack {
Image("photo")
.resizable()
.aspectRatio(contentMode: .fit)
Text("Details!")
Text("Refreshed: \(refreshed)")
}
}
.refresher(refreshView: EmojiRefreshView.init ) {
await Task.sleep(seconds: 1.5)
refreshed += 1
}
.navigationBarTitle("", displayMode: .inline)
}
}
public struct EmojiRefreshView: View {
@Binding var state: RefresherState
@State private var angle: Double = 0.0
@State private var isAnimating = false
var foreverAnimation: Animation {
Animation.linear(duration: 1.0)
.repeatForever(autoreverses: false)
}
public var body: some View {
VStack {
switch state.mode {
case .notRefreshing:
Text("🤪")
.onAppear {
isAnimating = false
}
case .pulling:
Text("😯")
.rotationEffect(.degrees(360 * state.dragPosition))
case .refreshing:
Text("😂")
.rotationEffect(.degrees(self.isAnimating ? 360.0 : 0.0))
.onAppear {
withAnimation(foreverAnimation) {
isAnimating = true
}
}
}
}
.scaleEffect(2)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
DetailsView(style: .default)
DetailsOverlayView()
}
}
extension Task where Success == Never, Failure == Never {
static func sleep(seconds: Double) async {
let duration = UInt64(seconds * 1_000_000_000)
try! await Task.sleep(nanoseconds: duration)
}
}