1 |
efrain |
1 |
//
|
|
|
2 |
// MainView.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 2/17/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import SwiftUI
|
|
|
9 |
import TTGSnackbar
|
|
|
10 |
|
|
|
11 |
struct MainView: View {
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
@StateObject private var appNavigation = AppNavigation()
|
|
|
16 |
@StateObject private var networkMonitor = NetworkMonitor()
|
|
|
17 |
@State private var showToast : Bool = true
|
|
|
18 |
|
|
|
19 |
private var appData = AppData.sharedInstance
|
|
|
20 |
private var localNotification = LocalNotificationManager()
|
17 |
efrain |
21 |
|
|
|
22 |
|
1 |
efrain |
23 |
|
|
|
24 |
var body: some View {
|
|
|
25 |
ZStack {
|
|
|
26 |
Color("color_window_background").ignoresSafeArea(.all)
|
|
|
27 |
|
|
|
28 |
VStack(spacing: 0) {
|
|
|
29 |
switch appNavigation.pageActive
|
|
|
30 |
{
|
|
|
31 |
case AppMainPage.intro1 :
|
|
|
32 |
IntroStep1View()
|
|
|
33 |
.environmentObject(appNavigation)
|
|
|
34 |
.environmentObject(networkMonitor)
|
|
|
35 |
.transition(.scale)
|
|
|
36 |
|
|
|
37 |
case AppMainPage.intro2 :
|
|
|
38 |
IntroStep2View()
|
|
|
39 |
.environmentObject(appNavigation)
|
|
|
40 |
.environmentObject(networkMonitor)
|
|
|
41 |
.transition(.scale)
|
|
|
42 |
|
|
|
43 |
case AppMainPage.intro3 :
|
|
|
44 |
IntroStep3View()
|
|
|
45 |
.environmentObject(appNavigation)
|
|
|
46 |
.environmentObject(networkMonitor)
|
|
|
47 |
.transition(.scale)
|
|
|
48 |
|
|
|
49 |
case AppMainPage.signin :
|
|
|
50 |
SigninView()
|
|
|
51 |
.environmentObject(appNavigation)
|
|
|
52 |
.environmentObject(networkMonitor)
|
|
|
53 |
.transition(.scale)
|
|
|
54 |
|
|
|
55 |
case AppMainPage.splash :
|
|
|
56 |
SplashScreenView()
|
|
|
57 |
.environmentObject(appNavigation)
|
|
|
58 |
.environmentObject(networkMonitor)
|
|
|
59 |
|
|
|
60 |
case AppMainPage.goodbye :
|
|
|
61 |
GoodByeView()
|
|
|
62 |
.environmentObject(appNavigation)
|
|
|
63 |
.environmentObject(networkMonitor)
|
2 |
efrain |
64 |
|
|
|
65 |
case AppMainPage.goodbyedeleteaccount :
|
|
|
66 |
GoodByeDeleteAccountView()
|
|
|
67 |
.environmentObject(appNavigation)
|
|
|
68 |
.environmentObject(networkMonitor)
|
1 |
efrain |
69 |
|
|
|
70 |
case AppMainPage.videoplayer :
|
|
|
71 |
VideoPlayerView()
|
|
|
72 |
.environmentObject(appNavigation)
|
|
|
73 |
.environmentObject(networkMonitor)
|
|
|
74 |
|
|
|
75 |
case AppMainPage.pdfviewer :
|
|
|
76 |
PdfViewerView()
|
|
|
77 |
.environmentObject(appNavigation)
|
|
|
78 |
.environmentObject(networkMonitor)
|
|
|
79 |
|
|
|
80 |
case AppMainPage.webviewer :
|
|
|
81 |
WebViewerView()
|
|
|
82 |
.environmentObject(appNavigation)
|
|
|
83 |
.environmentObject(networkMonitor)
|
|
|
84 |
|
|
|
85 |
case AppMainPage.download :
|
|
|
86 |
DownloadFileView()
|
|
|
87 |
.environmentObject(appNavigation)
|
|
|
88 |
.environmentObject(networkMonitor)
|
|
|
89 |
|
|
|
90 |
default :
|
|
|
91 |
HomeView()
|
|
|
92 |
.environmentObject(appNavigation)
|
|
|
93 |
.environmentObject(networkMonitor)
|
|
|
94 |
.transition(.scale)
|
|
|
95 |
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
//Spacer()
|
|
|
99 |
/*
|
|
|
100 |
if(self.networkMonitor.status == .disconnected) {
|
|
|
101 |
Text("The popup")
|
|
|
102 |
.frame(width: UIScreen.main.bounds.width, height: 60)
|
|
|
103 |
.background(Color("color_error_background"))
|
|
|
104 |
.foregroundColor(Color("color_error_foreground"))
|
|
|
105 |
.font(.callout)
|
|
|
106 |
}*/
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
.onAppear {
|
|
|
110 |
|
|
|
111 |
if appData.refreshContentMessageShowPending {
|
|
|
112 |
let snackbar = TTGSnackbar(message: appData.refreshContentMessage, duration: .forever)
|
|
|
113 |
// Action 1
|
|
|
114 |
snackbar.actionText = Config.LANG_COMMNON_UPDATE
|
|
|
115 |
snackbar.actionTextColor = UIColor( Color("color_snackbar_button_yes"))
|
|
|
116 |
snackbar.actionBlock = { (snackbar) in
|
|
|
117 |
DispatchQueue.main.async {
|
|
|
118 |
snackbar.dismiss()
|
|
|
119 |
refreshContent()
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
snackbar.show()
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
}
|
|
|
128 |
.onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMMAND_REFRESH_CONTENT))
|
|
|
129 |
{ data in
|
|
|
130 |
if data.userInfo != nil {
|
|
|
131 |
let userInfo = data.userInfo!
|
|
|
132 |
//userInfo["command"] as? String ?? ""
|
|
|
133 |
|
|
|
134 |
let newCapsules = userInfo.index(forKey: "new_capsules") == nil ? 0 : Int(userInfo["new_capsules"] as? String ?? "") ?? 0
|
|
|
135 |
|
|
|
136 |
let isForeground = (Int(userInfo["is_foreground"] as? String ?? "") ?? 0) == 1
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
let title = userInfo["title"] as? String ?? ""
|
|
|
140 |
let body = userInfo["body"] as? String ?? ""
|
|
|
141 |
|
|
|
142 |
let command = Constants.NOTIFICATION_COMMAND_REFRESH_CONTENT
|
|
|
143 |
let now = Date()
|
|
|
144 |
let dateFormatter = DateFormatter()
|
|
|
145 |
dateFormatter.dateFormat = Constants.FORMAT_DATE_YMD
|
|
|
146 |
|
|
|
147 |
let timeFormatter = DateFormatter()
|
|
|
148 |
timeFormatter.dateFormat = Constants.FORMAT_TIME_12
|
|
|
149 |
|
|
|
150 |
let dateOn = dateFormatter.string(from: now)
|
|
|
151 |
let timeOn = timeFormatter.string(from: now)
|
|
|
152 |
|
17 |
efrain |
153 |
let userNotificationDao = UserNotificationDao()
|
1 |
efrain |
154 |
var userNotification : UserNotificationModel = UserNotificationModel(userUuid: appData.userUuid, title: title, body: body, viewed: 0, url: "", command: command, dateOn: dateOn, timeOn: timeOn)
|
|
|
155 |
|
|
|
156 |
userNotificationDao.insert(userNotification: userNotification)
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
var message : String = Config.LANG_MESSAGE_NEW_CONTENT_THERE
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
if isForeground {
|
|
|
163 |
let snackbar = TTGSnackbar(message: message, duration: .forever)
|
|
|
164 |
// Action 1
|
|
|
165 |
snackbar.actionText = Config.LANG_COMMNON_UPDATE
|
|
|
166 |
snackbar.actionTextColor = UIColor( Color("color_snackbar_button_yes"))
|
|
|
167 |
snackbar.actionBlock = { (snackbar) in
|
|
|
168 |
DispatchQueue.main.async {
|
|
|
169 |
snackbar.dismiss()
|
|
|
170 |
refreshContent()
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
snackbar.show()
|
|
|
175 |
} else {
|
|
|
176 |
appData.refreshContentMessage = message
|
|
|
177 |
appData.refreshContentMessageShowPending = true
|
|
|
178 |
appData.save()
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
}
|
17 |
efrain |
183 |
/*.onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_DEVICE_REGISTER_NAME))
|
|
|
184 |
{ data in
|
|
|
185 |
if data.userInfo != nil {
|
|
|
186 |
let userInfo = data.userInfo!
|
|
|
187 |
let new_uuid = userInfo["uuid"] as? String ?? ""
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
if !new_uuid.isEmpty {
|
|
|
191 |
if appData.deviceUuid != new_uuid {
|
|
|
192 |
|
|
|
193 |
var sync = SyncModel()
|
|
|
194 |
sync.data = new_uuid
|
|
|
195 |
sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
|
|
|
196 |
|
|
|
197 |
let syncDao = SyncDao()
|
|
|
198 |
if syncDao.insert(record: sync) > 0 {
|
|
|
199 |
appData.deviceUuid = new_uuid
|
|
|
200 |
appData.save()
|
|
|
201 |
|
|
|
202 |
let syncAdapter = SyncAdapter()
|
|
|
203 |
syncAdapter.sync(isForeground: true) {
|
|
|
204 |
success in
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
}*/
|
|
|
211 |
.onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_FCM_REGISTER_NAME))
|
|
|
212 |
{ data in
|
|
|
213 |
if data.userInfo != nil {
|
|
|
214 |
let userInfo = data.userInfo!
|
|
|
215 |
let token = userInfo["token"] as? String ?? ""
|
|
|
216 |
|
|
|
217 |
|
|
|
218 |
if !token.isEmpty {
|
|
|
219 |
if appData.fcmToken != token {
|
|
|
220 |
|
|
|
221 |
var sync = SyncModel()
|
|
|
222 |
sync.data = token
|
|
|
223 |
sync.type = Constants.SYNC_ADAPTER_TYPE_FCM
|
|
|
224 |
|
|
|
225 |
let syncDao = SyncDao()
|
19 |
efrain |
226 |
let syncId = syncDao.insert(record: sync)
|
|
|
227 |
if syncId > 0 {
|
17 |
efrain |
228 |
appData.fcmToken = token
|
|
|
229 |
appData.save()
|
19 |
efrain |
230 |
|
|
|
231 |
sync.id = syncId
|
17 |
efrain |
232 |
|
|
|
233 |
let syncAdapter = SyncAdapter()
|
19 |
efrain |
234 |
syncAdapter.registerFcm(record: sync)
|
17 |
efrain |
235 |
}
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
}
|
|
|
239 |
}
|
1 |
efrain |
240 |
.onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMMAND_EXIT))
|
|
|
241 |
{ data in
|
|
|
242 |
|
17 |
efrain |
243 |
/*if data.userInfo != nil {
|
|
|
244 |
let userInfo = data.userInfo!
|
|
|
245 |
let isForeground = (Int(userInfo["is_foreground"] as? String ?? "") ?? 0) == 1
|
|
|
246 |
|
|
|
247 |
if isForeground {
|
|
|
248 |
appData.signoutActionRequired = true
|
|
|
249 |
appData.save()
|
|
|
250 |
} else {
|
|
|
251 |
DispatchQueue.main.async {
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
appData.userUuid = ""
|
|
|
256 |
appData.userFirstname = ""
|
|
|
257 |
appData.userLastname = ""
|
|
|
258 |
appData.userEmail = ""
|
|
|
259 |
appData.userImage = ""
|
|
|
260 |
appData.refreshContentActionRequired = false
|
|
|
261 |
appData.refreshContentMessage = ""
|
|
|
262 |
appData.refreshContentMessageShowPending = false
|
|
|
263 |
appData.signoutActionRequired = false
|
|
|
264 |
appData.save()
|
|
|
265 |
|
|
|
266 |
appNavigation.pageActive = .goodbye
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
}*/
|
1 |
efrain |
273 |
|
17 |
efrain |
274 |
appNavigation.pageActive = .goodbye
|
|
|
275 |
|
|
|
276 |
|
1 |
efrain |
277 |
|
|
|
278 |
|
|
|
279 |
|
|
|
280 |
}.onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_PUSH))
|
|
|
281 |
{ data in
|
|
|
282 |
|
|
|
283 |
let title = data.userInfo?["title"]! as? String ?? ""
|
|
|
284 |
let body = data.userInfo?["body"]! as? String ?? ""
|
|
|
285 |
let url = data.userInfo?["url"]! as? String ?? ""
|
|
|
286 |
|
|
|
287 |
let command = url.isEmpty ? "" : Constants.NOTIFICATION_COMMAND_OPEN_URL
|
|
|
288 |
let viewed = url.isEmpty ? 1 : 0
|
|
|
289 |
let now = Date()
|
|
|
290 |
let dateFormatter = DateFormatter()
|
|
|
291 |
dateFormatter.dateFormat = Constants.FORMAT_DATE_YMD
|
|
|
292 |
|
|
|
293 |
let timeFormatter = DateFormatter()
|
|
|
294 |
timeFormatter.dateFormat = Constants.FORMAT_TIME_12
|
|
|
295 |
|
|
|
296 |
let dateOn = dateFormatter.string(from: now)
|
|
|
297 |
let timeOn = timeFormatter.string(from: now)
|
|
|
298 |
|
17 |
efrain |
299 |
let userNotificationDao = UserNotificationDao()
|
1 |
efrain |
300 |
var userNotification : UserNotificationModel = UserNotificationModel(userUuid: appData.userUuid, title: title, body: body, viewed: viewed, url: url, command: command, dateOn: dateOn, timeOn: timeOn)
|
|
|
301 |
|
|
|
302 |
|
|
|
303 |
|
|
|
304 |
userNotificationDao.insert(userNotification: userNotification)
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
func refreshContent() -> Void {
|
|
|
311 |
appData.refreshContentMessage = ""
|
|
|
312 |
appData.refreshContentMessageShowPending = false
|
|
|
313 |
appData.refreshContentActionRequired = true
|
|
|
314 |
appData.save()
|
|
|
315 |
|
|
|
316 |
withAnimation {
|
|
|
317 |
appNavigation.subpageActive = .topics
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
|
|
|
321 |
}
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
struct MainView_Previews: PreviewProvider {
|
|
|
325 |
static var previews: some View {
|
|
|
326 |
MainView()
|
|
|
327 |
}
|
|
|
328 |
}
|
|
|
329 |
|