Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 17 | Rev 23 | 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: {
21 efrain 80
                    backToGallery()
1 efrain 81
                }, label: {
82
                    Image(systemName: "chevron.backward")
83
                    .frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
84
                    .aspectRatio(contentMode: .fit)
85
                    .foregroundColor(Color("color_app_bar_foreground"))
86
                })
87
                .padding(.leading, 16)
88
 
89
                Text(self.slideTitle)
90
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
91
                .foregroundColor(Color("color_app_bar_foreground"))
92
                    .padding(.leading, 4)
93
 
94
                Spacer()
95
            }
96
            .background(Color("color_app_bar_background"))
97
            .edgesIgnoringSafeArea(.top)
98
            .frame(height: 50)
99
            Divider()
17 efrain 100
 
1 efrain 101
 
17 efrain 102
            GeometryReader { geometry in
103
 
104
                VideoPlayer(player: player)
105
                .onAppear() {
106
                    player.play()
107
                }
108
                .onDisappear() {
109
                    player.pause()
110
                }
111
 
112
                .onReceive(timer, perform: { _ in
113
                    let duration =  CMTimeGetSeconds(playerItem.asset.duration)
114
                    if duration > 0 {
115
 
116
                        let currentTime =  CMTimeGetSeconds(playerItem.currentTime())
117
                            let diference = duration - currentTime
118
 
119
                            if diference < 10 {
120
                                self.isCompleted = true;
121
                                self.timer.upstream.connect().cancel()
122
                                timerActive = false
21 efrain 123
 
124
                                backToGallery()
17 efrain 125
                            }
126
                        }
127
                    })
11 efrain 128
            }
1 efrain 129
        }
130
    }
131
 
21 efrain 132
    private func backToGallery()
133
    {
134
        if self.url != nil {
135
            do {
136
                try FileManager.default.removeItem(at: self.url!)
137
                       print("Video temporal borrado")
138
             } catch {
139
                print(error)
140
            }
141
        }
142
 
143
        let dataService = DataService()
144
        if self.isCompleted {
145
            dataService.completeSlide(slide: slideModel)
146
        } else {
147
            dataService.incompleteSlide(slide: slideModel)
148
        }
149
 
150
        if self.timerActive {
151
            self.timer.upstream.connect().cancel()
152
        }
153
 
154
        AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
155
        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
156
        UIViewController.attemptRotationToDeviceOrientation()
157
 
158
 
159
        appNavigation.pageActive = .home
160
    }
1 efrain 161
 
21 efrain 162
 
1 efrain 163
 
164
 
165
}
166
 
167
 
168
struct VideoPlayerView_Previews: PreviewProvider {
169
    static var previews: some View {
17 efrain 170
        VideoPlayerView()
1 efrain 171
    }
172
}
173