Browse Source

Merge pull request #2088 from nextcloud/411/animation

411/animation
Marino Faggiana 2 years ago
parent
commit
528030ed6d

+ 5 - 4
iOSClient/Data/NCDataSource.swift

@@ -26,10 +26,11 @@ import NCCommunication
 
 class NCDataSource: NSObject {
 
-    public var metadatas: [tableMetadata] = []
-    public var metadatasForSection: [NCMetadataForSection] = []
+    var metadatas: [tableMetadata] = []
+    var metadatasForSection: [NCMetadataForSection] = []
 
-    public var directory: tableDirectory?
+    var directory: tableDirectory?
+    var groupByField: String = ""
 
     private var sectionsValue: [String] = []
     private var providers: [NCCSearchProvider]?
@@ -42,7 +43,6 @@ class NCDataSource: NSObject {
     private var directoryOnTop: Bool = true
     private var favoriteOnTop: Bool = true
     private var filterLivePhoto: Bool = true
-    private var groupByField: String = ""
 
     override init() {
         super.init()
@@ -201,6 +201,7 @@ class NCDataSource: NSObject {
 
     // MARK: -
 
+    @discardableResult
     func appendMetadatasToSection(_ metadatas: [tableMetadata], metadataForSection: NCMetadataForSection, lastSearchResult: NCCSearchResult) -> [IndexPath] {
         
         guard let sectionIndex =  getSectionIndex(metadataForSection.sectionValue) else { return [] }

+ 30 - 18
iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

@@ -804,32 +804,39 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
 
     func tapButtonSwitch(_ sender: Any) {
 
-        if collectionView.collectionViewLayout == gridLayout {
+        if layoutForView?.layout == NCGlobal.shared.layoutGrid {
+
             // list layout
             headerMenu?.buttonSwitch.accessibilityLabel = NSLocalizedString("_grid_view_", comment: "")
             layoutForView?.layout = NCGlobal.shared.layoutList
             NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: layoutForView?.layout)
-            self.collectionView.collectionViewLayout.invalidateLayout()
-            self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { _ in
-                self.groupByField = "name"
+            self.groupByField = "name"
+            if self.dataSource.groupByField != self.groupByField {
                 self.dataSource.changeGroupByField(self.groupByField)
-                self.collectionView.reloadData()
-            })
+            }
+
+            self.collectionView.reloadData()
+            self.collectionView.collectionViewLayout.invalidateLayout()
+            self.collectionView.setCollectionViewLayout(self.listLayout, animated: true)
+
         } else {
+
             // grid layout
             headerMenu?.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
             layoutForView?.layout = NCGlobal.shared.layoutGrid
             NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: layoutForView?.layout)
-            self.collectionView.collectionViewLayout.invalidateLayout()
-            self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { _ in
-                if self.isSearching {
-                    self.groupByField = "name"
-                } else {
-                    self.groupByField = "classFile"
-                }
+            if self.isSearching {
+                self.groupByField = "name"
+            } else {
+                self.groupByField = "classFile"
+            }
+            if self.dataSource.groupByField != self.groupByField {
                 self.dataSource.changeGroupByField(self.groupByField)
-                self.collectionView.reloadData()
-            })
+            }
+
+            self.collectionView.reloadData()
+            self.collectionView.collectionViewLayout.invalidateLayout()
+            self.collectionView.setCollectionViewLayout(self.gridLayout, animated: true)
         }
     }
 
@@ -1731,7 +1738,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
 
                 self.headerMenu = header
 
-                if collectionView.collectionViewLayout == gridLayout {
+                if layoutForView?.layout == NCGlobal.shared.layoutGrid {
                     header.setImageSwitchList()
                     header.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
                 } else {
@@ -1757,7 +1764,12 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
                 header.setRichWorkspaceText(richWorkspaceText)
 
                 header.setSectionHeight(heightHeaderSection)
-                header.labelSection.text = self.dataSource.getSectionValue(indexPath: indexPath)
+                if heightHeaderSection == 0 {
+                    header.labelSection.text = ""
+
+                } else {
+                    header.labelSection.text = self.dataSource.getSectionValue(indexPath: indexPath)
+                }
                 header.labelSection.textColor = NCBrandColor.shared.label
 
                 return header
@@ -1842,7 +1854,7 @@ extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
             }
         }
 
-        if isSearching || collectionView.collectionViewLayout == gridLayout || dataSource.numberOfSections() > 1 {
+        if isSearching || layoutForView?.layout == NCGlobal.shared.layoutGrid || dataSource.numberOfSections() > 1 {
             if section == 0 {
                 return (getHeaderHeight(), headerRichWorkspace, NCGlobal.shared.heightSection)
             } else {

+ 4 - 0
iOSClient/Main/Collection Common/NCGridCell.swift

@@ -129,6 +129,10 @@ class NCGridCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellProto
         accessibilityValue = nil
     }
 
+    override func snapshotView(afterScreenUpdates afterUpdates: Bool) -> UIView? {
+        return nil
+    }
+
     @IBAction func touchUpInsideMore(_ sender: Any) {
         delegate?.tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, image: imageItem.image, sender: sender)
     }

+ 4 - 0
iOSClient/Main/Collection Common/NCListCell.swift

@@ -151,6 +151,10 @@ class NCListCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellProto
         accessibilityValue = nil
     }
 
+    override func snapshotView(afterScreenUpdates afterUpdates: Bool) -> UIView? {
+        return nil
+    }
+    
     @IBAction func touchUpInsideShare(_ sender: Any) {
         delegate?.tapShareListItem(with: objectId, sender: sender)
     }

+ 19 - 18
iOSClient/Select/NCSelect.swift

@@ -276,31 +276,28 @@ class NCSelect: UIViewController, UIGestureRecognizerDelegate, UIAdaptivePresent
 
     func tapButtonSwitch(_ sender: Any) {
 
-        if collectionView.collectionViewLayout == gridLayout {
+        if layoutForView?.layout == NCGlobal.shared.layoutGrid {
+
             // list layout
             headerMenu?.buttonSwitch.accessibilityLabel = NSLocalizedString("_grid_view_", comment: "")
-            UIView.animate(withDuration: 0.0, animations: {
-                self.collectionView.collectionViewLayout.invalidateLayout()
-                self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { _ in
-                    self.collectionView.reloadData()
-                })
-            })
             layoutForView?.layout = NCGlobal.shared.layoutList
             NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: layoutForView?.layout)
+
+            self.collectionView.reloadData()
+            self.collectionView.collectionViewLayout.invalidateLayout()
+            self.collectionView.setCollectionViewLayout(self.listLayout, animated: true)
+
         } else {
+
             // grid layout
             headerMenu?.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
-            UIView.animate(withDuration: 0.0, animations: {
-                self.collectionView.collectionViewLayout.invalidateLayout()
-                self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { _ in
-                    self.collectionView.reloadData()
-                })
-            })
             layoutForView?.layout = NCGlobal.shared.layoutGrid
             NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: layoutForView?.layout)
-        }
 
