Skip to content

Instantly share code, notes, and snippets.

View dmytro-anokhin's full-sized avatar
🔨

Dmytro Anokhin dmytro-anokhin

🔨
View GitHub Profile
let options = URLImageOptions(identifier: "user-avatar")
URLImage(url: url,
options: options) { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
}
URLImage(url: url,
empty: {
EmptyView()
},
inProgress: { progress in
Image(systemName: "photo")
},
failure: { error, retry in
Text("Failed")
},
URLImage(url: url) { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
}
//
// URLImageWidget.swift
// URLImageWidget
//
// Created by Dmytro Anokhin on 08/12/2020.
//
import WidgetKit
import SwiftUI
import Intents
extension DecodableRemoteContent where Decoder == JSONDecoder {
convenience init(urlSession: URLSession = .shared, url: URL, type: Value.Type) {
self.init(urlSession: urlSession, url: url, type: type, decoder: JSONDecoder())
}
}
import SwiftUI
import RemoteContentView
struct Post : Codable {
var id: Int
// ...
}
extension RemoteContentView where Empty == EmptyView, Progress == ActivityIndicator, Failure == Text {
init<R: RemoteContent>(remoteContent: R,
content: @escaping (_ value: Value) -> Content) where R.ObjectWillChangePublisher == ObservableObjectPublisher,
R.Value == Value
{
self.init(remoteContent: remoteContent,
empty: { EmptyView() },
progress: { ActivityIndicator() },
failure: { error, _ in Text(error.localizedDescription) },
final class DecodableRemoteContent<Value, Decoder> : RemoteContent where Value : Decodable,
Decoder : TopLevelDecoder,
Decoder.Input == Data
{
unowned let urlSession: URLSession
let url: URL
let type: Value.Type
let decoder: Decoder
init(urlSession: URLSession = .shared, url: URL, type: Value.Type, decoder: Decoder) {
struct RemoteContentView<Value, Empty, Progress, Failure, Content> : View where Empty : View,
Progress : View,
Failure : View,
Content : View
{
let empty: () -> Empty
let progress: () -> Progress
let failure: (_ error: Error, _ retry: @escaping () -> Void) -> Failure
let content: (_ value: Value) -> Content
final class AnyRemoteContent<Value> : RemoteContent {
init<R: RemoteContent>(_ remoteContent: R) where R.ObjectWillChangePublisher == ObjectWillChangePublisher,
R.Value == Value {
objectWillChangeClosure = {
remoteContent.objectWillChange
}
loadingStateClosure = {