Quellcode durchsuchen

Refactor `downloadedFile` + fix available offline flag

Flag-icon was not updated after downloading to reflect that the file is available offline.

Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Henrik Storch vor 3 Jahren
Ursprung
Commit
4542063589

+ 10 - 5
iOSClient/Data/NCDataSource.swift

@@ -183,15 +183,20 @@ class NCDataSource: NSObject {
 
         var index: Int?
 
-        if ocIdTemp != nil {
-            index = self.getIndexMetadata(ocId: ocIdTemp!)
+        if let ocIdTemp = ocIdTemp {
+            index = self.getIndexMetadata(ocId: ocIdTemp)
         } else {
             index = self.getIndexMetadata(ocId: ocId)
         }
 
-        if index != nil {
-            if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
-                metadatas[index!] = metadata
+        guard let index = index, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return nil }
+        metadatas[index] = metadata
+
+        let size = CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView)
+        if size > 0 {
+            let tableLocalFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
+            if tableLocalFile?.offline ?? false {
+                metadataOffLine.append(metadata.ocId)
             }
         }
 

+ 9 - 7
iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

@@ -448,13 +448,15 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
     @objc func downloadedFile(_ notification: NSNotification) {
 
-        if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, let _ = userInfo["errorCode"] as? Int, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
-            if let row = dataSource.reloadMetadata(ocId: metadata.ocId) {
-                let indexPath = IndexPath(row: row, section: 0)
-                if indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section) {
-                    collectionView?.reloadItems(at: [indexPath])
-                }
-            }
+        guard let userInfo = notification.userInfo as NSDictionary?,
+              let ocId = userInfo["ocId"] as? String,
+              let _ = userInfo["errorCode"] as? Int,
+              let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId),
+              let row = dataSource.reloadMetadata(ocId: metadata.ocId)
+        else { return }
+        let indexPath = IndexPath(row: row, section: 0)
+        if indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section) {
+            collectionView?.reloadItems(at: [indexPath])
         }
     }
 

+ 89 - 110
iOSClient/Main/NCFunctionCenter.swift

@@ -42,127 +42,106 @@ import JGProgressHUD
 
     @objc func downloadedFile(_ notification: NSNotification) {
 
-        if let userInfo = notification.userInfo as NSDictionary? {
-            if let ocId = userInfo["ocId"] as? String, let selector = userInfo["selector"] as? String, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
-
-                if metadata.account != appDelegate.account { return }
-
-                if errorCode == 0 {
-
-                    switch selector {
-                    case NCGlobal.shared.selectorLoadFileQuickLook:
-
-                        let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
-                        CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
-
-                        var editingMode = false
-                        if #available(iOS 13.0, *) {
-                            editingMode = true
-                        }
-
-                        let viewerQuickLook = NCViewerQuickLook(with: URL(fileURLWithPath: fileNamePath), editingMode: editingMode, metadata: metadata)
-                        let navigationController = UINavigationController(rootViewController: viewerQuickLook)
-                        navigationController.modalPresentationStyle = .overFullScreen
-
-                        self.appDelegate.window?.rootViewController?.present(navigationController, animated: true)
-
-                    case NCGlobal.shared.selectorLoadFileView:
-
-                        if UIApplication.shared.applicationState == UIApplication.State.active {
-
-                            if metadata.contentType.contains("opendocument") && !NCUtility.shared.isRichDocument(metadata) {
-
-                                self.openDocumentController(metadata: metadata)
-
-                            } else if metadata.classFile == NCCommunicationCommon.typeClassFile.compress.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.unknow.rawValue {
-
-                                self.openDocumentController(metadata: metadata)
-
-                            } else {
-
-                                if let viewController = self.appDelegate.activeViewController {
-                                    let imageIcon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
-                                    NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon)
-                                }
-                            }
-                        }
-
-                    case NCGlobal.shared.selectorOpenIn:
-
-                        if UIApplication.shared.applicationState == UIApplication.State.active {
-
-                            self.openDocumentController(metadata: metadata)
-                        }
-
-                    case NCGlobal.shared.selectorLoadCopy:
-
-                        copyPasteboard()
-
-                    case NCGlobal.shared.selectorLoadOffline:
-
-                        NCManageDatabase.shared.setLocalFile(ocId: metadata.ocId, offline: true)
-
-                    case NCGlobal.shared.selectorPrint:
-
-                        printDocument(metadata: metadata)
-
-                    case NCGlobal.shared.selectorSaveAlbum:
-
-                        saveAlbum(metadata: metadata)
-
-                    case NCGlobal.shared.selectorSaveBackground:
-
-                        saveBackground(metadata: metadata)
-
-                    case NCGlobal.shared.selectorSaveAlbumLivePhotoIMG, NCGlobal.shared.selectorSaveAlbumLivePhotoMOV:
-
-                        var metadata = metadata
-                        var metadataMOV = metadata
-                        guard let metadataTMP = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) else { break }
-
-                        if selector == NCGlobal.shared.selectorSaveAlbumLivePhotoIMG {
-                            metadataMOV = metadataTMP
-                        }
-
-                        if selector == NCGlobal.shared.selectorSaveAlbumLivePhotoMOV {
-                            metadata = metadataTMP
-                        }
-
-                        if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && CCUtility.fileProviderStorageExists(metadataMOV.ocId, fileNameView: metadataMOV.fileNameView) {
-                            saveLivePhotoToDisk(metadata: metadata, metadataMov: metadataMOV)
-                        }
-
-                    case NCGlobal.shared.selectorSaveAsScan:
+        guard let userInfo = notification.userInfo as NSDictionary?,
+              let ocId = userInfo["ocId"] as? String,
+              let selector = userInfo["selector"] as? String,
+              let errorCode = userInfo["errorCode"] as? Int,
+              let errorDescription = userInfo["errorDescription"] as? String,
+              let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId),
+              metadata.account == appDelegate.account
+        else { return }
+
+        guard errorCode == 0 else {
+            // File do not exists on server, remove in local
+            if errorCode == NCGlobal.shared.errorResourceNotFound || errorCode == NCGlobal.shared.errorBadServerResponse {
+                do {
+                    try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId))
+                } catch { }
+                NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
+                NCManageDatabase.shared.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
+                
+            } else {
+                NCContentPresenter.shared.messageNotification("_download_file_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode, priority: .max)
+            }
+            return
+        }
+        
+        switch selector {
+        case NCGlobal.shared.selectorLoadFileQuickLook:
+            let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
+            CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
+
+            var editingMode = false
+            if #available(iOS 13.0, *) {
+                editingMode = true
+            }
 
