diff --git a/sample-swift/SendBird-iOS/Chatting/ChattingView.swift b/sample-swift/SendBird-iOS/Chatting/ChattingView.swift index 13b3ee0..e284a43 100644 --- a/sample-swift/SendBird-iOS/Chatting/ChattingView.swift +++ b/sample-swift/SendBird-iOS/Chatting/ChattingView.swift @@ -140,7 +140,7 @@ class ChattingView: ReusableViewFromXib, UITableViewDelegate, UITableViewDataSou self.outgoingVideoFileMessageSizingTableViewCell?.isHidden = true self.addSubview(self.outgoingVideoFileMessageSizingTableViewCell!) } - + func scrollToBottom(animated: Bool, force: Bool) { if self.messages.count == 0 { return @@ -153,8 +153,17 @@ class ChattingView: ReusableViewFromXib, UITableViewDelegate, UITableViewDataSou self.chattingTableView.scrollToRow(at: IndexPath.init(row: self.messages.count - 1, section: 0), at: UITableViewScrollPosition.bottom, animated: false) } - // JC TODO: Create method that starts deletion timer for the last incoming message cell. - // The purpose is to start the timer countdown animation. + func startDeletionCountdown(timeInterval: TimeInterval) { + if chattingTableView.visibleCells.count <= 0 { + return + } + + guard let cell = chattingTableView.visibleCells.last as? IncomingUserMessageTableViewCell else { + return + } + + cell.startDeletionTimer(timeInterval: timeInterval) + } func scrollToPosition(position: Int) { if self.messages.count == 0 { diff --git a/sample-swift/SendBird-iOS/Chatting/ViewCell/IncomingUserMessageTableViewCell.swift b/sample-swift/SendBird-iOS/Chatting/ViewCell/IncomingUserMessageTableViewCell.swift index 77b3536..5d01a98 100644 --- a/sample-swift/SendBird-iOS/Chatting/ViewCell/IncomingUserMessageTableViewCell.swift +++ b/sample-swift/SendBird-iOS/Chatting/ViewCell/IncomingUserMessageTableViewCell.swift @@ -19,6 +19,7 @@ class IncomingUserMessageTableViewCell: UITableViewCell { @IBOutlet weak var messageLabel: UILabel! @IBOutlet weak var messageDateLabel: UILabel! @IBOutlet weak var messageContainerView: UIView! + @IBOutlet weak var deletionCountdownLabel: UILabel! @IBOutlet weak var dateLabelContainerHeight: NSLayoutConstraint! @IBOutlet weak var messageDateLabelWidth: NSLayoutConstraint! @@ -37,6 +38,9 @@ class IncomingUserMessageTableViewCell: UITableViewCell { private var message: SBDUserMessage! private var prevMessage: SBDBaseMessage! private var displayNickname: Bool = true + + var timer = Timer() + var deletionTime: TimeInterval = 30 static func nib() -> UINib { return UINib(nibName: String(describing: self), bundle: Bundle(for: self)) @@ -45,6 +49,10 @@ class IncomingUserMessageTableViewCell: UITableViewCell { static func cellReuseIdentifier() -> String { return String(describing: self) } + + override func awakeFromNib() { + deletionCountdownLabel.isHidden = true + } @objc private func clickProfileImage() { if self.delegate != nil { @@ -59,6 +67,24 @@ class IncomingUserMessageTableViewCell: UITableViewCell { self.delegate?.clickMessage(view: self, message: self.message!) } } + + func startDeletionTimer(timeInterval: TimeInterval) { + deletionCountdownLabel.isHidden = false + deletionCountdownLabel.text = String(Int(timeInterval)) + deletionTime = timeInterval + Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in + if self.deletionTime <= 0 { + timer.invalidate() + NotificationCenter.default.post(name: NSNotification.Name(rawValue: Constants.deleteMessage), + object: nil, + userInfo: ["message": self.message]) + return + } + + self.deletionTime -= 1 + self.deletionCountdownLabel.text = String(Int(self.deletionTime)) + } + } func setModel(aMessage: SBDUserMessage) { self.message = aMessage diff --git a/sample-swift/SendBird-iOS/Chatting/ViewCell/IncomingUserMessageTableViewCell.xib b/sample-swift/SendBird-iOS/Chatting/ViewCell/IncomingUserMessageTableViewCell.xib index 44b5776..f374e37 100644 --- a/sample-swift/SendBird-iOS/Chatting/ViewCell/IncomingUserMessageTableViewCell.xib +++ b/sample-swift/SendBird-iOS/Chatting/ViewCell/IncomingUserMessageTableViewCell.xib @@ -1,11 +1,11 @@ - + - + @@ -15,7 +15,7 @@ - + @@ -97,6 +97,16 @@ + + + + @@ -151,6 +164,7 @@ + diff --git a/sample-swift/SendBird-iOS/Common/Constants.swift b/sample-swift/SendBird-iOS/Common/Constants.swift index 75be64e..79dc102 100644 --- a/sample-swift/SendBird-iOS/Common/Constants.swift +++ b/sample-swift/SendBird-iOS/Common/Constants.swift @@ -9,6 +9,8 @@ import UIKit class Constants: NSObject { + static let deleteMessage = "deleteMessage" + static func navigationBarTitleColor() -> UIColor { return UIColor(red: 128.0/255.0, green: 90.0/255.0, blue: 255.0/255.0, alpha: 1) } diff --git a/sample-swift/SendBird-iOS/GroupChannel/SnapChatViewController.swift b/sample-swift/SendBird-iOS/GroupChannel/SnapChatViewController.swift index ef9a333..39eb3a8 100644 --- a/sample-swift/SendBird-iOS/GroupChannel/SnapChatViewController.swift +++ b/sample-swift/SendBird-iOS/GroupChannel/SnapChatViewController.swift @@ -8,10 +8,34 @@ class SnapChatViewController: GroupChannelChattingViewController, SBDChannelDele SBDMain.add(self as SBDChannelDelegate, identifier: self.delegateIdentifier) } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + NotificationCenter.default.addObserver(self, + selector: #selector(deleteMessage(notification:)), + name: NSNotification.Name(rawValue: Constants.deleteMessage), + object: nil) + } - // JC TODO: Start observing global notification to delete the message. - // This should happen after the countdown expires. + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + NotificationCenter.default.removeObserver(self) + } + + func deleteMessage(notification: Notification) { + guard let message = notification.userInfo?["message"] as? SBDBaseMessage else { + return + } + + groupChannel.delete(message) { (error) in + NSLog("message deleted") + } + } + let deletionTime: TimeInterval = 10 + // MARK: SBDChannelDelegate func channel(_ sender: SBDBaseChannel, didReceive message: SBDBaseMessage) { if sender == self.groupChannel { @@ -23,7 +47,8 @@ class SnapChatViewController: GroupChannelChattingViewController, SBDChannelDele self.chattingView.scrollToBottom(animated: true, force: false) } - // JC TODO: Message read by recipient, start timer to delete message + // To get the last message, use sender.lastMessage + chattingView.startDeletionCountdown(timeInterval: deletionTime) } } @@ -33,9 +58,16 @@ class SnapChatViewController: GroupChannelChattingViewController, SBDChannelDele self.chattingView.chattingTableView.reloadData() } - // JC TODO: Start a simultaneous timer as above for message deletion from the senders end. - // The reason is because there isn't a backend service to sync the deletion so - // timers have to be started on the client from both ends (sender and receiver + guard let lastMessage = sender.lastMessage else { + return + } + + // Start simultaneous timer for message deletion from senders end. + Timer.scheduledTimer(withTimeInterval: deletionTime, repeats: false, block: { (timer) in + sender.delete(lastMessage, completionHandler: { (error) in + NSLog("message deleted") + }) + }) } }