Skip to content

Commit 52839bb

Browse files
committed
Get current message being displayed without specifying an id
1 parent de4220f commit 52839bb

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## 4.1.1
5+
6+
### Features
7+
* #152 Get current message being displayed without specifying an `id`
8+
49
## 4.1.0
510

611
### Features

SwiftMessages.podspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Pod::Spec.new do |spec|
22
spec.name = 'SwiftMessages'
3-
spec.version = '4.1.0'
3+
spec.version = '4.1.1'
44
spec.license = { :type => 'MIT' }
55
spec.homepage = 'https://github.com/SwiftKickMobile/SwiftMessages'
66
spec.authors = { 'Timothy Moose' => 'tim@swiftkick.it' }
77
spec.summary = 'A very flexible message bar for iOS written in Swift.'
8-
spec.source = {:git => 'https://github.com/SwiftKickMobile/SwiftMessages.git', :tag => '4.1.0'}
8+
spec.source = {:git => 'https://github.com/SwiftKickMobile/SwiftMessages.git', :tag => '4.1.1'}
99
spec.platform = :ios, '8.0'
1010
spec.ios.deployment_target = '8.0'
1111
spec.source_files = 'SwiftMessages/**/*.swift'

SwiftMessages/SwiftMessages.swift

+27-14
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ open class SwiftMessages {
405405
*/
406406
open func hide(id: String) {
407407
messageQueue.sync {
408-
if id == current?.id {
408+
if id == _current?.id {
409409
hideCurrent()
410410
}
411411
queue = queue.filter { $0.id != id }
@@ -429,7 +429,7 @@ open class SwiftMessages {
429429
return
430430
}
431431
}
432-
if id == current?.id {
432+
if id == _current?.id {
433433
hideCurrent()
434434
}
435435
queue = queue.filter { $0.id != id }
@@ -486,7 +486,7 @@ open class SwiftMessages {
486486
fileprivate var queue: [Presenter] = []
487487
fileprivate var delays = Delays()
488488
fileprivate var counts: [String : Int] = [:]
489-
fileprivate var current: Presenter? = nil {
489+
fileprivate var _current: Presenter? = nil {
490490
didSet {
491491
if oldValue != nil {
492492
let delayTime = DispatchTime.now() + pauseBetweenMessages
@@ -500,7 +500,7 @@ open class SwiftMessages {
500500
fileprivate func enqueue(presenter: Presenter) {
501501
if presenter.config.ignoreDuplicates {
502502
counts[presenter.id] = (counts[presenter.id] ?? 0) + 1
503-
if current?.id == presenter.id && current?.isHiding == false { return }
503+
if _current?.id == presenter.id && _current?.isHiding == false { return }
504504
if queue.filter({ $0.id == presenter.id }).count > 0 { return }
505505
}
506506
func doEnqueue() {
@@ -520,9 +520,9 @@ open class SwiftMessages {
520520
}
521521

522522
fileprivate func dequeueNext() {
523-
guard self.current == nil, queue.count > 0 else { return }
523+
guard self._current == nil, queue.count > 0 else { return }
524524
let current = queue.removeFirst()
525-
self.current = current
525+
self._current = current
526526
// Set `autohideToken` before the animation starts in case
527527
// the dismiss gesture begins before we've queued the autohide
528528
// block on animation completion.
@@ -545,31 +545,31 @@ open class SwiftMessages {
545545
}
546546
} catch {
547547
strongSelf.messageQueue.sync {
548-
strongSelf.current = nil
548+
strongSelf._current = nil
549549
}
550550
}
551551
}
552552
}
553553

554554
fileprivate func internalHide(id: String) {
555-
if id == current?.id {
555+
if id == _current?.id {
556556
hideCurrent()
557557
}
558558
queue = queue.filter { $0.id != id }
559559
delays.ids.remove(id)
560560
}
561561

562562
fileprivate func hideCurrent() {
563-
guard let current = current, !current.isHiding else { return }
563+
guard let current = _current, !current.isHiding else { return }
564564
let delay = current.delayHide ?? 0
565565
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self, weak current] in
566566
guard let strongCurrent = current else { return }
567567
strongCurrent.hide { (completed) in
568568
guard completed, let strongSelf = self, let strongCurrent = current else { return }
569569
strongSelf.messageQueue.sync {
570-
guard strongSelf.current === strongCurrent else { return }
570+
guard strongSelf._current === strongCurrent else { return }
571571
strongSelf.counts[strongCurrent.id] = nil
572-
strongSelf.current = nil
572+
strongSelf._current = nil
573573
}
574574
}
575575
}
@@ -578,7 +578,7 @@ open class SwiftMessages {
578578
fileprivate weak var autohideToken: AnyObject?
579579

580580
fileprivate func queueAutoHide() {
581-
guard let current = current else { return }
581+
guard let current = _current else { return }
582582
autohideToken = current
583583
if let pauseDuration = current.pauseDuration {
584584
let delayTime = DispatchTime.now() + pauseDuration
@@ -598,6 +598,19 @@ open class SwiftMessages {
598598

599599
extension SwiftMessages {
600600

601+
/**
602+
Returns the message view of type `T` if it is currently being shown or hidden.
603+
604+
- Returns: The view of type `T` if it is currently being shown or hidden.
605+
*/
606+
public func current<T: UIView>() -> T? {
607+
var view: T?
608+
messageQueue.sync {
609+
view = _current?.view as? T
610+
}
611+
return view
612+
}
613+
601614
/**
602615
Returns a message view with the given `id` if it is currently being shown or hidden.
603616

@@ -607,7 +620,7 @@ extension SwiftMessages {
607620
public func current<T: UIView>(id: String) -> T? {
608621
var view: T?
609622
messageQueue.sync {
610-
if let current = current, current.id == id {
623+
if let current = _current, current.id == id {
611624
view = current.view as? T
612625
}
613626
}
@@ -670,7 +683,7 @@ extension SwiftMessages: PresenterDelegate {
670683
}
671684

672685
private func presenter(forAnimator animator: Animator) -> Presenter? {
673-
if let current = current, animator === current.animator {
686+
if let current = _current, animator === current.animator {
674687
return current
675688
}
676689
let queued = queue.filter { $0.animator === animator }

0 commit comments

Comments
 (0)