Skip to content

Commit

Permalink
Add custom background and foreground Color
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Hidalgo authored and Marc Hidalgo committed Jan 9, 2025
1 parent d5531f2 commit f4037a1
Showing 1 changed file with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,35 @@ import SwiftUI
} label: {
Text("Marc es muy del Madrid")
}
.inAppNotification(message: $state)
.inAppNotification(message: $state, textColor: .white, backgroundColor: .orange)

Check failure on line 15 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

View workflow job for this annotation

GitHub Actions / build

cannot infer contextual base in reference to member 'white'

Check failure on line 15 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

View workflow job for this annotation

GitHub Actions / build

cannot infer contextual base in reference to member 'orange'

Check failure on line 15 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

View workflow job for this annotation

GitHub Actions / build

cannot infer contextual base in reference to member 'white'

Check failure on line 15 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

View workflow job for this annotation

GitHub Actions / build

cannot infer contextual base in reference to member 'orange'
}

public extension SwiftUI.View {

/// Presents a sheet where the sheet's height is the contained view's intrinsic height
/// **Note:** It doesn't work if `Content` is embedded in a `NavigationView`
/// Presents an in-app notification as an overlay on the current view.
/// The notification displays a message with customizable text color and background color.
/// - Parameters:
/// - isPresented: the Binding that controls the presentation
/// - onDismiss: a callback to be called on dismissal
/// - content: the content to be presented
func inAppNotification(message: Binding<String?>) -> some View {
self.modifier(InAppToastModifier(isPresented: message))
/// - message: A binding to a string that triggers the notification when set. Setting it to `nil` dismisses the notification.
/// - textColor: The color of the notification's text. Defaults to `.white`.
/// - backgroundColor: The color of the notification's background. Defaults to `.green`.
func inAppNotification(
message: Binding<String?>,
textColor: UIColor = .white,

Check failure on line 28 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

View workflow job for this annotation

GitHub Actions / build

cannot find type 'UIColor' in scope

Check failure on line 28 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

View workflow job for this annotation

GitHub Actions / build

cannot find type 'UIColor' in scope

Check failure on line 28 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

View workflow job for this annotation

GitHub Actions / build

cannot find type 'UIColor' in scope
backgroundColor: UIColor = .green

Check failure on line 29 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

View workflow job for this annotation

GitHub Actions / build

cannot find type 'UIColor' in scope

Check failure on line 29 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

View workflow job for this annotation

GitHub Actions / build

cannot find type 'UIColor' in scope

Check failure on line 29 in Sources/BSWInterfaceKit/SwiftUI/ViewModifiers/InAppNotificationModifier.swift

View workflow job for this annotation

GitHub Actions / build

cannot find type 'UIColor' in scope
) -> some View {
self.modifier(InAppToastModifier(
isPresented: message,
textColor: textColor,
backgroundColor: backgroundColor
))
}
}

private struct InAppToastModifier: ViewModifier {

let isPresented: Binding<String?>
let textColor: UIColor
let backgroundColor: UIColor
@State private var anchorView = UIView()

func body(content: Content) -> some View {
Expand All @@ -47,9 +57,9 @@ private struct InAppToastModifier: ViewModifier {
guard let sourceVC = view.next() as UIViewController? else { return }
InAppNotifications.showNotification(
fromVC: sourceVC,
backgroundColor: .green,
backgroundColor: backgroundColor,
image: nil,
title: TextStyler.styler.attributedString(value),
title: TextStyler.styler.attributedString(value, color: textColor),
message: nil,
dismissDelay: 2) {
self.isPresented.wrappedValue = nil
Expand Down

0 comments on commit f4037a1

Please sign in to comment.