Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

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

Rev 11 Rev 17
Línea 5... Línea 5...
5
//  Created by Efrain Yanez Recanatini on 4/24/22.
5
//  Created by Efrain Yanez Recanatini on 4/24/22.
6
//
6
//
7
import Foundation
7
import Foundation
8
import SwiftUI
8
import SwiftUI
9
import AVKit
9
import AVKit
10
import NavigationStack
10
import Network
-
 
11
import Alamofire
-
 
12
import SwiftyJSON
Línea 11... Línea 13...
11
 
13
 
-
 
14
struct VideoPlayerView: View {
12
struct VideoPlayerView: View {
15
    @EnvironmentObject private var appNavigation : AppNavigation
13
    @State private var timerActive : Bool = false
16
    @State private var timerActive : Bool = false
14
    @State private var isCompleted : Bool = false
-
 
15
    @State private var backToGallery : Bool = false
17
    @State private var isCompleted : Bool = false
16
  
18
 
17
    private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
-
 
18
    private let appDao = AppDao.sharedInstance
19
    private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
19
   
-
 
20
    private var playerItem : AVPlayerItem
-
 
21
    private var player : AVPlayer
20
 
22
    private var slideModel : SlideModel
21
    private var slideModel : SlideModel
-
 
22
    private var slideTitle : String
-
 
23
    
-
 
24
    
-
 
25
    private var url : URL?
-
 
26
    private var playerItem : AVPlayerItem
-
 
27
    private var player : AVPlayer
Línea 23... Línea 28...
23
    private var slideTitle : String
28
    private var appData = AppData.sharedInstance
24
 
29
 
-
 
30
    init()
25
    init(slideUuid : String)
31
    {
-
 
32
        print("urlExternalDownloaded : \(appData.urlExternalDownloaded)")
26
    {
33
        self.url = URL(string: appData.urlExternalDownloaded)
27
        let appData = appDao.selectOne()
34
        
Línea -... Línea 35...
-
 
35
        let slideDao = SlideDao.sharedInstance
-
 
36
        self.slideModel = slideDao.selectByUuid(uuid: appData.slideUuidActive)
-
 
37
 
28
        let slideDao = SlideDao.sharedInstance
38
        print("videoplayer appData slideUuid : \(appData.slideUuidActive)")
29
        self.slideModel = slideDao.selectByUuid(uuid: slideUuid)
39
        print("videoplayer slideModel slideUuid : \(self.slideModel.uuid)")
30
 
40
        
31
        if self.slideModel.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
41
        if self.slideModel.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
32
            slideTitle = String(Array(self.slideModel.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
42
            slideTitle = String(Array(self.slideModel.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
Línea 33... Línea -...
33
        } else {
-
 
-
 
43
        } else {
34
            slideTitle = self.slideModel.name
44
            slideTitle = self.slideModel.name
Línea 35... Línea 45...
35
        }
45
        }
36
        
46
        
37
        let videoUrl = URL(string: slideModel.file)
47
 
38
        let headerSecurity = HeaderSecurity()
48
        let headerSecurity = HeaderSecurity()
39
        
49
        
40
        let headers: [String: String] = [
50
        let headers: [String: String] = [
41
            Constants.HTTP_HEADER_ACCEPT: Constants.HTTP_HEADER_ACCEPT_VALUE,
51
            Constants.HTTP_HEADER_ACCEPT: Constants.HTTP_HEADER_ACCEPT_VALUE,
Línea 42... Línea 52...
42
            Constants.HTTP_HEADER_SECURITY_RAND: String(headerSecurity.rand),
52
            Constants.HTTP_HEADER_SECURITY_RAND: String(headerSecurity.rand),
Línea 43... Línea 53...
43
            Constants.HTTP_HEADER_SECURITY_TOKEN: appData.deviceUuid,
53
            Constants.HTTP_HEADER_SECURITY_TOKEN: appData.deviceUuid,
44
            Constants.HTTP_HEADER_SECURITY_CREATED: String(headerSecurity.created) ,
54
            Constants.HTTP_HEADER_SECURITY_CREATED: String(headerSecurity.created) ,
Línea 45... Línea 55...
45
            Constants.HTTP_HEADER_SECURITY_SECRET: headerSecurity.secret,
55
            Constants.HTTP_HEADER_SECURITY_SECRET: headerSecurity.secret,
46
        ]
56
        ]
Línea 47... Línea 57...
47
        
57
        
-
 
58
        let assets = AVURLAsset(url: self.url!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
Línea 48... Línea 59...
48
        let assets = AVURLAsset(url: videoUrl!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
59
 
Línea 49... Línea 60...
49
 
60
        playerItem = AVPlayerItem(asset: assets)
50
        playerItem = AVPlayerItem(asset: assets)
61
        let newTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale:1)
51
        let newTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale:1)
-
 
52
        
-
 
53
        player = AVPlayer(playerItem: playerItem)
-
 
54
        player.seek(to: newTime)
-
 
55
      
-
 
56
      
62
        
57
        self.timerActive = true
-
 
58
        
-
 
Línea 59... Línea 63...
59
 
63
        player = AVPlayer(playerItem: playerItem)
60
 
64
        player.seek(to: newTime)
-
 
65
      
-
 
66
      
-
 
67
        self.timerActive = true
-
 
68
 
-
 
69
        
-
 
70
 
-
 
71
 
-
 
72
    }
-
 
73
    
61
    }
74
    var body: some View {
62
    
75
        VStack(spacing: 0) {
63
    var body: some View {
76
            
64
        VStack(spacing: 0) {
77
            
65
            PopView(
78
            HStack {
Línea 87... Línea 100...
87
                    AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
100
                    AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
88
                    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
101
                    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
89
                    UIViewController.attemptRotationToDeviceOrientation()
102
                    UIViewController.attemptRotationToDeviceOrientation()
Línea 90... Línea 103...
90
                    
103
                    
Línea 91... Línea 104...
91
 
104
 
Línea 112... Línea 125...
112
            }
125
            }
113
            .background(Color("color_app_bar_background"))
126
            .background(Color("color_app_bar_background"))
114
            .edgesIgnoringSafeArea(.top)
127
            .edgesIgnoringSafeArea(.top)
115
            .frame(height: 50)
128
            .frame(height: 50)
116
            Divider()
129
            Divider()
-
 
130
            
Línea 117... Línea -...
117
 
-
 
118
            VideoPlayer(player: player)
-
 
119
            .onAppear() {
-
 
120
                player.play()
-
 
121
            }
-
 
122
            .onDisappear() {
-
 
123
                player.pause()
-
 
124
            }
131
 
125
            .onReceive(timer, perform: { _ in
-
 
126
                let duration =  CMTimeGetSeconds(playerItem.asset.duration)
-
 
127
                if duration > 0 {
-
 
128
 
-
 
129
                    let currentTime =  CMTimeGetSeconds(playerItem.currentTime())
-
 
130
                    let diference = duration - currentTime
132
            GeometryReader { geometry in
131
                       
133
                
132
                    if diference < 10 {
-
 
133
                        self.isCompleted = true;
-
 
134
                        self.timer.upstream.connect().cancel()
134
                VideoPlayer(player: player)
135
                        timerActive = false
135
                .onAppear() {
136
                    }
136
                    player.play()
-
 
137
                }
-
 
138
                .onDisappear() {
-
 
139
                    player.pause()
-
 
140
                }
-
 
141
                    
-
 
142
                .onReceive(timer, perform: { _ in
-
 
143
                    let duration =  CMTimeGetSeconds(playerItem.asset.duration)
-
 
144
                    if duration > 0 {
-
 
145
 
-
 
146
                        let currentTime =  CMTimeGetSeconds(playerItem.currentTime())
-
 
147
                            let diference = duration - currentTime
-
 
148
                                   
-
 
149
                            if diference < 10 {
-
 
150
                                self.isCompleted = true;
-
 
151
                                self.timer.upstream.connect().cancel()
-
 
152
                                timerActive = false
-
 
153
                            }
-
 
154
                        }
137
                }
155
                    })
138
            })
156
            }
139
        }
157
        }
Línea 145... Línea 163...
145
}
163
}
Línea 146... Línea 164...
146
 
164
 
147
 
165
 
148
struct VideoPlayerView_Previews: PreviewProvider {
166
struct VideoPlayerView_Previews: PreviewProvider {
149
    static var previews: some View {
167
    static var previews: some View {
150
        VideoPlayerView(slideUuid: "")
168
        VideoPlayerView()