From 9ec00d2f44f8a1f5923d98f7c01b28b0896ede71 Mon Sep 17 00:00:00 2001 From: kevinzhow Date: Thu, 4 Jun 2015 08:12:01 +0800 Subject: [PATCH] Add send state sync and read state sync --- Yep/Services/FayeService.swift | 20 +++---- Yep/Services/YepService.swift | 2 + Yep/Services/YepServiceSync.swift | 26 +++++++++ .../ConversationViewController.swift | 1 + .../Cells/ChatLeftText/ChatLeftTextCell.swift | 2 +- .../ChatRightAudio/ChatRightAudioCell.xib | 4 +- .../ChatRightImage/ChatRightImageCell.xib | 4 +- .../ChatRightLocationCell.xib | 4 +- .../ChatRightText/ChatRightTextCell.swift | 55 ++++++++++++++++++- .../Cells/ChatRightText/ChatRightTextCell.xib | 4 +- .../ChatRightVideo/ChatRightVideoCell.xib | 4 +- 11 files changed, 104 insertions(+), 22 deletions(-) diff --git a/Yep/Services/FayeService.swift b/Yep/Services/FayeService.swift index 06bea611..0f94fd09 100644 --- a/Yep/Services/FayeService.swift +++ b/Yep/Services/FayeService.swift @@ -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: diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index 5aa55088..1fac5bd7 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -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) } }) diff --git a/Yep/Services/YepServiceSync.swift b/Yep/Services/YepServiceSync.swift index 2141681b..af469b43 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -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 { diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index d824073e..c6d80947 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -16,6 +16,7 @@ import MapKit struct MessageNotification { static let MessageSent = "MessageSentNotification" static let MessageRead = "MessageReadNotification" + static let MessageStateChanged = "MessageStateChangedNotification" } class ConversationViewController: BaseViewController { diff --git a/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift b/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift index a304416f..d586bde2 100644 --- a/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift +++ b/Yep/Views/Cells/ChatLeftText/ChatLeftTextCell.swift @@ -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 diff --git a/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.xib b/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.xib index 85cd8f92..90d8630a 100644 --- a/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.xib +++ b/Yep/Views/Cells/ChatRightAudio/ChatRightAudioCell.xib @@ -69,7 +69,7 @@ - + @@ -103,7 +103,7 @@ - + diff --git a/Yep/Views/Cells/ChatRightImage/ChatRightImageCell.xib b/Yep/Views/Cells/ChatRightImage/ChatRightImageCell.xib index 242e588e..09a76990 100644 --- a/Yep/Views/Cells/ChatRightImage/ChatRightImageCell.xib +++ b/Yep/Views/Cells/ChatRightImage/ChatRightImageCell.xib @@ -26,7 +26,7 @@ - + @@ -58,6 +58,6 @@ - + diff --git a/Yep/Views/Cells/ChatRightLocation/ChatRightLocationCell.xib b/Yep/Views/Cells/ChatRightLocation/ChatRightLocationCell.xib index 3c838124..7d4fb9a1 100644 --- a/Yep/Views/Cells/ChatRightLocation/ChatRightLocationCell.xib +++ b/Yep/Views/Cells/ChatRightLocation/ChatRightLocationCell.xib @@ -26,7 +26,7 @@ - + @@ -57,6 +57,6 @@ - + diff --git a/Yep/Views/Cells/ChatRightText/ChatRightTextCell.swift b/Yep/Views/Cells/ChatRightText/ChatRightTextCell.swift index 693fc727..621dfe57 100644 --- a/Yep/Views/Cells/ChatRightText/ChatRightTextCell.swift +++ b/Yep/Views/Cells/ChatRightText/ChatRightTextCell.swift @@ -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 { diff --git a/Yep/Views/Cells/ChatRightText/ChatRightTextCell.xib b/Yep/Views/Cells/ChatRightText/ChatRightTextCell.xib index 5e78083c..8c9abd97 100644 --- a/Yep/Views/Cells/ChatRightText/ChatRightTextCell.xib +++ b/Yep/Views/Cells/ChatRightText/ChatRightTextCell.xib @@ -42,7 +42,7 @@ Hello World - + @@ -87,6 +87,6 @@ Hello World - + diff --git a/Yep/Views/Cells/ChatRightVideo/ChatRightVideoCell.xib b/Yep/Views/Cells/ChatRightVideo/ChatRightVideoCell.xib index 7aa1ec93..8d88e649 100644 --- a/Yep/Views/Cells/ChatRightVideo/ChatRightVideoCell.xib +++ b/Yep/Views/Cells/ChatRightVideo/ChatRightVideoCell.xib @@ -29,7 +29,7 @@ - + @@ -64,7 +64,7 @@ - +