Kaynağa Gözat

Merge pull request #2401 from nextcloud/VLC_Improvements

Vlc improvements
Marino Faggiana 1 yıl önce
ebeveyn
işleme
f9b4d7095e

+ 1 - 1
Brand/Database.swift

@@ -26,4 +26,4 @@ import Foundation
 // Database Realm
 //
 let databaseName                    = "nextcloud.realm"
-let databaseSchemaVersion: UInt64   = 290
+let databaseSchemaVersion: UInt64   = 292

+ 37 - 8
iOSClient/Data/NCManageDatabase+Video.swift

@@ -25,11 +25,15 @@ import Foundation
 import RealmSwift
 import NextcloudKit
 
-class tableVideo: Object {
+typealias tableVideo = tableVideoV2
+class tableVideoV2: Object {
 
     @objc dynamic var account = ""
     @objc dynamic var ocId = ""
     @objc dynamic var position: Float = 0
+    @objc dynamic var width: Int = 0
+    @objc dynamic var height: Int = 0
+    @objc dynamic var length: Int = 0
     @objc dynamic var codecNameVideo: String?
     @objc dynamic var codecNameAudio: String?
     @objc dynamic var codecAudioChannelLayout: String?
@@ -44,7 +48,7 @@ class tableVideo: Object {
 
 extension NCManageDatabase {
 
-    func addVideo(metadata: tableMetadata, position: Float) {
+    func addVideo(metadata: tableMetadata, position: Float? = nil, width: Int? = nil, height: Int? = nil, length: Int? = nil) {
 
         if metadata.livePhoto { return }
         let realm = try! Realm()
@@ -53,17 +57,42 @@ extension NCManageDatabase {
             try realm.write {
                 if let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first {
 
-                    result.position = position
+                    if let position = position {
+                        result.position = position
+                    }
+                    if let width = width {
+                        result.width = width
+                    }
+                    if let height = height {
+                        result.height = height
+                    }
+                    if let length = length {
+                        result.length = length
+                    }
+
                     realm.add(result, update: .all)
 
                 } else {
 
-                    let addObject = tableVideo()
+                    let result = tableVideo()
 
-                    addObject.account = metadata.account
-                    addObject.ocId = metadata.ocId
-                    addObject.position = position
-                    realm.add(addObject, update: .all)
+                    result.account = metadata.account
+                    result.ocId = metadata.ocId
+
+                    if let position = position {
+                        result.position = position
+                    }
+                    if let width = width {
+                        result.width = width
+                    }
+                    if let height = height {
+                        result.height = height
+                    }
+                    if let length = length {
+                        result.length = length
+                    }
+
+                    realm.add(result, update: .all)
                 }
             }
         } catch let error {

+ 13 - 9
iOSClient/Data/NCManageDatabase.swift

@@ -95,6 +95,10 @@ class NCManageDatabase: NSObject {
                         migration.deleteData(forType: tableMetadata.className())
                     }
 
+                    if oldSchemaVersion < 292 {
+                        migration.deleteData(forType: tableVideo.className())
+                    }
+
                 }, shouldCompactOnLaunch: { totalBytes, usedBytes in
 
                     // totalBytes refers to the size of the file on disk in bytes (data + free space)
@@ -108,14 +112,14 @@ class NCManageDatabase: NSObject {
 
             do {
                 _ = try Realm(configuration: configCompact)
-            } catch {
+            } catch let error {
                 if let databaseFileUrlPath = databaseFileUrlPath {
                     do {
 #if !EXTENSION
-                        let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_database_corrupt_")
-                        NCContentPresenter.shared.showError(error: error, priority: .max)
+                        let nkError = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: error.localizedDescription)
+                        NCContentPresenter.shared.showError(error: nkError, priority: .max)
 #endif
-                        NextcloudKit.shared.nkCommonInstance.writeLog("DATABASE CORRUPT: removed")
+                        NextcloudKit.shared.nkCommonInstance.writeLog("DATABASE ERROR: \(error.localizedDescription)")
                         try FileManager.default.removeItem(at: databaseFileUrlPath)
                     } catch {}
                 }
@@ -132,16 +136,16 @@ class NCManageDatabase: NSObject {
         // Verify Database, if corrupt remove it
         do {
             _ = try Realm()
-        } catch {
+        } catch let error {
             if let databaseFileUrlPath = databaseFileUrlPath {
                 do {
 #if !EXTENSION
-                    let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_database_corrupt_")
-                    NCContentPresenter.shared.showError(error: error, priority: .max)
+                    let nkError = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: error.localizedDescription)
+                    NCContentPresenter.shared.showError(error: nkError, priority: .max)
 #endif
-                    NextcloudKit.shared.nkCommonInstance.writeLog("DATABASE CORRUPT: removed")
+                    NextcloudKit.shared.nkCommonInstance.writeLog("DATABASE ERROR: \(error.localizedDescription)")
                     try FileManager.default.removeItem(at: databaseFileUrlPath)
-                } catch {}
+                } catch { }
             }
         }
 

+ 2 - 0
iOSClient/NCGlobal.swift

@@ -364,6 +364,8 @@ class NCGlobal: NSObject {
     let notificationCenterDownloadedThumbnail                   = "DownloadedThumbnail"             // userInfo: ocId
 
     let notificationCenterOpenMediaDetail                       = "openMediaDetail"                 // userInfo: ocId
+    
+    let notificationCenterStartTimerAutoHideMediaPage           = "startTimerAutoHideMediaPage"
 
     let notificationCenterDismissScanDocument                   = "dismissScanDocument"
     let notificationCenterDismissUploadAssets                   = "dismissUploadAssets"

+ 15 - 11
iOSClient/Viewer/NCViewerMedia/NCPlayer/NCPlayer.swift

@@ -34,16 +34,18 @@ class NCPlayer: NSObject {
     internal var thumbnailer: VLCMediaThumbnailer?
     internal var metadata: tableMetadata
     internal var singleTapGestureRecognizer: UITapGestureRecognizer!
-    internal var width: Int64?
-    internal var height: Int64?
+    internal var width: Int?
+    internal var height: Int?
+    internal var length: Int?
     internal let fileNamePreviewLocalPath: String
-    internal let fileNameIconLocalPath: String
 
     internal weak var playerToolBar: NCPlayerToolBar?
     internal weak var viewerMediaPage: NCViewerMediaPage?
 
     weak var imageVideoContainer: imageVideoContainerView?
 
+    internal var counterSeconds: Double = 0
+
     // MARK: - View Life Cycle
 
     init(imageVideoContainer: imageVideoContainerView, playerToolBar: NCPlayerToolBar?, metadata: tableMetadata, viewerMediaPage: NCViewerMediaPage?) {
@@ -54,7 +56,6 @@ class NCPlayer: NSObject {
         self.viewerMediaPage = viewerMediaPage
 
         fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
-        fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
 
         super.init()
     }
@@ -98,6 +99,9 @@ class NCPlayer: NSObject {
             thumbnailer = VLCMediaThumbnailer(media: media, andDelegate: self)
         }
 
+        player?.play()
+        player?.pause()
+
         NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
     }
 
@@ -212,15 +216,18 @@ extension NCPlayer: VLCMediaPlayerDelegate {
             print("Played mode: ENDED")
             break
         case .error:
-            playerToolBar?.disableAllControl()
             let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_error_something_wrong_")
             NCContentPresenter.shared.showError(error: error, priority: .max)
             print("Played mode: ERROR")
             break
         case .playing:
             let size = player.videoSize
-            self.width = Int64(size.width)
-            self.height = Int64(size.height)
+            if let mediaLength = player.media?.length.intValue {
+                self.length = Int(mediaLength)
+            }
+            self.width = Int(size.width)
+            self.height = Int(size.height)
+            NCManageDatabase.shared.addVideo(metadata: metadata, width: self.width, height: self.height, length: self.length)
             print("Played mode: PLAYING")
             break
         case .paused:
@@ -248,7 +255,7 @@ extension NCPlayer: VLCMediaPlayerDelegate {
     }
 
     func mediaPlayerSnapshot(_ aNotification: Notification) {
-        print("Snapshot saved on \(fileNameIconLocalPath)")
+        print("Snapshot saved")
     }
 
     func mediaPlayerStartedRecording(_ player: VLCMediaPlayer) {
@@ -273,9 +280,6 @@ extension NCPlayer: VLCMediaThumbnailerDelegate {
             if let data = image?.jpegData(compressionQuality: 0.5) {
                 try data.write(to: URL(fileURLWithPath: fileNamePreviewLocalPath), options: .atomic)
             }
-            if let data = image?.jpegData(compressionQuality: 0.5) {
-                try data.write(to: URL(fileURLWithPath: fileNameIconLocalPath), options: .atomic)
-            }
         } catch let error as NSError {
             print("GeneratorImagePreview localized error:")
             print(error.localizedDescription)

+ 18 - 39
iOSClient/Viewer/NCViewerMedia/NCPlayer/NCPlayerToolBar.swift

@@ -51,16 +51,6 @@ class NCPlayerToolBar: UIView {
     private var ncplayer: NCPlayer?
     private var metadata: tableMetadata?
     private var wasInPlay: Bool = false
-    private var timerAutoHide: Timer?
-    private var timerAutoHideSeconds: Double {
-        get {
-            if NCUtility.shared.isSimulator() { // for test
-                return 3
-            } else {
-                return 5
-            }
-        }
-    }
 
     // MARK: - View Life Cycle
 
@@ -99,6 +89,10 @@ class NCPlayerToolBar: UIView {
         backButton.setImage(NCUtility.shared.loadImage(named: "gobackward.10", color: .white), for: .normal)
 
         forwardButton.setImage(NCUtility.shared.loadImage(named: "goforward.10", color: .white), for: .normal)
+
+        // Normally hide
+        self.alpha = 0
+        self.isHidden = true
     }
 
     required init?(coder aDecoder: NSCoder) {
@@ -106,7 +100,6 @@ class NCPlayerToolBar: UIView {
     }
 
     deinit {
-        timerAutoHide?.invalidate()
         print("deinit NCPlayerToolBar")
     }
 
@@ -134,7 +127,11 @@ class NCPlayerToolBar: UIView {
             muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
         }
 
-        show()
+        if viewerMediaScreenMode == .normal {
+            show()
+        } else {
+            hide()
+        }
     }
 
     public func update() {
@@ -155,23 +152,10 @@ class NCPlayerToolBar: UIView {
         MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = positionInSecond
     }
 
-    public func disableAllControl() {
-
-        muteButton.isEnabled = false
-        playButton.isEnabled = false
-        forwardButton.isEnabled = false
-        backButton.isEnabled = false
-        playbackSlider.isEnabled = false
-    }
-
     // MARK: -
 
     public func show() {
 
-        if let ncplayer = ncplayer, ncplayer.isPlay() {
-            startTimerAutoHide()
-        }
-
         UIView.animate(withDuration: 0.3, animations: {
             self.alpha = 1
         }, completion: { (_: Bool) in
@@ -179,13 +163,7 @@ class NCPlayerToolBar: UIView {
         })
     }
 
-    private func startTimerAutoHide() {
-
-        timerAutoHide?.invalidate()
-        timerAutoHide = Timer.scheduledTimer(timeInterval: timerAutoHideSeconds, target: self, selector: #selector(hide), userInfo: nil, repeats: false)
-    }
-
-    @objc func hide() {
+    func hide() {
 
         UIView.animate(withDuration: 0.3, animations: {
             self.alpha = 0
@@ -221,6 +199,7 @@ class NCPlayerToolBar: UIView {
             playbackSliderEvent = .began
         case .moved:
             ncplayer.playerPosition(newPosition)
+            NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterStartTimerAutoHideMediaPage)
             playbackSliderEvent = .moved
         case .ended:
             ncplayer.playerPosition(newPosition)
@@ -230,8 +209,6 @@ class NCPlayerToolBar: UIView {
         default:
             break
         }
-
-        startTimerAutoHide()
     }
 
     // MARK: - Action
@@ -245,11 +222,11 @@ class NCPlayerToolBar: UIView {
 
         if ncplayer.isPlay() {
             ncplayer.playerPause()
-            timerAutoHide?.invalidate()
         } else {
             ncplayer.playerPlay()
-            startTimerAutoHide()
         }
+
+        NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterStartTimerAutoHideMediaPage)
     }
 
     @IBAction func tapMute(_ sender: Any) {
@@ -266,7 +243,7 @@ class NCPlayerToolBar: UIView {
             muteButton.setImage(NCUtility.shared.loadImage(named: "audioOn", color: .white), for: .normal)
         }
 
-        startTimerAutoHide()
+        NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterStartTimerAutoHideMediaPage)
     }
 
     @IBAction func tapForward(_ sender: Any) {
@@ -274,7 +251,8 @@ class NCPlayerToolBar: UIView {
         guard let ncplayer = ncplayer else { return }
 
         ncplayer.jumpForward(10)
-        startTimerAutoHide()
+
+        NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterStartTimerAutoHideMediaPage)
     }
 
     @IBAction func tapBack(_ sender: Any) {
@@ -282,6 +260,7 @@ class NCPlayerToolBar: UIView {
         guard let ncplayer = ncplayer else { return }
 
         ncplayer.jumpBackward(10)
-        startTimerAutoHide()
+
+        NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterStartTimerAutoHideMediaPage)
     }
 }

+ 6 - 2
iOSClient/Viewer/NCViewerMedia/NCViewerMedia.swift

@@ -141,7 +141,7 @@ class NCViewerMedia: UIViewController {
             }
         }
 
-        if viewerMediaPage?.screenMode == .normal {
+        if viewerMediaScreenMode == .normal {
 
             viewerMediaPage?.navigationController?.setNavigationBarHidden(false, animated: true)
 
@@ -178,7 +178,11 @@ class NCViewerMedia: UIViewController {
                         }
                     }
                 } else {
-                    playerToolBar?.show()
+                    if viewerMediaScreenMode == .normal {
+                        playerToolBar?.show()
+                    } else {
+                        playerToolBar?.hide()
+                    }
                 }
             }
             

+ 67 - 12
iOSClient/Viewer/NCViewerMedia/NCViewerMediaPage.swift

@@ -27,15 +27,15 @@ import MediaPlayer
 import JGProgressHUD
 import Alamofire
 
+enum ScreenMode {
+    case full, normal
+}
+var viewerMediaScreenMode: ScreenMode = .normal
+
 class NCViewerMediaPage: UIViewController {
 
     @IBOutlet weak var progressView: UIProgressView!
 
-    enum ScreenMode {
-        case full, normal
-    }
-    var screenMode: ScreenMode = .normal
-
     var pageViewController: UIPageViewController {
         return self.children[0] as! UIPageViewController
     }
@@ -65,8 +65,31 @@ class NCViewerMediaPage: UIViewController {
     var nextTrackCommand: Any?
     var previousTrackCommand: Any?
 
+    private var timerAutoHide: Timer?
+    private var timerAutoHideSeconds: Double {
+        get {
+            if NCUtility.shared.isSimulator() {
+                return 5
+            } else {
+                return 4
+            }
+        }
+    }
+
     // MARK: - View Life Cycle
 
+    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
+        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
+
+        viewerMediaScreenMode = .normal
+    }
+
+    required init?(coder: NSCoder) {
+        super.init(coder: coder)
+        
+        viewerMediaScreenMode = .normal
+    }
+
     override func viewDidLoad() {
         super.viewDidLoad()
 
@@ -95,6 +118,8 @@ class NCViewerMediaPage: UIViewController {
         progressView.trackTintColor = .clear
         progressView.progress = 0
 
+        startTimerAutoHide()
+
         NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
@@ -106,10 +131,14 @@ class NCViewerMediaPage: UIViewController {
         NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
 
         NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
+
+        NotificationCenter.default.addObserver(self, selector: #selector(startTimerAutoHide), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterStartTimerAutoHideMediaPage), object: nil)
     }
 
     deinit {
 
+        timerAutoHide?.invalidate()
+
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
@@ -121,6 +150,8 @@ class NCViewerMediaPage: UIViewController {
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
 
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
+
+        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterStartTimerAutoHideMediaPage), object: nil)
     }
 
     override func viewDidDisappear(_ animated: Bool) {
@@ -134,7 +165,7 @@ class NCViewerMediaPage: UIViewController {
 
     override var preferredStatusBarStyle: UIStatusBarStyle {
 
-        if screenMode == .normal {
+        if viewerMediaScreenMode == .normal {
             return .default
         } else {
             return .lightContent
@@ -142,7 +173,7 @@ class NCViewerMediaPage: UIViewController {
     }
 
     override var prefersHomeIndicatorAutoHidden: Bool {
-        return screenMode == .full
+        return viewerMediaScreenMode == .full
     }
 
     override var prefersStatusBarHidden: Bool {
@@ -174,7 +205,7 @@ class NCViewerMediaPage: UIViewController {
         NCViewer.shared.toggleMenu(viewController: self, metadata: currentViewController.metadata, webView: false, imageIcon: imageIcon)
     }
 
-    func changeScreenMode(mode: ScreenMode, toggleToolbar: Bool) {
+    func changeScreenMode(mode: ScreenMode) {
 
         if mode == .normal {
 
@@ -182,6 +213,10 @@ class NCViewerMediaPage: UIViewController {
             hideStatusBar = false
             progressView.isHidden = false
 
+            if metadatas[currentIndex].classFile == NKCommon.TypeClassFile.video.rawValue || metadatas[currentIndex].classFile == NKCommon.TypeClassFile.audio.rawValue {
+                currentViewController.playerToolBar?.show()
+            }
+
             NCUtility.shared.colorNavigationController(navigationController, backgroundColor: .systemBackground, titleColor: .label, tintColor: nil, withoutShadow: false)
             view.backgroundColor = .systemBackground
             textColor = .label
@@ -192,17 +227,35 @@ class NCViewerMediaPage: UIViewController {
             hideStatusBar = true
             progressView.isHidden = true
 
+            if metadatas[currentIndex].classFile == NKCommon.TypeClassFile.video.rawValue || metadatas[currentIndex].classFile == NKCommon.TypeClassFile.audio.rawValue {
+                currentViewController.playerToolBar?.hide()
+            }
+
             view.backgroundColor = .black
             textColor = .white
         }
 
-        screenMode = mode
+        viewerMediaScreenMode = mode
 
+        startTimerAutoHide()
         setNeedsStatusBarAppearanceUpdate()
         setNeedsUpdateOfHomeIndicatorAutoHidden()
         currentViewController.reloadDetail()
     }
 
+    @objc func startTimerAutoHide() {
+
+        timerAutoHide?.invalidate()
+        timerAutoHide = Timer.scheduledTimer(timeInterval: timerAutoHideSeconds, target: self, selector: #selector(autoHide), userInfo: nil, repeats: true)
+    }
+
+    @objc func autoHide() {
+
+        if metadatas[currentIndex].classFile == NKCommon.TypeClassFile.video.rawValue || metadatas[currentIndex].classFile == NKCommon.TypeClassFile.audio.rawValue, viewerMediaScreenMode == .normal {
+            changeScreenMode(mode: .full)
+        }
+    }
+
     // MARK: - NotificationCenter
 
     @objc func downloadedFile(_ notification: NSNotification) {
@@ -497,6 +550,8 @@ extension NCViewerMediaPage: UIPageViewControllerDelegate, UIPageViewControllerD
 
         guard let nextViewController = pendingViewControllers.first as? NCViewerMedia else { return }
         nextIndex = nextViewController.index
+
+        startTimerAutoHide()
     }
 
     // END TRANSITION
@@ -556,10 +611,10 @@ extension NCViewerMediaPage: UIGestureRecognizerDelegate {
 
     @objc func didSingleTapWith(gestureRecognizer: UITapGestureRecognizer) {
 
-        if screenMode == .full {
-            changeScreenMode(mode: .normal, toggleToolbar: true)
+        if viewerMediaScreenMode == .full {
+            changeScreenMode(mode: .normal)
         } else {
-            changeScreenMode(mode: .full, toggleToolbar: true)
+            changeScreenMode(mode: .full)
         }
     }