mirror of
https://github.com/yattee/yattee.git
synced 2025-08-05 02:04:07 +00:00
Use CachedAsyncImage if possible
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import CachedAsyncImage
|
||||
import SDWebImageSwiftUI
|
||||
import SwiftUI
|
||||
|
||||
@@ -6,6 +7,28 @@ struct ThumbnailView: View {
|
||||
private let thumbnails = ThumbnailsModel.shared
|
||||
|
||||
var body: some View {
|
||||
viewForThumbnailExtension
|
||||
}
|
||||
|
||||
var thumbnailExtension: String? {
|
||||
guard let url else { return nil }
|
||||
let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
|
||||
return urlComponents?.path.components(separatedBy: ".").last
|
||||
}
|
||||
|
||||
@ViewBuilder var viewForThumbnailExtension: some View {
|
||||
if thumbnailExtension != nil {
|
||||
if thumbnailExtension == "webp" {
|
||||
webImage
|
||||
} else {
|
||||
asyncImageIfAvailable
|
||||
}
|
||||
} else {
|
||||
asyncImageIfAvailable
|
||||
}
|
||||
}
|
||||
|
||||
var webImage: some View {
|
||||
WebImage(url: url)
|
||||
.resizable()
|
||||
.onFailure { _ in
|
||||
@@ -13,8 +36,31 @@ struct ThumbnailView: View {
|
||||
thumbnails.insertUnloadable(url)
|
||||
}
|
||||
}
|
||||
.placeholder {
|
||||
Rectangle().fill(Color("PlaceholderColor"))
|
||||
.placeholder { placeholder }
|
||||
}
|
||||
|
||||
@ViewBuilder var asyncImageIfAvailable: some View {
|
||||
if #available(iOS 15, macOS 12, *) {
|
||||
CachedAsyncImage(url: url, urlCache: BaseCacheModel.imageCache) { phase in
|
||||
switch phase {
|
||||
case let .success(image):
|
||||
image
|
||||
.resizable()
|
||||
case .failure:
|
||||
placeholder.onAppear {
|
||||
guard let url else { return }
|
||||
thumbnails.insertUnloadable(url)
|
||||
}
|
||||
default:
|
||||
placeholder
|
||||
}
|
||||
}
|
||||
} else {
|
||||
webImage
|
||||
}
|
||||
}
|
||||
|
||||
var placeholder: some View {
|
||||
Rectangle().fill(Color("PlaceholderColor"))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user