Skip to content

Commit 12226d3

Browse files
author
Kevin Lohman
committed
Enhancement: Allow for filtering of chats, change filter parameter for automatic filtering
1 parent e625322 commit 12226d3

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

SimpleChat/LGSimpleChat/LGSimpleChat.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ class LGChatController : UIViewController, UITableViewDelegate, UITableViewDataS
254254
var pendingGravatarLoad = [ String ]()
255255

256256

257+
// Set this value to filter messages
258+
var filter : NSPredicate? {
259+
didSet {
260+
tableView.reloadData()
261+
}
262+
}
263+
257264
typealias isOrderedBefore = (LGChatMessage, LGChatMessage) -> Bool
258265

259266
// Set this value to apply sorting
@@ -473,9 +480,17 @@ class LGChatController : UIViewController, UITableViewDelegate, UITableViewDataS
473480
// MARK: UITableViewDelegate
474481

475482
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
483+
let message = messages[indexPath.row]
484+
485+
if let filter = filter {
486+
if filter.evaluateWithObject(message) == false {
487+
return 0.0 // Filtered
488+
}
489+
}
490+
476491
let padding: CGFloat = 10.0
477492
sizingCell.bounds.size.width = CGRectGetWidth(self.view.bounds)
478-
let height = self.sizingCell.setupWithMessage(messages[indexPath.row]).height + padding;
493+
let height = self.sizingCell.setupWithMessage(message).height + padding;
479494
return height
480495
}
481496

@@ -498,6 +513,12 @@ class LGChatController : UIViewController, UITableViewDelegate, UITableViewDataS
498513
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
499514
let cell = tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath) as! LGChatMessageCell
500515
let message = self.messages[indexPath.row]
516+
cell.hidden = false
517+
if let filter = filter {
518+
if filter.evaluateWithObject(message) == false {
519+
cell.hidden = true
520+
}
521+
}
501522
cell.gravatarString = message.gravatarString
502523
cell.opponentImageView.image = message.sentBy == .Opponent ? self.opponentImage : nil
503524
if let gravatarString = cell.gravatarString {

0 commit comments

Comments
 (0)