marinofaggiana 4 years ago
parent
commit
c6bf6ef978

+ 12 - 1
iOSClient/Favorites/NCFavorite.swift

@@ -98,9 +98,10 @@ class NCFavorite: UIViewController, UIGestureRecognizerDelegate, NCListCellDeleg
         NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataSource), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataSource), object: nil)
+        NotificationCenter.default.addObserver(self, selector: #selector(downloadFileStart(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadFileStart), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
-        NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(uploadFileStart(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadFileStart), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(uploadFileStart(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadFileStart), object: nil)
+        NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_progressTask), object:nil)
         NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_progressTask), object:nil)
 
 
         changeTheming()
         changeTheming()
@@ -153,6 +154,16 @@ class NCFavorite: UIViewController, UIGestureRecognizerDelegate, NCListCellDeleg
         }
         }
     }
     }
     
     
+    @objc func downloadFileStart(_ notification: NSNotification) {
+        if self.view?.window == nil { return }
+        
+        if let userInfo = notification.userInfo as NSDictionary? {
+            if let metadata = userInfo["metadata"] as? tableMetadata {
+                NCCollectionCommon.shared.notificationDownloadFileStart(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
+            }
+        }
+    }
+    
     @objc func downloadedFile(_ notification: NSNotification) {
     @objc func downloadedFile(_ notification: NSNotification) {
         if self.view?.window == nil { return }
         if self.view?.window == nil { return }
         
         

+ 13 - 5
iOSClient/Main/NCCollectionCommon.swift

@@ -343,7 +343,7 @@ class NCCollectionCommon: NSObject {
     func notificationDeleteFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata, errorCode: Int, errorDescription: String ,onlyLocal: Bool) {
     func notificationDeleteFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata, errorCode: Int, errorDescription: String ,onlyLocal: Bool) {
         if errorCode == 0 {
         if errorCode == 0 {
             if !onlyLocal {
             if !onlyLocal {
-                dataSource?.deleteMetadata(ocId: metadata.ocId)
+                _ = dataSource?.deleteMetadata(ocId: metadata.ocId)
             }
             }
             collectionView?.reloadData()
             collectionView?.reloadData()
         } else {
         } else {
@@ -351,15 +351,23 @@ class NCCollectionCommon: NSObject {
         }
         }
     }
     }
     
     
+    func notificationDownloadFileStart(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
+        if dataSource?.reloadMetadata(ocId: metadata.ocId) ?? false {
+            collectionView?.reloadData()
+        }
+    }
+    
     func notificationDownloadedFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
     func notificationDownloadedFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata) {
-        dataSource?.reloadMetadata(ocId: metadata.ocId)
-        collectionView?.reloadData()
+        if dataSource?.reloadMetadata(ocId: metadata.ocId) ?? false {
+            collectionView?.reloadData()
+        }
     }
     }
     
     
     func notificationUploadedFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata, serverUrl: String, account: String) {
     func notificationUploadedFile(collectionView: UICollectionView?, dataSource: NCDataSource?, metadata: tableMetadata, serverUrl: String, account: String) {
         if metadata.serverUrl == serverUrl && metadata.account == account {
         if metadata.serverUrl == serverUrl && metadata.account == account {
-            dataSource?.reloadMetadata(ocId: metadata.ocId)
-            collectionView?.reloadData()
+            if dataSource?.reloadMetadata(ocId: metadata.ocId) ?? false {
+                collectionView?.reloadData()
+            }
         }
         }
     }
     }
     
     

+ 8 - 2
iOSClient/Main/NCDataSource.swift

@@ -136,18 +136,24 @@ import Foundation
         return (directories, files, size)
         return (directories, files, size)
     }
     }
     
     
-    @objc func deleteMetadata(ocId: String) {
+    @objc func deleteMetadata(ocId: String) -> Bool {
+        var found = false
         if let index = self.getIndexMetadata(ocId: ocId) {
         if let index = self.getIndexMetadata(ocId: ocId) {
             metadatas.remove(at: index)
             metadatas.remove(at: index)
+            found = true
         }
         }
+        return found
     }
     }
     
     
-    @objc func reloadMetadata(ocId: String) {
+    @objc func reloadMetadata(ocId: String) -> Bool {
+        var found = false
         if let index = self.getIndexMetadata(ocId: ocId) {
         if let index = self.getIndexMetadata(ocId: ocId) {
             if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", ocId)) {
             if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", ocId)) {
                 metadatas[index] = metadata
                 metadatas[index] = metadata
+                found = true
             }
             }
         }
         }
+        return found
     }
     }
     
     
     @objc func addMetadata(_ metadata: tableMetadata) {
     @objc func addMetadata(_ metadata: tableMetadata) {

+ 4 - 1
iOSClient/Networking/NCOperationQueue.swift

@@ -235,7 +235,10 @@ class NCOperationSynchronization: ConcurrentOperation {
                 }
                 }
             } else {
             } else {
                 if self.download {
                 if self.download {
-                    NCOperationQueue.shared.download(metadata: metadata, selector: self.selector, setFavorite: false)
+                    let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
+                    if localFile == nil || localFile?.etag != metadata.etag {
+                        NCOperationQueue.shared.download(metadata: metadata, selector: self.selector, setFavorite: false)
+                    }
                 }
                 }
                 self.finish()
                 self.finish()
             }
             }

+ 3 - 3
iOSClient/Utility/NotificationCenter+MainThread.swift

@@ -26,9 +26,9 @@ import Foundation
 extension NotificationCenter {
 extension NotificationCenter {
 
 
     func postOnMainThread(name: String, object anObject: Any? = nil, userInfo aUserInfo: [AnyHashable : Any]? = nil) {
     func postOnMainThread(name: String, object anObject: Any? = nil, userInfo aUserInfo: [AnyHashable : Any]? = nil) {
-        if UIApplication.shared.applicationState == .background {
-            return
-        }
+//        if UIApplication.shared.applicationState == .background {
+//            return
+//        }
         DispatchQueue.main.async {
         DispatchQueue.main.async {
             NotificationCenter.default.post(name: Notification.Name.init(rawValue: name), object: anObject, userInfo: aUserInfo)
             NotificationCenter.default.post(name: Notification.Name.init(rawValue: name), object: anObject, userInfo: aUserInfo)
         }
         }