Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 23 | Rev 28 | 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
 
25 efrain 47
        let assets = AVURLAsset(url: self.url!)
1 efrain 48
        playerItem = AVPlayerItem(asset: assets)
49
        let newTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale:1)
50
 
51
        player = AVPlayer(playerItem: playerItem)
52
        player.seek(to: newTime)
17 efrain 53
 
1 efrain 54
    }
55
 
56
    var body: some View {
57
        VStack(spacing: 0) {
8 efrain 58
 
17 efrain 59
 
1 efrain 60
            HStack {
61
                Button(action: {
23 efrain 62
 
63
 
64
 
21 efrain 65
                    backToGallery()
1 efrain 66
                }, label: {
67
                    Image(systemName: "chevron.backward")
68
                    .frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
69
                    .aspectRatio(contentMode: .fit)
70
                    .foregroundColor(Color("color_app_bar_foreground"))
71
                })
72
                .padding(.leading, 16)
73
 
74
                Text(self.slideTitle)
75
                .font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
76
                .foregroundColor(Color("color_app_bar_foreground"))
77
                    .padding(.leading, 4)
78
 
79
                Spacer()
80
            }
81
            .background(Color("color_app_bar_background"))
82
            .edgesIgnoringSafeArea(.top)
83
            .frame(height: 50)
84
            Divider()
17 efrain 85
 
1 efrain 86
 
17 efrain 87
            GeometryReader { geometry in
88
 
89
                VideoPlayer(player: player)
90
                .onAppear() {
91
                    player.play()
92
                }
93
                .onDisappear() {
94
                    player.pause()
95
                }
96
 
97
                .onReceive(timer, perform: { _ in
98
                    let duration =  CMTimeGetSeconds(playerItem.asset.duration)
99
                    if duration > 0 {
100
 
101
                        let currentTime =  CMTimeGetSeconds(playerItem.currentTime())
102
                            let diference = duration - currentTime
25 efrain 103
 
104
                        print("duration: \(duration) currentTime : \(currentTime) diference: \(diference) ")
105
 
17 efrain 106
                            if diference < 10 {
107
                                self.isCompleted = true;
23 efrain 108
                            } else if diference <= 0 {
25 efrain 109
                                self.isCompleted = true;
110
 
17 efrain 111
                                self.timer.upstream.connect().cancel()
112
                                timerActive = false
21 efrain 113
                                backToGallery()
17 efrain 114
                            }
23 efrain 115
 
116
 
17 efrain 117
                        }
118
                    })
11 efrain 119
            }
1 efrain 120
        }
121
    }
122
 
21 efrain 123
    private func backToGallery()
124
    {
125
        if self.url != nil {
126
            do {
127
                try FileManager.default.removeItem(at: self.url!)
128
                       print("Video temporal borrado")
129
             } catch {
130
                print(error)
131
            }
132
        }
133
 
134
        let dataService = DataService()
135
        if self.isCompleted {
136
            dataService.completeSlide(slide: slideModel)
137
        } else {
138
            dataService.incompleteSlide(slide: slideModel)
139
        }
140
 
141
        if self.timerActive {
142
            self.timer.upstream.connect().cancel()
143
        }
144
 
145
        AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
146
        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
147
        UIViewController.attemptRotationToDeviceOrientation()
148
 
149
 
150
        appNavigation.pageActive = .home
151
    }
1 efrain 152
 
21 efrain 153
 
1 efrain 154
 
155
 
156
}
157
 
158
 
159
struct VideoPlayerView_Previews: PreviewProvider {
160
    static var previews: some View {
17 efrain 161
        VideoPlayerView()
1 efrain 162
    }
163
}
164