Skip to content
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

Queue #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Queue #144

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
27 changes: 27 additions & 0 deletions ExampleProject/ExamplesScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct ExamplesScreen: View {
@State var showSubtitle = false
@State var backgroundType: StatusBarNotificationBackgroundType = .pill

@State var queueContent: [StatusBarNotification]? = nil // for queue counter

func showDefaultNotification(_ text: String, completion: @escaping (NotificationPresenter) -> ()) {
let styleName = NotificationPresenter.shared.addStyle(named: "default_sample") { style in
style.backgroundStyle.backgroundType = backgroundType
Expand Down Expand Up @@ -299,6 +301,31 @@ struct ExamplesScreen: View {
}
}
}

Section("Queue") {
Button("Clear queue") {
NotificationPresenter.shared.clearQueue()
queueContent = NotificationPresenter.shared.queue
}
Button("Add to queue") {
NotificationPresenter.shared.addToQueue(notification: StatusBarNotification(title: "From queue", styleName: "default_sample"))
queueContent = NotificationPresenter.shared.queue
}
Button("Present first from queue") {
NotificationPresenter.shared.presentFromQueue(duration: 5.0)
if NotificationPresenter.shared.queue.count == 0 {
NotificationPresenter.shared.present("Queue is empty", styleName: "default_sample", duration: 5.0)
}
queueContent = NotificationPresenter.shared.queue
}
Button("Present queue") {
NotificationPresenter.shared.presentQueue()
queueContent = NotificationPresenter.shared.queue
}
if queueContent != nil {
Text("\(queueContent!.count) notification\(queueContent!.count == 1 ? "" : "s") in queue")
}
}
}
.navigationTitle(title)
.navigationBarTitleDisplayMode(.inline)
Expand Down
56 changes: 56 additions & 0 deletions JDStatusBarNotification/Public/NotificationPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
//

import SwiftUI
import Foundation

/// Struct for storing notifications in queue
public struct StatusBarNotification {
public let title: String?
public let subtitle: String?
public let style: StatusBarNotificationStyle?
public let styleName: String
public let completion: NotificationPresenter.Completion?

public init(title: String?, subtitle: String? = nil, style: StatusBarNotificationStyle? = nil, styleName: String, completion: NotificationPresenter.Completion? = nil) {
self.title = title
self.subtitle = subtitle
self.style = style
self.styleName = styleName
self.completion = completion
}
}

/**
* The NotificationPresenter let's you present notifications below the statusBar.
Expand All @@ -28,6 +46,8 @@ public class NotificationPresenter: NSObject, NotificationWindowDelegate {
var overlayWindow: NotificationWindow?
var styleCache: StyleCache

public private(set) var queue: [StatusBarNotification]

/// Provides access to the shared presenter. This is the entry point to present, style and dismiss notifications.
///
/// - Returns: An initialized ``NotificationPresenter`` instance.
Expand All @@ -36,6 +56,7 @@ public class NotificationPresenter: NSObject, NotificationWindowDelegate {

private override init() {
styleCache = StyleCache()
queue = []
}

/// Called upon animation completion.
Expand Down Expand Up @@ -72,6 +93,41 @@ public class NotificationPresenter: NSObject, NotificationWindowDelegate {
return view
}

// MARK: - Queue

/// Adds a ``StatusBarNotification`` to the queue (FIFO).
/// - Parameter notification: the notification to store in the queue.
public func addToQueue(notification: StatusBarNotification) {
queue.append(notification)
}

/// Removes all elements from the queue and dismisses the current notification.
public func clearQueue() {
queue = []
dismiss()
}

/// Presents the first notification from the queue.
/// - Parameter duration: Duration in secons.
public func presentFromQueue(duration: Double? = nil) {
if queue.count > 0 {
present(queue[0].title ?? "", subtitle: queue[0].subtitle, styleName: queue[0].styleName, duration: duration, completion: queue[0].completion)
queue.remove(at: 0)
}
}

/// Presents all notification of the queue. Recursively adds an async job for `duration` + 0.5 seconds until queue is empty.
/// NOTE: `duration` doesn't support `nil` yet because the notifications are displayed all at a time by default and the time until dismissal must be known.
/// - Parameter duration: duration in seconds.
public func presentQueue(duration: Double = 2.0) {
if self.queue.count > 0 {
self.presentFromQueue(duration: duration)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration + 0.5) {
self.presentQueue()
}
}
}

// MARK: - NotificationWindowDelegate

func didDismissStatusBar() {
Expand Down