From d45e7c76502bac828d2b331ab5f79bd44859dc5f Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 26 Mar 2015 12:24:57 +0800 Subject: [PATCH] send text in group conversation --- .../ConversationViewController.swift | 79 ++++++++++++------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index a942f2c0..c68d86e6 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -70,39 +70,33 @@ class ConversationViewController: UIViewController { messageToolbar.textSendAction = { messageToolbar in let text = messageToolbar.messageTextField.text! - sendText(text, toRecipient: self.conversation.withFriend!.userID, recipientType: "User", failureHandler: { (reason, errorMessage) -> () in - defaultFailureHandler(reason, errorMessage) - // TODO: sendText 错误提醒 + if let withFriend = self.conversation.withFriend { + sendText(text, toRecipient: withFriend.userID, recipientType: "User", failureHandler: { (reason, errorMessage) -> () in + defaultFailureHandler(reason, errorMessage) + // TODO: sendText 错误提醒 - }, completion: { success -> Void in - println("sendText: \(success)") - - // 完成后,Message 就插入数据库了,下面插入 Cell 才有可能 - dispatch_async(dispatch_get_main_queue()) { - // 先重新准备 Layout - let layout = self.conversationCollectionView.collectionViewLayout as! ConversationLayout - layout.needUpdate = true + }, completion: { success -> Void in + println("sendText: \(success)") - // 再插入 Cell - let newMessageIndexPath = NSIndexPath(forItem: self.conversation.messages.count - 1, inSection: 0) - self.conversationCollectionView.insertItemsAtIndexPaths([newMessageIndexPath]) + // 完成后,Message 就插入数据库了,下面插入 Cell 才有可能 + dispatch_async(dispatch_get_main_queue()) { + self.updateUIAfterSentText(text) + } + }) + } else if let withGroup = self.conversation.withGroup { + sendText(text, toRecipient: withGroup.groupID, recipientType: "Circle", failureHandler: { (reason, errorMessage) -> () in + defaultFailureHandler(reason, errorMessage) + // TODO: sendText 错误提醒 - UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in - // TODO: 不使用魔法数字 - let rect = text.boundingRectWithSize(CGSize(width: self.messageTextLabelMaxWidth, height: CGFloat(FLT_MAX)), options: .UsesLineFragmentOrigin | .UsesFontLeading, attributes: self.messageTextAttributes, context: nil) + }, completion: { success -> Void in + println("sendText: \(success)") - let height = max(rect.height + 14 + 20, 40 + 20) + 10 - self.conversationCollectionView.contentOffset.y += height - - }, completion: { (finished) -> Void in - - }) - - // Clean - self.messageToolbar.messageTextField.text = "" - self.messageToolbar.state = .Default - } - }) + // 完成后,Message 就插入数据库了,下面插入 Cell 才有可能 + dispatch_async(dispatch_get_main_queue()) { + self.updateUIAfterSentText(text) + } + }) + } } } @@ -118,6 +112,33 @@ class ConversationViewController: UIViewController { setConversaitonCollectionViewOriginalContentInsetBottom(messageToolbar.intrinsicContentSize().height) } + // MARK: Actions + + func updateUIAfterSentText(text: String) { + // 先重新准备 Layout + let layout = self.conversationCollectionView.collectionViewLayout as! ConversationLayout + layout.needUpdate = true + + // 再插入 Cell + let newMessageIndexPath = NSIndexPath(forItem: self.conversation.messages.count - 1, inSection: 0) + self.conversationCollectionView.insertItemsAtIndexPaths([newMessageIndexPath]) + + UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in + // TODO: 不使用魔法数字 + let rect = text.boundingRectWithSize(CGSize(width: self.messageTextLabelMaxWidth, height: CGFloat(FLT_MAX)), options: .UsesLineFragmentOrigin | .UsesFontLeading, attributes: self.messageTextAttributes, context: nil) + + let height = max(rect.height + 14 + 20, 40 + 20) + 10 + self.conversationCollectionView.contentOffset.y += height + + }, completion: { (finished) -> Void in + + }) + + // Clean + self.messageToolbar.messageTextField.text = "" + self.messageToolbar.state = .Default + } + // MARK: Keyboard func handleKeyboardWillShowNotification(notification: NSNotification) {