-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMainView.swift
More file actions
248 lines (227 loc) · 8 KB
/
Copy pathMainView.swift
File metadata and controls
248 lines (227 loc) · 8 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
// Copyright (c) 2019 Razeware LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
// distribute, sublicense, create a derivative work, and/or sell copies of the
// Software in any work that is designed, intended, or marketed for pedagogical or
// instructional purposes related to programming, coding, application development,
// or information technology. Permission for such use, copying, modification,
// merger, publication, distribution, sublicensing, creation of derivative works,
// or sale is expressly withheld.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import SwiftUI
struct MainView: View {
@Environment(MessageBus.self) private var messageBus
@Environment(DataManager.self) private var dataManager
@Environment(\.sessionController) private var sessionController
@State var isShowingForceAppUpdatesAlert = false
@State var itemTagId: String?
@State var isResetting = false
@State var isShowingResetConfirmationDialog = false
@State private var isShowingAcceptPrivacySheet = false
@State private var arePrivacyAccepted = false
@State private var isShowingAcceptTermsSheet = false
@State private var areTermsAccepted = false
private let tabViewModel = TabViewModel()
var body: some View {
ZStack {
contentView
.background(Color.backgroundColor)
.overlay(MessageBarView(messageBus: messageBus), alignment: .bottom)
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb, perform: handleBackgroundTagReading)
.onChange(of: sessionController.shouldUpdatePrivacy) {
if sessionController.shouldUpdatePrivacy {
isShowingAcceptPrivacySheet = true
}
}
.onChange(of: sessionController.shouldUpdateTerms) {
if sessionController.shouldUpdateTerms {
isShowingAcceptTermsSheet = true
}
}
.confirmationDialog(
String.itemTagAlreadyCompleted,
isPresented: $isShowingResetConfirmationDialog
) {
Button(String.reset, role: .destructive) {
resetTag(itemTagId: itemTagId!)
}
Button(String.cancel, role: .cancel) {
isShowingResetConfirmationDialog = false
}
} message: {
Text(String.areYouSure)
}
}
}
}
// MARK: - private
private extension MainView {
@ViewBuilder var contentView: some View {
if !sessionController.isLoggedIn {
OnboardingView()
} else {
switch sessionController.permissionState {
case .loaded:
tabBarView
case .notLoaded, .loading:
PermissionsLoadingView()
case .error:
ErrorView(
buttonAction: logout,
buttonTitle: .backToStartScreen
)
}
}
}
@ViewBuilder var tabBarView: some View {
switch sessionController.sessionState {
case .online:
if dataManager.isRebuildingRepositories {
AppTabView(
shopListView: LoadingView.init,
scanView: LoadingView.init,
settingsView: LoadingView.init
)
.environment(tabViewModel)
} else {
if sessionController.shouldUpdateApp {
AppTabView(
shopListView: NeedAppUpdatesView.init,
scanView: NeedAppUpdatesView.init,
settingsView: NeedAppUpdatesView.init
)
.environment(tabViewModel)
} else {
AppTabView(
shopListView: shopListView,
scanView: scanView,
settingsView: settingsView
)
.environment(tabViewModel)
.sheet(isPresented: $isShowingAcceptPrivacySheet) {
NavigationStack {
AcceptPrivacyView(arePrivacyAccepted: $arePrivacyAccepted)
.interactiveDismissDisabled(!arePrivacyAccepted)
}
}
.sheet(isPresented: $isShowingAcceptTermsSheet) {
NavigationStack {
AcceptTermsView(areTermsAccepted: $areTermsAccepted)
.interactiveDismissDisabled(!areTermsAccepted)
}
}
}
}
case .offline:
AppTabView(
shopListView: OfflineView.init,
scanView: OfflineView.init,
settingsView: OfflineView.init
)
.environment(tabViewModel)
case .unknown:
LoadingView()
}
}
func shopListView() -> ShopListView {
let viewModel = ShopListViewModel(
sessionController: sessionController,
shopRepository: dataManager.shopRepository,
itemTagRepository: dataManager.itemTagRepository,
tabViewModel: tabViewModel,
mainTab: .shops,
messageBus: messageBus
)
return ShopListView(viewModel: viewModel)
}
func scanView() -> ScanView {
.init(
itemTagRepository: dataManager.itemTagRepository
)
}
func settingsView() -> SettingsView {
.init(accountPasswordRepository: dataManager.accountPasswordRepository)
}
func handleBackgroundTagReading(_ userActivity: NSUserActivity) {
guard sessionController.isLoggedIn else {
messageBus.post(message: Message(level: .error, message: String.pleaseSignIn, autoDismiss: false))
return
}
let ndefMessage = userActivity.ndefMessagePayload
guard !ndefMessage.records.isEmpty,
ndefMessage.records[0].typeNameFormat != .empty else {
return
}
let itemTagInfo = Utility.extractItemTagInfoFrom(message: ndefMessage)
if itemTagInfo.success {
itemTagId = itemTagInfo.id
completeTag(itemTagId: itemTagId!)
} else {
messageBus.post(message: Message(level: .error, message: itemTagInfo.message, autoDismiss: false))
tabViewModel.selectedTab = .scan
}
}
func completeTag(itemTagId: String) {
Task { @MainActor in
do {
let itemTag = try await dataManager.itemTagRepository.complete(id: itemTagId)
sessionController.completeScanResult = CompleteScanResult(
itemTag: itemTag,
type: .completed
)
if itemTag.alreadyCompleted! {
isShowingResetConfirmationDialog = true
}
} catch {
sessionController.completeScanResult = CompleteScanResult(
type: .failed,
message: error.localizedDescription
)
}
sessionController.didBackgroundTagReading = true
tabViewModel.selectedTab = .scan
}
}
private func resetTag(itemTagId: String) {
Task { @MainActor in
isResetting = true
do {
let itemTag = try await dataManager.itemTagRepository.reset(id: itemTagId)
sessionController.completeScanResult = CompleteScanResult(
itemTag: itemTag,
type: .reset
)
} catch {
sessionController.completeScanResult = CompleteScanResult(
type: .failed,
message: error.localizedDescription
)
}
isResetting = false
sessionController.didBackgroundTagReading = true
tabViewModel.selectedTab = .scan
}
}
func logout() {
Task {
try await sessionController.logout()
}
}
}