generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a generic AsyncButton with support for throwing closures (#8)
Co-authored-by: Paul Schmiedmayer <[email protected]>
- Loading branch information
1 parent
bae5330
commit 10ae4b1
Showing
17 changed files
with
549 additions
and
34 deletions.
There are no files selected for viewing
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
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
31 changes: 31 additions & 0 deletions
31
Sources/SpeziViews/Environment/ProcessingDebounceDuration.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SwiftUI | ||
|
||
|
||
/// An `EnvironmentKey` that provides a generalized configuration for debounce durations for any processing-related operations. | ||
/// | ||
/// This might be helpful to provide extensive customization points without introducing clutter in the initializer of views. | ||
/// The ``AsyncButton`` is one example where this `EnvironmentKey` is used. | ||
public struct ProcessingDebounceDuration: EnvironmentKey { | ||
public static let defaultValue: Duration = .milliseconds(150) | ||
} | ||
|
||
|
||
extension EnvironmentValues { | ||
/// Refer to the documentation of ``ProcessingDebounceDuration``. | ||
public var processingDebounceDuration: Duration { | ||
get { | ||
self[ProcessingDebounceDuration.self] | ||
} | ||
set { | ||
self[ProcessingDebounceDuration.self] = newValue | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SwiftUI | ||
|
||
|
||
private struct ProcessingOverlay<Overlay: View>: ViewModifier { | ||
fileprivate var isProcessing: Bool | ||
@ViewBuilder fileprivate var overlay: () -> Overlay | ||
|
||
|
||
func body(content: Content) -> some View { | ||
content | ||
.opacity(isProcessing ? 0.0 : 1.0) | ||
.overlay { | ||
if isProcessing { | ||
overlay() | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
extension View { | ||
/// Modifies the view to be replaced by an processing indicator based on the supplied condition. | ||
/// - Parameters: | ||
/// - state: The `ViewState` that is used to determine whether the view is replaced by the processing overlay. | ||
/// We consider the view to be processing if the state is ``ViewState/processing``. | ||
/// - overlay: A view which which the modified view is overlayed with when state is processing. | ||
/// - Returns: A view that may render processing state. | ||
public func processingOverlay<Overlay: View>( | ||
isProcessing state: ViewState, | ||
@ViewBuilder overlay: @escaping () -> Overlay = { ProgressView() } | ||
) -> some View { | ||
processingOverlay(isProcessing: state == .processing, overlay: overlay) | ||
} | ||
|
||
/// Modifies the view to be replaced by an processing indicator based on the supplied condition. | ||
/// - Parameters: | ||
/// - processing: A Boolean value that determines whether the view is replaced by the processing overlay. | ||
/// - overlay: A view which which the modified view is overlayed with when state is processing. | ||
/// - Returns: A view that may render processing state. | ||
public func processingOverlay<Overlay: View>( | ||
isProcessing processing: Bool, | ||
@ViewBuilder overlay: @escaping () -> Overlay = { ProgressView() } | ||
) -> some View { | ||
modifier(ProcessingOverlay(isProcessing: processing, overlay: overlay)) | ||
} | ||
} |
Oops, something went wrong.