send text in group conversation

This commit is contained in:
nixzhu
2015-03-26 12:24:57 +08:00
parent 703288dbd0
commit d45e7c7650
@@ -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) {