Tusker/Tusker/Views/CustomEmojiImageView.swift

58 lines
1.3 KiB
Swift

//
// CustomEmojiImageView.swift
// Tusker
//
// Created by Shadowfacts on 10/11/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import SwiftUI
import Pachyderm
import WebURLFoundationExtras
struct CustomEmojiImageView: View {
let emoji: Emoji
@State private var request: ImageCache.Request?
@State private var image: UIImage?
var body: some View {
imageView
.onAppear(perform: self.loadImage)
.onDisappear(perform: self.cancelRequest)
}
@ViewBuilder
private var imageView: some View {
if let image = image {
Image(uiImage: image)
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
} else {
Image(systemName: "smiley.fill")
}
}
@MainActor
private func loadImage() {
request = ImageCache.emojis.get(URL(emoji.url)!) { (_, image) in
DispatchQueue.main.async {
self.request = nil
if let image = image {
self.image = image
}
}
}
}
private func cancelRequest() {
request?.cancel()
}
}
//struct CustomEmojiImageView_Previews: PreviewProvider {
// static var previews: some View {
// CustomEmojiImageView()
// }
//}