Swiftpack.co -  danielsaidi/SystemNotification as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
danielsaidi/SystemNotification
SystemNotification is a SwiftUI library that lets you mimic the native iOS system notification in your iOS apps.
.package(url: "https://github.com/danielsaidi/SystemNotification.git", from: "0.3.1")

SystemNotification


Version Platform Swift 5.3 MIT License Twitter: @danielsaidi

About SystemNotification

SystemNotification is lets you mimic the native iOS system notification (that is presented when e.g. toggling silent mode on and off) in your SwiftUI apps.

The result can look like this...or completely different:

These notifications have a default style, but can be customized to fit your specific app.

Installation

Swift Package Manager

https://github.com/danielsaidi/SystemNotification.git

CocoaPods

pod SystemNotification

Getting started

SystemNotification contains a SystemNotification view, that can be created with an isActive binding, an optional configuration and a custom view.

For instance:

let notification = SystemNotification(
    isActive: $isNotificationActive,
    configuration: .init(edge: .bottom)) { _ in
        SystemNotificationMessage(
            icon: Image(systemName: "exclamationmark.triangle"), 
            title: "Alert", 
            text: "Something happened!",
            configuration: .init(iconColor: .red)
        )
    }

You can use any view you like in the notification:

let notification = SystemNotification(
    isActive: $isNotificationActive,
    configuration: .init(edge: .bottom)) { _ in
        Color.red.frame(height: 150)
    }

If you want to use a standard SystemNotificationMessage, you can use this shorthand:

let notification = SystemNotification(
    icon: Image?,
    title: LocalizedStringKey,
    text: LocalizedStringKey,
    isActive: $isNotificationActive,
    configuration: SystemNotification.Configuration(edge: .bottom))

You can add any notification to any view, using the systemNotification modifier:

List(items) { item
   HStack { item.name }
}.systemNotification(notification)

You can now present the notification by setting isNotificationActive to true.

SystemNotificationContext

While the above examples show how easy it is to add a notification to a view, you may want to present many different notifications.

Instead of having to use a state and a modifier per notification, you can use a SystemNotificationContext to present multiple notifications with a single modifier.

Just create a @StateObject in your presenting view, then use the context-specific modifier:

@StateObject private var context = SystemNotificationContext()

List(items) { item
   HStack { item.name }
}.systemNotification(context: context)

You can now present notifications buy using the present functions of the context. You can present any view and use any configuration:

context.present(
    content: notification,
    configuration: .init(backgroundColor: .red))

I really recomment the context approach for more complex use-cases.

Configurations

SystemNotification

A SystemNotification can be configured with a configuration that specifies:

  • animation
  • backgroundColor
  • cornerRadius
  • edge
  • minWidth
  • duration
  • shadowRadius
  • shadowOffset
  • shadowRadius

You can customize these properties to control how the notification looks and behaves.

The default configuration makes the notification look and behave like the native one.

SystemNotificationMessage

A SystemNotificationMessage view can be configured with a configuration that specifies:

  • iconColor
  • iconFont
  • padding
  • textColor
  • textFont
  • titleColor
  • titleFont

You can customize these properties to control how the notification message looks.

The default configuration makes the message look like the native one.

Demo app

This repo contains a basic demo app that demonstrates how to use the bottom sheet.

Just open the Demo project and run the app.

Contact me

Feel free to reach out if you have questions or if you want to contribute in any way:

License

SystemNotification is available under the MIT license. See LICENSE file for more info.

GitHub

link
Stars: 82
Last commit: 2 hours ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

2 hours ago

This version fixes a preview bug that caused the library not to build for macOS.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API