Browse Source

download video

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 1 year ago
parent
commit
40529343db

+ 12 - 4
iOSClient/Networking/NCNetworking.swift

@@ -351,7 +351,9 @@ class NCNetworking: NSObject, NKCommonDelegate {
             self.downloadRequest[fileNameLocalPath] = request
 
             NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, status: NCGlobal.shared.metadataStatusDownloading)
-            NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadStartFile, userInfo: ["ocId":metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account])
+            if notificationCenterProgressTask {
+                NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadStartFile, userInfo: ["ocId":metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account])
+            }
             
         }, taskHandler: { (_) in
             
@@ -369,7 +371,9 @@ class NCNetworking: NSObject, NKCommonDelegate {
             if afError?.isExplicitlyCancelledError ?? false {
 
                 NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, session: "", sessionError: "", sessionSelector: selector, sessionTaskIdentifier: 0, status: NCGlobal.shared.metadataStatusNormal)
-                NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadCancelFile, userInfo: ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account])
+                if notificationCenterProgressTask {
+                    NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadCancelFile, userInfo: ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account])
+                }
 
             } else if error == .success {
 
@@ -381,12 +385,16 @@ class NCNetworking: NSObject, NKCommonDelegate {
                 }
                 CCUtility.setExif(metadata) { _, _, _, _, _ in }
 #endif
-                NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account, "selector": selector, "error": error])
+                if notificationCenterProgressTask {
+                    NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account, "selector": selector, "error": error])
+                }
 
             } else {
 
                 NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, session: "", sessionError: error.errorDescription, sessionSelector: selector, sessionTaskIdentifier: 0, status: NCGlobal.shared.metadataStatusDownloadError)
-                NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account, "selector": selector, "error": error])
+                if notificationCenterProgressTask {
+                    NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account, "selector": selector, "error": error])
+                }
             }
 
             DispatchQueue.main.async { completion(afError, error) }

+ 33 - 14
iOSClient/Viewer/NCViewerMedia/NCViewerMedia.swift

@@ -27,6 +27,8 @@ import NextcloudKit
 import EasyTipView
 import SwiftUI
 import MobileVLCKit
+import JGProgressHUD
+import Alamofire
 
 class NCViewerMedia: UIViewController {
 
@@ -155,7 +157,37 @@ class NCViewerMedia: UIViewController {
                         if error == .success, let url = url {
                             ncplayer.openAVPlayer(url: url, autoplay: autoplay)
                         } else {
-                            NCContentPresenter.shared.showError(error: error)
+                            let hud = JGProgressHUD()
+                            var downloadRequest: DownloadRequest?
+
+                            hud.indicatorView = JGProgressHUDRingIndicatorView()
+                            hud.textLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
+                            if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView { indicatorView.ringWidth = 1.5 }
+                            hud.tapOnHUDViewBlock = { _ in
+                                if let request = downloadRequest {
+                                    request.cancel()
+                                }
+                            }
+                            if let view = self.appDelegate.window?.rootViewController?.view {
+                                hud.show(in: view)
+                            }
+                            NCNetworking.shared.download(metadata: self.metadata, selector: "", notificationCenterProgressTask: false) { request in
+                                downloadRequest = request
+                            } progressHandler: { progress in
+                                hud.progress = Float(progress.fractionCompleted)
+                            } completion: { afError, error in
+                                if error == .success {
+                                    hud.dismiss()
+                                    if CCUtility.fileProviderStorageExists(self.metadata) {
+                                        let url = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
+                                        ncplayer.openAVPlayer(url: url, autoplay: autoplay)
+                                    }
+                                } else {
+                                    hud.indicatorView = JGProgressHUDErrorIndicatorView()
+                                    hud.textLabel.text = error.errorDescription
+                                    hud.dismiss(afterDelay: NCGlobal.shared.dismissAfterSecond)
+                                }
+                            }
                         }
                     }
                 } else {
@@ -568,16 +600,3 @@ extension NCViewerMedia: EasyTipViewDelegate {
         self.tipView?.dismiss()
     }
 }
-
-// MARK: -
-
-/*
-class imageVideoContainerView: UIImageView {
-    var playerLayer: CALayer?
-    var metadata: tableMetadata?
-    override func layoutSublayers(of layer: CALayer) {
-        super.layoutSublayers(of: layer)
-        playerLayer?.frame = self.bounds
-    }
-}
-*/