Skip to main content

Start by scrolling to the end (hacky)

ID
7dd10e9
date
2023-06-08 11:59:18+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
eb04cac
message
Start by scrolling to the end (hacky)
changed files
3 files, 34 additions, 8 deletions

Changed files

BlinkReviewer/BlinkReviewer/ContentView.swift (213) → BlinkReviewer/BlinkReviewer/ContentView.swift (280)

diff --git a/BlinkReviewer/BlinkReviewer/ContentView.swift b/BlinkReviewer/BlinkReviewer/ContentView.swift
index 905ad9c..707e3ca 100644
--- a/BlinkReviewer/BlinkReviewer/ContentView.swift
+++ b/BlinkReviewer/BlinkReviewer/ContentView.swift
@@ -8,7 +8,9 @@
 import SwiftUI
 
 struct ContentView: View {
+    var assets = getAllPhotos()
+    
     var body: some View {
-        PhotoReviewer(assets: getAllPhotos())
+        PhotoReviewer(assets: assets, selectedAssetIndex: assets.count - 1)
     }
 }

BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift (1174) → BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift (1162)

diff --git a/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift b/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift
index 1d92e2b..c2517f2 100644
--- a/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift
+++ b/BlinkReviewer/BlinkReviewer/Views/PhotoReviewer.swift
@@ -10,7 +10,7 @@ import Photos
 
 struct PhotoReviewer: View {
     var assets: [PHAsset]
-    @State private var selectedAssetIndex: Int = 0
+    @State var selectedAssetIndex: Int
     
     var body: some View {
         VStack {

BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift (1550) → BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift (2829)

diff --git a/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift b/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift
index 9df6cc4..fa3d18b 100644
--- a/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift
+++ b/BlinkReviewer/BlinkReviewer/Views/ThumbnailList.swift
@@ -12,6 +12,14 @@ struct ThumbnailList: View {
     var assets: [PHAsset]
     @Binding var selectedAssetIndex: Int
     
+    func displaySelectedAssetIndex() -> Int {
+        assets.count - 1 - selectedAssetIndex
+    }
+    
+    func displayAssets() -> [PHAsset] {
+        assets.reversed()
+    }
+    
     var body: some View {
         ScrollViewReader { proxy in
             ScrollView(.horizontal) {
@@ -22,21 +30,37 @@ struct ThumbnailList: View {
                     // 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(assets.enumerated()), id: \.element.localIdentifier) { index, asset in
+                    ForEach(Array(displayAssets().enumerated()), id: \.element.localIdentifier) { index, asset in
                         ThumbnailImage(
                             thumbnail: asset.getThumbnail(),
-                            isSelected: assets[selectedAssetIndex].localIdentifier == asset.localIdentifier
+                            isSelected: displayAssets()[displaySelectedAssetIndex()].localIdentifier == asset.localIdentifier
                         ).onTapGesture {
-                            selectedAssetIndex = index
+                            selectedAssetIndex = assets.count - 1 - index
                         }
                     }
-                }.padding()
-            }.frame(height: 70)
+                    // 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: 70)
+                .flipsForRightToLeftLayoutDirection(true)
+                .environment(\.layoutDirection, .rightToLeft)
                 .onChange(of: selectedAssetIndex, perform: { newIndex in
                     withAnimation {
-                        proxy.scrollTo(assets[newIndex].localIdentifier, anchor: .center)
+                        proxy.scrollTo(displayAssets()[displaySelectedAssetIndex()].localIdentifier, anchor: .center)
                     }
                 })
+                .onAppear {
+                    proxy.scrollTo(displayAssets()[displaySelectedAssetIndex()].localIdentifier, anchor: .center)
+                }
         }
     }
 }