mirror of
https://github.com/wahyd4/Yep.git
synced 2026-08-10 21:36:49 +10:00
Add send state sync and read state sync
This commit is contained in:
@@ -71,7 +71,8 @@ class FayeService: NSObject, MZFayeClientDelegate {
|
||||
client.setExtension(extensionData, forChannel: personalChannel)
|
||||
client.setExtension(extensionData, forChannel: "handshake")
|
||||
client.setExtension(extensionData, forChannel: "connect")
|
||||
|
||||
let realm = Realm()
|
||||
|
||||
client.subscribeToChannel(personalChannel, usingBlock: { data in
|
||||
// println("subscribeToChannel: \(data)")
|
||||
if let
|
||||
@@ -105,17 +106,16 @@ class FayeService: NSObject, MZFayeClientDelegate {
|
||||
if let
|
||||
recipientID = messageDataInfo["recipient_id"] as? String,
|
||||
messageID = messageDataInfo["id"] as? String {
|
||||
|
||||
println("Mark Message \(messageID) As Read")
|
||||
|
||||
// dispatch_async(dispatch_get_main_queue()) {
|
||||
//
|
||||
// var operation = MessageStateOperation(type: .Read, messageID: messageID)
|
||||
//
|
||||
// ConversationOperationQueue.sharedManager.addNewQperationQueue(operation)
|
||||
//
|
||||
// NSNotificationCenter.defaultCenter().postNotificationName(MessageNotification.MessageRead, object: nil)
|
||||
// }
|
||||
|
||||
if let message = messageWithMessageID(messageID, inRealm: realm) {
|
||||
realm.write {
|
||||
message.sendState = MessageSendState.Read.rawValue
|
||||
}
|
||||
|
||||
NSNotificationCenter.defaultCenter().postNotificationName(MessageNotification.MessageStateChanged, object: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -1211,6 +1211,8 @@ func sendMessageWithMediaType(mediaType: MessageMediaType, inFilePath filePath:
|
||||
message.messageID = messageID
|
||||
message.sendState = MessageSendState.Successed.rawValue
|
||||
realm.commitWrite()
|
||||
|
||||
NSNotificationCenter.defaultCenter().postNotificationName(MessageNotification.MessageStateChanged, object: nil)
|
||||
completion(success: true)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -791,6 +791,31 @@ func syncUnreadMessagesAndDoFurtherAction(furtherAction: () -> Void) {
|
||||
syncMessageWithMessageInfo(messageInfo, inRealm: realm, andDoFurtherAction: nil)
|
||||
}
|
||||
|
||||
var messages = realm.objects(Message)
|
||||
|
||||
realm.write {
|
||||
for oldMessage in messages {
|
||||
|
||||
if allUnreadMessages.count < 1 {
|
||||
if oldMessage.sendState == MessageSendState.Successed.rawValue {
|
||||
oldMessage.sendState = MessageSendState.Read.rawValue
|
||||
oldMessage.readed = true
|
||||
}
|
||||
} else {
|
||||
for messageInfo in allUnreadMessages {
|
||||
if let messageID = messageInfo["id"] as? String {
|
||||
if oldMessage.messageID != messageID && oldMessage.sendState == MessageSendState.Successed.rawValue {
|
||||
oldMessage.sendState = MessageSendState.Read.rawValue
|
||||
oldMessage.readed = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// do futher action
|
||||
println("加个打印,希望能等到 Realm 在线程间同步好")
|
||||
furtherAction()
|
||||
@@ -807,6 +832,7 @@ func syncMessageWithMessageInfo(messageInfo: JSONDictionary, inRealm realm: Real
|
||||
}
|
||||
|
||||
if let messageID = messageInfo["id"] as? String {
|
||||
|
||||
var message = messageWithMessageID(messageID, inRealm: realm)
|
||||
|
||||
if message == nil {
|
||||
|
||||
@@ -16,6 +16,7 @@ import MapKit
|
||||
struct MessageNotification {
|
||||
static let MessageSent = "MessageSentNotification"
|
||||
static let MessageRead = "MessageReadNotification"
|
||||
static let MessageStateChanged = "MessageStateChangedNotification"
|
||||
}
|
||||
|
||||
class ConversationViewController: BaseViewController {
|
||||
|
||||
@@ -57,7 +57,7 @@ class ChatLeftTextCell: UICollectionViewCell {
|
||||
|
||||
func configureWithMessage(message: Message, textContentLabelWidth: CGFloat) {
|
||||
textContentLabel.text = message.textContent
|
||||
|
||||
|
||||
textContentLabelWidthConstraint.constant = max(YepConfig.minMessageTextLabelWidth, textContentLabelWidth)
|
||||
textContentLabel.textAlignment = textContentLabelWidth < YepConfig.minMessageTextLabelWidth ? .Center : .Left
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@
|
||||
<constraint firstItem="ynj-aF-z4D" firstAttribute="leading" secondItem="XTQ-XN-AqX" secondAttribute="trailing" constant="5" id="uLL-Bt-Lsk"/>
|
||||
</constraints>
|
||||
</view>
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_dot_unread" translatesAutoresizingMaskIntoConstraints="NO" id="dkr-y8-xRJ" userLabel="Dot Image View">
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_dot_sending" translatesAutoresizingMaskIntoConstraints="NO" id="dkr-y8-xRJ" userLabel="Dot Image View">
|
||||
<rect key="frame" x="95" y="50" width="6" height="6"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="width" constant="6" id="DRh-dD-dg1"/>
|
||||
@@ -103,7 +103,7 @@
|
||||
</collectionViewCell>
|
||||
</objects>
|
||||
<resources>
|
||||
<image name="icon_dot_unread" width="6" height="6"/>
|
||||
<image name="icon_dot_sending" width="8" height="8"/>
|
||||
<image name="icon_play" width="30" height="30"/>
|
||||
<image name="right_tail_bubble" width="48" height="40"/>
|
||||
</resources>
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
<constraint firstAttribute="width" constant="151" id="A4j-a7-d3h"/>
|
||||
</constraints>
|
||||
</imageView>
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_dot_unread" translatesAutoresizingMaskIntoConstraints="NO" id="C9m-E2-m8h" userLabel="Dot Image View">
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_dot_sending" translatesAutoresizingMaskIntoConstraints="NO" id="C9m-E2-m8h" userLabel="Dot Image View">
|
||||
<rect key="frame" x="39" y="58" width="6" height="6"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="width" secondItem="C9m-E2-m8h" secondAttribute="height" multiplier="1:1" id="MYy-7Z-yQo"/>
|
||||
@@ -58,6 +58,6 @@
|
||||
</collectionViewCell>
|
||||
</objects>
|
||||
<resources>
|
||||
<image name="icon_dot_unread" width="6" height="6"/>
|
||||
<image name="icon_dot_sending" width="8" height="8"/>
|
||||
</resources>
|
||||
</document>
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
<constraint firstAttribute="width" constant="192" id="7Ua-Pv-tfQ"/>
|
||||
</constraints>
|
||||
</imageView>
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_dot_unread" translatesAutoresizingMaskIntoConstraints="NO" id="eTJ-7c-9qB" userLabel="Dot Image View">
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_dot_sending" translatesAutoresizingMaskIntoConstraints="NO" id="eTJ-7c-9qB" userLabel="Dot Image View">
|
||||
<rect key="frame" x="46" y="56" width="6" height="6"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="width" constant="6" id="2wj-AP-vhT"/>
|
||||
@@ -57,6 +57,6 @@
|
||||
</collectionViewCell>
|
||||
</objects>
|
||||
<resources>
|
||||
<image name="icon_dot_unread" width="6" height="6"/>
|
||||
<image name="icon_dot_sending" width="8" height="8"/>
|
||||
</resources>
|
||||
</document>
|
||||
|
||||
@@ -25,6 +25,12 @@ class ChatRightTextCell: UICollectionViewCell {
|
||||
@IBOutlet weak var textContentLabelTrailingConstraint: NSLayoutConstraint!
|
||||
@IBOutlet weak var textContentLabelLeadingConstraint: NSLayoutConstraint!
|
||||
@IBOutlet weak var textContentLabelWidthConstraint: NSLayoutConstraint!
|
||||
|
||||
private var messageStateChangeContent = 0
|
||||
|
||||
var messageData: Message!
|
||||
|
||||
let sendingAnimationName = "RotationOnStateAnimation"
|
||||
|
||||
override func awakeFromNib() {
|
||||
super.awakeFromNib()
|
||||
@@ -53,11 +59,20 @@ class ChatRightTextCell: UICollectionViewCell {
|
||||
bubbleTailImageView.tintColor = UIColor.rightBubbleTintColor()
|
||||
|
||||
gapBetweenDotImageViewAndBubbleConstraint.constant = YepConfig.ChatCell.gapBetweenDotImageViewAndBubble
|
||||
|
||||
NSNotificationCenter.defaultCenter().addObserver(self, selector: "messageStateUpdated", name: MessageNotification.MessageStateChanged, object: nil)
|
||||
}
|
||||
|
||||
func messageStateUpdated() {
|
||||
changeStateImage(messageData.sendState)
|
||||
}
|
||||
|
||||
func configureWithMessage(message: Message, textContentLabelWidth: CGFloat) {
|
||||
messageData = message
|
||||
|
||||
textContentLabel.text = message.textContent
|
||||
|
||||
changeStateImage(message.sendState)
|
||||
|
||||
textContentLabelWidthConstraint.constant = max(YepConfig.minMessageTextLabelWidth, textContentLabelWidth)
|
||||
textContentLabel.textAlignment = textContentLabelWidth < YepConfig.minMessageTextLabelWidth ? .Center : .Left
|
||||
|
||||
@@ -69,6 +84,44 @@ class ChatRightTextCell: UICollectionViewCell {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func changeStateImage(state: MessageSendState.RawValue) {
|
||||
switch state {
|
||||
case MessageSendState.NotSend.rawValue:
|
||||
dotImageView.hidden = false
|
||||
dotImageView.image = UIImage(named: "icon_dot_sending")
|
||||
rotationAnimationOnImageView()
|
||||
case MessageSendState.Successed.rawValue:
|
||||
dotImageView.hidden = false
|
||||
dotImageView.image = UIImage(named: "icon_dot_unread")
|
||||
removeSendingAnimation()
|
||||
case MessageSendState.Read.rawValue:
|
||||
removeSendingAnimation()
|
||||
dotImageView.hidden = true
|
||||
case MessageSendState.Failed.rawValue:
|
||||
removeSendingAnimation()
|
||||
dotImageView.hidden = true
|
||||
default:
|
||||
removeSendingAnimation()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func rotationAnimationOnImageView() {
|
||||
var animation = CABasicAnimation(keyPath: "transform.rotation.z")
|
||||
animation.fromValue = 0.0
|
||||
animation.toValue = 2*M_PI
|
||||
animation.duration = 3.0
|
||||
animation.repeatCount = MAXFLOAT
|
||||
dotImageView.layer.addAnimation(animation, forKey: sendingAnimationName)
|
||||
}
|
||||
|
||||
func removeSendingAnimation() {
|
||||
dotImageView.layer.removeAnimationForKey(sendingAnimationName)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
extension ChatRightTextCell: TTTAttributedLabelDelegate {
|
||||
|
||||
@@ -42,7 +42,7 @@ Hello World</string>
|
||||
<constraint firstAttribute="width" constant="40" id="hm6-ty-e5H"/>
|
||||
</constraints>
|
||||
</imageView>
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_dot_unread" translatesAutoresizingMaskIntoConstraints="NO" id="l93-41-FuF" userLabel="Dot Image View">
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_dot_sending" translatesAutoresizingMaskIntoConstraints="NO" id="l93-41-FuF" userLabel="Dot Image View">
|
||||
<rect key="frame" x="316" y="63" width="6" height="6"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="width" secondItem="l93-41-FuF" secondAttribute="height" multiplier="1:1" id="1c1-JW-Hdk"/>
|
||||
@@ -87,6 +87,6 @@ Hello World</string>
|
||||
<resources>
|
||||
<image name="bubble_body" width="42" height="40"/>
|
||||
<image name="bubble_right_tail" width="13" height="14"/>
|
||||
<image name="icon_dot_unread" width="6" height="6"/>
|
||||
<image name="icon_dot_sending" width="8" height="8"/>
|
||||
</resources>
|
||||
</document>
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_playvideo" translatesAutoresizingMaskIntoConstraints="NO" id="4Zc-y0-R7I" userLabel="Play Image View">
|
||||
<rect key="frame" x="111" y="43" width="30" height="30"/>
|
||||
</imageView>
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_dot_unread" translatesAutoresizingMaskIntoConstraints="NO" id="t6J-8M-Prm" userLabel="Dot Image View">
|
||||
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="icon_dot_sending" translatesAutoresizingMaskIntoConstraints="NO" id="t6J-8M-Prm" userLabel="Dot Image View">
|
||||
<rect key="frame" x="52" y="55" width="6" height="6"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="width" secondItem="t6J-8M-Prm" secondAttribute="height" multiplier="1:1" id="bga-IS-jlb"/>
|
||||
@@ -64,7 +64,7 @@
|
||||
</collectionViewCell>
|
||||
</objects>
|
||||
<resources>
|
||||
<image name="icon_dot_unread" width="6" height="6"/>
|
||||
<image name="icon_dot_sending" width="8" height="8"/>
|
||||
<image name="icon_playvideo" width="30" height="30"/>
|
||||
</resources>
|
||||
</document>
|
||||
|
||||
Reference in New Issue
Block a user