Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions sample-swift/SendBird-iOS/Chatting/ChattingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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))
Expand All @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -15,7 +15,7 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="245"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM">
<rect key="frame" x="0.0" y="0.0" width="320" height="244"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="245"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="etk-pH-YdC" userLabel="Date Container View">
Expand Down Expand Up @@ -97,6 +97,16 @@
<userDefinedRuntimeAttribute type="boolean" keyPath="layer.masksToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="30" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dsF-yr-ARN">
<rect key="frame" x="52" y="193.5" width="30" height="20"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="A3R-8G-L4z"/>
<constraint firstAttribute="width" constant="30" id="AzX-dw-LWo"/>
</constraints>
<fontDescription key="fontDescription" type="boldSystem" pointSize="16"/>
<color key="textColor" red="0.27439621185747887" green="0.45437489808887699" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="99:99 PM" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="da1-hz-pe6" userLabel="Message Date Label">
<rect key="frame" x="222" y="189.5" width="74" height="14"/>
<constraints>
Expand All @@ -111,11 +121,13 @@
<constraint firstItem="uXW-rK-eho" firstAttribute="top" secondItem="rQm-22-hdZ" secondAttribute="top" id="1s2-F2-g44"/>
<constraint firstItem="etk-pH-YdC" firstAttribute="centerX" secondItem="H2p-sc-9uM" secondAttribute="centerX" id="5rk-Ve-eXf"/>
<constraint firstItem="da1-hz-pe6" firstAttribute="leading" secondItem="uXW-rK-eho" secondAttribute="trailing" constant="8" id="Q8J-hr-Apx"/>
<constraint firstItem="dsF-yr-ARN" firstAttribute="leading" secondItem="uXW-rK-eho" secondAttribute="leading" constant="-10" id="TFw-1C-J9t"/>
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="da1-hz-pe6" secondAttribute="trailing" constant="24" id="WA7-oh-scj"/>
<constraint firstAttribute="trailing" secondItem="etk-pH-YdC" secondAttribute="trailing" id="X26-Op-hii"/>
<constraint firstItem="etk-pH-YdC" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="top" id="Y5E-PX-4j6"/>
<constraint firstItem="da1-hz-pe6" firstAttribute="bottom" secondItem="uXW-rK-eho" secondAttribute="bottom" id="aTe-sb-SmZ"/>
<constraint firstItem="uXW-rK-eho" firstAttribute="leading" secondItem="rQm-22-hdZ" secondAttribute="trailing" constant="8" id="aU7-lV-qKd"/>
<constraint firstItem="dsF-yr-ARN" firstAttribute="bottom" secondItem="uXW-rK-eho" secondAttribute="bottom" constant="10" id="bWI-g3-pRc"/>
<constraint firstItem="uXW-rK-eho" firstAttribute="top" secondItem="etk-pH-YdC" secondAttribute="bottom" constant="10" id="md4-fa-bgy"/>
<constraint firstItem="rQm-22-hdZ" firstAttribute="leading" secondItem="H2p-sc-9uM" secondAttribute="leading" constant="14" id="niF-WR-zOW"/>
<constraint firstItem="etk-pH-YdC" firstAttribute="leading" secondItem="H2p-sc-9uM" secondAttribute="leading" id="rsH-1F-YJd"/>
Expand All @@ -127,6 +139,7 @@
<outlet property="dateLabelContainerHeight" destination="lFp-Fb-0uI" id="gp3-yp-Zxh"/>
<outlet property="dateSeperatorContainerView" destination="etk-pH-YdC" id="BCQ-UP-c9L"/>
<outlet property="dateSeperatorLabel" destination="te8-sc-Jve" id="iss-MS-aSt"/>
<outlet property="deletionCountdownLabel" destination="dsF-yr-ARN" id="hHP-2f-Bby"/>
<outlet property="messageContainerBottomPadding" destination="Sjy-yz-TB8" id="sJR-rh-nWQ"/>
<outlet property="messageContainerLeftMargin" destination="aU7-lV-qKd" id="Vjy-a2-VH3"/>
<outlet property="messageContainerLeftPadding" destination="QOW-3C-7S8" id="xTV-fd-pyd"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class OutgoingUserMessageTableViewCell: UITableViewCell {
@IBOutlet weak var dateSeperatorLabel: UILabel!
@IBOutlet weak var messageContainerView: UIView!
@IBOutlet weak var sendStatusLabel: UILabel!

@IBOutlet weak var deletionCountdownLabel: UILabel!

@IBOutlet weak var dateContainerHeight: NSLayoutConstraint!
@IBOutlet weak var dateContainerTopMargin: NSLayoutConstraint!
@IBOutlet weak var dateContainerBottomMargin: NSLayoutConstraint!
Expand All @@ -37,7 +38,7 @@ class OutgoingUserMessageTableViewCell: UITableViewCell {

private var message: SBDUserMessage!
private var prevMessage: SBDBaseMessage!

static func nib() -> UINib {
return UINib(nibName: String(describing: self), bundle: Bundle(for: self))
}
Expand All @@ -46,12 +47,16 @@ class OutgoingUserMessageTableViewCell: UITableViewCell {
return String(describing: self)
}

override func awakeFromNib() {
deletionCountdownLabel.isHidden = true
}

@objc private func clickUserMessage() {
if self.delegate != nil {
self.delegate?.clickMessage(view: self, message: self.message!)
}
}

@objc private func clickResendUserMessage() {
if self.delegate != nil {
self.delegate?.clickResend(view: self, message: self.message!)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -14,7 +15,7 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="184"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM">
<rect key="frame" x="0.0" y="0.0" width="320" height="183"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="184"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Yru-DR-yL3" userLabel="Date Container View">
Expand Down Expand Up @@ -69,9 +70,21 @@
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="30" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="u8o-BF-8h0">
<rect key="frame" x="151" y="112" width="30" height="20"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="85C-fY-l6a"/>
<constraint firstAttribute="width" constant="30" id="aYt-IW-0Ko"/>
</constraints>
<fontDescription key="fontDescription" type="boldSystem" pointSize="12"/>
<color key="textColor" red="1" green="0.70644162975426239" blue="0.49991299404674783" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="0.50196078431372548" green="0.3529411764705882" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="bottom" secondItem="u8o-BF-8h0" secondAttribute="bottom" id="JaE-zQ-es3"/>
<constraint firstAttribute="trailing" secondItem="u8o-BF-8h0" secondAttribute="trailing" id="W2G-wB-27f"/>
<constraint firstAttribute="trailing" secondItem="MEf-kT-Jbd" secondAttribute="trailing" constant="10" id="Y0s-df-fjC"/>
<constraint firstAttribute="bottom" secondItem="MEf-kT-Jbd" secondAttribute="bottom" constant="10" id="kwH-qB-1dY"/>
<constraint firstItem="MEf-kT-Jbd" firstAttribute="leading" secondItem="218-qq-guS" secondAttribute="leading" constant="10" id="lCv-uc-7wR"/>
Expand Down Expand Up @@ -151,6 +164,7 @@
<outlet property="dateSeperatorContainerView" destination="Yru-DR-yL3" id="sfC-YG-P0Z"/>
<outlet property="dateSeperatorLabel" destination="Qzq-eO-w3c" id="ubX-f3-ICw"/>
<outlet property="deleteMessageButton" destination="Ud2-qG-Ln4" id="Eos-IF-6Jt"/>
<outlet property="deletionCountdownLabel" destination="u8o-BF-8h0" id="6En-sl-PmY"/>
<outlet property="messageContainerBottomPadding" destination="kwH-qB-1dY" id="RAg-hp-wVz"/>
<outlet property="messageContainerLeftMargin" destination="0kG-ca-LB9" id="G8D-aw-hDi"/>
<outlet property="messageContainerLeftPadding" destination="lCv-uc-7wR" id="xe6-VI-UVf"/>
Expand Down
2 changes: 2 additions & 0 deletions sample-swift/SendBird-iOS/Common/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand All @@ -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")
})
})
}
}

Expand Down