Created
December 13, 2020 14:37
-
-
Save nodahikaru/f060b59320b339b23ff69b434c4cf34f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RemoteImage.swift | |
// List | |
// | |
// Created by noda on 12/13/20. | |
// | |
import SwiftUI | |
struct RemoteImage: View { | |
private enum LoadState { | |
case loading, success, failure | |
} | |
private class Loader: ObservableObject { | |
var data = Data() | |
var state = LoadState.loading | |
init(url: String) { | |
guard let parsedURL = URL(string: url) else { | |
fatalError("Invalid URL: \(url)") | |
} | |
URLSession.shared.dataTask(with: parsedURL) { data, response, error in | |
if let data = data, data.count > 0 { | |
self.data = data | |
self.state = .success | |
} else { | |
self.state = .failure | |
} | |
DispatchQueue.main.async { | |
self.objectWillChange.send() | |
} | |
}.resume() | |
} | |
} | |
@StateObject private var loader: Loader | |
var loading: Image | |
var failure: Image | |
var body: some View { | |
selectImage() | |
.resizable() | |
} | |
init(url: String, loading: Image = Image(systemName: "photo"), failure: Image = Image(systemName: "multiply.circle")) { | |
_loader = StateObject(wrappedValue: Loader(url: url)) | |
self.loading = loading | |
self.failure = failure | |
} | |
private func selectImage() -> Image { | |
switch loader.state { | |
case .loading: | |
return loading | |
case .failure: | |
return failure | |
default: | |
if let image = UIImage(data: loader.data) { | |
return Image(uiImage: image) | |
} else { | |
return failure | |
} | |
} | |
} | |
} | |
struct RemoteImage_Previews: PreviewProvider { | |
static var previews: some View { | |
RemoteImage(url: "https://cf.geekdo-images.com/thumb/img/sD_qvrzIbvfobJj0ZDAaq-TnQPs=/fit-in/200x150/pic2649952.jpg") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment