Skip to main content

Blink/Views/Thumbnails/ThumbnailList.swift

1//
2// NewThumbnailList.swift
3// BlinkReviewer
4//
5// Created by Alex Chan on 10/06/2023.
6//
8import SwiftUI
10struct ThumbnailList: View {
11 @EnvironmentObject var photosLibrary: PhotosLibrary
12 @Binding var focusedAssetIndex: Int
14 var body: some View {
15 ScrollViewReader { proxy in
16 PHAssetHStack(assetIdentifiers: photosLibrary.assetIdentifiers) { localIdentifier, index in
17 ThumbnailImage(
18 index: index,
19 state: photosLibrary.state(ofLocalIdentifier: localIdentifier),
20 isFavorite: photosLibrary.isFavorite(localIdentifier: localIdentifier),
21 isFocused: index == focusedAssetIndex,
22 getAssetImage: {
23 photosLibrary.getThumbnail(for: photosLibrary.asset(at: index))
24 }
25 )
26 .environmentObject(photosLibrary)
27 .onTapGesture {
28 focusedAssetIndex = index
29 }
30 }
31 // When the focusedAssetIndex changes, scroll to the new position.
32 //
33 // By default this is an animated scroll, but if the user is scrolling
34 // a long way, we skip the animation and jump straight to it -- if somebody
35 // jumps over several thousand images in one go, it looks better to snap
36 // rather than do a jaggy animation.
37 .onChange(of: focusedAssetIndex, perform: { [oldIndex = focusedAssetIndex] newIndex in
38 withAnimation(abs(newIndex - oldIndex) < 100 ? .default : nil) {
39 proxy.scrollTo(
40 photosLibrary.asset(at: newIndex).localIdentifier,
41 anchor: .center
42 )
43 }
44 })
45 }
46 }