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