Cache fit/fill as well as height

This commit is contained in:
William Casarin
2023-04-03 15:07:32 -07:00
parent e0d4841147
commit 41f692a0c4
2 changed files with 16 additions and 18 deletions

View File

@@ -50,19 +50,21 @@ struct ImageCarousel: View {
@State private var open_sheet: Bool = false @State private var open_sheet: Bool = false
@State private var current_url: URL? = nil @State private var current_url: URL? = nil
@State private var height: CGFloat? = nil @State private var image_fill: ImageFill? = nil
@State private var filling: Bool = false
init(previews: PreviewCache, evid: String, urls: [URL]) { init(previews: PreviewCache, evid: String, urls: [URL]) {
_open_sheet = State(initialValue: false) _open_sheet = State(initialValue: false)
_current_url = State(initialValue: nil) _current_url = State(initialValue: nil)
_height = State(initialValue: previews.lookup_image_height(evid)) _image_fill = State(initialValue: previews.lookup_image_meta(evid))
_filling = State(initialValue: false)
self.urls = urls self.urls = urls
self.evid = evid self.evid = evid
self.previews = previews self.previews = previews
} }
var filling: Bool {
image_fill?.filling == true
}
var body: some View { var body: some View {
TabView { TabView {
ForEach(urls, id: \.absoluteString) { url in ForEach(urls, id: \.absoluteString) { url in
@@ -77,7 +79,7 @@ struct ImageCarousel: View {
view.framePreloadCount = 3 view.framePreloadCount = 3
} }
.imageModifier({ img in .imageModifier({ img in
guard self.height == nil else { guard self.image_fill == nil else {
return return
} }
let img_size = img.size let img_size = img.size
@@ -86,12 +88,8 @@ struct ImageCarousel: View {
DispatchQueue.main.async { DispatchQueue.main.async {
let fill = calculate_image_fill(geo: geo, img_size: img_size, is_animated: is_animated, maxHeight: maxHeight, minHeight: minHeight) let fill = calculate_image_fill(geo: geo, img_size: img_size, is_animated: is_animated, maxHeight: maxHeight, minHeight: minHeight)
if let filling = fill.filling { self.previews.cache_image_meta(evid: evid, image_fill: fill)
self.filling = filling self.image_fill = fill
}
self.previews.cache_image_height(evid: evid, height: fill.height)
self.height = fill.height
} }
}) })
.aspectRatio(contentMode: filling ? .fill : .fit) .aspectRatio(contentMode: filling ? .fill : .fit)
@@ -106,7 +104,7 @@ struct ImageCarousel: View {
.fullScreenCover(isPresented: $open_sheet) { .fullScreenCover(isPresented: $open_sheet) {
ImageView(urls: urls) ImageView(urls: urls)
} }
.frame(height: height ?? 0) .frame(height: image_fill?.height ?? 0)
.onTapGesture { .onTapGesture {
open_sheet = true open_sheet = true
} }

View File

@@ -25,18 +25,18 @@ enum Preview {
class PreviewCache { class PreviewCache {
private var previews: [String: Preview] private var previews: [String: Preview]
private var image_heights: [String: CGFloat] private var image_meta: [String: ImageFill]
func lookup(_ evid: String) -> Preview? { func lookup(_ evid: String) -> Preview? {
return previews[evid] return previews[evid]
} }
func lookup_image_height(_ evid: String) -> CGFloat? { func lookup_image_meta(_ evid: String) -> ImageFill? {
return image_heights[evid] return image_meta[evid]
} }
func cache_image_height(evid: String, height: CGFloat) { func cache_image_meta(evid: String, image_fill: ImageFill) {
self.image_heights[evid] = height self.image_meta[evid] = image_fill
} }
func store(evid: String, preview: LPLinkMetadata?) { func store(evid: String, preview: LPLinkMetadata?) {
@@ -50,6 +50,6 @@ class PreviewCache {
init() { init() {
self.previews = [:] self.previews = [:]
self.image_heights = [:] self.image_meta = [:]
} }
} }