浏览代码

improve detect LivePhoto

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 年之前
父节点
当前提交
bc1873e43d
共有 2 个文件被更改,包括 46 次插入55 次删除
  1. 45 24
      iOSClient/Data/NCManageDatabase+Metadata.swift
  2. 1 31
      iOSClient/Media/NCMedia.swift

+ 45 - 24
iOSClient/Data/NCManageDatabase+Metadata.swift

@@ -253,7 +253,24 @@ extension NCManageDatabase {
             counter += 1
         }
 
-        completion(metadataFolder, metadataFolders, setLivePhoto(metadatas: metadatas))
+        //
+        // Detect Live photo
+        //
+        var metadataOutput: [tableMetadata] = []
+        metadatas = metadatas.sorted(by: {$0.fileNameView < $1.fileNameView})
+
+        for index in metadatas.indices {
+            let metadata = metadatas[index]
+            if index < metadatas.count - 1,
+                metadata.fileNoExtension == metadatas[index+1].fileNoExtension,
+                ((metadata.classFile == NKCommon.typeClassFile.image.rawValue && metadatas[index+1].classFile == NKCommon.typeClassFile.video.rawValue) || (metadata.classFile == NKCommon.typeClassFile.video.rawValue && metadatas[index+1].classFile == NKCommon.typeClassFile.image.rawValue)){
+                metadata.livePhoto = true
+                metadatas[index+1].livePhoto = true
+            }
+            metadataOutput.append(metadata)
+        }
+
+        completion(metadataFolder, metadataFolders, metadataOutput)
     }
 
     @objc func createMetadata(account: String, user: String, userId: String, fileName: String, fileNameView: String, ocId: String, serverUrl: String, urlBase: String, url: String, contentType: String, isLivePhoto: Bool = false, isUrl: Bool = false, name: String = NCGlobal.shared.appName, subline: String? = nil, iconName: String? = nil, iconUrl: String? = nil) -> tableMetadata {
@@ -560,25 +577,6 @@ extension NCManageDatabase {
         }
     }
 
-    func setLivePhoto(metadatas: [tableMetadata]) -> [tableMetadata] {
-
-        let numMetadatas: Int = metadatas.count - 1
-        var metadataOutput: [tableMetadata] = []
-        let metadatas = metadatas.sorted(by: {$0.fileNameView < $1.fileNameView})
-
-        for index in metadatas.indices {
-            let metadata = metadatas[index]
-            if index < numMetadatas,
-               metadata.fileNoExtension == metadatas[index+1].fileNoExtension,
-               ((metadata.classFile == NKCommon.typeClassFile.image.rawValue && metadatas[index+1].classFile == NKCommon.typeClassFile.video.rawValue) || (metadata.classFile == NKCommon.typeClassFile.video.rawValue && metadatas[index+1].classFile == NKCommon.typeClassFile.image.rawValue)){
-                metadata.livePhoto = true
-                metadatas[index+1].livePhoto = true
-            }
-            metadataOutput.append(metadata)
-        }
-        return metadataOutput
-    }
-
     @objc func updateMetadatasFavorite(account: String, metadatas: [tableMetadata]) {
 
         let realm = try! Realm()
@@ -879,14 +877,37 @@ extension NCManageDatabase {
         return tableMetadata.init(value: result)
     }
 
-    func getMetadatasMedia(predicate: NSPredicate) -> [tableMetadata] {
+    func getMetadatasMedia(predicate: NSPredicate, livePhoto: Bool) -> [tableMetadata] {
 
         let realm = try! Realm()
-        realm.refresh()
+        var metadatas: [tableMetadata] = []
 
         let results = realm.objects(tableMetadata.self).filter(predicate).sorted(byKeyPath: "fileNameView", ascending: false)
-
-        return Array(results.map { tableMetadata.init(value: $0) })
+        if livePhoto {
+            for index in results.indices {
+                let metadata = results[index]
+                if index < results.count - 1, metadata.fileNoExtension == results[index+1].fileNoExtension {
+                    var update = false
+                    if !metadata.livePhoto || !results[index+1].livePhoto { update = true }
+                    metadata.livePhoto = true
+                    results[index+1].livePhoto = true
+                    if update {
+                        NCManageDatabase.shared.addMetadatas([metadata, results[index+1]])
+                    }
+                }
+                if metadata.livePhoto {
+                    if metadata.classFile == NKCommon.typeClassFile.image.rawValue {
+                        metadatas.append(tableMetadata.init(value: metadata))
+                    }
+                    continue
+                } else {
+                    metadatas.append(tableMetadata.init(value: metadata))
+                }
+            }
+            return metadatas
+        } else {
+            return Array(results.map { tableMetadata.init(value: $0) })
+        }
     }
 
     func isMetadataShareOrMounted(metadata: tableMetadata, metadataFolder: tableMetadata?) -> Bool {

+ 1 - 31
iOSClient/Media/NCMedia.swift

@@ -437,31 +437,6 @@ extension NCMedia {
     @objc func reloadDataSourceWithCompletion(_ completion: @escaping (_ metadatas: [tableMetadata]) -> Void) {
         guard !appDelegate.account.isEmpty else { return }
 
-        func setLivePhoto(metadatas: [tableMetadata]) {
-
-            let numMetadatas: Int = metadatas.count - 1
-            self.metadatas.removeAll()
-
-            for index in metadatas.indices {
-                let metadata = metadatas[index]
-                if index < numMetadatas, metadata.fileNoExtension == metadatas[index+1].fileNoExtension {
-                    var update = false
-                    if !metadata.livePhoto || !metadatas[index+1].livePhoto { update = true }
-                    metadata.livePhoto = true
-                    metadatas[index+1].livePhoto = true
-                    if update {
-                        NCManageDatabase.shared.addMetadatas([tableMetadata.init(value: metadata), tableMetadata.init(value: metadatas[index+1])])
-                    }
-                }
-                if metadata.livePhoto {
-                    if metadata.classFile == NKCommon.typeClassFile.image.rawValue { self.metadatas.append(metadata) }
-                    continue
-                } else {
-                    self.metadatas.append(metadata)
-                }
-            }
-        }
-
         if account != appDelegate.account {
             self.metadatas = []
             account = appDelegate.account
@@ -487,12 +462,7 @@ extension NCMedia {
 
         guard let predicate = predicate else { return }
         DispatchQueue.global().async {
-            let metadatas = NCManageDatabase.shared.getMetadatasMedia(predicate: predicate)
-            if self.livePhoto {
-                setLivePhoto(metadatas: metadatas)
-            } else {
-                self.metadatas = metadatas
-            }
+            self.metadatas = NCManageDatabase.shared.getMetadatasMedia(predicate: predicate, livePhoto: self.livePhoto)
             switch CCUtility.getMediaSortDate() {
             case "date":
                 self.metadatas = self.metadatas.sorted(by: {($0.date as Date) > ($1.date as Date)} )