-
Notifications
You must be signed in to change notification settings - Fork 0
/
Refreshable.swift
55 lines (44 loc) · 1.3 KB
/
Refreshable.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
// During WWDC21, Apple introduced a new modifier .refreshable you to showcase ProgressView and reloads with an action to perform like networking request.
// Here’s how you it works.
import SwiftUI
struct User: Codable, Identifiable {
let id: Int
let name: String
let email: String
}
struct ContentView: View {
@State private var users: [User] = [
User(id: 0, name: "Tim Jobs", email: "tim@jobs.com")
]
var body: some View {
NavigationView {
List(users) { user in
ListRowView(user: user)
}
.refreshable {
await loadUsers()
}
.navigationTitle("My App")
}
}
func loadUsers() async {
do {
let url = URL(string: "https://jsonplaceholder.typicode.com/users")!
let (data, _) = try await URLSession.shared.data(from: url)
users = try JSONDecoder().decode([User].self, from: data)
} catch {
users = []
}
}
}
struct ListRowView: View {
let user: User
var body: some View {
VStack(alignment: .leading) {
Text(user.name)
.font(.headline)
Text(user.email)
.foregroundColor(.secondary)
}
}
}