Skip to main content

WIP

ID
44bdc1f
date
2023-06-09 18:21:48+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
0d34ad6
message
WIP
changed files
4 files, 71 additions, 68 deletions

Changed files

BlinkReviewer/BlinkReviewer/Photos/PhotosLibrary.swift (3213) → BlinkReviewer/BlinkReviewer/Photos/PhotosLibrary.swift (3493)

diff --git a/BlinkReviewer/BlinkReviewer/Photos/PhotosLibrary.swift b/BlinkReviewer/BlinkReviewer/Photos/PhotosLibrary.swift
index 58498bf..359a26f 100644
--- a/BlinkReviewer/BlinkReviewer/Photos/PhotosLibrary.swift
+++ b/BlinkReviewer/BlinkReviewer/Photos/PhotosLibrary.swift
@@ -59,23 +59,28 @@ class PhotosLibrary: NSObject, ObservableObject, PHPhotoLibraryChangeObserver {
             
             let options = PHFetchOptions()
             options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
-            options.fetchLimit = 150
+            options.fetchLimit = 300
             
             self.assets2 = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: options)
             
+            print("self.approvedAssets = \(self.approvedAssets.count)")
             self.approvedAssets = PHAsset.fetchAssets(in: self.approved, options: nil)
+            print("self.approvedAssets = \(self.approvedAssets.count)")
             self.rejectedAssets = PHAsset.fetchAssets(in: self.rejected, options: nil)
             self.needsActionAssets = PHAsset.fetchAssets(in: self.needsAction, options: nil)
             
             self.isPhotoLibraryAuthorized = PHPhotoLibrary.authorizationStatus() == .authorized
             
             print("self.isPhotoLibraryAuthorized = \(self.isPhotoLibraryAuthorized)")
