2022-12-17 03:07:30 +05:30
|
|
|
import CachedAsyncImage
|
2022-12-11 05:21:21 +05:30
|
|
|
import SDWebImageSwiftUI
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ThumbnailView: View {
|
|
|
|
var url: URL?
|
2022-12-13 17:43:58 +05:30
|
|
|
private let thumbnails = ThumbnailsModel.shared
|
2022-12-11 05:21:21 +05:30
|
|
|
|
|
|
|
var body: some View {
|
2022-12-17 19:08:21 +05:30
|
|
|
if url != nil {
|
|
|
|
viewForThumbnailExtension
|
2022-12-22 01:47:02 +05:30
|
|
|
} else {
|
|
|
|
placeholder
|
2022-12-17 19:08:21 +05:30
|
|
|
}
|
2022-12-17 03:07:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var thumbnailExtension: String? {
|
2022-12-17 19:08:21 +05:30
|
|
|
guard let url,
|
|
|
|
let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return nil }
|
|
|
|
|
|
|
|
let pathComponents = urlComponents.path.components(separatedBy: ".")
|
|
|
|
guard pathComponents.count > 1 else { return nil }
|
|
|
|
|
|
|
|
return pathComponents.last
|
2022-12-17 03:07:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder var viewForThumbnailExtension: some View {
|
2022-12-23 00:07:05 +05:30
|
|
|
if AccountsModel.shared.app != .piped, thumbnailExtension != nil {
|
2022-12-17 03:07:30 +05:30
|
|
|
if thumbnailExtension == "webp" {
|
|
|
|
webImage
|
|
|
|
} else {
|
|
|
|
asyncImageIfAvailable
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-17 19:08:21 +05:30
|
|
|
webImage
|
2022-12-17 03:07:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var webImage: some View {
|
2022-12-11 18:30:46 +05:30
|
|
|
WebImage(url: url)
|
|
|
|
.resizable()
|
|
|
|
.onFailure { _ in
|
|
|
|
if let url {
|
|
|
|
thumbnails.insertUnloadable(url)
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
2022-12-11 05:21:21 +05:30
|
|
|
}
|
2022-12-17 03:07:30 +05:30
|
|
|
.placeholder { placeholder }
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder var asyncImageIfAvailable: some View {
|
2023-07-01 22:08:11 +05:30
|
|
|
CachedAsyncImage(url: url, urlCache: BaseCacheModel.imageCache, transaction: Transaction(animation: .default)) { phase in
|
|
|
|
switch phase {
|
|
|
|
case let .success(image):
|
|
|
|
image
|
|
|
|
.resizable()
|
|
|
|
case .failure:
|
|
|
|
placeholder.onAppear {
|
|
|
|
guard let url else { return }
|
|
|
|
thumbnails.insertUnloadable(url)
|
2022-12-17 03:07:30 +05:30
|
|
|
}
|
2023-07-01 22:08:11 +05:30
|
|
|
default:
|
|
|
|
placeholder
|
2022-12-11 17:08:57 +05:30
|
|
|
}
|
2022-12-17 03:07:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var placeholder: some View {
|
|
|
|
Rectangle().fill(Color("PlaceholderColor"))
|
2022-12-11 05:21:21 +05:30
|
|
|
}
|
|
|
|
}
|