-                        saveAsScan(metadata: metadata)
+            let viewerQuickLook = NCViewerQuickLook(with: URL(fileURLWithPath: fileNamePath), editingMode: editingMode, metadata: metadata)
+            let navigationController = UINavigationController(rootViewController: viewerQuickLook)
+            navigationController.modalPresentationStyle = .overFullScreen
 
-                    case NCGlobal.shared.selectorOpenDetail:
+            self.appDelegate.window?.rootViewController?.present(navigationController, animated: true)
 
-                        NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterOpenMediaDetail, userInfo: ["ocId": metadata.ocId])
+        case NCGlobal.shared.selectorLoadFileView:
+            guard UIApplication.shared.applicationState == UIApplication.State.active else { break }
 
-                    default:
+            if metadata.contentType.contains("opendocument") && !NCUtility.shared.isRichDocument(metadata) {
+                self.openDocumentController(metadata: metadata)
+            } else if metadata.classFile == NCCommunicationCommon.typeClassFile.compress.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.unknow.rawValue {
+                self.openDocumentController(metadata: metadata)
+            } else {
+                if let viewController = self.appDelegate.activeViewController {
+                    let imageIcon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
+                    NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon)
+                }
+            }
+            
+        case NCGlobal.shared.selectorOpenIn:
+            if UIApplication.shared.applicationState == UIApplication.State.active {
+                self.openDocumentController(metadata: metadata)
+            }
+            
+        case NCGlobal.shared.selectorLoadCopy:
+            copyPasteboard()
+            
+        case NCGlobal.shared.selectorLoadOffline:
+            NCManageDatabase.shared.setLocalFile(ocId: metadata.ocId, offline: true)
+            
+        case NCGlobal.shared.selectorPrint:
+            printDocument(metadata: metadata)
+            
+        case NCGlobal.shared.selectorSaveAlbum:
+            saveAlbum(metadata: metadata)
+            
+        case NCGlobal.shared.selectorSaveBackground:
+            saveBackground(metadata: metadata)
+            
+        case NCGlobal.shared.selectorSaveAlbumLivePhotoIMG, NCGlobal.shared.selectorSaveAlbumLivePhotoMOV:
 
-                        break
-                    }
+            var metadata = metadata
+            var metadataMOV = metadata
+            guard let metadataTMP = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) else { break }
 
-                } else {
+            if selector == NCGlobal.shared.selectorSaveAlbumLivePhotoIMG {
+                metadataMOV = metadataTMP
+            }
 
-                    // File do not exists on server, remove in local
-                    if errorCode == NCGlobal.shared.errorResourceNotFound || errorCode == NCGlobal.shared.errorBadServerResponse {
+            if selector == NCGlobal.shared.selectorSaveAlbumLivePhotoMOV {
+                metadata = metadataTMP
+            }
 
-                        do {
-                            try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId))
-                        } catch { }
+            if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && CCUtility.fileProviderStorageExists(metadataMOV.ocId, fileNameView: metadataMOV.fileNameView) {
+                saveLivePhotoToDisk(metadata: metadata, metadataMov: metadataMOV)
+            }
 
-                        NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
-                        NCManageDatabase.shared.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
+        case NCGlobal.shared.selectorSaveAsScan:
+            saveAsScan(metadata: metadata)
 
-                    } else {
+        case NCGlobal.shared.selectorOpenDetail:
+            NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterOpenMediaDetail, userInfo: ["ocId": metadata.ocId])
 
-                        NCContentPresenter.shared.messageNotification("_download_file_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode, priority: .max)
-                    }
-                }
-            }
+        default:
+            break
         }
     }
 

+ 1 - 1
iOSClient/Menu/NCCollectionViewCommon+Menu.swift

@@ -36,7 +36,6 @@ extension NCCollectionViewCommon {
         metadatas.forEach { metadata in
             NCFunctionCenter.shared.setMetadataAvalableOffline(metadata, isOffline: isOffline)
         }
-        self.tapSelect(sender: self)
         self.reloadDataSource()
     }
 
@@ -485,6 +484,7 @@ extension NCCollectionViewCommon {
                     } else {
                         self.setMetadatasAvalableOffline(selectedMetadatas, isOffline: isAnyOffline)
                     }
+                    self.tapSelect(sender: self)
                 }
             )
         )