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
|
|
|
10 |
import Network
|
|
|
11 |
import Alamofire
|
|
|
12 |
import SwiftyJSON
|
|
|
13 |
|
|
|
14 |
struct VideoPlayerView: View {
|
|
|
15 |
@EnvironmentObject private var appNavigation : AppNavigation
|
|
|
16 |
@State private var timerActive : Bool = false
|
|
|
17 |
@State private var isCompleted : Bool = false
|
|
|
18 |
|
|
|
19 |
private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
|
|
|
20 |
|
|
|
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
|
21 |
efrain |
28 |
private var appData = Environment(\.appData).wrappedValue
|
1 |
efrain |
29 |
|
|
|
30 |
init()
|
|
|
31 |
{
|
|
|
32 |
print("urlExternalDownloaded : \(appData.urlExternalDownloaded)")
|
|
|
33 |
self.url = URL(string: appData.urlExternalDownloaded)
|
|
|
34 |
|
17 |
efrain |
35 |
let slideDao = SlideDao()
|
1 |
efrain |
36 |
self.slideModel = slideDao.selectByUuid(uuid: appData.slideUuidActive)
|
|
|
37 |
|
|
|
38 |
print("videoplayer appData slideUuid : \(appData.slideUuidActive)")
|
|
|
39 |
print("videoplayer slideModel slideUuid : \(self.slideModel.uuid)")
|
|
|
40 |
|
|
|
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 |
|
|
|
47 |
let headerSecurity = HeaderSecurity()
|
|
|
48 |
|
|
|
49 |
let headers: [String: String] = [
|
|
|
50 |
Constants.HTTP_HEADER_ACCEPT: Constants.HTTP_HEADER_ACCEPT_VALUE,
|
|
|
51 |
Constants.HTTP_HEADER_SECURITY_RAND: String(headerSecurity.rand),
|
|
|
52 |
Constants.HTTP_HEADER_SECURITY_TOKEN: appData.deviceUuid,
|
|
|
53 |
Constants.HTTP_HEADER_SECURITY_CREATED: String(headerSecurity.created) ,
|
|
|
54 |
Constants.HTTP_HEADER_SECURITY_SECRET: headerSecurity.secret,
|
|
|
55 |
]
|
|
|
56 |
|
|
|
57 |
let assets = AVURLAsset(url: self.url!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
|
|
|
58 |
|
|
|
59 |
playerItem = AVPlayerItem(asset: assets)
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
let newTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale:1)
|
|
|
63 |
|
|
|
64 |
player = AVPlayer(playerItem: playerItem)
|
|
|
65 |
player.seek(to: newTime)
|
|
|
66 |
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
var body: some View {
|
|
|
72 |
VStack(spacing: 0) {
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
HStack {
|
|
|
76 |
Button(action: {
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
backToGallery()
|
|
|
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()
|
|
|
100 |
|
|
|
101 |
|
|
|
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 |
print("duration: \(duration) currentTime : \(currentTime) diference: \(diference) ")
|
|
|
120 |
|
|
|
121 |
if diference < 10 {
|
|
|
122 |
self.isCompleted = true;
|
|
|
123 |
} else if diference <= 2 {
|
|
|
124 |
|
|
|
125 |
self.timer.upstream.connect().cancel()
|
|
|
126 |
timerActive = false
|
|
|
127 |
backToGallery()
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
}
|
|
|
132 |
})
|
|
|
133 |
}
|
|
|
134 |
}.onReceive(NotificationCenter.default.publisher(for: NSNotification.Name.AVPlayerItemDidPlayToEndTime), perform: { data in
|
|
|
135 |
|
|
|
136 |
self.isCompleted = true;
|
|
|
137 |
self.timer.upstream.connect().cancel()
|
|
|
138 |
timerActive = false
|
|
|
139 |
|
|
|
140 |
backToGallery()
|
|
|
141 |
})
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
private func backToGallery()
|
|
|
145 |
{
|
|
|
146 |
if self.url != nil {
|
|
|
147 |
do {
|
|
|
148 |
try FileManager.default.removeItem(at: self.url!)
|
|
|
149 |
print("Video temporal borrado")
|
|
|
150 |
} catch {
|
|
|
151 |
print(error)
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
let dataService = DataService()
|
|
|
156 |
if self.isCompleted {
|
|
|
157 |
dataService.completeSlide(slide: slideModel)
|
|
|
158 |
} else {
|
|
|
159 |
dataService.incompleteSlide(slide: slideModel)
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
if self.timerActive {
|
|
|
163 |
self.timer.upstream.connect().cancel()
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
|
|
|
167 |
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
|
|
|
168 |
UIViewController.attemptRotationToDeviceOrientation()
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
appNavigation.pageActive = .home
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
struct VideoPlayerView_Previews: PreviewProvider {
|
|
|
177 |
static var previews: some View {
|
|
|
178 |
VideoPlayerView()
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|