Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 9 | Rev 17 | 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
struct VideoPlayerView: View {
13
    @State private var timerActive : Bool = false
14
    @State private var isCompleted : Bool = false
8 efrain 15
    @State private var backToGallery : Bool = false
1 efrain 16
 
17
    private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
11 efrain 18
    private let appDao = AppDao.sharedInstance
19
 
1 efrain 20
    private var playerItem : AVPlayerItem
21
    private var player : AVPlayer
22
    private var slideModel : SlideModel
23
    private var slideTitle : String
24
 
8 efrain 25
    init(slideUuid : String)
1 efrain 26
    {
11 efrain 27
        let appData = appDao.selectOne()
1 efrain 28
        let slideDao = SlideDao.sharedInstance
8 efrain 29
        self.slideModel = slideDao.selectByUuid(uuid: slideUuid)
1 efrain 30
 
31
        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]) + "..."
33
        } else {
34
            slideTitle = self.slideModel.name
35
        }
36
 
37
        let videoUrl = URL(string: slideModel.file)
38
        let headerSecurity = HeaderSecurity()
39
 
40
        let headers: [String: String] = [
41
            Constants.HTTP_HEADER_ACCEPT: Constants.HTTP_HEADER_ACCEPT_VALUE,
42
            Constants.HTTP_HEADER_SECURITY_RAND: String(headerSecurity.rand),
43
            Constants.HTTP_HEADER_SECURITY_TOKEN: appData.deviceUuid,
44
            Constants.HTTP_HEADER_SECURITY_CREATED: String(headerSecurity.created) ,
45
            Constants.HTTP_HEADER_SECURITY_SECRET: headerSecurity.secret,
46
        ]
47
 
48
        let assets = AVURLAsset(url: videoUrl!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
11 efrain 49
 
1 efrain 50
        playerItem = AVPlayerItem(asset: assets)
51
        let newTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale:1)
52
 
53
        player = AVPlayer(playerItem: playerItem)
54
        player.seek(to: newTime)
55
 
56
 
57
        self.timerActive = true
58
 
59
 
60
 
61
    }
62
 
63
    var body: some View {
64
        VStack(spacing: 0) {
8 efrain 65
            PopView(
66
                destination: .root,
67
                isActive: self.$backToGallery,
68
                label: {
69
                    Text("")
70
 
71
                }
72
            ).frame( height: 0)
73
 
1 efrain 74
            HStack {
75
                Button(action: {
76
                    let dataService = DataService()
77
                    if self.isCompleted {
78
                        dataService.completeSlide(slide: slideModel)
79
                    } else {
80
                        dataService.incompleteSlide(slide: slideModel)
81
                    }
82
 
83
                    if self.timerActive {
84
                        self.timer.upstream.connect().cancel()
85
                    }
86
 
87
                    AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
88
                    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
89
                    UIViewController.attemptRotationToDeviceOrientation()
90
 
91
 
8 efrain 92
                    self.backToGallery.toggle()
93
 
94
 
1 efrain 95
 
96
                }, label: {
97
 
98
 
99
                    Image(systemName: "chevron.backward")
100
                    .frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
101
                    .aspectRatio(contentMode: .fit)
102
                    .foregroundColor(Color("color_app_bar_foreground"))
103
                })
104
                .padding(.leading, 16)
105
 
106
                Text(self.slideTitle)
107
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
108
                .foregroundColor(Color("color_app_bar_foreground"))
109
                    .padding(.leading, 4)
110
 
111
                Spacer()
112
            }
113
            .background(Color("color_app_bar_background"))
114
            .edgesIgnoringSafeArea(.top)
115
            .frame(height: 50)
116
            Divider()
117
 
118
            VideoPlayer(player: player)
11 efrain 119
            .onAppear() {
120
                player.play()
121
            }
122
            .onDisappear() {
123
                player.pause()
124
            }
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
131
 
132
                    if diference < 10 {
133
                        self.isCompleted = true;
134
                        self.timer.upstream.connect().cancel()
135
                        timerActive = false
136
                    }
1 efrain 137
                }
11 efrain 138
            })
1 efrain 139
        }
140
    }
141
 
142
 
143
 
144
 
145
}
146
 
147
 
148
struct VideoPlayerView_Previews: PreviewProvider {
149
    static var previews: some View {
8 efrain 150
        VideoPlayerView(slideUuid: "")
1 efrain 151
    }
152
}
153