commit 41f692a0c49b745d977e7ee7f27d33e6df6c7412
parent e0d4841147da89370fce90313944fec7155075a8
Author: William Casarin <jb55@jb55.com>
Date: Mon, 3 Apr 2023 15:07:32 -0700
Cache fit/fill as well as height
Diffstat:
2 files changed, 16 insertions(+), 18 deletions(-)
diff --git a/damus/Components/ImageCarousel.swift b/damus/Components/ImageCarousel.swift
@@ -50,19 +50,21 @@ struct ImageCarousel: View {
@State private var open_sheet: Bool = false
@State private var current_url: URL? = nil
- @State private var height: CGFloat? = nil
- @State private var filling: Bool = false
+ @State private var image_fill: ImageFill? = nil
init(previews: PreviewCache, evid: String, urls: [URL]) {
_open_sheet = State(initialValue: false)
_current_url = State(initialValue: nil)
- _height = State(initialValue: previews.lookup_image_height(evid))
- _filling = State(initialValue: false)
+ _image_fill = State(initialValue: previews.lookup_image_meta(evid))
self.urls = urls
self.evid = evid
self.previews = previews
}
+ var filling: Bool {
+ image_fill?.filling == true
+ }
+
var body: some View {
TabView {
ForEach(urls, id: \.absoluteString) { url in
@@ -77,7 +79,7 @@ struct ImageCarousel: View {
view.framePreloadCount = 3
}
.imageModifier({ img in
- guard self.height == nil else {
+ guard self.image_fill == nil else {
return
}
let img_size = img.size
@@ -86,12 +88,8 @@ struct ImageCarousel: View {
DispatchQueue.main.async {
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.filling = filling
- }
-
- self.previews.cache_image_height(evid: evid, height: fill.height)
- self.height = fill.height
+ self.previews.cache_image_meta(evid: evid, image_fill: fill)
+ self.image_fill = fill
}
})
.aspectRatio(contentMode: filling ? .fill : .fit)
@@ -106,7 +104,7 @@ struct ImageCarousel: View {
.fullScreenCover(isPresented: $open_sheet) {
ImageView(urls: urls)
}
- .frame(height: height ?? 0)
+ .frame(height: image_fill?.height ?? 0)
.onTapGesture {
open_sheet = true
}
diff --git a/damus/Util/PreviewCache.swift b/damus/Util/PreviewCache.swift
@@ -25,18 +25,18 @@ enum Preview {
class PreviewCache {
private var previews: [String: Preview]
- private var image_heights: [String: CGFloat]
+ private var image_meta: [String: ImageFill]
func lookup(_ evid: String) -> Preview? {
return previews[evid]
}
- func lookup_image_height(_ evid: String) -> CGFloat? {
- return image_heights[evid]
+ func lookup_image_meta(_ evid: String) -> ImageFill? {
+ return image_meta[evid]
}
- func cache_image_height(evid: String, height: CGFloat) {
- self.image_heights[evid] = height
+ func cache_image_meta(evid: String, image_fill: ImageFill) {
+ self.image_meta[evid] = image_fill
}
func store(evid: String, preview: LPLinkMetadata?) {
@@ -50,6 +50,6 @@ class PreviewCache {
init() {
self.previews = [:]
- self.image_heights = [:]
+ self.image_meta = [:]
}
}