ソースを参照

Last opening date (#2662)

* Update NCUtilityFileSystem.swift

---------

Co-authored-by: Marino Faggiana <8616947+marinofaggiana@users.noreply.github.com>
Marino Faggiana 1 年間 前
コミット
a190d3ea54

+ 1 - 1
Brand/Database.swift

@@ -26,4 +26,4 @@ import Foundation
 // Database Realm
 //
 let databaseName                    = "nextcloud.realm"
-let databaseSchemaVersion: UInt64   = 321
+let databaseSchemaVersion: UInt64   = 322

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

@@ -37,6 +37,7 @@ class tableLocalFile: Object {
     @objc dynamic var fileName = ""
     @objc dynamic var ocId = ""
     @objc dynamic var offline: Bool = false
+    @objc dynamic var lastOpeningDate = NSDate()
 
     override static func primaryKey() -> String {
         return "ocId"
@@ -206,4 +207,41 @@ extension NCManageDatabase {
 
         return []
     }
+
+    func getResultsTableLocalFile(predicate: NSPredicate, sorted: String, ascending: Bool) -> Results<tableLocalFile>? {
+
+        do {
+            let realm = try Realm()
+            realm.refresh()
+            return realm.objects(tableLocalFile.self).filter(predicate).sorted(byKeyPath: sorted, ascending: ascending)
+        } catch let error as NSError {
+            NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
+        }
+
+        return nil
+    }
+
+    func setLastOpeningDate(metadata: tableMetadata) {
+
+        do {
+            let realm = try Realm()
+            try realm.write {
+                if let result = realm.objects(tableLocalFile.self).filter("ocId == %@", metadata.ocId).first {
+                    result.lastOpeningDate = NSDate()
+                } else {
+                    let addObject = tableLocalFile()
+                    addObject.account = metadata.account
+                    addObject.etag = metadata.etag
+                    addObject.exifDate = NSDate()
+                    addObject.exifLatitude = "-1"
+                    addObject.exifLongitude = "-1"
+                    addObject.ocId = metadata.ocId
+                    addObject.fileName = metadata.fileName
+                    realm.add(addObject, update: .all)
+                }
+            }
+        } catch let error {
+            NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
+        }
+    }
 }

+ 16 - 28
iOSClient/Utility/NCUtilityFileSystem.swift

@@ -532,45 +532,33 @@ class NCUtilityFileSystem: NSObject {
         let minimumDate = Date().addingTimeInterval(-days * 24 * 60 * 60)
         let url = URL(fileURLWithPath: directory)
         var offlineDir: [String] = []
-        var offlineFiles: [String] = []
 
         if let directories = NCManageDatabase.shared.getTablesDirectory(predicate: NSPredicate(format: "offline == true"), sorted: "serverUrl", ascending: true) {
             for directory: tableDirectory in directories {
                 offlineDir.append(getDirectoryProviderStorageOcId(directory.ocId))
             }
         }
-
-        let files = NCManageDatabase.shared.getTableLocalFiles(predicate: NSPredicate(format: "offline == true"), sorted: "fileName", ascending: true)
-        for file: tableLocalFile in files {
-            offlineFiles.append(getDirectoryProviderStorageOcId(file.ocId, fileNameView: file.fileName))
-        }
-
-        func meetsRequirement(date: Date) -> Bool {
-            return date < minimumDate
-        }
+        let resultsLocalFile = NCManageDatabase.shared.getResultsTableLocalFile(predicate: NSPredicate(format: "offline == false"), sorted: "lastOpeningDate", ascending: true)
 
         let manager = FileManager.default
         if let enumerator = manager.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: []) {
             for case let fileURL as URL in enumerator {
                 if let attributes = try? manager.attributesOfItem(atPath: fileURL.path) {
-                    if let date = CCUtility.getATime(fileURL.path) {
-                        if attributes[.size] as? Double == 0 { continue }
-                        if attributes[.type] as? FileAttributeType == FileAttributeType.typeDirectory { continue }
-                        if fileURL.pathExtension == NCGlobal.shared.extensionPreview { continue }
-                        // check offline
-                        if offlineFiles.contains(fileURL.path) { continue }
-                        let filter = offlineDir.filter({ fileURL.path.hasPrefix($0)})
-                        if !filter.isEmpty { continue }
-                        // check date
-                        if meetsRequirement(date: date) {
-                            let folderURL = fileURL.deletingLastPathComponent()
-                            let ocId = folderURL.lastPathComponent
-                            do {
-                                try manager.removeItem(atPath: fileURL.path)
-                            } catch { }
-                            manager.createFile(atPath: fileURL.path, contents: nil, attributes: nil)
-                            NCManageDatabase.shared.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", ocId))
-                        }
+                    if attributes[.size] as? Double == 0 { continue }
+                    if attributes[.type] as? FileAttributeType == FileAttributeType.typeDirectory { continue }
+                    if fileURL.pathExtension == NCGlobal.shared.extensionPreview { continue }
+                    // check directory offline
+                    let filter = offlineDir.filter({ fileURL.path.hasPrefix($0)})
+                    if !filter.isEmpty { continue }
+                    // -----------------------
+                    let folderURL = fileURL.deletingLastPathComponent()
+                    let ocId = folderURL.lastPathComponent
+                    if let result = resultsLocalFile?.filter({ $0.ocId == ocId }).first, (result.lastOpeningDate as Date) < minimumDate {
+                        do {
+                            try manager.removeItem(atPath: fileURL.path)
+                        } catch { }
+                        manager.createFile(atPath: fileURL.path, contents: nil, attributes: nil)
+                        NCManageDatabase.shared.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", ocId))
                     }
                 }
             }

+ 3 - 0
iOSClient/Viewer/NCViewer.swift

@@ -88,6 +88,9 @@ class NCViewer: NSObject {
         // DOCUMENTS
         if metadata.classFile == NKCommon.TypeClassFile.document.rawValue {
 
+            // Set Last Opening Date
+            NCManageDatabase.shared.setLastOpeningDate(metadata: metadata)
+
             // PDF
             if metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf" {
 

+ 3 - 0
iOSClient/Viewer/NCViewerMedia/NCViewerMedia.swift

@@ -153,6 +153,9 @@ class NCViewerMedia: UIViewController {
     override func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)
 
+        // Set Last Opening Date
+        NCManageDatabase.shared.setLastOpeningDate(metadata: metadata)
+
         viewerMediaPage?.clearCommandCenter()
 
         if metadata.isAudioOrVideo {