Skip to main content

Fix the problem with albums not updating

ID
66d65fe
date
2023-06-09 23:49:26+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
1d997c8
message
Fix the problem with albums not updating
changed files
2 files, 39 additions, 25 deletions

Changed files

BlinkReviewer/BlinkReviewer/Views/FocusedImage/AlbumInfoOverlay.swift (1833) → BlinkReviewer/BlinkReviewer/Views/FocusedImage/AlbumInfoOverlay.swift (2213)

diff --git a/BlinkReviewer/BlinkReviewer/Views/FocusedImage/AlbumInfoOverlay.swift b/BlinkReviewer/BlinkReviewer/Views/FocusedImage/AlbumInfoOverlay.swift
index 885f4f2..1214259 100644
--- a/BlinkReviewer/BlinkReviewer/Views/FocusedImage/AlbumInfoOverlay.swift
+++ b/BlinkReviewer/BlinkReviewer/Views/FocusedImage/AlbumInfoOverlay.swift
@@ -8,35 +8,42 @@ import Photos
 ///     [Cats] [Cross-stitch] [Stuff I did in 2023]
 ///
 struct AlbumInfoOverlay: ViewModifier {
-    @State var asset: PHAsset?
+    var albums: [PHAssetCollection]
+    
+    init(asset: PHAsset?) {
+        // Note: it's important to look up the list of albums here, and not
+        // defer it to the `body()` function.
+        //
+        // When Swift hears about a change to the Photos Library (e.g. adding
+        // a photo to an album), it will recreate this view, but if none of
+        // the data has changed it won't bother re-rendering.  If the album
+        // lookup is inside `body()`, it never gets run because SwiftUI thinks
+        // the data is unchanged.  Putting it here ensures we get fresh data.
+        self.albums = asset?.albums() ?? []
+    }
     
-    // TODO: This doesn't update properly :-/
     func body(content: Content) -> some View {
-        if let thisAsset = asset {
-            content.overlay(alignment: Alignment(horizontal: .center, vertical: .top)) {
-                HStack {
-                    ForEach(thisAsset.albums(), id: \.localIdentifier) { album in
-                        if let title = album.localizedTitle {
-                            // Don't show the names of the meta-albums used to manage
-                            // review state.
-                            if (title != "Approved" && title != "Rejected" && title != "Needs Action") {
-                                
-                                // The icon was chosen to match the one used for albums
-                                // in the sidebar in Photos.
-                                Text("\(Image(systemName: "rectangle.stack")) \(title)")
-                                    .fontWeight(.bold)
-                                    .font(.title2)
-                                    .padding(5)
-                                    .background(.white.opacity(0.9))
-                                    .cornerRadius(7.0)
-                                    .shadow(radius: 2.0)
-                            }
+        content.overlay(alignment: Alignment(horizontal: .center, vertical: .top)) {
+            HStack {
+                ForEach(albums, id: \.localIdentifier) { album in
+                    if let title = album.localizedTitle {
+                        // Don't show the names of the meta-albums used to manage
+                        // review state.
+                        if (title != "Approved" && title != "Rejected" && title != "Needs Action") {
+                            
+                            // The icon was chosen to match the one used for albums
+                            // in the sidebar in Photos.
+                            Text("\(Image(systemName: "rectangle.stack")) \(title)")
+                                .fontWeight(.bold)
+                                .font(.title2)
+                                .padding(5)
+                                .background(.white.opacity(0.9))
+                                .cornerRadius(7.0)
+                                .shadow(radius: 2.0)
                         }
                     }
-                }.padding()
-            }
-        } else {
-            content
+                }
+            }.padding()
         }
     }
 }

BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift (11564) → BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift (11865)

diff --git a/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift b/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift
index 832a67a..58ced0e 100644
--- a/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift
+++ b/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift
@@ -176,6 +176,13 @@ struct PhotoReviewer: View {
                     focusedAssetIndex += 1
                 }
             
+            case let e where e.characters == "c": // "c"
+                let crossStitch = getAlbum(withName: "Cross stitch")
+            
+                try! PHPhotoLibrary.shared().performChangesAndWait {
+                    focusedAsset.toggle(inAlbum: crossStitch)
+                }
+            
             default:
                 logger.info("Received unhandled keyboard event: \(event, privacy: .public)")
                 break