Skip to content

Add AsyncImage like API for WebImage #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions SDWebImageSwiftUI/Classes/WebImage2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// WebImage2.swift
//
//
// Created by Kyle on 2023/9/10.
//

import SwiftUI

public struct WebImageEventContext {
// TODO, Configuration and Information of image
}

struct LoadingState {
var url: URL?
var phase: WebImagePhase
init() {
url = nil
phase = .empty
}

func load() {}
}

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
public struct WebImage2<Content>: View where Content: View {
var url: URL?
var scale: CGFloat
var transaction: Transaction
var content: (WebImagePhase) -> Content

@State private var loadingState = LoadingState()

public init(url: URL?, scale: CGFloat = 1) where Content == Image {
self.init(url: url, scale: scale) { phase in
phase.image ?? Image("")
}
}

public init<I, P>(url: URL?, scale: CGFloat = 1, @ViewBuilder content: @escaping (Image) -> I, @ViewBuilder placeholder: @escaping () -> P) where Content == _ConditionalContent<I, P>, I: View, P: View {
self.init(url: url, scale: scale) { phase in
if let i = phase.image {
content(i)
} else {
placeholder()
}
}
}

public init(url: URL?, scale: CGFloat = 1, transaction: Transaction = Transaction(), @ViewBuilder content: @escaping (WebImagePhase) -> Content) {
self.url = url
self.scale = scale
self.transaction = transaction
self.content = { phase in
content(phase)
}
}

public var body: some View {
content(loadingState.phase)
.onAppear {
load(url)
}
.onDisappear {}
.onChange(of: url) { load($0) }
}

private func load(_: URL?) {}
}

@available(iOS 13.0, *)
struct LoadingError: Error {}

@available(iOS 13.0, *)
public enum WebImagePhase {
/// No image is loaded.
case empty

/// An image succesfully loaded.
case success(Image)

/// An image failed to load with an error.
case failure(Error)

public var image: Image? {
switch self {
case let .success(image):
image
case .empty, .failure:
nil
}
}

/// The error that occurred when attempting to load an image, if any.
public var error: Error? {
switch self {
case .empty, .success:
nil
case let .failure(error):
error
}
}
}