Skip to main content

WIP make it faster (but more complicated)

ID
3bba5d9
date
2023-06-15 08:15:55+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
7859ac3
message
WIP make it faster (but more complicated)
changed files
2 files, 57 additions, 30 deletions

Changed files

BlinkReviewer/Blink.xcodeproj/project.pbxproj (34154) → BlinkReviewer/Blink.xcodeproj/project.pbxproj (34154)

diff --git a/BlinkReviewer/Blink.xcodeproj/project.pbxproj b/BlinkReviewer/Blink.xcodeproj/project.pbxproj
index 30bff5c..8b267a5 100644
--- a/BlinkReviewer/Blink.xcodeproj/project.pbxproj
+++ b/BlinkReviewer/Blink.xcodeproj/project.pbxproj
@@ -588,7 +588,7 @@
 				CODE_SIGN_ENTITLEMENTS = Blink/Blink.entitlements;
 				CODE_SIGN_STYLE = Automatic;
 				COMBINE_HIDPI_IMAGES = YES;
-				CURRENT_PROJECT_VERSION = 18;
+				CURRENT_PROJECT_VERSION = 30;
 				DEVELOPMENT_ASSET_PATHS = "\"Blink/Preview Content\"";
 				ENABLE_PREVIEWS = YES;
 				GENERATE_INFOPLIST_FILE = YES;
@@ -615,7 +615,7 @@
 				CODE_SIGN_ENTITLEMENTS = Blink/Blink.entitlements;
 				CODE_SIGN_STYLE = Automatic;
 				COMBINE_HIDPI_IMAGES = YES;
-				CURRENT_PROJECT_VERSION = 18;
+				CURRENT_PROJECT_VERSION = 30;
 				DEVELOPMENT_ASSET_PATHS = "\"Blink/Preview Content\"";
 				ENABLE_PREVIEWS = YES;
 				GENERATE_INFOPLIST_FILE = YES;
@@ -640,7 +640,7 @@
 				ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
 				BUNDLE_LOADER = "$(TEST_HOST)";
 				CODE_SIGN_STYLE = Automatic;
-				CURRENT_PROJECT_VERSION = 18;
+				CURRENT_PROJECT_VERSION = 30;
 				GENERATE_INFOPLIST_FILE = YES;
 				MACOSX_DEPLOYMENT_TARGET = 13.3;
 				MARKETING_VERSION = 1.0;
@@ -658,7 +658,7 @@
 				ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
 				BUNDLE_LOADER = "$(TEST_HOST)";
 				CODE_SIGN_STYLE = Automatic;
-				CURRENT_PROJECT_VERSION = 18;
+				CURRENT_PROJECT_VERSION = 30;
 				GENERATE_INFOPLIST_FILE = YES;
 				MACOSX_DEPLOYMENT_TARGET = 13.3;
 				MARKETING_VERSION = 1.0;
