Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 21 | Rev 25 | 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
//  VideoPlayerView.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 4/24/22.
6
//
7
import Foundation
8
import SwiftUI
9
import AVKit
17 efrain 10
import Network
11
import Alamofire
12
import SwiftyJSON
1 efrain 13
 
14
struct VideoPlayerView: View {
17 efrain 15
    @EnvironmentObject private var appNavigation : AppNavigation
1 efrain 16
    @State private var timerActive : Bool = false
17
    @State private var isCompleted : Bool = false
17 efrain 18
 
1 efrain 19
    private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
17 efrain 20
 
21
    private var slideModel : SlideModel
22
    private var slideTitle : String
23
 
24
 
25
    private var url : URL?
1 efrain 26
    private var playerItem : AVPlayerItem
27
    private var player : AVPlayer
17 efrain 28
    private var appData = AppData.sharedInstance
1 efrain 29
 
17 efrain 30
    init()
1 efrain 31
    {
17 efrain 32
        print("urlExternalDownloaded : \(appData.urlExternalDownloaded)")
33
        self.url = URL(string: appData.urlExternalDownloaded)
34
 
1 efrain 35
        let slideDao = SlideDao.sharedInstance
17 efrain 36
        self.slideModel = slideDao.selectByUuid(uuid: appData.slideUuidActive)
1 efrain 37
 
17 efrain 38
        print("videoplayer appData slideUuid : \(appData.slideUuidActive)")
39
        print("videoplayer slideModel slideUuid : \(self.slideModel.uuid)")
40
 
1 efrain 41
        if self.slideModel.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
42
            slideTitle = String(Array(self.slideModel.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
43
        } else {
44
            slideTitle = self.slideModel.name
45
        }
46
 
17 efrain 47
 
1 efrain 48
        let headerSecurity = HeaderSecurity()
49
 
50
        let headers: [String: String] = [
51
            Constants.HTTP_HEADER_ACCEPT: Constants.HTTP_HEADER_ACCEPT_VALUE,
52
            Constants.HTTP_HEADER_SECURITY_RAND: String(headerSecurity.rand),
53
            Constants.HTTP_HEADER_SECURITY_TOKEN: appData.deviceUuid,
54
            Constants.HTTP_HEADER_SECURITY_CREATED: String(headerSecurity.created) ,
55
            Constants.HTTP_HEADER_SECURITY_SECRET: headerSecurity.secret,
56
        ]
57
 
17 efrain 58
        let assets = AVURLAsset(url: self.url!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
11 efrain 59
 
1 efrain 60
        playerItem = AVPlayerItem(asset: assets)
61
        let newTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale:1)
62
 
63
        player = AVPlayer(playerItem: playerItem)
64
        player.seek(to: newTime)
65
 
66
 
67
        self.timerActive = true
17 efrain 68
 
1 efrain 69
 
70
 
71
 
72
    }
73
 
74
    var body: some View {
75
        VStack(spacing: 0) {
8 efrain 76
 
17 efrain 77
 
1 efrain 78
            HStack {
79
                Button(action: {
23 efrain 80
 
81
 
82
 
21 efrain 83
                    backToGallery()
1 efrain 84
                }, label: {
85
                    Image(systemName: "chevron.backward")
86
                    .frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
87
                    .aspectRatio(contentMode: .fit)
88
                    .foregroundColor(Color("color_app_bar_foreground"))
89
                })
90
                .padding(.leading, 16)
91
 
92
                Text(self.slideTitle)
93
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
94
                .foregroundColor(Color("color_app_bar_foreground"))
95
                    .padding(.leading, 4)
96
 
97
                Spacer()
98
            }
99
            .background(Color("color_app_bar_background"))
100
            .edgesIgnoringSafeArea(.top)
101
            .frame(height: 50)
102
            Divider()
17 efrain 103
 
1 efrain 104
 
17 efrain 105
            GeometryReader { geometry in
106
 
107
                VideoPlayer(player: player)
108
                .onAppear() {
109
                    player.play()
110
                }
111
                .onDisappear() {
112
                    player.pause()
113
                }
114
 
115
                .onReceive(timer, perform: { _ in
116
                    let duration =  CMTimeGetSeconds(playerItem.asset.duration)
117
                    if duration > 0 {
118
 
119
                        let currentTime =  CMTimeGetSeconds(playerItem.currentTime())
120
                            let diference = duration - currentTime
121
 
122
                            if diference < 10 {
123
                                self.isCompleted = true;
23 efrain 124
                            } else if diference <= 0 {
17 efrain 125
                                self.timer.upstream.connect().cancel()
126
                                timerActive = false
21 efrain 127
                                backToGallery()
17 efrain 128
                            }
23 efrain 129
 
130
 
17 efrain 131
                        }
132
                    })
11 efrain 133
            }
1 efrain 134
        }
135
    }
136
 
21 efrain 137
    private func backToGallery()
138
    {
139
        if self.url != nil {
140
            do {
141
                try FileManager.default.removeItem(at: self.url!)
142
                       print("Video temporal borrado")
143
             } catch {
144
                print(error)
145
            }
146
        }
147
 
148
        let dataService = DataService()
149
        if self.isCompleted {
150
            dataService.completeSlide(slide: slideModel)
151
        } else {
152
            dataService.incompleteSlide(slide: slideModel)
153
        }
154
 
155
        if self.timerActive {
156
            self.timer.upstream.connect().cancel()
157
        }
158
 
159
        AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
160
        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
161
        UIViewController.attemptRotationToDeviceOrientation()
162
 
163
 
164
        appNavigation.pageActive = .home
165
    }
1 efrain 166
 
21 efrain 167
 
1 efrain 168
 
169
 
170
}
171
 
172
 
173
struct VideoPlayerView_Previews: PreviewProvider {
174
    static var previews: some View {
17 efrain 175
        VideoPlayerView()
1 efrain 176
    }
177
}
178