Browse Source

optimized downloadThumbnail, used ActivityIndicator, UI/UX

marinofaggiana 6 years ago
parent
commit
d3ab1e9bf9

+ 4 - 2
iOSClient/Main/NCMainCommon.swift

@@ -208,7 +208,7 @@ class NCMainCommon: NSObject, PhotoEditorDelegate {
     
     //MARK: -
     
-    func collectionViewCellForItemAt(_ indexPath: IndexPath, collectionView: UICollectionView, cell: UICollectionViewCell, metadata: tableMetadata, metadataFolder: tableMetadata?, serverUrl: String, isEditMode: Bool, selectFileID: [String], autoUploadFileName: String, autoUploadDirectory: String, hideButtonMore: Bool, source: UIViewController) {
+    func collectionViewCellForItemAt(_ indexPath: IndexPath, collectionView: UICollectionView, cell: UICollectionViewCell, metadata: tableMetadata, metadataFolder: tableMetadata?, serverUrl: String, isEditMode: Bool, selectFileID: [String], autoUploadFileName: String, autoUploadDirectory: String, hideButtonMore: Bool, downloadThumbnail: Bool,source: UIViewController) {
         
         var image: UIImage?
         var imagePreview = false
@@ -225,7 +225,9 @@ class NCMainCommon: NSObject, PhotoEditorDelegate {
         }
         
         // Download preview
-        NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata, view: collectionView, indexPath: indexPath)
+        if downloadThumbnail {
+            NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata, view: collectionView, indexPath: indexPath)
+        }
         
         // Share
         let sharesLink = appDelegate.sharesLink.object(forKey: serverUrl + metadata.fileName)

+ 1 - 0
iOSClient/Main/Section/NCSectionHeaderFooter.swift

@@ -166,6 +166,7 @@ class NCSectionMediaHeader: UICollectionReusableView {
     
     @IBOutlet weak var labelSection: UILabel!
     @IBOutlet weak var labelLeadingConstraint: NSLayoutConstraint!
+    @IBOutlet weak var labelHeightConstraint: NSLayoutConstraint!
     @IBOutlet weak var labelWidthConstraint: NSLayoutConstraint!
     
     override func awakeFromNib() {

+ 4 - 2
iOSClient/Main/Section/NCSectionMediaHeader.xib

@@ -17,11 +17,12 @@
             <autoresizingMask key="autoresizingMask"/>
             <subviews>
                 <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="LabelSectionHeader" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="gzy-cT-Gjn" userLabel="Label Section">
-                    <rect key="frame" x="109" y="15.5" width="158" height="19.5"/>
+                    <rect key="frame" x="109" y="14" width="158" height="22"/>
                     <constraints>
+                        <constraint firstAttribute="height" constant="22" id="fSy-kX-khX"/>
                         <constraint firstAttribute="width" constant="158" id="q8E-Aw-w6I"/>
                     </constraints>
-                    <fontDescription key="fontDescription" type="boldSystem" pointSize="16"/>
+                    <fontDescription key="fontDescription" type="system" pointSize="14"/>
                     <nil key="textColor"/>
                     <nil key="highlightedColor"/>
                 </label>
@@ -32,6 +33,7 @@
             </constraints>
             <viewLayoutGuide key="safeArea" id="EFn-SN-cxu"/>
             <connections>
+                <outlet property="labelHeightConstraint" destination="fSy-kX-khX" id="I8c-I3-WSP"/>
                 <outlet property="labelLeadingConstraint" destination="lwa-IO-uij" id="V9f-rn-jae"/>
                 <outlet property="labelSection" destination="gzy-cT-Gjn" id="yG6-wJ-CmI"/>
                 <outlet property="labelWidthConstraint" destination="q8E-Aw-w6I" id="3kq-Fd-E9d"/>

+ 63 - 14
iOSClient/Media/NCMedia.swift

@@ -125,12 +125,22 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
         selectSearchSections()
     }
     
+    override func viewDidAppear(_ animated: Bool) {
+        super.viewDidAppear(animated)
+        
+        collectionView?.reloadDataThenPerform {
+            self.downloadThumbnail()
+        }
+    }
+    
     override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
         super.viewWillTransition(to: size, with: coordinator)
         
         coordinator.animate(alongsideTransition: nil) { _ in
             self.collectionView.collectionViewLayout.invalidateLayout()
-            self.collectionView.reloadData()
+            self.collectionView?.reloadDataThenPerform {
+                self.downloadThumbnail()
+            }
             self.actionSheet?.viewDidLayoutSubviews()
         }
     }
@@ -258,7 +268,9 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
             case 0:
                 isEditMode = false
                 selectFileID.removeAll()
-                collectionView.reloadData()
+                collectionView?.reloadDataThenPerform {
+                    self.downloadThumbnail()
+                }
             case 1:
                 deleteItems()
             default: ()
@@ -345,7 +357,8 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
         }
         
         if addPast {
-            CCGraphics.addImage(toTitle: NSLocalizedString("_media_", comment: ""), colorTitle: NCBrandColor.sharedInstance.brandText, imageTitle: CCGraphics.changeThemingColorImage(UIImage.init(named: "load"), multiplier: 2, color: NCBrandColor.sharedInstance.brandText), imageRight: false, navigationItem: self.navigationItem)
+            //CCGraphics.addImage(toTitle: NSLocalizedString("_media_", comment: ""), colorTitle: NCBrandColor.sharedInstance.brandText, imageTitle: CCGraphics.changeThemingColorImage(UIImage.init(named: "load"), multiplier: 2, color: NCBrandColor.sharedInstance.brandText), imageRight: false, navigationItem: self.navigationItem)
+            NCUtility.sharedInstance.startActivityIndicator(view: self.view, bottom: 50)
         }
         loadingSearch = true
 
