From a97ce3fd72c17786a3b8b5142bfa6ea8ac4286b5 Mon Sep 17 00:00:00 2001 From: Daniel Saidi Date: Wed, 24 Apr 2024 11:30:13 +0200 Subject: [PATCH] Update readme --- README.md | 60 +++++++++++++++++-- .../SystemNotification.md | 2 +- .../SystemNotification.swift | 58 ++++++++++++++++++ 3 files changed, 113 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9357c93..da1cc68 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ The result can look like this, or completely different:

-These notifications can be styled and customized to great extent. You can also use completely custom views. +System notifications can be styled and customized to great extent. You can use the native-looking `SystemNotificationMessage` view as content, or any custom view. @@ -39,21 +39,69 @@ https://github.com/danielsaidi/SystemNotification.git ## Getting started -After adding SystemNotification to your project, you can add a system notification to a view hierarchy just as you add a `sheet`, `alert` and `fullScreenModal`: +You can add a system notification to any view just as you add a `sheet`, `alert` and `fullScreenModal`, by using a `.systemNotification` view modifier with a state binding or a `SystemNotificationContext` instance. + +State-based notifications take a boolean state binding and a view builder: ```swift import SystemNotification struct MyView: View { + @State + var isActive = false + var body: some View { - Text("Hello, world") - .systemNotification(...) + VStack { + Button("Show notification") { + isActive = true + } + } + .systemNotification(isActive: $isActive) { + Text("You can use any custom content view") + .padding() + } } } ``` -You can use both state- and context and message-based notifications and style your notifications to great extent. +Context-based notifications just take a `SystemNotificationContext` instance and can then show many different notifications with a single modifier: + +```swift +import SystemNotification + +struct MyView: View { + + @StateObject + var notification = SystemNotificationContext() + + var body: some View { + VStack { + Button("Show text") { + notification.present { + Text("Context-based notifications are more flexible.") + .padding() + .multilineTextAlignment(.center) + } + } + Button("Show message") { + notification.present { + SystemNotificationMessage( + icon: Text("👍"), + title: "Great job!", + text: "You presented a native-looking message!" + ) + } + } + } + .systemNotification(notification) + } +} +``` + +The `SystemNotificationMessage` view lets you easily mimic a native notification view, with an icon, title and text. + +You can use the `.systemNotificationConfiguration` and `.systemNotificationStyle` view modifiers to apply custom configurations and styles, and the `.systemNotificationMessageStyle` to style the message. For more information, please see the [getting started guide][Getting-Started]. @@ -67,7 +115,7 @@ The [online documentation][Documentation] has more information, articles, code e ## Demo Application -The demo app lets you explore the library with iOS, macOS, and visionOS. To try it out, just open and run the `Demo` project. +The demo app lets you explore the library. To try it out, just open and run the `Demo` project. diff --git a/Sources/SystemNotification/SystemNotification.docc/SystemNotification.md b/Sources/SystemNotification/SystemNotification.docc/SystemNotification.md index a9cea4a..cdb70ed 100644 --- a/Sources/SystemNotification/SystemNotification.docc/SystemNotification.md +++ b/Sources/SystemNotification/SystemNotification.docc/SystemNotification.md @@ -8,7 +8,7 @@ SystemNotification is a SwiftUI SDK that lets you mimic the native iOS system no ![SystemNotification logo](Logo.png) -SystemNotification is a SwiftUI SDK that lets you mimic the native iOS system notification that are presented when you toggle silent mode, connect your AirPods, etc. +SystemNotification is a SwiftUI SDK that lets you mimic the native iOS system notification that are presented when you toggle silent mode, connect your AirPods, etc. These notifications can be styled and customized to great extent. You can also use completely custom views. diff --git a/Sources/SystemNotification/SystemNotification.swift b/Sources/SystemNotification/SystemNotification.swift index 3e58441..18acc10 100644 --- a/Sources/SystemNotification/SystemNotification.swift +++ b/Sources/SystemNotification/SystemNotification.swift @@ -62,6 +62,7 @@ public struct SystemNotification: View { content(isActive) .background(style.backgroundColor) .background(style.backgroundMaterial) + .compositingGroup() .cornerRadius(style.cornerRadius ?? 1_000) .shadow( color: style.shadowColor, @@ -215,3 +216,60 @@ private extension SystemNotification { return Preview() } + +#Preview("README #1") { + + struct MyView: View { + + @State + var isActive = false + + var body: some View { + VStack { + Button("Show notification") { + isActive = true + } + } + .systemNotification(isActive: $isActive) { + Text("You can use any custom content view") + .padding() + } + } + } + + return MyView() +} + + +#Preview("README #2") { + + struct MyView: View { + + @StateObject + var notification = SystemNotificationContext() + + var body: some View { + VStack { + Button("Show text") { + notification.present { + Text("Context-based notifications are more flexible.") + .padding() + .multilineTextAlignment(.center) + } + } + Button("Show message") { + notification.present { + SystemNotificationMessage( + icon: Text("👍"), + title: "Great job!", + text: "You presented a native-looking message!" + ) + } + } + } + .systemNotification(notification) + } + } + + return MyView() +}