-        reloadDataSource()
+            self.collectionView.reloadData()
+            self.collectionView.collectionViewLayout.invalidateLayout()
+            self.collectionView.setCollectionViewLayout(self.gridLayout, animated: true)
+        }
     }
 
     func tapButtonOrder(_ sender: Any) {
@@ -614,7 +611,7 @@ extension NCSelect: UICollectionViewDataSource {
 
                 self.headerMenu = header
 
-                if collectionView.collectionViewLayout == gridLayout {
+                if layoutForView?.layout == NCGlobal.shared.layoutGrid  {
                     header.setImageSwitchList()
                     header.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
                 } else {
@@ -633,7 +630,11 @@ extension NCSelect: UICollectionViewDataSource {
                 header.setRichWorkspaceText(richWorkspaceText)
 
                 header.setSectionHeight(heightHeaderSection)
-                header.labelSection.text = self.dataSource.getSectionValue(indexPath: indexPath)
+                if heightHeaderSection == 0 {
+                    header.labelSection.text = ""
+                } else {
+                    header.labelSection.text = self.dataSource.getSectionValue(indexPath: indexPath)
+                }
                 header.labelSection.textColor = NCBrandColor.shared.label
 
                 return header
@@ -682,7 +683,7 @@ extension NCSelect: UICollectionViewDelegateFlowLayout {
             }
         }
 
-        if isSearching || collectionView.collectionViewLayout == gridLayout || dataSource.numberOfSections() > 1 {
+        if isSearching || layoutForView?.layout == NCGlobal.shared.layoutGrid  || dataSource.numberOfSections() > 1 {
             if section == 0 {
                 return (NCGlobal.shared.heightButtonsView, headerRichWorkspace, NCGlobal.shared.heightSection)
             } else {

+ 2 - 2
iOSClient/Trash/NCTrash+CollectionView.swift

@@ -79,7 +79,7 @@ extension NCTrash: UICollectionViewDataSource {
 
         var cell: NCTrashCellProtocol & UICollectionViewCell
 
-        if collectionView.collectionViewLayout == listLayout {
+        if layoutForView?.layout ==  NCGlobal.shared.layoutList {
             guard let listCell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCTrashListCell else { return UICollectionViewCell() }
             listCell.delegate = self
             cell = listCell
@@ -146,7 +146,7 @@ extension NCTrash: UICollectionViewDataSource {
             guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as? NCSectionHeaderMenu
             else { return UICollectionReusableView() }
 
-            if collectionView.collectionViewLayout == gridLayout {
+            if layoutForView?.layout == NCGlobal.shared.layoutGrid {
                 header.setImageSwitchList()
                 header.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
             } else {

+ 50 - 56
iOSClient/Trash/NCTrash.swift

@@ -99,10 +99,6 @@ class NCTrash: UIViewController, NCSelectableNavigationView, NCTrashListCellDele
             collectionView.collectionViewLayout = gridLayout
         }
 
-        if trashPath.isEmpty {
-            guard let userId = (appDelegate.userId as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed) else { return }
-            trashPath = appDelegate.urlBase + "/" + NCUtilityFileSystem.shared.getWebDAV(account: appDelegate.account) + "/trashbin/" + userId + "/trash/"
-        }
         setNavigationItem()
         reloadDataSource()
     }
@@ -137,28 +133,25 @@ class NCTrash: UIViewController, NCSelectableNavigationView, NCTrashListCellDele
     func tapButtonSwitch(_ sender: Any) {
 
         if collectionView.collectionViewLayout == gridLayout {
+
             // list layout
-            UIView.animate(withDuration: 0.0, animations: {
-                self.collectionView.collectionViewLayout.invalidateLayout()
-                self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { _ in
-                    self.collectionView.reloadData()
-                })
-            })
             layoutForView?.layout = NCGlobal.shared.layoutList
             NCUtility.shared.setLayoutForView(key: NCGlobal.shared.layoutViewTrash, serverUrl: "", layout: layoutForView?.layout)
+
+            self.collectionView.reloadData()
+            self.collectionView.collectionViewLayout.invalidateLayout()
+            self.collectionView.setCollectionViewLayout(self.listLayout, animated: true)
+
         } else {
+
             // grid layout
-            UIView.animate(withDuration: 0.0, animations: {
-                self.collectionView.collectionViewLayout.invalidateLayout()
-                self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { _ in
-                    self.collectionView.reloadData()
-                })
-            })
             layoutForView?.layout = NCGlobal.shared.layoutGrid
             NCUtility.shared.setLayoutForView(key: NCGlobal.shared.layoutViewTrash, serverUrl: "", layout: layoutForView?.layout)
-        }
 
-        reloadDataSource()
+            self.collectionView.reloadData()
+            self.collectionView.collectionViewLayout.invalidateLayout()
+            self.collectionView.setCollectionViewLayout(self.gridLayout, animated: true)
+        }
     }
 
     func tapButtonOrder(_ sender: Any) {
@@ -246,10 +239,8 @@ class NCTrash: UIViewController, NCSelectableNavigationView, NCTrashListCellDele
     @objc func reloadDataSource(forced: Bool = true) {
 
         layoutForView = NCUtility.shared.getLayoutForView(key: NCGlobal.shared.layoutViewTrash, serverUrl: "")
-
         datasource.removeAll()
-
-        guard let tashItems = NCManageDatabase.shared.getTrash(filePath: trashPath, sort: layoutForView?.sort, ascending: layoutForView?.ascending, account: appDelegate.account) else {
+        guard let trashPath = self.getTrashPath(), let tashItems = NCManageDatabase.shared.getTrash(filePath: trashPath, sort: layoutForView?.sort, ascending: layoutForView?.ascending, account: appDelegate.account) else {
             return
         }
 
@@ -272,6 +263,17 @@ class NCTrash: UIViewController, NCSelectableNavigationView, NCTrashListCellDele
             }
         }
     }
+
+    func getTrashPath() -> String? {
+
+        if self.trashPath.isEmpty {
+            guard let userId = (appDelegate.userId as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed) else { return nil }
+            let trashPath = appDelegate.urlBase + "/" + NCUtilityFileSystem.shared.getWebDAV(account: appDelegate.account) + "/trashbin/" + userId + "/trash/"
+            return trashPath
+        } else {
+            return self.trashPath
+        }
+    }
 }
 
 // MARK: - NC API & Algorithm
@@ -282,40 +284,35 @@ extension NCTrash {
 
         NCCommunication.shared.listingTrash(showHiddenFiles: false, queue: NCCommunicationCommon.shared.backgroundQueue) { account, items, errorCode, errorDescription in
 
-            if errorCode == 0 && account == self.appDelegate.account {
-                NCManageDatabase.shared.deleteTrash(filePath: self.trashPath, account: self.appDelegate.account)
-                NCManageDatabase.shared.addTrash(account: account, items: items)
-            } else if errorCode != 0 {
+            DispatchQueue.main.async { self.refreshControl.endRefreshing() }
+
+            guard errorCode == 0, account == self.appDelegate.account, let trashPath = self.getTrashPath() else {
                 NCContentPresenter.shared.showError(description: errorDescription, errorCode: errorCode)
-            } else {
-                print("[LOG] It has been changed user during networking process, error.")
+                return
             }
 
-            DispatchQueue.main.async {
-                self.refreshControl.endRefreshing()
-                self.reloadDataSource()
-            }
+            NCManageDatabase.shared.deleteTrash(filePath: trashPath, account: self.appDelegate.account)
+            NCManageDatabase.shared.addTrash(account: account, items: items)
+
+            DispatchQueue.main.async { self.reloadDataSource() }
         }
     }
 
     func restoreItem(with fileId: String) {
 
-        guard let tableTrash = NCManageDatabase.shared.getTrashItem(fileId: fileId, account: appDelegate.account) else {
-            return
-        }
-
+        guard let tableTrash = NCManageDatabase.shared.getTrashItem(fileId: fileId, account: appDelegate.account) else { return }
         let fileNameFrom = tableTrash.filePath + tableTrash.fileName
         let fileNameTo = appDelegate.urlBase + "/" + NCUtilityFileSystem.shared.getWebDAV(account: appDelegate.account) + "/trashbin/" + appDelegate.userId + "/restore/" + tableTrash.fileName
 
         NCCommunication.shared.moveFileOrFolder(serverUrlFileNameSource: fileNameFrom, serverUrlFileNameDestination: fileNameTo, overwrite: true) { account, errorCode, errorDescription in
-            if errorCode == 0 && account == self.appDelegate.account {
-                NCManageDatabase.shared.deleteTrash(fileId: fileId, account: account)
-                self.reloadDataSource()
-            } else if errorCode != 0 {
+
+            guard errorCode == 0, account == self.appDelegate.account else {
                 NCContentPresenter.shared.showError(description: errorDescription, errorCode: errorCode)
-            } else {
-                print("[LOG] It has been changed user during networking process, error.")
+                return
             }
+
+            NCManageDatabase.shared.deleteTrash(fileId: fileId, account: account)
+            self.reloadDataSource()
         }
     }
 
@@ -324,34 +321,31 @@ extension NCTrash {
         let serverUrlFileName = appDelegate.urlBase + "/" + NCUtilityFileSystem.shared.getWebDAV(account: appDelegate.account) + "/trashbin/" + appDelegate.userId + "/trash"
 
         NCCommunication.shared.deleteFileOrFolder(serverUrlFileName) { account, errorCode, errorDescription in
-            if errorCode == 0 && account == self.appDelegate.account {
-                NCManageDatabase.shared.deleteTrash(fileId: nil, account: self.appDelegate.account)
-            } else if errorCode != 0 {
+
+            guard errorCode == 0, account == self.appDelegate.account else {
                 NCContentPresenter.shared.showError(description: errorDescription, errorCode: errorCode)
-            } else {
-                print("[LOG] It has been changed user during networking process, error.")
+                return
             }
+
+            NCManageDatabase.shared.deleteTrash(fileId: nil, account: self.appDelegate.account)
             self.reloadDataSource()
         }
     }
 
     func deleteItem(with fileId: String) {
 
-        guard let tableTrash = NCManageDatabase.shared.getTrashItem(fileId: fileId, account: appDelegate.account) else {
-            return
-        }
-
+        guard let tableTrash = NCManageDatabase.shared.getTrashItem(fileId: fileId, account: appDelegate.account) else { return }
         let serverUrlFileName = tableTrash.filePath + tableTrash.fileName
 
         NCCommunication.shared.deleteFileOrFolder(serverUrlFileName) { account, errorCode, errorDescription in
-            if errorCode == 0 && account == self.appDelegate.account {
-                NCManageDatabase.shared.deleteTrash(fileId: fileId, account: account)
-                self.reloadDataSource()
-            } else if errorCode != 0 {
+
+            guard errorCode == 0, account == self.appDelegate.account else {
                 NCContentPresenter.shared.showError(description: errorDescription, errorCode: errorCode)
-            } else {
-                print("[LOG] It has been changed user during networking process, error.")
+                return
             }
+
+            NCManageDatabase.shared.deleteTrash(fileId: fileId, account: account)
+            self.reloadDataSource()
         }
     }