Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 31 | Rev 38 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  MainView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 2/17/22.
6
//
7
 
8
import SwiftUI
17 efrain 9
import TTGSnackbar
1 efrain 10
 
11
struct MainView: View {
17 efrain 12
 
13
 
14
 
1 efrain 15
    @StateObject private var appNavigation = AppNavigation()
16
    @StateObject private var networkMonitor = NetworkMonitor()
17
    @State private var showToast : Bool = true
18
 
17 efrain 19
    private var appData = AppData.sharedInstance
20
    private var localNotification = LocalNotificationManager()
1 efrain 21
 
22
    var body: some View {
23
        ZStack {
24
            Color("color_window_background").ignoresSafeArea(.all)
25
 
26
           VStack(spacing: 0) {
8 efrain 27
                switch appNavigation.pageActive
28
                {
29
                    case AppMainPage.intro1 :
1 efrain 30
                        IntroStep1View()
31
                        .environmentObject(appNavigation)
32
                        .environmentObject(networkMonitor)
33
                        .transition(.scale)
34
 
8 efrain 35
                case AppMainPage.intro2 :
1 efrain 36
                        IntroStep2View()
37
                        .environmentObject(appNavigation)
38
                        .environmentObject(networkMonitor)
39
                        .transition(.scale)
40
 
8 efrain 41
                case AppMainPage.intro3 :
1 efrain 42
                        IntroStep3View()
43
                        .environmentObject(appNavigation)
44
                        .environmentObject(networkMonitor)
45
                        .transition(.scale)
46
 
8 efrain 47
                case  AppMainPage.signin :
1 efrain 48
                        SigninView()
49
                        .environmentObject(appNavigation)
50
                        .environmentObject(networkMonitor)
51
                        .transition(.scale)
52
 
8 efrain 53
                case AppMainPage.splash :
54
                    SplashScreenView()
55
                    .environmentObject(appNavigation)
56
                    .environmentObject(networkMonitor)
57
 
58
                case AppMainPage.goodbye :
59
                    GoodByeView()
60
                    .environmentObject(appNavigation)
61
                    .environmentObject(networkMonitor)
17 efrain 62
 
63
                case AppMainPage.videoplayer :
64
                    VideoPlayerView()
65
                    .environmentObject(appNavigation)
66
                    .environmentObject(networkMonitor)
8 efrain 67
 
17 efrain 68
                case AppMainPage.pdfviewer :
69
                    PdfViewerView()
70
                    .environmentObject(appNavigation)
71
                    .environmentObject(networkMonitor)
72
 
73
                case AppMainPage.webviewer :
74
                    WebViewerView()
75
                    .environmentObject(appNavigation)
76
                    .environmentObject(networkMonitor)
77
 
19 efrain 78
                case AppMainPage.download :
79
                    DownloadFileView()
80
                    .environmentObject(appNavigation)
81
                    .environmentObject(networkMonitor)
82
 
8 efrain 83
                default :
84
                    HomeView()
85
                    .environmentObject(appNavigation)
86
                    .environmentObject(networkMonitor)
87
                    .transition(.scale)
1 efrain 88
 
8 efrain 89
                }
1 efrain 90
 
91
                    //Spacer()
92
                    /*
93
                    if(self.networkMonitor.status == .disconnected) {
94
                        Text("The popup")
95
                        .frame(width: UIScreen.main.bounds.width, height: 60)
96
                        .background(Color("color_error_background"))
97
                        .foregroundColor(Color("color_error_foreground"))
98
                        .font(.callout)
99
                    }*/
100
            }
24 efrain 101
        }
102
        .onAppear {
31 efrain 103
 
32 efrain 104
            if appData.signoutActionRequired {
105
                signout()
106
            } else {
107
                if appData.refreshContentMessageShowPending {
108
                    let snackbar = TTGSnackbar(message: appData.refreshContentMessage, duration: .forever)
109
                    // Action 1
110
                    snackbar.actionText = Config.LANG_COMMNON_UPDATE
111
                    snackbar.actionTextColor = UIColor( Color("color_snackbar_button_yes"))
112
                    snackbar.actionBlock = { (snackbar) in  refreshContent() }
113
 
31 efrain 114
 
32 efrain 115
                    snackbar.show()
116
 
117
 
118
                }
24 efrain 119
            }
120
        }
121
        .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMMAND_REFRESH_CONTENT))
