I would like to speed up this simple code that gets slow when I hit the "Cancella/Seleziona" button in the navigation bar, because it redraws the grid even though ThumbnailView(asset: asset, size: 150) is still the same.
import SwiftUI
import Photos
struct ContentView: View {
@State private var theId = 0
@State private var showCancelOrSelected = true
@ObservedObject var photos = PhotosModel()
private var adaptiveLayout = [GridItem(.adaptive(minimum: 150))]
var body: some View {
NavigationView{
ScrollView(.vertical, showsIndicators: false, content: {
LazyVGrid(columns: adaptiveLayout, spacing: 5) {
ForEach(photos.allAssets) { asset in
if showCancelOrSelected {
NavigationLink(destination: DetailImageView(asset: asset)) {
ThumbnailView(asset: asset, size: 150)
.equatable()
}
} else {
ThumbnailView(asset: asset, size: 150)
.equatable()
}
}
.id(theId)
}
})
.navigationBarTitle("ULTIME \(photos.allAssets.count) FOTO", displayMode: .inline)
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailing) {
Button(showCancelOrSelected ? "Cancella" : "Seleziona") {
showCancelOrSelected.toggle()
theId += 1
print("showCancelOrSelected: \(showCancelOrSelected)")
}
}
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
I tried making ThumbnailView compatible with Equatable protocol like this and adding .equatable (), but to no avail.
struct ThumbnailView: View, Equatable {
let asset:PHAsset
let size:Int
var body: some View {
Image(uiImage: imageFromAsset(asset: asset, size: size))
.resizable()
.frame(width: CGFloat(size), height: CGFloat(size))
}
static func == (lhs: ThumbnailView, rhs: ThumbnailView) -> Bool {
return lhs.size == rhs.size && lhs.asset == rhs.asset
}
}
Can anyone tell me how to prevent the grid from being redesigned?
Aucun commentaire:
Enregistrer un commentaire