@@ -356,8 +369,9 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
             self.loadingSearch = false
 
             self.refreshControl.endRefreshing()
-            self.navigationItem.titleView = nil
-            self.navigationItem.title = NSLocalizedString("_media_", comment: "")
+            NCUtility.sharedInstance.stopActivityIndicator()
+            //self.navigationItem.titleView = nil
+            //self.navigationItem.title = NSLocalizedString("_media_", comment: "")
             
             if errorCode == 0 && account == self.appDelegate.activeAccount {
                 
@@ -403,7 +417,9 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
                     }
                 }
                 
-                self.collectionView?.reloadData()
+                self.collectionView?.reloadDataThenPerform {
+                    self.downloadThumbnail()
+                }
                 
             }  else {
                 
@@ -421,18 +437,20 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
             return
         }
         
-        if self.sectionDatasource.allRecordsDataSource.count == 0 {
+        if sectionDatasource.allRecordsDataSource.count == 0 {
             
             let gteDate = Calendar.current.date(byAdding: .day, value: -30, to: Date())
-            self.search(lteDate: Date(), gteDate: gteDate!, addPast: true, setDistantPast: false)
+            search(lteDate: Date(), gteDate: gteDate!, addPast: true, setDistantPast: false)
             
         } else {
             
             let gteDate = NCManageDatabase.sharedInstance.getTableMediaDate(account: self.appDelegate.activeAccount, order: .orderedAscending)
-            self.search(lteDate: Date(), gteDate: gteDate, addPast: false, setDistantPast: false)
+            search(lteDate: Date(), gteDate: gteDate, addPast: false, setDistantPast: false)
         }
         
-        self.collectionView?.reloadData()
+        collectionView?.reloadDataThenPerform {
+            self.downloadThumbnail()
+        }
     }
     
     private func selectSearchSections() {
@@ -464,6 +482,14 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
         }
     }
     
+    private func downloadThumbnail() {
+        for item in collectionView.indexPathsForVisibleItems {
+            if let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(item, sectionDataSource: sectionDatasource) {
+               NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata, view: collectionView, indexPath: item)
+            }
+        }
+    }
+    
     // MARK: COLLECTIONVIEW METHODS
     
     public func collectionViewReloadDataSource(loadNetworkDatasource: Bool) {
@@ -483,7 +509,9 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
                     self.loadNetworkDatasource()
                 }
                 
-                self.collectionView?.reloadData()
+                self.collectionView?.reloadDataThenPerform {
+                    self.downloadThumbnail()
+                }
             }
         }
     }
@@ -506,8 +534,8 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
             
             header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
             header.labelSection.textColor = .white
-            header.labelSection.layer.cornerRadius = 11
-            header.labelSection.layer.masksToBounds = true
+            header.labelHeightConstraint.constant = 20
+            header.labelSection.layer.cornerRadius = 10
             header.labelSection.layer.backgroundColor = UIColor(red: 152.0/255.0, green: 167.0/255.0, blue: 181.0/255.0, alpha: 0.8).cgColor
             let width = header.labelSection.intrinsicContentSize.width + 30
             let leading = collectionView.bounds.width / 2 - width / 2
@@ -565,7 +593,7 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
         
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridMediaCell
       
-        NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: nil, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectFileID: selectFileID, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: true, source: self)
+        NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: nil, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectFileID: selectFileID, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: true, downloadThumbnail: false, source: self)
         
         return cell
     }
@@ -615,6 +643,27 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
     }
 }
 
+extension FastScrollCollectionView
+{
+    /// Calls reloadsData() on self, and ensures that the given closure is
+    /// called after reloadData() has been completed.
+    ///
+    /// Discussion: reloadData() appears to be asynchronous. i.e. the
+    /// reloading actually happens during the next layout pass. So, doing
+    /// things like scrolling the collectionView immediately after a
+    /// call to reloadData() can cause trouble.
+    ///
+    /// This method uses CATransaction to schedule the closure.
+    
+    func reloadDataThenPerform(_ closure: @escaping (() -> Void))
+    {
+        CATransaction.begin()
+        CATransaction.setCompletionBlock(closure)
+        self.reloadData()
+        CATransaction.commit()
+    }
+}
+
 // MARK: - UIScrollViewDelegate
 
 extension NCMedia: UIScrollViewDelegate {

+ 1 - 1
iOSClient/Offline/NCOffline.swift

@@ -594,7 +594,7 @@ class NCOffline: UIViewController ,UICollectionViewDataSource, UICollectionViewD
             cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
         }
         
-        NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: nil, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectFileID: selectFileID, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: false, source: self)
+        NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: nil, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectFileID: selectFileID, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: false, downloadThumbnail: true, source: self)
         
         return cell
     }

+ 1 - 1
iOSClient/Select/NCSelect.swift

@@ -656,7 +656,7 @@ class NCSelect: UIViewController ,UICollectionViewDataSource, UICollectionViewDe
             cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
         }
         
-        NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: metadataFolder, serverUrl: serverUrl, isEditMode: isEditMode, selectFileID: selectFileID, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory ,hideButtonMore: true, source: self)
+        NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: metadataFolder, serverUrl: serverUrl, isEditMode: isEditMode, selectFileID: selectFileID, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory ,hideButtonMore: true, downloadThumbnail: true, source: self)
         
         return cell
     }