mirror of
https://github.com/wahyd4/Yep.git
synced 2026-08-18 17:25:59 +10:00
send text in group conversation
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user