Proyectos de Subversion Iphone Microlearning

Rev

| 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
11
 
12
 
13
struct CardGalleryView: View {
14
 
15
    private var colorButtonLauncherBackGround = Color(UIColor(hex: Config.COLOR_BUTTON_GALLERY_LAUNCHER_BACKGROUND) ?? .gray)
16
 
17
    private var buttonShow : Bool = false
18
    private var buttonTitle : String = ""
19
 
20
    @ObservedObject private var viewModel = SlideCardViewModel()
21
    private var position = 0
22
 
23
    @State private var goToVideoPlayer : Bool = false
24
    //@State private var finalizeShow : Bool = false
25
    @State private var goToFinishCapsule : Bool = false
26
    @State private var goToFinishTopic : Bool = false
27
 
28
 
29
 
30
 
31
 
32
    init (slideUuid : String, position : Int) {
33
        let preference = Preference.sharedInstance
34
        self.viewModel.fetch(slideUuid: slideUuid, userUuid: preference.userUuid)
35
        self.position = 0
36
 
37
 
38
 
39
 
40
 
41
        self.buttonShow = false
42
        if self.viewModel.slide.type == Constants.SLIDE_TYPE_QUIZ  {
43
            self.buttonTitle = "Realizar test"
44
            self.buttonShow = true
45
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO {
46
            self.buttonTitle = "Ver video"
47
            self.buttonShow = true
48
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_AUDIO   {
49
            self.buttonTitle = "Escuchar audio"
50
            self.buttonShow = true
51
        } else if self.viewModel.slide.type == Constants.SLIDE_TYPE_DOCUMENT {
52
            self.buttonTitle = "Ver documento"
53
            self.buttonShow = true
54
        } else {
55
            self.buttonShow = false
56
        }
57
 
58
        /*
59
        if !self.viewModel.slide.background.isEmpty {
60
            self.created = Int(round(Date().timeIntervalSince1970))
61
            self.rand = Int.random(in: 0..<8999) + 1000
62
            self.token = preference.deviceUuid
63
 
64
            let password = preference.password
65
            let md5Data = MD5(string: "\(preference.password)" + ":" + "\(created)" + ":" + "\(rand)")
66
            self.secret =  md5Data.map {
67
                String(format: "%02hhx", $0)
68
 
69
            }.joined()
70
            //print(secret)
71
        }
72
        */
73
    }
74
 
75
 
76
 
77
 
78
    var body: some View {
79
 
80
        GeometryReader { geometry in
81
            let checkMarkX = (geometry.size.width - 50) / 2
82
            let checkMarkY = -1 * ((geometry.size.height - 50) / 2)
83
 
84
            let buttonX = (geometry.size.width - 180) / 2
85
            let buttonY = (geometry.size.height - 80) / 2
86
 
87
            //if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO {
88
            NavigationLink("", destination: VideoPlayerView(slideModel: self.viewModel.slide, position:  self.position), isActive: self.$goToVideoPlayer).frame( height: 0)
89
            //}
90
 
91
            //if self.finalizeShow {
92
 
93
                NavigationLink("", destination: FinishCapsuleView(capsuleUuid: self.viewModel.slide.capsuleUuid), isActive: self.$goToFinishCapsule).frame( height: 0)
94
 
95
                NavigationLink("", destination: FinishTopicView(capsuleUuid: self.viewModel.slide.capsuleUuid), isActive: self.$goToFinishTopic).frame( height: 0)
96
            //}
97
 
98
 
99
 
100
 
101
            ZStack {
102
 
103
 
104
                if self.viewModel.slide.completed == 1  {
105
 
106
                    Image(uiImage: UIImage(named: "ic_slide_completado_checkmark") ?? UIImage()).alignmentGuide(.top, computeValue: { dimension in
107
                       10
108
                    }).offset(x: checkMarkX, y: checkMarkY).zIndex(10000)
109
                }
110
 
111
 
112
 
113
 
114
                if self.buttonShow {
115
 
116
                    Button(action: {
117
                        btnPlay()
118
                    }, label: {
119
                        Text(self.buttonTitle)
120
                        .font(.callout)
121
                            .foregroundColor(.black)
122
 
123
                    })
124
 
125
                    .padding(.vertical, 15)
126
                    .padding(.horizontal, 10)
127
                    .background(colorButtonLauncherBackGround)
128
                    .cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
129
                    .offset(x: buttonX, y: buttonY)
130
                    .zIndex(10000)
131
                }
132
 
133
                if (self.viewModel.progressCapsule.progress >= 100 && self.viewModel.progressCapsule.completed == 0)
134
                    || (self.viewModel.progressTopic.progress >= 100 && self.viewModel.progressTopic.completed == 0) {
135
                    Button(action: {
136
 
137
                       // let preference = Preference.sharedInstance
138
                        //let progressDao = ProgressDao()
139
                        //let progressTopic = progressDao.selectByTopicUuidAndUserUuid(topicUuid: self.viewModel.slide.topicUuid, userUuid: preference.userUuid)
140
 
141
                        if self.viewModel.progressTopic.id > 0 && self.viewModel.progressTopic.progress >= 100 && self.viewModel.progressTopic.completed == 0 {
142
 
143
                            self.goToFinishTopic = true
144
 
145
                        } else {
146
 
147
//let progressCapsule = progressDao.selectByCapsuleUuidAndUserUuid(capsuleUuid: self.viewModel.slide.capsuleUuid, userUuid: preference.userUuid)
148
 
149
                            if self.viewModel.progressCapsule.id > 0 && self.viewModel.progressCapsule.progress >= 100 && self.viewModel.progressCapsule.completed == 0 {
150
 
151
                                self.goToFinishCapsule = true
152
                            }
153
 
154
                        }
155
 
156
 
157
                    }, label: {
158
                        Text("Finalizar")
159
                        .font(.callout)
160
                            .foregroundColor(.black)
161
 
162
                    })
163
 
164
                    .padding()
165
                    .background(colorButtonLauncherBackGround)
166
                    .cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
167
                    .zIndex(10000)
168
 
169
                }
170
 
171
 
172
 
173
 
174
                VStack {
175
                    Spacer()
176
 
177
 
178
                    HStack {
179
                        if viewModel.slide.background.isEmpty && viewModel.slide.file.isEmpty {
180
 
181
 
182
                            Image(uiImage: UIImage(named: "logo") ?? UIImage())
183
                                .resizable()
184
                                .aspectRatio(contentMode: .fit)
185
 
186
 
187
                        } else {
188
 
189
                            CustomAsyncImage(
190
                                url: URL(string: self.viewModel.slide.type == Constants.SLIDE_TYPE_IMAGE ? self.viewModel.slide.file : self.viewModel.slide.background)!,
191
                                placeholder: {
192
                                    Spacer()
193
                                    Text("Cargando...").font(.footnote).foregroundColor(.black)
194
                                    Spacer()
195
 
196
                                },
197
                                image: {
198
                                    Image(uiImage: $0).resizable()
199
                                }
200
                            )
201
                        }
202
                    }.padding(.horizontal, 5)
203
 
204
                    Spacer()
205
                }
206
                .zIndex(500)
207
 
208
 
209
 
210
            } .onAppear {
211
                //print("CardGalleryView : \(self.viewModel.slide.name) " )
212
 
213
                self.goToVideoPlayer = false
214
                self.goToFinishCapsule = false
215
                self.goToFinishTopic = false
216
 
217
            } .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_COMPLETED_SLIDE))
218
            { data in
219
               // print("CardGalleryView  Receive " )
220
 
221
                if data.userInfo != nil {
222
                    if let slideUuid = data.userInfo?["slideUuid"]! as? String {
223
                        if !slideUuid.isEmpty && slideUuid == self.viewModel.slide.uuid {
224
 
225
                            let preference = Preference.sharedInstance
226
                            self.viewModel.fetch(slideUuid: slideUuid, userUuid: preference.userUuid)
227
                        }
228
                    }
229
                }
230
            } .onReceive(NotificationCenter.default.publisher(for: Constants.NOTIFICATION_NAME_INCOMPLETED_SLIDE))
231
            { data in
232
               // print("CardGalleryView  Receive " )
233
 
234
                if data.userInfo != nil {
235
                    if let slideUuid = data.userInfo?["slideUuid"]! as? String {
236
                        if !slideUuid.isEmpty && slideUuid == self.viewModel.slide.uuid {
237
 
238
 
239
                        }
240
                    }
241
                }
242
            }
243
        }
244
    }
245
 
246
    private func btnPlay()
247
    {
248
        if self.viewModel.slide.type == Constants.SLIDE_TYPE_VIDEO {
249
           self.goToVideoPlayer = true
250
        }
251
    }
252
    /*
253
    func MD5(string: String) -> Data {
254
        let length = Int(CC_MD5_DIGEST_LENGTH)
255
        let messageData = string.data(using:.utf8)!
256
        var digestData = Data(count: length)
257
 
258
        _ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
259
        messageData.withUnsafeBytes { messageBytes -> UInt8 in
260
        if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
261
                let messageLength = CC_LONG(messageData.count)
262
                    CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
263
                }
264
            return 0
265
            }
266
        }
267
        return digestData
268
    }*/
269
 
270
 
271
}
272
 
273
struct CardGalleryView_Previews: PreviewProvider {
274
    static var previews: some View {
275
        CardGalleryView(slideUuid: "S123", position: 0)
276
    }
277
 
278
}
279