From 5ef57dc37924ee62644f72252e5e48023f084e5b Mon Sep 17 00:00:00 2001 From: Ethan Pippin Date: Mon, 17 Jan 2022 17:41:19 -0700 Subject: [PATCH] better fallback image implementation --- Shared/Views/ImageView.swift | 75 ++++++++---------------------------- 1 file changed, 15 insertions(+), 60 deletions(-) diff --git a/Shared/Views/ImageView.swift b/Shared/Views/ImageView.swift index 27ef4a24..9dad2701 100644 --- a/Shared/Views/ImageView.swift +++ b/Shared/Views/ImageView.swift @@ -11,7 +11,10 @@ import SwiftUI struct ImageView: View { - private let sources: [URL] + @State + private var sources: [URL] + private var currentURL: URL? { sources.first } + private let blurhash: String private let failureInitials: String @@ -65,68 +68,20 @@ struct ImageView: View { } var body: some View { - ImageViewBackgroundA(index: 0, - sources: sources, - placeholderView: placeholderView, - failureView: failureImage) - } -} - -// Two image view are necessary to switch between one another to appease the type system -// as a recursive view with itself isn't valid - -fileprivate struct ImageViewBackgroundA: View { - - let index: Int - let sources: [URL] - let placeholderView: () -> PlaceholderView - let failureView: () -> FailureView - - var body: some View { - LazyImage(source: sources[index]) { state in - if let image = state.image { - image - } else if state.error != nil { - if index + 1 == sources.count { - failureView() + if let u = currentURL { + LazyImage(source: u) { state in + if let image = state.image { + image + } else if state.error != nil { + failureImage().onAppear { sources.removeFirst() } } else { - ImageViewBackgroundB(index: index + 1, - sources: sources, - placeholderView: placeholderView, - failureView: failureView) + placeholderView() } - } else { - placeholderView() } + .pipeline(ImagePipeline(configuration: .withDataCache)) + .id(u) + } else { + failureImage() } - .pipeline(ImagePipeline(configuration: .withDataCache)) - } -} - -fileprivate struct ImageViewBackgroundB: View { - - let index: Int - let sources: [URL] - let placeholderView: () -> PlaceholderView - let failureView: () -> FailureView - - var body: some View { - LazyImage(source: sources[index]) { state in - if let image = state.image { - image - } else if state.error != nil { - if index + 1 == sources.count { - failureView() - } else { - ImageViewBackgroundA(index: index + 1, - sources: sources, - placeholderView: placeholderView, - failureView: failureView) - } - } else { - placeholderView() - } - } - .pipeline(ImagePipeline(configuration: .withDataCache)) } }