Skip to content
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

macOS #1

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PackageDescription

let package = Package(
name: "AsyncView",
platforms: [.iOS(.v15)],
platforms: [.macOS(.v12), .iOS(.v15)],
products: [
.library(
name: "AsyncView",
Expand Down
14 changes: 7 additions & 7 deletions Sources/AsyncView/AsyncModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import SwiftUI
open class AsyncModel<Success>: ObservableObject {
@MainActor @Published public private(set) var result = AsyncResult<Success>.empty

public typealias AsyncOperation = () async throws -> Success
public typealias AsyncOperation = (Bool) async throws -> Success

private var asyncOperationBlock: AsyncOperation = {
private var asyncOperationBlock: AsyncOperation = { _ in
fatalError("Override asyncOperation or pass a asyncOperationBlock to use async model")
}

Expand All @@ -15,17 +15,17 @@ open class AsyncModel<Success>: ObservableObject {
}
}

open func asyncOperation() async throws -> Success {
try await self.asyncOperationBlock()
open func asyncOperation(forceRefreshRequested: Bool) async throws -> Success {
try await self.asyncOperationBlock(forceRefreshRequested)
}

@MainActor
public func load() async {
public func load(forceRefreshRequested: Bool) async {
if case .inProgress = self.result { return }
self.result = .inProgress

do {
self.result = .success(try await self.asyncOperation())
self.result = .success(try await self.asyncOperation(forceRefreshRequested: forceRefreshRequested))
} catch {
self.result = .failure(error)
}
Expand All @@ -35,7 +35,7 @@ open class AsyncModel<Success>: ObservableObject {
public func loadIfNeeded() async {
switch self.result {
case .empty, .failure:
await self.load()
await self.load(forceRefreshRequested: false)
case .inProgress, .success:
break
}
Expand Down
10 changes: 6 additions & 4 deletions Sources/AsyncView/AsyncModelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ public struct AsyncModelView<Success, Content: View>: View {
public var body: some View {
AsyncResultView(
result: model.result,
reloadAction: { Task { await model.load() } },
reloadAction: { Task { await model.load(forceRefreshRequested: true) } },
content: content
)
.task {
await model.loadIfNeeded()
.onAppear {
Task {
await model.loadIfNeeded()
}
}
.refreshable {
await model.load()
await model.load(forceRefreshRequested: true)
}
}
}
15 changes: 14 additions & 1 deletion Sources/AsyncView/ErrorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@ import SwiftUI
struct ErrorView: View {
let error: Error
let reloadAction: (() -> Void)?
@State private var showingPopover = false

var body: some View {
VStack(spacing: 10) {
Text(error.localizedDescription)
Button(error.localizedDescription) {
showingPopover = true
}
#if os(iOS)
.buttonStyle(.borderedProminent)
#else
.buttonStyle(.link)
#endif
.popover(isPresented: $showingPopover) {
Text(String(NSString(string: "\(error)")))
.font(.caption)
.padding()
}
if let reloadAction = reloadAction {
Button(
action: reloadAction,
Expand Down