Browse Source

improvements

Signed-off-by: Marino Faggiana <8616947+marinofaggiana@users.noreply.github.com>
Marino Faggiana 1 năm trước cách đây
mục cha
commit
5530d04d02

+ 13 - 0
iOSClient/Data/NCManageDatabase+LocalFile.swift

@@ -194,6 +194,19 @@ extension NCManageDatabase {
         return nil
     }
 
+    func getResultsTableLocalFile(predicate: NSPredicate) -> Results<tableLocalFile>? {
+
+        do {
+            let realm = try Realm()
+            realm.refresh()
+            return realm.objects(tableLocalFile.self).filter(predicate)
+        } catch let error as NSError {
+            NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
+        }
+
+        return nil
+    }
+
     func getTableLocalFiles(predicate: NSPredicate, sorted: String, ascending: Bool) -> [tableLocalFile] {
 
         do {

+ 0 - 10
iOSClient/Data/NCManageDatabase+Metadata.swift

@@ -1135,16 +1135,6 @@ extension NCManageDatabase {
         }
     }
 
-    func isDownloadMetadata(_ metadata: tableMetadata, download: Bool) -> Bool {
-
-        let localFile = getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
-        let fileSize = utilityFileSystem.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView)
-        if (localFile != nil || download) && (localFile?.etag != metadata.etag || fileSize == 0) {
-            return true
-        }
-        return false
-    }
-
     func getMetadataConflict(account: String, serverUrl: String, fileNameView: String) -> tableMetadata? {
 
         // verify exists conflict

+ 15 - 1
iOSClient/Files/NCFiles.swift

@@ -157,6 +157,20 @@ class NCFiles: NCCollectionViewCommon {
             return
         }
 
+        func downloadMetadata(_ metadata: tableMetadata) -> Bool {
+
+            let fileSize = utilityFileSystem.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView)
+            guard fileSize > 0 else { return false }
+
+            if let localFile = NCManageDatabase.shared.getResultsTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))?.first {
+                if localFile.etag != metadata.etag {
+                    return true
+                }
+            }
+
+            return false
+        }
+
         NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Reload data source network files forced \(isForced)")
 
         isReloadDataSourceNetworkInProgress = true
@@ -164,7 +178,7 @@ class NCFiles: NCCollectionViewCommon {
 
         networkReadFolder(isForced: isForced) { tableDirectory, metadatas, metadatasUpdate, metadatasDelete, error in
             if error == .success {
-                for metadata in metadatas ?? [] where !metadata.directory && NCManageDatabase.shared.isDownloadMetadata(metadata, download: false) {
+                for metadata in metadatas ?? [] where !metadata.directory && downloadMetadata(metadata) {
                     if self.appDelegate.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
                         self.appDelegate.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: NCGlobal.shared.selectorDownloadFile))
                     }

+ 3 - 1
iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

@@ -1380,8 +1380,10 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
 
         } else {
 
+            let tableLocalFile = NCManageDatabase.shared.getResultsTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))?.first
+
             // image local
-            if NCManageDatabase.shared.getTableLocalFile(ocId: metadata.ocId) != nil {
+            if let tableLocalFile, tableLocalFile.offline {
                 a11yValues.append(NSLocalizedString("_offline_", comment: ""))
                 cell.fileLocalImage?.image = NCImageCache.images.offlineFlag
             } else if utilityFileSystem.fileProviderStorageExists(metadata) {

+ 10 - 1
iOSClient/Networking/NCNetworking.swift

@@ -1042,7 +1042,7 @@ class NCNetworking: NSObject, NKCommonDelegate {
                             let serverUrl = metadata.serverUrl + "/" + metadata.fileName
                             NCManageDatabase.shared.addDirectory(encrypted: metadata.e2eEncrypted, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, etag: metadata.etag, permissions: metadata.permissions, serverUrl: serverUrl, account: metadata.account)
                         } else if selector == NCGlobal.shared.selectorSynchronizationOffline,
-                                  NCManageDatabase.shared.isDownloadMetadata(metadata, download: true),
+                                  self.synchronizeMetadata(metadata),
                                   let appDelegate = (UIApplication.shared.delegate as? AppDelegate),
                                   appDelegate.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
                             appDelegate.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: selector))
@@ -1054,6 +1054,15 @@ class NCNetworking: NSObject, NKCommonDelegate {
 #endif
     }
 
+    func synchronizeMetadata(_ metadata: tableMetadata) -> Bool {
+
+        let localFile = NCManageDatabase.shared.getResultsTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))?.first
+        if localFile?.etag != metadata.etag || utilityFileSystem.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) == 0 {
+            return true
+        }
+        return false
+    }
+
     // MARK: - Search
 
     /// WebDAV search

+ 1 - 1
iOSClient/Networking/NCService.swift

@@ -301,7 +301,7 @@ class NCService: NSObject {
         let files = NCManageDatabase.shared.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", account), sorted: "fileName", ascending: true)
         for file: tableLocalFile in files {
             guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(file.ocId) else { continue }
-            if NCManageDatabase.shared.isDownloadMetadata(metadata, download: true),
+            if NCNetworking.shared.synchronizeMetadata(metadata),
                appDelegate.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
                 appDelegate.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: NCGlobal.shared.selectorDownloadFile))
             }