-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathShopDetailView.swift
More file actions
253 lines (227 loc) · 6.76 KB
/
Copy pathShopDetailView.swift
File metadata and controls
253 lines (227 loc) · 6.76 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
//
// ShopDetailView.swift
// NativeAppTemplate
//
// Created by Daisuke Adachi on 2023/02/09.
//
import SwiftUI
import TipKit
struct ReadInstructionsTip: Tip {
var title: Text {
Text(String.readInstructions)
}
var message: Text? {
Text(String.haveFun)
}
var image: Image? {
Image(systemName: "info.circle")
}
}
struct ShopDetailView: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.mainTab) private var mainTab
@Environment(TabViewModel.self) private var tabViewModel
@Environment(MessageBus.self) private var messageBus
@Environment(\.sessionController) private var sessionController
@State private var isFetching = true
@State private var isResetting = false
@State private var isCompleting = false
@State private var itemTags: [ItemTag]?
private let shopRepository: ShopRepositoryProtocol
private let itemTagRepository: ItemTagRepositoryProtocol
private var shopId: String
private var shop: Binding<Shop> {
Binding {
shopRepository.findBy(id: shopId)
} set: { _ in
}
}
init(
shopRepository: ShopRepositoryProtocol,
itemTagRepository: ItemTagRepositoryProtocol,
shopId: String
) {
self.shopRepository = shopRepository
self.itemTagRepository = itemTagRepository
self.shopId = shopId
}
}
// MARK: - View
extension ShopDetailView {
var body: some View {
contentView
.onAppear {
tabViewModel.showingDetailView[mainTab] = true
}
.task {
reload()
}
}
}
// MARK: - private
private extension ShopDetailView {
var contentView: some View {
@ViewBuilder var contentView: some View {
if isFetching || isResetting || isCompleting {
LoadingView()
} else {
shopDetailView
}
}
return contentView
}
var header: some View {
ScrollView(.horizontal) {
VStack(alignment: .leading, spacing: 0) {
let tip = ReadInstructionsTip()
TipView(tip, arrowEdge: .bottom)
.tint(.alarm)
Text("\(String.instructions):")
.foregroundStyle(.contentText)
HStack(alignment: .firstTextBaseline) {
Text(verbatim: "1.")
.font(.uiCaption)
.foregroundStyle(.contentText)
HStack {
let openServerNumberTagsWebpage = "\(String.open) [\(String.serverNumberTagsWebpage)](\(shop.wrappedValue.displayShopServerUrl))."
Text(.init(openServerNumberTagsWebpage))
.font(.uiCaption)
.foregroundStyle(.contentText)
}
}
HStack(alignment: .firstTextBaseline) {
Text(verbatim: "2.")
.font(.uiCaption)
.foregroundStyle(.contentText)
Text("\(String.swipeNumberTagBelow) \(String.tapDisplayedButton)")
.font(.uiCaption)
.foregroundStyle(.contentText)
}
HStack(alignment: .firstTextBaseline) {
Text(verbatim: "3.")
.font(.uiCaption)
.foregroundStyle(.contentText)
Text(String.serverNumberTagsWebpageWillBeUpdated)
.font(.uiCaption)
.foregroundStyle(.contentText)
}
Link(String.learnMore, destination: URL(string: String.howToUseUrl)!)
}
}
}
var cardsView: some View {
ForEach(itemTagRepository.itemTags, id: \.id) { itemTag in
ShopDetailCardView(itemTag: itemTag)
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
if itemTag.state == ItemTagState.idled {
Button { completeTag(itemTagId: itemTag.id) } label: {
Label(String.complete, systemImage: "bolt.fill")
.labelStyle(.titleOnly)
}
.tint(.blue)
} else {
Button(role: .destructive) { resetTag(itemTagId: itemTag.id) } label: {
Label(String.reset, systemImage: "trash")
.labelStyle(.titleOnly)
}
.tint(.red)
}
}
.listRowBackground(Color.cardBackground)
}
}
var shopDetailView: some View {
VStack {
header
.padding(.top)
.padding(.horizontal, 8)
List {
Section {
cardsView
} header: {
EmptyView()
.id(ScrollToTopID(mainTab: mainTab, detail: true))
}
}
.scrollContentBackground(.hidden)
.accessibility(identifier: "shopDetailView")
.refreshable {
reload()
}
}
.navigationTitle(shop.wrappedValue.name)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
NavigationLink(
destination: ShopSettingsView(
viewModel: ShopSettingsViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
itemTagRepository: itemTagRepository,
messageBus: messageBus,
shopId: shop.wrappedValue.id
)
)
) {
Image(systemName: "gearshape.fill")
}
}
}
}
func reload() {
// Avoid fetching shop detail error
guard sessionController.isLoggedIn else {
return
}
fetchShopDetail()
}
private func fetchShopDetail() {
Task { @MainActor in
isFetching = true
do {
_ = try await shopRepository.fetchDetail(id: shopId)
_ = try await itemTagRepository.fetchAll(shopId: shopId)
isFetching = false
} catch {
messageBus.post(
message: Message(
level: .error,
message: error.localizedDescription,
autoDismiss: false
)
)
dismiss()
}
}
}
func completeTag(itemTagId: String) {
Task { @MainActor in
isCompleting = true
do {
let itemTag = try await itemTagRepository.complete(id: itemTagId)
if itemTag.alreadyCompleted! {
messageBus.post(message: Message(level: .warning, message: .itemTagAlreadyCompleted, autoDismiss: false))
} else {
messageBus.post(message: Message(level: .success, message: .itemTagCompleted))
}
} catch {
messageBus.post(message: Message(level: .error, message: "\(String.itemTagCompletedError) \(error.localizedDescription)", autoDismiss: false))
}
isCompleting = false
reload()
}
}
func resetTag(itemTagId: String) {
Task { @MainActor in
isResetting = true
do {
_ = try await itemTagRepository.reset(id: itemTagId)
messageBus.post(message: Message(level: .success, message: .itemTagReset))
} catch {
messageBus.post(message: Message(level: .error, message: "\(String.itemTagResetError) \(error.localizedDescription)", autoDismiss: false))
}
isResetting = false
reload()
}
}
}