Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 1 | Rev 9 | 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
//  CardGalleryView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 3/6/22.
6
//
7
 
8
import Foundation
9
import SwiftUI
10
import AVKit
8 efrain 11
import NavigationStack
1 efrain 12
 
13
struct CardGalleryView: View {
14
 
8 efrain 15
    @EnvironmentObject private var networkMonitor : NetworkMonitor
1 efrain 16
    @EnvironmentObject private var appNavigation : AppNavigation
17
    @ObservedObject private var viewModel = GalleryCardViewModel()
18
 
8 efrain 19
    @State private var goToVideoPlayer : Bool = false
20
    @State private var goToPdfViewer : Bool = false
21
    @State private var goToWebViewer : Bool = false
22
 
23
    @State private var showGlobalAlert : Bool = false
24
    @State private var titleGlobalAlert : String = ""
25
    @State private var messageGlobalAlert : String = ""
26
 
27
 
1 efrain 28
    private let appData = AppData.sharedInstance
29
 
30
    private var buttonShow : Bool = false
31
    private var buttonTitle : String = ""
32
 
8 efrain 33
 
1 efrain 34
 
35
 
36
 
37
 
38
 
39
 
40
    init (slideUuid : String) {
41
 
42
        self.viewModel.fetch(slideUuid: slideUuid, userUuid: appData.userUuid)
43
 
44
 
45
 
46
 
47
 
48
        self.buttonShow = false
49
        if self.viewModel.slide.type == Constants.SLIDE_TYPE_QUIZ  {
50
            self.buttonTitle = Config.LANG_BUTTON_LAUNCH_QUIZ
51
            self.buttonShow = true
52
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO {
53
            self.buttonTitle =  Config.LANG_BUTTON_LAUNCH_VIDEO_PLAYER
54
            self.buttonShow = true
55
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_AUDIO   {
56
            self.buttonTitle = Config.LANG_BUTTON_LAUNCH_AUDIO_PLAYER
57
            self.buttonShow = true
58
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_DOCUMENT {
59
            self.buttonTitle = Config.LANG_BUTTON_LAUNCH_VIEW_PDF
60
            self.buttonShow = true
8 efrain 61
 
62
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_TEXT {
63
            self.buttonTitle = Config.LANG_BUTTON_LAUNCH_VIEW_PDF
64
            self.buttonShow = true
1 efrain 65
        } else {
66
            self.buttonShow = false
67
        }
68
 
69
    }
70
 
71
 
72
 
73
 
74
    var body: some View {
75
        GeometryReader { geometry in
76
            let checkMarkX = (geometry.size.width - 50) / 2
77
            let checkMarkY = -1 * ((geometry.size.height - 50) / 2)
78
 
79
            let buttonX = (geometry.size.width - 180) / 2
80
            let buttonY = (geometry.size.height - 80) / 2
81
 
82
            ZStack {
83
                if self.viewModel.slide.completed == 1  {
8 efrain 84
                    Image(uiImage: UIImage(named: "ic_slide_completado_checkmark") ?? UIImage()).offset(x: checkMarkX, y: checkMarkY).zIndex(10000)
1 efrain 85
                }
86
 
87
                if self.buttonShow {
88
 
89
                    Button(action: {
90
                        btnPlay()
91
                    }, label: {
92
                        Text(self.buttonTitle)
93
                            .font(Font.custom(Config.FONT_NAME_BOLD, size: Config.FONT_SIZE_BUTTONS))
94
                        .foregroundColor(Color("color_button_gallery_foreground"))
95
 
96
                    })
97
 
98
                    .padding(.vertical, 15)
99
                    .padding(.horizontal, 10)
100
                    .background(Color("color_button_gallery_background"))
101
                    .cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
102
                    .offset(x: buttonX, y: buttonY)
103
                    .zIndex(10000)
104
                }
105
 
106
                if (self.viewModel.progressCapsule >= 100 && self.viewModel.completedCapsule == 0)
107
                    || (self.viewModel.progressTopic >= 100 && self.viewModel.completedTopic == 0) {
108
                    Button(action: {
109
                        if self.viewModel.progressTopic >= 100 && self.viewModel.completedTopic == 0 {
110
 
111
                            appNavigation.subpageActive = .finishtopic
112
 
113
                        } else {
114
                            if self.viewModel.progressCapsule >= 100 && self.viewModel.completedCapsule == 0 {
115
                                appNavigation.subpageActive = .finishcapsule
116
                            }
117
                        }
118
                    }, label: {
119
                        Text(Config.LANG_BUTTON_FINISH_CAPSULE_OR_TOPIC)
120
                        .font(.callout)
121
                        .foregroundColor(.black)
122
                    })
123
                    .padding()
124
                    .background(Color("color_button_gallery_background"))
125
                    .cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
126
                    .zIndex(10000)
127
                }
128
 
129
                VStack {
8 efrain 130
 
131
                    if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO || self.viewModel.slide.type == Constants.SLIDE_TYPE_AUDIO {
132
                    PushView(
133
                        destination: VideoPlayerView(slideUuid: viewModel.slide.uuid),
134
                        isActive: self.$goToVideoPlayer,
135
                        label: {
136
                            Text("")
137
                        }).frame(height: 0)
138
                    }
139
 
140
                    if self.viewModel.slide.type == Constants.SLIDE_TYPE_DOCUMENT {
141
 
142
                        PushView(
143
                            destination: PdfViewerView(slideUuid: viewModel.slide.uuid),
144
                            isActive: self.$goToPdfViewer,
145
                            label: {
146
                                Text("")
147
                            }).frame(height: 0)
148
                    }
149
 
150
 
151
                    if self.viewModel.slide.type == Constants.SLIDE_TYPE_TEXT {
152
 
153
                        PushView(
154
                            destination: WebViewerView(slideUuid: viewModel.slide.uuid),
155
                            isActive: self.$goToWebViewer,
156
                            label: {
157
                                Text("")
158
                        }).frame(height: 0)
159
                    }
160
 
1 efrain 161
                    Spacer()
162
                    HStack {
163
                        if viewModel.slide.background.isEmpty && viewModel.slide.file.isEmpty {
164
                            Image(uiImage: UIImage(named: "logo") ?? UIImage())
165
                            .resizable()
166
                            .aspectRatio(contentMode: .fit)
167
                        } else {
168
                            CustomAsyncImage(
169
                                url: URL(string: self.viewModel.slide.type == Constants.SLIDE_TYPE_IMAGE ? self.viewModel.slide.file : self.viewModel.slide.background)!,
170
                                placeholder: {
171
                                    Spacer()
172
                                    Text(Config.LANG_COMMON_LOADING).font(.footnote).foregroundColor(.black)
173
                                    Spacer()
174
                                },
175
                                image: {
176
                                    Image(uiImage: $0).resizable()
177
                                }
178
                            )
179
                        }
180
                    }.padding(.horizontal, 5)
181
 
182
                    Spacer()
183
                }
184
               // .zIndex(500)
185
 
186
            } .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMPLETED_SLIDE))
187
            { data in
188
               // print("CardGalleryView  Receive " )
189
 
190
                if data.userInfo != nil {
191
                    if let slideUuid = data.userInfo?["slideUuid"]! as? String {
192
                        if !slideUuid.isEmpty && slideUuid == self.viewModel.slide.uuid {
193
                            self.viewModel.fetchProgress(slideUuid: slideUuid, userUuid: appData.userUuid)
194
                        }
195
                    }
196
                }
197
 
8 efrain 198
            } .alert(isPresented: $showGlobalAlert) {
199
                Alert(
200
                    title: Text(self.titleGlobalAlert),
201
                    message: Text(self.messageGlobalAlert),
202
                    dismissButton: .default(Text(Config.LANG_COMMON_OK))
203
                )
1 efrain 204
            }
205
        }
206
    }
207
 
208
    private func btnPlay()
209
    {
210
 
8 efrain 211
        if networkMonitor.status == .disconnected {
1 efrain 212
 
8 efrain 213
            self.titleGlobalAlert = Config.LANG_ERROR_NETWORK_TITLE
214
            self.messageGlobalAlert = Config.LANG_ERROR_NETWORK_MESSAGE_LONG
215
            self.showGlobalAlert  = true
216
            return
217
        }
218
 
219
 
1 efrain 220
        if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO {
221
            AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
222
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
223
            UIViewController.attemptRotationToDeviceOrientation()
224
 
225
 
8 efrain 226
            self.goToVideoPlayer = true
227
 
228
 
229
 
1 efrain 230
        }  else if self.viewModel.slide.type == Constants.SLIDE_TYPE_AUDIO {
231
 
8 efrain 232
            self.goToVideoPlayer = true
233
 
1 efrain 234
        }  else if self.viewModel.slide.type == Constants.SLIDE_TYPE_DOCUMENT {
235
 
8 efrain 236
            self.goToPdfViewer = true
237
 
1 efrain 238
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_TEXT {
239
 
8 efrain 240
            self.goToWebViewer = true
1 efrain 241
 
242
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_QUIZ {
243
 
8 efrain 244
 
1 efrain 245
        }
246
    }
247
 
248
 
249
 
250
}
251
 
252
struct CardGalleryView_Previews: PreviewProvider {
253
    static var previews: some View {
254
        CardGalleryView(slideUuid: "S123")
255
    }
256
 
257
}
258