Skip to main content

Add some syntactic sugar for looking up state by index

ID
f6262e5
date
2023-06-11 19:58:20+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
bea0037
message
Add some syntactic sugar for looking up state by index
changed files
2 files, 20 additions, 2 deletions

Changed files

BlinkReviewer/Blink/Photos/PhotosLibrary.swift (7073) → BlinkReviewer/Blink/Photos/PhotosLibrary.swift (7877)

diff --git a/BlinkReviewer/Blink/Photos/PhotosLibrary.swift b/BlinkReviewer/Blink/Photos/PhotosLibrary.swift
index 249d841..9a8d844 100644
--- a/BlinkReviewer/Blink/Photos/PhotosLibrary.swift
+++ b/BlinkReviewer/Blink/Photos/PhotosLibrary.swift
@@ -129,6 +129,20 @@ class PhotosLibrary: NSObject, ObservableObject, PHPhotoLibraryChangeObserver {
         assets.object(at: index)
     }
     
+    /// Get the review state of a given asset.
+    ///
+    /// These methods are called repeatedly on every view (when we get the
+    /// state of thumbnails), so they need to be *fast*.
+    ///
+    /// This is why we cache the list of rejected/needs action/approved assets --
+    /// to make this method fast and performant.
+    ///
+    /// Note: it's possibly for an asset to be in multiple albums if the user
+    /// fiddles with it, so we show the "most destructive" state first -- the
+    /// state that might cause data loss if the user deletes all their rejected
+    /// images.  If they toggle the state in the app, we'll fix it.
+    ///
+    /// TODO: Log a warning here? Resolve somehow?
     func state(of asset: PHAsset) -> ReviewState? {
         if self.rejectedAssets.contains(asset) {
             return .Rejected
@@ -145,6 +159,10 @@ class PhotosLibrary: NSObject, ObservableObject, PHPhotoLibraryChangeObserver {
         return nil
     }
     
+    func state(ofAssetAtIndex index: Int) -> ReviewState? {
+        state(of: asset(at: index))
+    }
+    
     // Implements a basic cache for thumbnail images.
     //
     // Thumbnail images are small and easily reused; I've put them here because

BlinkReviewer/Blink/Views/PhotoReviewer.swift (14971) → BlinkReviewer/Blink/Views/PhotoReviewer.swift (14929)

diff --git a/BlinkReviewer/Blink/Views/PhotoReviewer.swift b/BlinkReviewer/Blink/Views/PhotoReviewer.swift
index 04b49dd..fb5670c 100644
--- a/BlinkReviewer/Blink/Views/PhotoReviewer.swift
+++ b/BlinkReviewer/Blink/Views/PhotoReviewer.swift
@@ -319,7 +319,7 @@ struct PhotoReviewer: View {
             case let e where e.characters == "u":
                 if photosLibrary.state(of: focusedAsset) != nil {
                     if let lastUnreviewed = (focusedAssetIndex..<photosLibrary.assets.count).first(where: { index in
-                        photosLibrary.state(of: photosLibrary.assets.object(at: index)) == nil
+                        photosLibrary.state(ofAssetAtIndex: index) == nil
                     }) {
                         focusedAssetIndex = lastUnreviewed
                     }
@@ -330,7 +330,7 @@ struct PhotoReviewer: View {
                 while true {
                     let randomIndex = (0..<photosLibrary.assets.count).randomElement()!
                     
-                    if photosLibrary.state(of: photosLibrary.assets.object(at: randomIndex)) == nil {
+                    if photosLibrary.state(ofAssetAtIndex: randomIndex) == nil {
                         focusedAssetIndex = randomIndex
                         break
                     }