+            print("self.assets2.count = \(self.assets2.count)")
             
             printElapsed("get photos library data")
         }
     }
     
     func state(for asset: PHAsset) -> ReviewState? {
+        print("evaluating state for \(asset.localIdentifier)")
+        
         if self.rejectedAssets.contains(asset) {
             return .Rejected
         }

BlinkReviewer/BlinkReviewer/Views/PHAssetImage.swift (4533) → BlinkReviewer/BlinkReviewer/Views/PHAssetImage.swift (4535)

diff --git a/BlinkReviewer/BlinkReviewer/Views/PHAssetImage.swift b/BlinkReviewer/BlinkReviewer/Views/PHAssetImage.swift
index 1bff24e..b207fc9 100644
--- a/BlinkReviewer/BlinkReviewer/Views/PHAssetImage.swift
+++ b/BlinkReviewer/BlinkReviewer/Views/PHAssetImage.swift
@@ -68,7 +68,7 @@ class PHAssetImage: NSObject, ObservableObject {
                 return
             }
 
-            print("regenerating image for \(thisAsset.localIdentifier)")
+//            print("regenerating image for \(thisAsset.localIdentifier)")
 
             
             // This implementation is based on code in a Stack Overflow answer

BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift (5950) → BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift (6230)

diff --git a/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift b/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift
index ce1ecae..e29a7e3 100644
--- a/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift
+++ b/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift
@@ -20,27 +20,32 @@ struct PhotoReviewer: View {
         if photosLibrary.isPhotoLibraryAuthorized {
             ZStack {
                 VStack {
-                    ThumbnailList(selectedAssetIndex: $selectedAssetIndex)
+                    let binding = Binding {
+                        selectedAssetIndex == -1 ? photosLibrary.assets2.count - 1 : selectedAssetIndex
+                    } set: {
+                        self.selectedAssetIndex = $0
+                    }
+                    
+                    ThumbnailList(selectedAssetIndex: binding)
                         .environmentObject(photosLibrary)
                         .background(.gray.opacity(0.3))
                     
-//                    FullSizeImage(image: fullSizeImage)
-//                        .background(.black)
+                    FullSizeImage(image: fullSizeImage)
+                        .background(.black)
                 }
+                .background(.black)
                 .onAppear {
                     selectedAssetIndex = photosLibrary.assets2.count - 1
-                }
-                .background(.black)
-//                .onAppear {
-//                    fullSizeImage.asset = photosLibrary.assets2.object(at: selectedAssetIndex)
-//
-//                    NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event in
-//                        handleKeyEvent(event)
-//                        return event
-//                    }
-//                }.onChange(of: selectedAssetIndex, perform: { newIndex in
-//                    fullSizeImage.asset = photosLibrary.assets2.object(at: newIndex)
-//                })
+                    
+                    fullSizeImage.asset = photosLibrary.assets2.object(at: photosLibrary.assets2.count - 1 - selectedAssetIndex)
+
+                    NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event in
+                        handleKeyEvent(event)
+                        return event
+                    }
+                }.onChange(of: selectedAssetIndex, perform: { newIndex in
+                    fullSizeImage.asset = photosLibrary.assets2.object(at: photosLibrary.assets2.count - 1 - newIndex)
+                })
                 
                 if showStatistics {
                     HStack {

BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift (3684) → BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift (3670)

diff --git a/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift b/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift
index 41763d5..90f9378 100644
--- a/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift
+++ b/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift
@@ -28,64 +28,57 @@ struct ThumbnailList: View {
 
     @Binding var selectedAssetIndex: Int
     
-//    func displaySelectedAssetIndex() -> Int {
-//        photosLibrary.assets2.count - 1 - selectedAssetIndex
-//    }
-//
-//    func displayAssets() -> [PHAsset] {
-//        photosLibrary.assets2.reversed()
-//    }
-    
     private var assets: PHFetchResultCollection {
-        print(PHFetchResultCollection(fetchResult: photosLibrary.assets2).indices)
         return PHFetchResultCollection(fetchResult: photosLibrary.assets2)
     }
     
     var body: some View {
-        ScrollViewReader { proxy in
-            ScrollView(.horizontal) {
-                LazyHStack(spacing: 5) {
-                    // TODO: placeholder images for start/end
-                    
-                    // Implementation note: we use the localIdentifier rather than the
-                    // array index as the id here, because the app gets way slower if
-                    // you use the array index -- it tries to regenerate a bunch of
-                    // the thumbnails every time you change position.
-                    ForEach(Array(zip(assets.indices, assets)), id: \.1.localIdentifier) { index, asset in
-                        //                Text("asset \(index)")
-                        ThumbnailImage(
-                            thumbnail: PHAssetImage(asset, size: CGSize(width: 70, height: 70), deliveryMode: .opportunistic),
-                            state: photosLibrary.state(for: asset),
-                            isFavorite: asset.isFavorite,
-                            isSelected: photosLibrary.assets2.count - 1 - index == selectedAssetIndex
-                        ).onTapGesture {
-                            selectedAssetIndex = photosLibrary.assets2.count - 1 - index
+        VStack {
+            ScrollViewReader { proxy in
+                ScrollView(.horizontal) {
+                    LazyHStack(spacing: 5) {
+                        // TODO: placeholder images for start/end
+                        
+                        // Implementation note: we use the localIdentifier rather than the
+                        // array index as the id here, because the app gets way slower if
+                        // you use the array index -- it tries to regenerate a bunch of
+                        // the thumbnails every time you change position.
+                        
+                        // wassup with reversing???? lazy loading innit
+                        ForEach(Array(zip(assets.indices, assets)), id: \.1.localIdentifier) { index, asset in
+                            //                Text("asset \(index)")
+                            ThumbnailImage(
+                                thumbnail: PHAssetImage(asset, size: CGSize(width: 70, height: 70), deliveryMode: .opportunistic),
+                                state: photosLibrary.state(for: asset),
+                                isFavorite: asset.isFavorite,
+                                isSelected: photosLibrary.assets2.count - 1 - index == selectedAssetIndex
+                            ).onTapGesture {
+                                selectedAssetIndex = photosLibrary.assets2.count - 1 - index
+                            }
                         }
-                    }
-                    
-                    // Note: these two uses of RTL direction are a way to get the LazyHStack
-                    // to start on the right-hand side (i.e. the newest image) without loading
-                    // everything else in the view.
-                    //
-                    // I suspect this may get easier with the new scrollPosition API, coming
-                    // in the 2023 OS releases.  TODO: Investigate this new API when available.
-                    //
-                    // See https://developer.apple.com/documentation/swiftui/view/scrollposition(initialanchor:)
-                        .flipsForRightToLeftLayoutDirection(true)
-                        .environment(\.layoutDirection, .rightToLeft)
-                    }.padding()
-            }
-                .frame(height: 90)
-                .flipsForRightToLeftLayoutDirection(true)
-                .environment(\.layoutDirection, .rightToLeft)
-                .onChange(of: selectedAssetIndex, perform: { newIndex in
-                    withAnimation {
-                        proxy.scrollTo(selectedAssetIndex, anchor: .center)
-                    }
-                })
-                .onAppear {
-                    proxy.scrollTo(selectedAssetIndex, anchor: .center)
+                        
+                        // Note: these two uses of RTL direction are a way to get the LazyHStack
+                        // to start on the right-hand side (i.e. the newest image) without loading
+                        // everything else in the view.
+                        //
+                        // I suspect this may get easier with the new scrollPosition API, coming
+                        // in the 2023 OS releases.  TODO: Investigate this new API when available.
+                        //
+                        // See https://developer.apple.com/documentation/swiftui/view/scrollposition(initialanchor:)
+                            .flipsForRightToLeftLayoutDirection(true)
+                            .environment(\.layoutDirection, .rightToLeft)
+                        }.padding()
                 }
+                    .frame(height: 90)
+                    .flipsForRightToLeftLayoutDirection(true)
+                    .environment(\.layoutDirection, .rightToLeft)
+                    .onChange(of: selectedAssetIndex, perform: { newIndex in
+                        // fires on change + initial run
+                        withAnimation {
+                            proxy.scrollTo(assets[photosLibrary.assets2.count - 1 - selectedAssetIndex].localIdentifier, anchor: .center)
+                        }
+                    })
+            }
         }
     }
 }