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

Relocate MainActor from class declaration #543

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 29 additions & 2 deletions SwiftMessages/SwiftMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ private let globalInstance = SwiftMessages()
It behaves like a queue, only showing one message at a time. Message views that
adopt the `Identifiable` protocol (as `MessageView` does) will have duplicates removed.
*/
@MainActor
open class SwiftMessages {

/**
Expand Down Expand Up @@ -408,6 +407,7 @@ open class SwiftMessages {
- Parameter config: The configuration options.
- Parameter view: The view to be displayed.
*/
@MainActor
open func show(config: Config, view: UIView) {
let presenter = Presenter(config: config, view: view, delegate: self)
enqueue(presenter: presenter)
Expand All @@ -420,6 +420,7 @@ open class SwiftMessages {
- Parameter config: The configuration options.
- Parameter view: The view to be displayed.
*/
@MainActor
public func show(view: UIView) {
show(config: defaultConfig, view: view)
}
Expand Down Expand Up @@ -462,6 +463,7 @@ open class SwiftMessages {
/**
Hide the current message being displayed by animating it away.
*/
@MainActor
open func hide(animated: Bool = true) {
hideCurrent(animated: animated)
}
Expand All @@ -470,6 +472,7 @@ open class SwiftMessages {
Hide the current message, if there is one, by animating it away and
clear the message queue.
*/
@MainActor
open func hideAll() {
queue.removeAll()
delays.removeAll()
Expand All @@ -483,6 +486,7 @@ open class SwiftMessages {
views, such as `MessageView`, that adopt the `Identifiable` protocol.
- Parameter id: The identifier of the message to remove.
*/
@MainActor
open func hide(id: String) {
if id == _current?.id {
hideCurrent()
Expand All @@ -497,6 +501,7 @@ open class SwiftMessages {
given message ID are equal. This can be useful for messages that may be
shown from multiple code paths to ensure that all paths are ready to hide.
*/
@MainActor
open func hideCounted(id: String) {
if let count = counts[id] {
if count < 2 {
Expand Down Expand Up @@ -567,24 +572,27 @@ open class SwiftMessages {
private var presenters = Set<Presenter>()
}

@MainActor
func show(presenter: Presenter) {
enqueue(presenter: presenter)
}

fileprivate var queue: [Presenter] = []
@MainActor
fileprivate var delays = Delays()
fileprivate var counts: [String : Int] = [:]
fileprivate var _current: Presenter? = nil {
didSet {
if oldValue != nil {
Task { [weak self] in
Task { @MainActor [weak self] in
try? await Task.sleep(seconds: self?.pauseBetweenMessages ?? 0)
self?.dequeueNext()
}
}
}
}

@MainActor
fileprivate func enqueue(presenter: Presenter) {
if presenter.config.ignoreDuplicates {
counts[presenter.id] = (counts[presenter.id] ?? 0) + 1
Expand All @@ -611,6 +619,7 @@ open class SwiftMessages {
}
}

@MainActor
fileprivate func dequeueNext() {
guard queue.count > 0 else { return }
if let _current, !_current.isOrphaned { return }
Expand All @@ -637,6 +646,7 @@ open class SwiftMessages {
}
}

@MainActor
fileprivate func internalHide(presenter: Presenter) {
if presenter == _current {
hideCurrent()
Expand All @@ -646,6 +656,7 @@ open class SwiftMessages {
}
}

@MainActor
fileprivate func hideCurrent(animated: Bool = true) {
guard let current = _current, !current.isHiding else { return }
let action = { [weak self] in
Expand All @@ -665,6 +676,7 @@ open class SwiftMessages {

fileprivate weak var autohideToken: Presenter?

@MainActor
fileprivate func queueAutoHide() {
guard let current = _current else { return }
autohideToken = current
Expand Down Expand Up @@ -707,6 +719,7 @@ extension SwiftMessages {
- Parameter id: The id of a message that adopts `Identifiable`.
- Returns: The view with matching id if currently being shown or hidden.
*/
@MainActor
public func current<T: UIView>(id: String) -> T? {
_current?.id == id ? _current?.view as? T : nil
}
Expand All @@ -717,6 +730,7 @@ extension SwiftMessages {
- Parameter id: The id of a message that adopts `Identifiable`.
- Returns: The view with matching id if currently queued to be shown.
*/
@MainActor
public func queued<T: UIView>(id: String) -> T? {
queue.first { $0.id == id }?.view as? T
}
Expand All @@ -728,6 +742,7 @@ extension SwiftMessages {
- Parameter id: The id of a message that adopts `Identifiable`.
- Returns: The view with matching id if currently queued to be shown.
*/
@MainActor
public func currentOrQueued<T: UIView>(id: String) -> T? {
return current(id: id) ?? queued(id: id)
}
Expand All @@ -739,10 +754,12 @@ extension SwiftMessages {

extension SwiftMessages: PresenterDelegate {

@MainActor
func hide(presenter: Presenter) {
self.internalHide(presenter: presenter)
}

@MainActor
public func hide(animator: Animator) {
guard let presenter = self.presenter(forAnimator: animator) else { return }
self.internalHide(presenter: presenter)
Expand All @@ -752,6 +769,7 @@ extension SwiftMessages: PresenterDelegate {
autohideToken = nil
}

@MainActor
public func panEnded(animator: Animator) {
queueAutoHide()
}
Expand Down Expand Up @@ -879,26 +897,32 @@ extension SwiftMessages {
globalInstance.show(config: config, viewProvider: viewProvider)
}

@MainActor
public static func show(view: UIView) {
globalInstance.show(view: view)
}

@MainActor
public static func show(config: Config, view: UIView) {
globalInstance.show(config: config, view: view)
}

@MainActor
public static func hide(animated: Bool = true) {
globalInstance.hide(animated: animated)
}

@MainActor
public static func hideAll() {
globalInstance.hideAll()
}

@MainActor
public static func hide(id: String) {
globalInstance.hide(id: id)
}

@MainActor
public static func hideCounted(id: String) {
globalInstance.hideCounted(id: id)
}
Expand All @@ -921,14 +945,17 @@ extension SwiftMessages {
}
}

@MainActor
public static func current<T: UIView>(id: String) -> T? {
return globalInstance.current(id: id)
}

@MainActor
public static func queued<T: UIView>(id: String) -> T? {
return globalInstance.queued(id: id)
}

@MainActor
public static func currentOrQueued<T: UIView>(id: String) -> T? {
return globalInstance.currentOrQueued(id: id)
}
Expand Down