Skip to content

Commit

Permalink
Enhancement: Duplicate message prevention - Enabled by a bool (Off by…
Browse files Browse the repository at this point in the history
… default)
  • Loading branch information
Kevin Lohman committed Jul 26, 2016
1 parent cbecda0 commit e625322
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion SimpleChat/LGSimpleChat/LGSimpleChat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ class LGChatMessage : NSObject {
fatalError("LGChatMessage.FatalError : Initialization : Incompatible string set to SentByString!")
}
}

override var hashValue: Int {
get {
return Int.addWithOverflow(Int.addWithOverflow(sentBy.hashValue, (timeStamp ?? 0).hashValue).0, content.hashValue).0
}
}

override func isEqual(object: AnyObject?) -> Bool {
guard let object = object as? LGChatMessage else {
return false
}
return object.hashValue == self.hashValue
}
}

// MARK: Message Cell
Expand Down Expand Up @@ -253,6 +266,9 @@ class LGChatController : UIViewController, UITableViewDelegate, UITableViewDataS
}
}

// Set to true to perform duplicate checking
var checkForDuplicates = false

// MARK: Constants

private struct Constants {
Expand Down Expand Up @@ -418,7 +434,11 @@ class LGChatController : UIViewController, UITableViewDelegate, UITableViewDataS
// MARK: New messages

func addNewMessage(message: LGChatMessage) {
messages += [message]
if checkForDuplicates {
if messages.contains(message) {
return // Dupe
}
}

if let sort = sort {
let index = messages.insertionIndexOf(message, isOrderedBefore: sort)
Expand Down Expand Up @@ -538,6 +558,15 @@ extension Array {
}
}

extension Array where Element : Equatable {
// Remove first collection element that is equal to the given `object`:
mutating func removeObject(object : Generator.Element) {
if let index = self.indexOf(object) {
self.removeAtIndex(index)
}
}
}

// MARK: Chat Input

protocol LGChatInputDelegate : class {
Expand Down

0 comments on commit e625322

Please sign in to comment.