-
Notifications
You must be signed in to change notification settings - Fork 753
Introduce SwiftMessagesHideAction and Environment Key for SwiftUI #574
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: master
Are you sure you want to change the base?
Conversation
|
||
private struct SwiftMessagesHideKey: EnvironmentKey { | ||
/// By default, hide the current message. | ||
static let defaultValue: @MainActor (Bool) -> Void = { animated in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite the same thing as the built-in dismiss
environment variable, which is an instance of DismissAction
. DismissAction
's is a struct, which prevents the behavior from being modified or overwritten (not to mention, the initializer isn't public). DismissAction
that implements callAsFunction()
, allowing the simple invocation dismiss()
.
How about we make a struct SwiftMessagesHideAction
:
SwiftMessagesHideAction/callAsFunction()
callsSwiftMessages.hide(true)
- An additional function
SwiftMessagesHideAction/hide(_ animated: Bool
) can be invoked if animation needs to be specified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of minor change requests. In general, we prioritize APIs at the top of a file and implementation details below—the reader is usually most interested in how to use the code rather than how it was implemented.
/// Dismiss with animation. | ||
@MainActor | ||
public func callAsFunction() { | ||
SwiftMessages.hide(animated: true) | ||
} | ||
|
||
/// Dismiss, optionally animated. | ||
@MainActor | ||
public func callAsFunction(animated: Bool) { | ||
SwiftMessages.hide(animated: animated) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Dismiss with animation. | |
@MainActor | |
public func callAsFunction() { | |
SwiftMessages.hide(animated: true) | |
} | |
/// Dismiss, optionally animated. | |
@MainActor | |
public func callAsFunction(animated: Bool) { | |
SwiftMessages.hide(animated: animated) | |
} | |
/// Dismiss with option to disable animation. | |
@MainActor | |
public func callAsFunction(animated: Bool = true) { | |
SwiftMessages.hide(animated: animated) | |
} |
// MARK: ––– Environment Key & Value ––– | ||
|
||
private struct SwiftMessagesHideKey: EnvironmentKey { | ||
/// Default to our action struct, which itself defaults to animated. | ||
static let defaultValue: SwiftMessagesHideAction = SwiftMessagesHideAction() | ||
} | ||
|
||
public extension EnvironmentValues { | ||
/// Inject `@Environment(\.swiftMessagesHide)` into your views. | ||
/// - Use `hide()` for the default animated dismissal. | ||
/// - Use `hide(false)` to bypass animation. | ||
var swiftMessagesHide: SwiftMessagesHideAction { | ||
get { self[SwiftMessagesHideKey.self] } | ||
set { self[SwiftMessagesHideKey.self] = newValue } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// MARK: ––– Environment Key & Value ––– | |
private struct SwiftMessagesHideKey: EnvironmentKey { | |
/// Default to our action struct, which itself defaults to animated. | |
static let defaultValue: SwiftMessagesHideAction = SwiftMessagesHideAction() | |
} | |
public extension EnvironmentValues { | |
/// Inject `@Environment(\.swiftMessagesHide)` into your views. | |
/// - Use `hide()` for the default animated dismissal. | |
/// - Use `hide(false)` to bypass animation. | |
var swiftMessagesHide: SwiftMessagesHideAction { | |
get { self[SwiftMessagesHideKey.self] } | |
set { self[SwiftMessagesHideKey.self] = newValue } | |
} | |
} | |
public extension EnvironmentValues { | |
/// Inject `@Environment(\.swiftMessagesHide)` into your views. | |
/// - Use `hide()` for the default animated dismissal. | |
/// - Use `hide(false)` to bypass animation. | |
var swiftMessagesHide: SwiftMessagesHideAction { | |
get { self[SwiftMessagesHideKey.self] } | |
set { self[SwiftMessagesHideKey.self] = newValue } | |
} | |
} | |
private struct SwiftMessagesHideKey: EnvironmentKey { | |
/// Default to our action struct, which itself defaults to animated. | |
static let defaultValue: SwiftMessagesHideAction = SwiftMessagesHideAction() | |
} |
PR description
This PR addresses #549.
It introduces a
swiftMessagesHide
environment value for SwiftMessages that will allow SwiftUI messages to self-dismiss. This enhancement mirrors Apple’s own@Environment(\.dismiss)
pattern, allowing your custom message views to grab ahide()
orhide(animated:)
closure from the environment and invoke it as needed.How to Use
In your
View
, just declare the environment key like this:And then call
hide()
orhide(animated:)
to dismiss the swiftUI message.Demo
Check out the sample demo in
SwiftUIDemo/SwiftUIDemo/DemoView.swift
.