marinofaggiana 6 years ago
parent
commit
0a679dca60
2 changed files with 20 additions and 12 deletions
  1. 2 2
      iOSClient/Database/NCManageDatabase.swift
  2. 18 10
      iOSClient/Media/NCMedia.swift

+ 2 - 2
iOSClient/Database/NCManageDatabase.swift

@@ -2004,10 +2004,10 @@ class NCManageDatabase: NSObject {
         realm.refresh()
         
         if let entities = realm.objects(tablePhotos.self).filter("account = %@", account).max(by: { $0.date.compare($1.date as Date) == .orderedDescending }) {
-            return entities.date as Date
+            return Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: entities.date as Date)!
         }
         
-        return Date()
+        return Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date())!
     }
     
     @objc func deletePhotos(fileID: String) {

+ 18 - 10
iOSClient/Media/NCMedia.swift

@@ -53,7 +53,7 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
     private var addWidth: CGFloat = 10
     
     private var readRetry = 1
-    private let stepDays = -60
+    private let stepDays = -30
     private var isDistantPast = false
 
     private let refreshControl = UIRefreshControl()
@@ -558,21 +558,29 @@ class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDel
     func selectSearchSections() {
         
         let sections = NSMutableSet()
+        let lastDate = NCManageDatabase.sharedInstance.getTablePhotoLastDate(account: self.appDelegate.activeAccount)
+        var gteDate: Date?
+        
         for item in collectionView.indexPathsForVisibleItems {
-            let date = sectionDatasource.sections.object(at: item.section) as! Date
-            sections.add(date)
+            if let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(item, sectionDataSource: sectionDatasource) {
+                if let date = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: metadata.date as Date) {
+                    sections.add(date)
+                }
+            }
         }
         let sortedSections = sections.sorted { (date1, date2) -> Bool in
             (date1 as! Date).compare(date2 as! Date) == .orderedDescending
         }
-        if sortedSections.count == 1 {
-            let lteDate = Calendar.current.date(byAdding: .day, value: 1, to: sortedSections.first as! Date)!
-            let gteDate = Calendar.current.date(byAdding: .day, value: stepDays, to: sortedSections.first as! Date)!
-            search(lteDate: lteDate, gteDate: gteDate, activityIndicator: false)
-        } else if sortedSections.count > 1 {
+        
+        if sortedSections.count >= 1 {
             let lteDate = Calendar.current.date(byAdding: .day, value: 1, to: sortedSections.first as! Date)!
-            let gteDate = Calendar.current.date(byAdding: .day, value: stepDays, to: sortedSections.last as! Date)!
-            search(lteDate: lteDate, gteDate: gteDate, activityIndicator: false)
+            if lastDate == sortedSections.last as! Date {
+                gteDate = Calendar.current.date(byAdding: .day, value: stepDays, to: sortedSections.last as! Date)!
+                search(lteDate: lteDate, gteDate: gteDate!, activityIndicator: true)
+            } else {
+                gteDate = Calendar.current.date(byAdding: .day, value: -1, to: sortedSections.last as! Date)!
+                search(lteDate: lteDate, gteDate: gteDate!, activityIndicator: false)
+            }
         }
     }