-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplashView.swift
157 lines (133 loc) · 4.35 KB
/
SplashView.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
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
//
// SplashView.swift
// Ravens
//
// Created by Eric de Quartel on 12/11/2024.
//
import SwiftUI
import SwiftyBeaver
import BackgroundTasks
import UserNotifications
import MapKit
struct SplashView: View {
let log = SwiftyBeaver.self
@Binding var dataLoaded: Bool
@EnvironmentObject var locationManagerModel: LocationManagerModel
@EnvironmentObject var settings: Settings
@EnvironmentObject var languagesViewModel: LanguagesViewModel
@EnvironmentObject var speciesViewModel: SpeciesViewModel
@EnvironmentObject var speciesGroupViewModel: SpeciesGroupsViewModel
@EnvironmentObject var regionsViewModel: RegionsViewModel
@EnvironmentObject var regionListViewModel: RegionListViewModel
@EnvironmentObject var userViewModel: UserViewModel
@EnvironmentObject var keychainViewModel: KeychainViewModel
@EnvironmentObject var locationIdViewModel: LocationIdViewModel
@EnvironmentObject var geoJSONViewModel: GeoJSONViewModel
@EnvironmentObject var keyChainviewModel: KeychainViewModel
@State private var isLanguageDataLoaded = false
@State private var isFirstLanguageDataLoaded = false
@State private var isSecondLanguageDataLoaded = false
@State private var isSpeciesGroupDataLoaded = false
@State private var isRegionDataLoaded = false
@State private var isRegionListDataLoaded = false
@State private var isUserDataLoaded = false
@State private var isObservationsLocationDataLoaded = false
@State private var isGeoJSONDataLoaded = false
var body: some View {
VStack {
Text("Ravens")
.font(.system(size: 48))
.foregroundColor(.gray)
.bold()
LottieView(lottieFile: "ravenssun.json")
.frame(width: 150, height: 150)
}
.onChange(of: keyChainviewModel.token.isEmpty) { oldValue, newValue in
if !newValue { // `newValue` is false when the token is not empty
log.error("Token detected, loading data")
loadData()
} else {
log.error("Token not found, waiting for login")
}
}
.onAppear {
log.error("*** NEW LAUNCHING SPLASHVIEW ***")
if !keyChainviewModel.token.isEmpty {
loadData()
}
}
}
private func checkDataLoaded() {
if isFirstLanguageDataLoaded &&
isSecondLanguageDataLoaded &&
isSpeciesGroupDataLoaded &&
isLanguageDataLoaded &&
isRegionDataLoaded &&
isRegionListDataLoaded &&
isUserDataLoaded
{
self.dataLoaded = true
}
}
private func loadData() {
languagesViewModel.fetchLanguageData(
settings: settings,
completion: {
log.info("languagesViewModel Language data loaded")
isLanguageDataLoaded = true
checkDataLoaded()
})
speciesGroupViewModel.fetchData(
settings: settings,
completion: {
log.info("speciesGroupViewModel group data loaded")
isSpeciesGroupDataLoaded = true
checkDataLoaded()
})
regionsViewModel.fetchData(
settings: settings,
completion: {
log.info("regionsViewModel data loaded")
isRegionDataLoaded = true
checkDataLoaded()
})
regionListViewModel.fetchData(
settings: settings,
completion: {
log.info("regionListViewModel data loaded")
isRegionListDataLoaded = true
checkDataLoaded()
})
speciesViewModel.fetchDataFirst(
settings: settings,
completion: {
log.info("speciesViewModel First language data loaded")
speciesViewModel.parseHTMLFromURL(settings: settings, completion: {
log.error("html is parsed from start")
isFirstLanguageDataLoaded = true
checkDataLoaded()
})
})
speciesViewModel.fetchDataSecondLanguage(
settings: settings,
completion: {
log.info("speciesViewModel Second language data loaded")
isSecondLanguageDataLoaded = true
checkDataLoaded()
})
userViewModel.fetchUserData(
settings: settings,
completion: {
log.info("userViewModel data loaded: \(userViewModel.user?.id ?? 0)")
isUserDataLoaded = true
settings.userId = userViewModel.user?.id ?? 0
settings.userName = userViewModel.user?.name ?? ""
})
}
}
struct AnotherView: View {
@ObservedObject var viewModel: KeychainViewModel
var body: some View {
Text("Token: \(viewModel.token)")
}
}