17 efrain 122
        { data in
123
            if data.userInfo != nil {
124
                let newCapsules = Int(data.userInfo?["new_capsules"]! as? String ?? "0") ?? 0
125
                let isForeground = (Int(data.userInfo?["is_foreground"]! as? String ?? "0") ?? 0) == 1
126
 
127
                if newCapsules > 0 {
27 efrain 128
                    let title = data.userInfo?["title"]! as? String ?? ""
129
                    let body = data.userInfo?["body"]! as? String ?? ""
130
 
131
                    let command = Constants.NOTIFICATION_COMMAND_REFRESH_CONTENT
132
                    let now = Date()
133
                    let dateFormatter = DateFormatter()
134
                    dateFormatter.dateFormat = Constants.FORMAT_DATE_YMD
135
 
136
                    let timeFormatter = DateFormatter()
137
                    timeFormatter.dateFormat = Constants.FORMAT_TIME_12
138
 
139
                    let dateOn = dateFormatter.string(from: now)
140
                    let timeOn = timeFormatter.string(from: now)
141
 
142
                    let userNotificationDao = UserNotificationDao.sharedInstance
143
                    var userNotification : UserNotificationModel  = UserNotificationModel(userUuid: appData.userUuid, title: title, body: body, viewed: 0, url: "", command: command, dateOn: dateOn, timeOn: timeOn)
144
 
145
 
146
 
147
                    userNotificationDao.insert(userNotification: userNotification)
148
 
149
 
150
 
17 efrain 151
                    let message = newCapsules == 1 ? Config.LANG_MESSAGE_NEW_CONTENT_THERE_IS_ONE_CAPSULE : Config.LANG_MESSAGE_NEW_CONTENT_THERE_ARE_MULTIPLE_CAPSULES.replacingOccurrences(of: "%s", with: String(newCapsules))
152
 
153
                    if isForeground {
31 efrain 154
                        let snackbar = TTGSnackbar(message:  message, duration: .forever)
155
                        // Action 1
156
                        snackbar.actionText = Config.LANG_COMMNON_UPDATE
157
                        snackbar.actionTextColor = UIColor( Color("color_snackbar_button_yes"))
158
                        snackbar.actionBlock = { (snackbar) in  refreshContent() }
159
 
32 efrain 160
 
31 efrain 161
 
162
 
17 efrain 163
                        snackbar.show()
24 efrain 164
                    }  else {
165
                        appData.refreshContentMessage = message
25 efrain 166
                        appData.refreshContentMessageShowPending = true
24 efrain 167
                        appData.save()
17 efrain 168
                    }
169
                }
170
            }
1 efrain 171
 
172
        }
17 efrain 173
        .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMMAND_EXIT))
174
       { data in
32 efrain 175
            if data.userInfo != nil {
176
                let isForeground = (Int(data.userInfo?["is_foreground"]! as? String ?? "0") ?? 0) == 1
1 efrain 177
 
32 efrain 178
 
179
                if isForeground {
180
                    signout()
181
                } else {
182
                    appData.signoutActionRequired = true
183
                    appData.save()
184
                }
185
 
186
 
187
            }
17 efrain 188
 
32 efrain 189
 
1 efrain 190
 
24 efrain 191
       }.onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_PUSH))
192
        { data in
193
 
194
            let title = data.userInfo?["title"]! as? String ?? ""
195
            let body = data.userInfo?["body"]! as? String ?? ""
196
            let url = data.userInfo?["url"]! as? String ?? ""
25 efrain 197
 
27 efrain 198
            let command = url.isEmpty ? "" : Constants.NOTIFICATION_COMMAND_OPEN_URL
199
            let viewed = url.isEmpty ? 1 : 0
200
            let now = Date()
201
            let dateFormatter = DateFormatter()
202
            dateFormatter.dateFormat = Constants.FORMAT_DATE_YMD
25 efrain 203
 
27 efrain 204
            let timeFormatter = DateFormatter()
205
            timeFormatter.dateFormat = Constants.FORMAT_TIME_12
25 efrain 206
 
27 efrain 207
            let dateOn = dateFormatter.string(from: now)
208
            let timeOn = timeFormatter.string(from: now)
25 efrain 209
 
27 efrain 210
            let userNotificationDao = UserNotificationDao.sharedInstance
211
            var userNotification : UserNotificationModel  = UserNotificationModel(userUuid: appData.userUuid, title: title, body: body, viewed: viewed, url: url, command: command, dateOn: dateOn, timeOn: timeOn)
24 efrain 212
 
213
 
214
 
27 efrain 215
            userNotificationDao.insert(userNotification: userNotification)
24 efrain 216
        }
1 efrain 217
 
218
 
219
    }
31 efrain 220
 
32 efrain 221
    func signout() -> Void {
222
        appData.userUuid = ""
223
        appData.userFirstname = ""
224
        appData.userLastname = ""
225
        appData.userEmail = ""
226
        appData.userImage = ""
227
        appData.refreshContentActionRequired = false
31 efrain 228
        appData.refreshContentMessage = ""
229
        appData.refreshContentMessageShowPending = false
32 efrain 230
        appData.signoutActionRequired = false
31 efrain 231
        appData.save()
232
 
32 efrain 233
        appNavigation.pageActive = .goodbye
234
    }
235
 
236
    func refreshContent() -> Void {
237
        appData.refreshContentMessage = ""
238
        appData.refreshContentMessageShowPending = false
31 efrain 239
        appData.refreshContentActionRequired = true
240
        appData.save()
241
 
242
        withAnimation {
243
            appNavigation.subpageActive = .topics
244
        }
32 efrain 245
 
31 efrain 246
 
247
    }
1 efrain 248
}
249
 
250
struct MainView_Previews: PreviewProvider {
251
    static var previews: some View {
252
        MainView()
253
    }
254
}
255