Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mofeejegi
Copy link
Collaborator

@mofeejegi mofeejegi commented May 12, 2025

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 a hide() or hide(animated:) closure from the environment and invoke it as needed.

How to Use

In your View, just declare the environment key like this:

@Environment(\.swiftMessagesHide) private var hide

And then call hide() or hide(animated:) to dismiss the swiftUI message.

Demo

Check out the sample demo in SwiftUIDemo/SwiftUIDemo/DemoView.swift.

@mofeejegi mofeejegi changed the title Introduced SwiftMessagesHide Environment Key for SwiftUI Introduce SwiftMessagesHide Environment Key for SwiftUI May 12, 2025
@mofeejegi mofeejegi requested a review from wtmoose May 12, 2025 22:08
@mofeejegi mofeejegi self-assigned this May 13, 2025

private struct SwiftMessagesHideKey: EnvironmentKey {
/// By default, hide the current message.
static let defaultValue: @MainActor (Bool) -> Void = { animated in
Copy link
Member

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:

  1. SwiftMessagesHideAction/callAsFunction() calls SwiftMessages.hide(true)
  2. An additional function SwiftMessagesHideAction/hide(_ animated: Bool) can be invoked if animation needs to be specified.

@mofeejegi mofeejegi requested a review from wtmoose May 25, 2025 21:14
@mofeejegi mofeejegi changed the title Introduce SwiftMessagesHide Environment Key for SwiftUI Introduce SwiftMessagesHideAction and Environment Key for SwiftUI May 25, 2025
@mofeejegi mofeejegi requested review from wtmoose and removed request for wtmoose May 30, 2025 15:34
Copy link
Member

@wtmoose wtmoose left a 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.

Comment on lines +18 to +28
/// Dismiss with animation.
@MainActor
public func callAsFunction() {
SwiftMessages.hide(animated: true)
}

/// Dismiss, optionally animated.
@MainActor
public func callAsFunction(animated: Bool) {
SwiftMessages.hide(animated: animated)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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)
}

Comment on lines +31 to +46
// 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 }
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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()
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants