-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathMainView.swift
More file actions
74 lines (69 loc) · 2.31 KB
/
MainView.swift
File metadata and controls
74 lines (69 loc) · 2.31 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
//
// MainView.swift
// OCKSample
//
// Created by Corey Baker on 11/25/20.
// Copyright © 2020 Network Reconnaissance Lab. All rights reserved.
import CareKit
import CareKitStore
import CareKitUI
import SwiftUI
struct MainView: View {
@EnvironmentObject private var appDelegate: AppDelegate
@StateObject private var loginViewModel = LoginViewModel()
@State private var path = [MainViewPath]()
@State private var storeCoordinator = OCKStoreCoordinator()
var body: some View {
NavigationStack(path: $path) {
LoginView(viewModel: loginViewModel)
.navigationDestination(for: MainViewPath.self) { destination in
switch destination {
case .tabs:
if isSyncingWithRemote {
MainTabView(loginViewModel: loginViewModel)
.navigationBarHidden(true)
} else {
CareView()
.navigationBarHidden(true)
}
}
}
.onAppear {
guard isSyncingWithRemote else {
updatePath([.tabs])
return
}
guard !loginViewModel.isLoggedOut else {
updatePath([])
return
}
updatePath([.tabs])
}
}
.environment(\.careStore, storeCoordinator)
.onReceive(loginViewModel.$isLoggedOut, perform: { isLoggedOut in
guard !isLoggedOut else {
updatePath([])
return
}
updatePath([.tabs])
})
.onReceive(appDelegate.$storeCoordinator) { newStoreCoordinator in
guard storeCoordinator !== newStoreCoordinator else {
return
}
storeCoordinator = newStoreCoordinator
}
}
@MainActor
func updatePath(_ path: [MainViewPath]) {
self.path = path
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
.environment(\.appDelegate, AppDelegate())
.environment(\.careStore, Utility.createPreviewStore())
}
}