From 1e9db91af8f3120fe16ddb279dcfcf55e49e7395 Mon Sep 17 00:00:00 2001 From: Alex Ehlke Date: Fri, 24 Jun 2022 13:24:45 -0400 Subject: [PATCH 1/6] add macOS --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 51a6fa7..4166b83 100644 --- a/Package.swift +++ b/Package.swift @@ -4,7 +4,7 @@ import PackageDescription let package = Package( name: "AsyncView", - platforms: [.iOS(.v15)], + platforms: [.macOS(.v10_15), .iOS(.v15)], products: [ .library( name: "AsyncView", From 7b63c82949562572cb4b25438e97ef50900211c0 Mon Sep 17 00:00:00 2001 From: Alex Ehlke Date: Fri, 24 Jun 2022 13:25:58 -0400 Subject: [PATCH 2/6] macOS 11 --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 4166b83..10a1a73 100644 --- a/Package.swift +++ b/Package.swift @@ -4,7 +4,7 @@ import PackageDescription let package = Package( name: "AsyncView", - platforms: [.macOS(.v10_15), .iOS(.v15)], + platforms: [.macOS(.v11), .iOS(.v15)], products: [ .library( name: "AsyncView", From ec59ecfe90e8181fdbba85a481bef899bf6eb1bc Mon Sep 17 00:00:00 2001 From: Alex Ehlke Date: Sun, 26 Jun 2022 18:39:29 -0400 Subject: [PATCH 3/6] Update Package.swift --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 10a1a73..a7d0f4b 100644 --- a/Package.swift +++ b/Package.swift @@ -4,7 +4,7 @@ import PackageDescription let package = Package( name: "AsyncView", - platforms: [.macOS(.v11), .iOS(.v15)], + platforms: [.macOS(.v12), .iOS(.v15)], products: [ .library( name: "AsyncView", From 0ad21f637c57fbc3ad076424a57dccaf95a18e67 Mon Sep 17 00:00:00 2001 From: Alex Ehlke Date: Tue, 28 Jun 2022 18:47:30 -0400 Subject: [PATCH 4/6] Update ErrorView.swift --- Sources/AsyncView/ErrorView.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sources/AsyncView/ErrorView.swift b/Sources/AsyncView/ErrorView.swift index 996b3bb..b283940 100644 --- a/Sources/AsyncView/ErrorView.swift +++ b/Sources/AsyncView/ErrorView.swift @@ -3,10 +3,19 @@ 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 + } + .buttonStyle(.link) + .popover(isPresented: $showingPopover) { + Text(String(NSString(string: "\(error)"))) + .font(.caption) + .padding() + } if let reloadAction = reloadAction { Button( action: reloadAction, From aceed35b6f5584df4ed9594fbdf83346bb47ead8 Mon Sep 17 00:00:00 2001 From: Alex Ehlke Date: Thu, 3 Nov 2022 19:52:43 -0400 Subject: [PATCH 5/6] fix --- Sources/AsyncView/ErrorView.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/AsyncView/ErrorView.swift b/Sources/AsyncView/ErrorView.swift index b283940..7ea40a7 100644 --- a/Sources/AsyncView/ErrorView.swift +++ b/Sources/AsyncView/ErrorView.swift @@ -10,7 +10,11 @@ struct ErrorView: View { Button(error.localizedDescription) { showingPopover = true } +#if os(iOS) + .buttonStyle(.borderedProminent) +#else .buttonStyle(.link) +#endif .popover(isPresented: $showingPopover) { Text(String(NSString(string: "\(error)"))) .font(.caption) From 12d06ac2005bf14ce9afaf0009b821a828fc8cc2 Mon Sep 17 00:00:00 2001 From: Alex Ehlke Date: Fri, 4 Oct 2024 14:25:54 -0400 Subject: [PATCH 6/6] wip --- Sources/AsyncView/AsyncModel.swift | 14 +++++++------- Sources/AsyncView/AsyncModelView.swift | 10 ++++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Sources/AsyncView/AsyncModel.swift b/Sources/AsyncView/AsyncModel.swift index 35dcc34..83414fd 100644 --- a/Sources/AsyncView/AsyncModel.swift +++ b/Sources/AsyncView/AsyncModel.swift @@ -3,9 +3,9 @@ import SwiftUI open class AsyncModel: ObservableObject { @MainActor @Published public private(set) var result = AsyncResult.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") } @@ -15,17 +15,17 @@ open class AsyncModel: 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) } @@ -35,7 +35,7 @@ open class AsyncModel: ObservableObject { public func loadIfNeeded() async { switch self.result { case .empty, .failure: - await self.load() + await self.load(forceRefreshRequested: false) case .inProgress, .success: break } diff --git a/Sources/AsyncView/AsyncModelView.swift b/Sources/AsyncView/AsyncModelView.swift index 532df5f..c1f55b9 100644 --- a/Sources/AsyncView/AsyncModelView.swift +++ b/Sources/AsyncView/AsyncModelView.swift @@ -12,14 +12,16 @@ public struct AsyncModelView: 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) } } }