-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathUserListView.swift
104 lines (93 loc) · 3.92 KB
/
UserListView.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
import SwiftUI
import WordPressUI
public struct UserListView: View {
@StateObject
private var viewModel: UserListViewModel
private let currentUserId: Int32
private let userService: UserServiceProtocol
private let applicationTokenListDataProvider: ApplicationTokenListDataProvider
public init(currentUserId: Int32, userService: UserServiceProtocol, applicationTokenListDataProvider: ApplicationTokenListDataProvider) {
self.currentUserId = currentUserId
self.userService = userService
self.applicationTokenListDataProvider = applicationTokenListDataProvider
_viewModel = StateObject(wrappedValue: UserListViewModel(userService: userService, currentUserId: currentUserId))
}
public var body: some View {
ZStack {
Color(.systemGroupedBackground)
.ignoresSafeArea()
Group {
if let error = viewModel.error {
EmptyStateView(error.localizedDescription, systemImage: "exclamationmark.triangle.fill")
} else {
List(viewModel.sortedUsers) { section in
Section(section.headerText) {
if section.users.isEmpty {
Text(Strings.noUsersFound)
.font(.body)
.foregroundStyle(Color.secondary)
.listRowBackground(Color.clear)
} else {
ForEach(section.users) { user in
UserListItem(user: user, isCurrentUser: user.id == currentUserId, userService: userService, applicationTokenListDataProvider: applicationTokenListDataProvider)
}
}
}
}
.searchable(text: $viewModel.searchTerm, prompt: Text(Strings.searchPrompt))
.disableAutocorrection(true)
.textInputAutocapitalization(.never)
.refreshable(action: viewModel.refreshItems)
}
}
}
.navigationTitle(Strings.usersListTitle)
.toolbar {
ToolbarItem(placement: .principal) {
HStack {
if viewModel.isRefreshing {
ProgressView()
}
Text(Strings.usersListTitle)
.font(.headline)
}
}
}
.task(id: viewModel.mode) {
await viewModel.performQuery()
}
.task { await viewModel.onAppear() }
}
enum Strings {
static let searchPrompt = NSLocalizedString(
"userlist.searchprompt",
value: "Search",
comment: "An instruction for the user to tap to start searching"
)
static let usersListTitle = NSLocalizedString(
"userlist.title",
value: "Users",
comment: "The heading at the top of the user list"
)
static let noUsersFound = NSLocalizedString(
"userlist.nousersfound",
value: "No users found",
comment: "Shown when the user list is empty"
)
}
}
#Preview("Loading") {
NavigationView {
UserListView(currentUserId: 0, userService: MockUserProvider(), applicationTokenListDataProvider: StaticTokenProvider(tokens: .success(.testTokens)))
}
}
#Preview("Error") {
NavigationView {
UserListView(currentUserId: 0, userService: MockUserProvider(scenario: .error), applicationTokenListDataProvider: StaticTokenProvider(tokens: .success(.testTokens)))
}
}
#Preview("List") {
NavigationView {
UserListView(currentUserId: 0, userService: MockUserProvider(scenario: .dummyData), applicationTokenListDataProvider: StaticTokenProvider(tokens: .success(.testTokens)))
}
}