@@ -675,7 +675,7 @@
 			buildSettings = {
 				ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
 				CODE_SIGN_STYLE = Automatic;
-				CURRENT_PROJECT_VERSION = 18;
+				CURRENT_PROJECT_VERSION = 30;
 				GENERATE_INFOPLIST_FILE = YES;
 				MARKETING_VERSION = 1.0;
 				PRODUCT_BUNDLE_IDENTIFIER = net.alexwlchan.BlinkReviewerUITests;
@@ -691,7 +691,7 @@
 			buildSettings = {
 				ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
 				CODE_SIGN_STYLE = Automatic;
-				CURRENT_PROJECT_VERSION = 18;
+				CURRENT_PROJECT_VERSION = 30;
 				GENERATE_INFOPLIST_FILE = YES;
 				MARKETING_VERSION = 1.0;
 				PRODUCT_BUNDLE_IDENTIFIER = net.alexwlchan.BlinkReviewerUITests;

BlinkReviewer/Blink/Photos/PhotosLibrary.swift (11438) → BlinkReviewer/Blink/Photos/PhotosLibrary.swift (12709)

diff --git a/BlinkReviewer/Blink/Photos/PhotosLibrary.swift b/BlinkReviewer/Blink/Photos/PhotosLibrary.swift
index 4402eb4..fece0a4 100644
--- a/BlinkReviewer/Blink/Photos/PhotosLibrary.swift
+++ b/BlinkReviewer/Blink/Photos/PhotosLibrary.swift
@@ -76,24 +76,58 @@ class PhotosLibrary: NSObject, ObservableObject, PHPhotoLibraryChangeObserver {
             
             if let assetsChangeDetails = changeInstance.changeDetails(for: self.assets) {
                 self.assets = assetsChangeDetails.fetchResultAfterChanges
-                self.regenerateAssetIdentifiers()
+                
+                assetsChangeDetails.changedObjects.forEach { asset in
+                    if asset.isFavorite {
+                        self.favoriteAssetIdentifiers.insert(asset.localIdentifier)
+                    } else {
+                        self.favoriteAssetIdentifiers.remove(asset.localIdentifier)
+                    }
+                }
+                
+                if assetsChangeDetails.hasMoves {
+                    self.regenerateAssetIdentifiers()
+                }
+                
                 self.latestChangeDetails = assetsChangeDetails
             }
             
             if let approvedChangeDetails = changeInstance.changeDetails(for: self.approvedAssets) {
                 self.approvedAssets = approvedChangeDetails.fetchResultAfterChanges
+                
+                approvedChangeDetails.insertedObjects.forEach { asset in
+                    self.approvedAssetIdentifiers.insert(asset.localIdentifier)
+                }
+                
+                approvedChangeDetails.removedObjects.forEach { asset in
+                    self.approvedAssetIdentifiers.remove(asset.localIdentifier)
+                }
             }
             
             if let rejectedChangeDetails = changeInstance.changeDetails(for: self.rejectedAssets) {
                 self.rejectedAssets = rejectedChangeDetails.fetchResultAfterChanges
+                
+                rejectedChangeDetails.insertedObjects.forEach { asset in
+                    self.rejectedAssetIdentifiers.insert(asset.localIdentifier)
+                }
+                
+                rejectedChangeDetails.removedObjects.forEach { asset in
+                    self.rejectedAssetIdentifiers.remove(asset.localIdentifier)
+                }
             }
             
             if let needsActionChangeDetails = changeInstance.changeDetails(for: self.needsActionAssets) {
                 self.needsActionAssets = needsActionChangeDetails.fetchResultAfterChanges
+                
+                needsActionChangeDetails.insertedObjects.forEach { asset in
+                    self.needsActionAssetIdentifiers.insert(asset.localIdentifier)
+                }
+                
+                needsActionChangeDetails.removedObjects.forEach { asset in
+                    self.needsActionAssetIdentifiers.remove(asset.localIdentifier)
+                }
             }
             
-            self.regenerateAssetIdentifiers()
-            
             printElapsed("get all photos data (update)")
             
             self.isPhotoLibraryAuthorized = PHPhotoLibrary.authorizationStatus() == .authorized
@@ -129,6 +163,10 @@ class PhotosLibrary: NSObject, ObservableObject, PHPhotoLibraryChangeObserver {
                 self.needsActionAssets = PHAsset.fetchAssets(in: self.needsAction, options: nil)
                 
                 self.regenerateAssetIdentifiers()
+                
+                self.approvedAssetIdentifiers = getSetOfIdentifiers(fetchResult: self.approvedAssets)
+                self.rejectedAssetIdentifiers = getSetOfIdentifiers(fetchResult: self.rejectedAssets)
+                self.needsActionAssetIdentifiers = getSetOfIdentifiers(fetchResult: self.needsActionAssets)
             }
             
             printElapsed("get all photos data (new)")
@@ -267,28 +305,17 @@ class PhotosLibrary: NSObject, ObservableObject, PHPhotoLibraryChangeObserver {
             }
         }
         
-        var approvedAssetIdentifiers: Set<String> = Set()
-        
-        self.approvedAssets.enumerateObjects { asset, _, _ in
-            approvedAssetIdentifiers.insert(asset.localIdentifier)
-        }
-        
-        var rejectedAssetIdentifiers: Set<String> = Set()
-        
-        self.rejectedAssets.enumerateObjects { asset, _, _ in
-            rejectedAssetIdentifiers.insert(asset.localIdentifier)
-        }
-        
-        var needsActionAssetIdentifiers: Set<String> = Set()
-        
-        self.needsActionAssets.enumerateObjects { asset, _, _ in
-            needsActionAssetIdentifiers.insert(asset.localIdentifier)
-        }
-        
         self.assetIdentifiers = assetIdentifiers
         self.favoriteAssetIdentifiers = favoriteAssetIdentifiers
-        self.approvedAssetIdentifiers = approvedAssetIdentifiers
-        self.rejectedAssetIdentifiers = rejectedAssetIdentifiers
-        self.needsActionAssetIdentifiers = needsActionAssetIdentifiers
     }
 }
+
+func getSetOfIdentifiers(fetchResult: PHFetchResult<PHAsset>) -> Set<String> {
+    var result: Set<String> = Set()
+    
+    fetchResult.enumerateObjects { asset, _, _ in
+        result.insert(asset.localIdentifier)
+    }
+    
+    return result
+}