Skip to main content

reduce diff churn

ID
97888b1
date
2023-06-09 17:55:32+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
b299180
message
reduce diff churn
changed files
2 files, 16 additions, 13 deletions

Changed files

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

diff --git a/BlinkReviewer/BlinkReviewer/Photos/PhotosLibrary.swift b/BlinkReviewer/BlinkReviewer/Photos/PhotosLibrary.swift
index f09be91..58498bf 100644
--- a/BlinkReviewer/BlinkReviewer/Photos/PhotosLibrary.swift
+++ b/BlinkReviewer/BlinkReviewer/Photos/PhotosLibrary.swift
@@ -58,7 +58,7 @@ class PhotosLibrary: NSObject, ObservableObject, PHPhotoLibraryChangeObserver {
 //            self.assets = getAllPhotos()
             
             let options = PHFetchOptions()
-            options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
+            options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
             options.fetchLimit = 150
             
             self.assets2 = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: options)

BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift (3398) → BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift (3636)

diff --git a/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift b/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift
index ae76209..dbf7e40 100644
--- a/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift
+++ b/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift
@@ -19,7 +19,7 @@ struct PHFetchResultCollection: RandomAccessCollection, Equatable {
     var endIndex: Int { fetchResult.count }
 
     subscript(position: Int) -> PHAsset {
-        fetchResult.object(at: fetchResult.count - position - 1)
+        fetchResult.object(at: position)
     }
 }
 
@@ -47,18 +47,21 @@ struct ThumbnailList: View {
                 LazyHStack(spacing: 5) {
                     // TODO: placeholder images for start/end
                     
-                    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
-                            }
+                    // 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
                         }
+                    }
                     
                     // 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