Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 14 | Rev 19 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 14 Rev 17
Línea 6... Línea 6...
6
//
6
//
Línea 7... Línea 7...
7
 
7
 
8
import Foundation
8
import Foundation
9
import SwiftUI
9
import SwiftUI
10
import AVKit
10
import AVKit
-
 
11
import Network
-
 
12
import Alamofire
-
 
13
import SwiftyJSON
Línea 11... Línea 14...
11
import NavigationStack
14
import TTGSnackbar
Línea 12... Línea 15...
12
 
15
 
13
struct CardGalleryView: View {
16
struct CardGalleryView: View {
14
    
17
    
Línea 15... Línea -...
15
    @EnvironmentObject private var networkMonitor : NetworkMonitor
-
 
16
    @EnvironmentObject private var appNavigation : AppNavigation
-
 
17
    @ObservedObject private var viewModel = GalleryCardViewModel()
-
 
-
 
18
    @EnvironmentObject private var networkMonitor : NetworkMonitor
Línea 18... Línea 19...
18
    
19
    @EnvironmentObject private var appNavigation : AppNavigation
19
    @State private var goToVideoPlayer : Bool = false
20
    @ObservedObject private var viewModel = GalleryCardViewModel()
20
    @State private var goToPdfViewer : Bool = false
21
    
Línea -... Línea 22...
-
 
22
 
-
 
23
    
-
 
24
    @State private var showGlobalAlert : Bool = false
Línea 21... Línea 25...
21
    @State private var goToWebViewer : Bool = false
25
    @State private var titleGlobalAlert : String = ""
22
    
26
    @State private var messageGlobalAlert : String = ""
Línea 23... Línea 27...
23
    @State private var showGlobalAlert : Bool = false
27
    
24
    @State private var titleGlobalAlert : String = ""
-
 
25
    @State private var messageGlobalAlert : String = ""
-
 
26
    
-
 
-
 
28
    
Línea 27... Línea 29...
27
    
29
    @State private var isDownloadInProgress = false
28
 
-
 
29
    
-
 
30
    private var buttonShow : Bool = false
30
    @State private var isDownloadCompleted = false
31
    private var buttonTitle : String = ""
-
 
32
 
31
    @State private var bytesDownloaded : Int64 = 0
Línea 33... Línea -...
33
    
-
 
34
    private let userUuid : String
-
 
35
   
-
 
36
   
-
 
37
    
32
 
38
 
33
    
39
    
34
    private var buttonShow : Bool = false
40
    init (slideUuid : String) {
35
    private var buttonTitle : String = ""
41
        let appDao = AppDao.sharedInstance
36
 
Línea 78... Línea 73...
78
        GeometryReader { geometry in
73
        GeometryReader { geometry in
79
            let checkMarkX = (geometry.size.width - 50) / 2
74
            let checkMarkX = (geometry.size.width - 50) / 2
80
            let checkMarkY = -1 * ((geometry.size.height - 50) / 2)
75
            let checkMarkY = -1 * ((geometry.size.height - 50) / 2)
Línea 81... Línea 76...
81
            
76
            
82
            let buttonX = (geometry.size.width - 180) / 2
77
            let buttonX = (geometry.size.width - 180) / 2
-
 
78
           // let buttonY = (geometry.size.height - 80) / 2
-
 
79
            
Línea 83... Línea 80...
83
            let buttonY = (geometry.size.height - 80) / 2
80
            let buttonY = (geometry.size.height - 100) / 2
-
 
81
          
-
 
82
            ZStack {
-
 
83
                if self.isDownloadInProgress {
-
 
84
                    VStack {
-
 
85
                        ProgressView()
-
 
86
                        .progressViewStyle(CircularProgressViewStyle(tint: Color("color_progress_view_foreground")))
-
 
87
                        .scaleEffect(3, anchor: .center).zIndex(100000)
-
 
88
                        
-
 
89
                        Text(String( self.bytesDownloaded) + " bytes descargados").zIndex(100000)
-
 
90
                    }.background(Color("color_filter_background"))
84
          
91
                }
85
            ZStack {
92
                
86
                if self.viewModel.slide.completed == 1  {
93
                if self.viewModel.slide.completed == 1  {
Línea 87... Línea 94...
87
                    Image(uiImage: UIImage(named: "ic_slide_completado_checkmark") ?? UIImage()).offset(x: checkMarkX, y: checkMarkY).zIndex(10000)
94
                    Image(uiImage: UIImage(named: "ic_slide_completado_checkmark") ?? UIImage()).offset(x: checkMarkX, y: checkMarkY).zIndex(10000)
Línea 129... Línea 136...
129
                    .zIndex(10000)
136
                    .zIndex(10000)
130
                }
137
                }
Línea 131... Línea 138...
131
               
138
               
Línea 132... Línea -...
132
                VStack {
-
 
133
                
-
 
134
                    if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO || self.viewModel.slide.type == Constants.SLIDE_TYPE_AUDIO {
-
 
135
                    PushView(
-
 
136
                        destination: VideoPlayerView(slideUuid: viewModel.slide.uuid),
-
 
137
                        isActive: self.$goToVideoPlayer,
-
 
138
                        label: {
-
 
139
                            Text("")
-
 
140
                        }).frame(height: 0)
-
 
141
                    }
-
 
142
                    
-
 
143
                    if self.viewModel.slide.type == Constants.SLIDE_TYPE_DOCUMENT {
-
 
144
              
-
 
145
                        PushView(
-
 
146
                            destination: PdfViewerView(slideUuid: viewModel.slide.uuid),
-
 
147
                            isActive: self.$goToPdfViewer,
-
 
148
                            label: {
-
 
149
                                Text("")
-
 
150
                            }).frame(height: 0)
-
 
Línea 151... Línea -...
151
                    }
-
 
152
                    
-
 
153
                    
-
 
154
                    if self.viewModel.slide.type == Constants.SLIDE_TYPE_TEXT {
-
 
155
                    
-
 
156
                        PushView(
-
 
157
                            destination: WebViewerView(slideUuid: viewModel.slide.uuid),
-
 
158
                            isActive: self.$goToWebViewer,
-
 
159
                            label: {
-
 
Línea 160... Línea 139...
160
                                Text("")
139
                VStack {
161
                        }).frame(height: 0)
140
                
162
                    }
141
                    
163
                    
142
                    
Línea 187... Línea 166...
187
               // .zIndex(500)
166
               // .zIndex(500)
Línea 188... Línea 167...
188
              
167
              
189
            }.onAppear {
168
            }.onAppear {
190
                self.viewModel.fetchProgress(slideUuid: self.viewModel.slide.uuid, userUuid: self.userUuid)
169
                self.viewModel.fetchProgress(slideUuid: self.viewModel.slide.uuid, userUuid: self.userUuid)
191
            }
-
 
192
            
-
 
193
            /*.onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMPLETED_SLIDE))
-
 
194
            { data in
-
 
195
               // print("CardGalleryView  Receive " )
-
 
196
                let appData = appDao.selectOne()
-
 
197
                if data.userInfo != nil {
-
 
198
                    if let slideUuid = data.userInfo?["slideUuid"]! as? String {
-
 
199
                        if !slideUuid.isEmpty && slideUuid == self.viewModel.slide.uuid {
-
 
200
                            self.viewModel.fetchProgress(slideUuid: slideUuid, userUuid: appData.userUuid)
-
 
201
                        }
-
 
202
                    }
-
 
203
                }
-
 
204
             }
-
 
205
          */
170
            }
206
             .alert(isPresented: $showGlobalAlert) {
171
             .alert(isPresented: $showGlobalAlert) {
207
                Alert(
172
                Alert(
208
                    title: Text(self.titleGlobalAlert),
173
                    title: Text(self.titleGlobalAlert),
209
                    message: Text(self.messageGlobalAlert),
174
                    message: Text(self.messageGlobalAlert),
Línea 214... Línea 179...
214
    }
179
    }
Línea 215... Línea 180...
215
    
180
    
216
    private func btnPlay()
181
    private func btnPlay()
Línea -... Línea 182...
-
 
182
    {
-
 
183
        
-
 
184
        switch self.viewModel.slide.type
-
 
185
        {
-
 
186
            case Constants.SLIDE_TYPE_TEXT :
-
 
187
                appNavigation.pageActive = .webviewer
-
 
188
                break;
-
 
189
            
-
 
190
            case Constants.SLIDE_TYPE_QUIZ :
-
 
191
                let snackbar = TTGSnackbar(message: "Por implementar", duration: .long)
-
 
192
                snackbar.show()
-
 
193
                break;
-
 
194
            
-
 
195
            default :
-
 
196
                downloadFile();
-
 
197
                break;
-
 
198
        }
-
 
199
        
-
 
200
        
-
 
201
        
-
 
202
    }
-
 
203
    
217
    {
204
    private func downloadFile()
Línea 218... Línea 205...
218
        
205
    {
219
        if networkMonitor.status == .disconnected {
206
        if networkMonitor.status == .disconnected {
220
        
207
        
221
            self.titleGlobalAlert = Config.LANG_ERROR_NETWORK_TITLE
208
            self.titleGlobalAlert = Config.LANG_ERROR_NETWORK_TITLE
222
            self.messageGlobalAlert = Config.LANG_ERROR_NETWORK_MESSAGE_LONG
209
            self.messageGlobalAlert = Config.LANG_ERROR_NETWORK_MESSAGE_LONG
Línea -... Línea 210...
-
 
210
            self.showGlobalAlert  = true
-
 
211
            return
-
 
212
        }
Línea 223... Línea -...
223
            self.showGlobalAlert  = true
-
 
224
            return
-
 
225
        }
-
 
226
        
-
 
227
        
-
 
228
        if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO {
-
 
229
            AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
-
 
230
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
-
 
231
            UIViewController.attemptRotationToDeviceOrientation()
-
 
232
            
-
 
233
            
213
        
234
            self.goToVideoPlayer = true
-
 
235
            
-
 
236
            
-
 
237
            
-
 
238
        }  else if self.viewModel.slide.type == Constants.SLIDE_TYPE_AUDIO {
-
 
239
            
-
 
240
            self.goToVideoPlayer = true
-
 
241
            
214
        guard let url = URL(string: viewModel.slide.file) else {
242
        }  else if self.viewModel.slide.type == Constants.SLIDE_TYPE_DOCUMENT {
-
 
243
            
-
 
244
            self.goToPdfViewer = true
215
            return
245
            
-
 
Línea -... Línea 216...
-
 
216
        }
-
 
217
        
-
 
218
        let arrayFullFilename = viewModel.slide.file.split(separator: "/")
-
 
219
        let filename  = String(arrayFullFilename[arrayFullFilename.count - 1])
-
 
220
 
-
 
221
        
-
 
222
 
-
 
223
        let headerSecurity = HeaderSecurity()
-
 
224
        
-
 
225
        let headers: HTTPHeaders = [
-
 
226
            .init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
-
 
227
            .init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
-
 
228
            .init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
-
 
229
            .init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
-
 
230
            .accept(Constants.HTTP_HEADER_ACCEPT)
-
 
231
        ]
-
 
232
        
-
 
233
        AF.request(url, method: .get, headers: headers)
-
 
234
        .downloadProgress(closure : { (progress) in
-
 
235
            self.isDownloadInProgress = true
-
 
236
            self.bytesDownloaded = progress.completedUnitCount
-
 
237
            
-
 
238
        })
-
 
239
        .responseData{ (response) in
-
 
240
            self.isDownloadInProgress = false
-
 
241
                        
-
 
242
            if let data = response.data {
-
 
243
             let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
-
 
244
                let resourceURL = documentsURL.appendingPathComponent(filename)
-
 
245
                do {
-
 
246
                    try data.write(to: resourceURL)
-
 
247
 
-
 
248
                    appData.urlExternalDownloaded = resourceURL.absoluteString
-
 
249
                    appData.save()
-
 
250
 
-
 
251
                    switch self.viewModel.slide.type
-
 
252
                    {
-
 
253
                        case  Constants.SLIDE_TYPE_VIDEO  :
-
 
254
                           // AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
-
 
255
                           //UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
-
 
256
                          //UIViewController.attemptRotationToDeviceOrientation()
-
 
257
                            
-
 
258
                            
-
 
259
                            self.appNavigation.pageActive = .videoplayer
-
 
260
                            break
-
 
261
                   
-
 
262
                        case Constants.SLIDE_TYPE_AUDIO :
-
 
263
                           self.appNavigation.pageActive = .videoplayer
-
 
264
                            break
-
 
265
                       
-
 
266
                        default :
-
 
267
                            self.appNavigation.pageActive = .pdfviewer
-
 
268
                            break
246
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_TEXT {
269
                    }
-
 
270
                } catch {
247
            
271
                    self.titleGlobalAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
248
            self.goToWebViewer = true
272
                    self.messageGlobalAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
Línea 249... Línea -...
249
            
-
 
250
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_QUIZ {
-
 
251
        
273
                    self.showGlobalAlert  = true
Línea 252... Línea 274...
252
            
274
                }
253
        }
275
               
254
    }
276
            }