Skip to content

Commit

Permalink
Add support for strict concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Jun 7, 2024
1 parent c6e27d9 commit 5803f2c
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 42 deletions.
13 changes: 6 additions & 7 deletions Demo/Demo/DemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ struct DemoApp: App {
var body: some Scene {
WindowGroup {
content
.systemNotification(context) // Context-based notifications are versatile
.systemNotificationStyle(style)
.systemNotification(context) // Context-based notifications are flexible
.systemNotificationStyle(style) // This is how to set a global style
.tint(.orange)
}
}
}
Expand All @@ -45,10 +46,8 @@ private extension DemoApp {

var contentView: some View {
#if os(iOS)
TabView {
NavigationStack {
ContentView(style: $style)
}
NavigationStack {
ContentView(style: $style)
}
#else
ContentView()
Expand All @@ -62,7 +61,7 @@ private extension View {
self.tabItem {
Label(
"Tab \(index)",
systemImage: "0\(index).square")
systemImage: "0\(index).circle")
}
}
}
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ let package = Package(
targets: [
.target(
name: "SystemNotification",
dependencies: []
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency")
]
),
.testTarget(
name: "SystemNotificationTests",
Expand Down
6 changes: 6 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Until then, breaking changes can happen in any version, and deprecated features



## 1.1.1

This version adds support for strict concurrency.



## 1.1

This version adds predefined system notification messages and styles and makes it easier to present a message.
Expand Down
9 changes: 6 additions & 3 deletions Sources/SystemNotification/SystemNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,18 @@ private extension SystemNotification {
var style: SystemNotificationStyle {
initStyle ?? envStyle
}
}

@MainActor
private extension SystemNotification {

func handlePresentation(_ isPresented: Bool) {
guard isPresented else { return }
currentId = UUID()
let id = currentId
DispatchQueue.main.asyncAfter(deadline: .now() + config.duration) {
if id == currentId {
isActive = false
}
guard id == currentId else { return }
isActive = false
}
}
}
Expand Down
18 changes: 8 additions & 10 deletions Sources/SystemNotification/SystemNotificationConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,15 @@ public struct SystemNotificationConfiguration {
public extension SystemNotificationConfiguration {

/// The standard system notification configuration.
///
/// You can set this style to change the global default.
static var standard = Self()
static var standard: Self { .init() }

/// A standard toast configuration.
///
/// You can set this style to change the global default.
static var standardToast = Self(
animation: .bouncy,
edge: .bottom
)
static var standardToast: Self {
.init(
animation: .bouncy,
edge: .bottom
)
}
}

public extension View {
Expand Down Expand Up @@ -97,7 +95,7 @@ private extension SystemNotificationConfiguration {

struct Key: EnvironmentKey {

static var defaultValue: SystemNotificationConfiguration = .standard
static var defaultValue: SystemNotificationConfiguration { .init() }
}
}

Expand Down
31 changes: 19 additions & 12 deletions Sources/SystemNotification/SystemNotificationContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,35 @@ import SwiftUI
/// This context can be used to present system notifications
/// in a more flexible way.
public class SystemNotificationContext: ObservableObject {

public init() {}

public typealias Action = () -> Void

@Published
public var content = AnyView(EmptyView())

@Published
public var isActive = false


public var isActiveBinding: Binding<Bool> {
.init(get: { self.isActive },
set: { self.isActive = $0 }
)
}
}

@MainActor
public extension SystemNotificationContext {

/// Dismiss the current notification, if any.
public func dismiss() {
func dismiss() {
dismiss {}
}

/// Dismiss the current notification, if any.
public func dismiss(
func dismiss(
completion: @escaping Action
) {
guard isActive else { return completion() }
Expand All @@ -44,7 +48,7 @@ public class SystemNotificationContext: ObservableObject {
}

/// Present a system notification.
public func present<Content: View>(
func present<Content: View>(
_ content: Content,
afterDelay delay: TimeInterval = 0
) {
Expand All @@ -56,30 +60,33 @@ public class SystemNotificationContext: ObservableObject {
}

/// Present a system notification.
public func present<Content: View>(
func present<Content: View>(
afterDelay delay: TimeInterval = 0,
@ViewBuilder content: @escaping () -> Content
) {
present(content(), afterDelay: delay)
}

/// Present a system notification message.
public func presentMessage<IconType: View>(
func presentMessage<IconType: View>(
_ message: SystemNotificationMessage<IconType>,
afterDelay delay: TimeInterval = 0
) {
present(message, afterDelay: delay)
}
}

@MainActor
private extension SystemNotificationContext {

func perform(
_ action: @escaping Action,
after seconds: TimeInterval
) {
guard seconds > 0 else { return action() }
DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: action)
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
action()
}
}

func perform(after seconds: TimeInterval, action: @escaping Action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ private struct SilentModeBell: View {
}
}

@MainActor
private extension SilentModeBell {

func animate() {
Expand All @@ -130,7 +131,9 @@ private extension SilentModeBell {
}

func perform(after: Double, action: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: action)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
action()
}
}

var animationDamping: Double {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public struct SystemNotificationMessageStyle {
public extension SystemNotificationMessageStyle {

/// The standard system notification message style.
///
/// You can set this style to change the global default.
static var standard = Self()
static var standard: Self { .init() }
}

public extension View {
Expand All @@ -117,7 +115,7 @@ private extension SystemNotificationMessageStyle {

struct Key: EnvironmentKey {

static var defaultValue: SystemNotificationMessageStyle = .standard
static var defaultValue: SystemNotificationMessageStyle { .standard }
}
}

Expand Down
6 changes: 2 additions & 4 deletions Sources/SystemNotification/SystemNotificationStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ public struct SystemNotificationStyle {
public extension SystemNotificationStyle {

/// The standard system notification style.
///
/// You can set this style to change the global default.
static var standard = Self()
static var standard: Self { .init() }
}

public extension View {
Expand Down Expand Up @@ -132,7 +130,7 @@ private extension SystemNotificationStyle {

struct Key: EnvironmentKey {

static var defaultValue: SystemNotificationStyle = .standard
static var defaultValue: SystemNotificationStyle { .standard }
}
}

Expand Down

0 comments on commit 5803f2c

Please sign in to comment.