Skip to content

Introduce SwiftMessagesHideAction and Environment Key for SwiftUI #574

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

Open
wants to merge 2 commits into
base: master
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
4 changes: 4 additions & 0 deletions SwiftMessages.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
86BBA9061D5E040C00FE8F16 /* Identifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 864495551D4F7C390056EB2A /* Identifiable.swift */; };
86BBA9071D5E040C00FE8F16 /* MarginAdjustable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AAF81D1D5549680031EE32 /* MarginAdjustable.swift */; };
86BBA9081D5E040C00FE8F16 /* Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AAF82A1D580DD70031EE32 /* Error.swift */; };
B0E55A662DD110EA003D97B1 /* SwiftMessagesHideAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0E55A652DD110DB003D97B1 /* SwiftMessagesHideAction.swift */; };
E6E49F911D70A344006CB883 /* MessageView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 862C0CDA1D5A397F00D06168 /* MessageView.xib */; };
E6E49F921D70A349006CB883 /* StatusLine.xib in Resources */ = {isa = PBXBuildFile; fileRef = 862C0CDB1D5A397F00D06168 /* StatusLine.xib */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -191,6 +192,7 @@
86B48AFA1D5A41C900063E2B /* SwiftMessagesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftMessagesTests.swift; sourceTree = "<group>"; };
86B48AFC1D5A41C900063E2B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
86BBA8F81D5E01FC00FE8F16 /* CardView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = CardView.xib; path = Resources/CardView.xib; sourceTree = "<group>"; };
B0E55A652DD110DB003D97B1 /* SwiftMessagesHideAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftMessagesHideAction.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -255,6 +257,7 @@
228F7DDA2ACF7029006C9644 /* SwiftUI */ = {
isa = PBXGroup;
children = (
B0E55A652DD110DB003D97B1 /* SwiftMessagesHideAction.swift */,
223DE69C2C29E50B000161E5 /* MessageGeometryProxy.swift */,
228F7DDB2ACF7039006C9644 /* MessageHostingView.swift */,
228F7DDD2ACF703A006C9644 /* MessageViewConvertible.swift */,
Expand Down Expand Up @@ -589,6 +592,7 @@
220D386E2597AA5B00BB2B88 /* SwiftMessages.Config+Extensions.swift in Sources */,
2270044B1FAFA6DD0045DDC3 /* PhysicsAnimation.swift in Sources */,
224C3C902C28A2F900B50B18 /* TopBottomPresentable.swift in Sources */,
B0E55A662DD110EA003D97B1 /* SwiftMessagesHideAction.swift in Sources */,
86BBA9041D5E040600FE8F16 /* NSBundle+Extensions.swift in Sources */,
86BBA8FD1D5E03F800FE8F16 /* SwiftMessages.swift in Sources */,
86BBA9021D5E040600FE8F16 /* WindowViewController.swift in Sources */,
Expand Down
46 changes: 46 additions & 0 deletions SwiftMessages/SwiftMessagesHideAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// SwiftMessagesHideKey.swift
// SwiftMessages
//
// Created by Mofe Ejegi on 11/05/2025.
// Copyright © 2025 SwiftKick Mobile. All rights reserved.
//

import SwiftUI

/// A SwiftUI-style action for dismissing the current SwiftMessage.
///
/// - `hide()` → animated hide
/// - `hide(animated: Bool)` → explicit control
public struct SwiftMessagesHideAction {
public init() {}

/// Dismiss with animation.
@MainActor
public func callAsFunction() {
SwiftMessages.hide(animated: true)
}

/// Dismiss, optionally animated.
@MainActor
public func callAsFunction(animated: Bool) {
SwiftMessages.hide(animated: animated)
}
Comment on lines +18 to +28
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)
}

}

// 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 }
}
}
Comment on lines +31 to +46
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()
}

7 changes: 5 additions & 2 deletions SwiftUIDemo/SwiftUIDemo/DemoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import SwiftUI
import SwiftMessages

struct DemoView: View {

/// Use this to manually hide the swift message
@Environment(\.swiftMessagesHide) private var hide

/// Demonstrates purely data-driven message presentation.
@State var message: DemoMessage?
Expand Down Expand Up @@ -51,8 +54,8 @@ struct DemoView: View {
.swiftMessage(message: $message)
.swiftMessage(message: $messageWithButton) { message in
DemoMessageWithButtonView(message: message, style: .card) {
Button("Tap Me") {
print("Tap")
Button("Hide") {
hide()
}
.buttonStyle(.bordered)
}
Expand Down