Skip to content

Commit

Permalink
A better Queue implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JanGorman committed Jun 8, 2016
1 parent 2fc68e3 commit 0d8bc84
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
14 changes: 8 additions & 6 deletions SwiftMessageBar/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal protocol Identifiable {

}

internal final class Message: UIView, Identifiable {
internal final class Message: UIView {

private static let Padding: CGFloat = 10
private static let MessageOffset: CGFloat = 2
Expand Down Expand Up @@ -205,13 +205,15 @@ internal final class Message: UIView, Identifiable {
var availableWidth: CGFloat {
return width - Message.Padding * 2 - Message.IconSize
}

// MARK: Identifiable


}

extension Message: Identifiable {

internal func id() -> NSUUID {
return uuid
return uuid
}

}

extension UIView {
Expand Down
72 changes: 42 additions & 30 deletions SwiftMessageBar/SwiftMessageBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public final class SwiftMessageBar {

public func showMessageWithTitle(title: String? = nil, message: String? = nil, type: MessageType,
duration: NSTimeInterval = 3, dismiss: Bool = true, callback: Callback? = nil) -> NSUUID {
messageQueue.queue.last
let message = Message(title: title, message: message, backgroundColor: type.backgroundColor(fromConfig: config), titleFontColor: config.titleColor, messageFontColor: config.messageColor, icon: type.image(fromConfig: config), duration: duration, dismiss: dismiss, callback: callback)
messageQueue.enqueue(message)
if !isMessageVisible {
Expand All @@ -121,10 +120,8 @@ public final class SwiftMessageBar {
}

public func cancelAll(force force: Bool = false) {
guard !isMessageVisible && messageQueue.isEmpty() || force else {
return
}

guard !isMessageVisible && messageQueue.isEmpty || force else { return }

if let message = visibleMessage {
dismissMessage(message)
}
Expand Down Expand Up @@ -206,7 +203,7 @@ public final class SwiftMessageBar {
message.callback?()
}

if let messageBar = self where !messageBar.messageQueue.isEmpty() {
if let messageBar = self where !messageBar.messageQueue.isEmpty {
messageBar.dequeueNextMessage()
} else {
self?.messageWindow = nil
Expand Down Expand Up @@ -254,38 +251,53 @@ private class MessageBarController: UIViewController {
}

private struct Queue<T: Identifiable> {

private var queue = [T]()


private var left: [T]
private var right: [T]

var isEmpty: Bool {
return left.isEmpty && right.isEmpty
}

init() {
left = []
right = []
}

mutating func dequeue() -> T? {
return !queue.isEmpty ? queue.removeAtIndex(0) : nil
guard !(left.isEmpty && right.isEmpty) else { return nil }

if left.isEmpty {
left = right.reverse()
right.removeAll(keepCapacity: true)
}
return left.removeLast()
}

mutating func enqueue(newElement: T) {
queue.append(newElement)
}

func isEmpty() -> Bool {
return queue.isEmpty
right.append(newElement)
}

mutating func removeAll() {
queue.removeAll(keepCapacity: false)
left.removeAll()
right.removeAll()
}

mutating func removeWithId(id: NSUUID) {
if let idx = findElementIndexWithId(id) {
queue.removeAtIndex(idx)
}
if let idx = left.findWithId(id) {
left.removeAtIndex(idx)
}
if let idx = right.findWithId(id) {
right.removeAtIndex(idx)
}
}

private func findElementIndexWithId(id: NSUUID) -> Int? {
for (i, element) in queue.enumerate() {
if element.id() == id {
return i
}
}
return nil

}

private extension Array where Element: Identifiable {

func findWithId(id: NSUUID) -> Int? {
return enumerate().lazy.filter({ $1.id() == id }).first?.0
}
}

}

0 comments on commit 0d8bc84

Please sign in to comment.