From 7c327d8da18160992ff490ffe912b9145fee2da2 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Fri, 12 Jun 2015 12:35:37 +0800 Subject: [PATCH 1/7] fake messageID for SectionDate message --- Yep/Realm/Models.swift | 1 + Yep/Services/YepServiceSync.swift | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index a62eeb1e..95bd4eb7 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -399,6 +399,7 @@ func tryCreateSectionDateMessageInConversation(conversation: Conversation, befor // insert a new SectionDate Message let newSectionDateMessage = Message() + newSectionDateMessage.messageID = "sectionDate-" + NSUUID().UUIDString newSectionDateMessage.conversation = conversation newSectionDateMessage.mediaType = MessageMediaType.SectionDate.rawValue newSectionDateMessage.createdUnixTime = message.createdUnixTime - 1 // 比新消息早一秒 diff --git a/Yep/Services/YepServiceSync.swift b/Yep/Services/YepServiceSync.swift index ae0f5a0f..4c884c0e 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -965,10 +965,10 @@ func syncMessageWithMessageInfo(messageInfo: JSONDictionary, inRealm realm: Real message.conversation = conversation - var createdSectionDateMessage = false + var sectionDateMessageID: String? tryCreateSectionDateMessageInConversation(conversation, beforeMessage: message, inRealm: realm) { sectionDateMessage in realm.add(sectionDateMessage) - createdSectionDateMessage = true + sectionDateMessageID = sectionDateMessage.messageID } // 纪录消息的 detail 信息 @@ -1050,8 +1050,8 @@ func syncMessageWithMessageInfo(messageInfo: JSONDictionary, inRealm realm: Real // Do furtherAction after sync //println("syncMessageWithMessageInfo do furtherAction") - if createdSectionDateMessage { - furtherAction?(messageIDs: ["", messageID]) + if let sectionDateMessageID = sectionDateMessageID{ + furtherAction?(messageIDs: [sectionDateMessageID, messageID]) } else { furtherAction?(messageIDs: [messageID]) } From d42abfea20a4334a152dd941cf21ac6d11d6f042 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Fri, 12 Jun 2015 13:25:27 +0800 Subject: [PATCH 2/7] pop/push in conversationView will print "why?" multiple times when receive new message, VC not release --- .../Conversation/ConversationViewController.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 1ec3556a..5fcc8e80 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -910,12 +910,11 @@ class ConversationViewController: BaseViewController { let lastDisplayedMessagesRange = displayedMessagesRange displayedMessagesRange.length += newMessagesCount - - var indexPaths = [NSIndexPath]() - + if newMessagesCount > 0 { /* + //var indexPaths = [NSIndexPath]() // TODO: 下面插入逻辑的假设有问题,对方的新消息并不会一直排在最后一个 for i in 0.. Date: Fri, 12 Jun 2015 13:32:00 +0800 Subject: [PATCH 3/7] add [unowned self] for some closures --- Yep/ViewControllers/Contacts/ContactsViewController.swift | 4 ++-- .../Conversation/ConversationViewController.swift | 2 +- .../Conversations/ConversationsViewController.swift | 4 ++-- .../EditProfile/EditProfileViewController.swift | 4 ++-- Yep/ViewControllers/Profile/ProfileViewController.swift | 4 ++-- Yep/Views/Cells/ProfileHeader/ProfileHeaderCell.swift | 2 +- Yep/Views/Cells/SettingsUser/SettingsUserCell.swift | 6 +++--- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Yep/ViewControllers/Contacts/ContactsViewController.swift b/Yep/ViewControllers/Contacts/ContactsViewController.swift index 3d9991cd..1c8ee89f 100644 --- a/Yep/ViewControllers/Contacts/ContactsViewController.swift +++ b/Yep/ViewControllers/Contacts/ContactsViewController.swift @@ -34,11 +34,11 @@ class ContactsViewController: BaseViewController { contactsTableView.rowHeight = 80 contactsTableView.tableFooterView = UIView() - YepUserDefaults.nickname.bindListener("ContactsViewController.Nickname") { _ in + YepUserDefaults.nickname.bindListener("ContactsViewController.Nickname") { [unowned self] _ in self.updateContactsTableView() } - YepUserDefaults.avatarURLString.bindListener("ContactsViewController.Avatar") { _ in + YepUserDefaults.avatarURLString.bindListener("ContactsViewController.Avatar") { [unowned self] _ in self.updateContactsTableView() } } diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 5fcc8e80..de4baab5 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -240,7 +240,7 @@ class ConversationViewController: BaseViewController { NSNotificationCenter.defaultCenter().addObserver(self, selector: "cleanForLogout", name: EditProfileViewController.Notification.Logout, object: nil) - YepUserDefaults.avatarURLString.bindListener("ConversationViewController") { _ in + YepUserDefaults.avatarURLString.bindListener("ConversationViewController") { [unowned self] _ in self.reloadConversationCollectionView() } diff --git a/Yep/ViewControllers/Conversations/ConversationsViewController.swift b/Yep/ViewControllers/Conversations/ConversationsViewController.swift index ad5a35d9..b2edab2c 100644 --- a/Yep/ViewControllers/Conversations/ConversationsViewController.swift +++ b/Yep/ViewControllers/Conversations/ConversationsViewController.swift @@ -51,11 +51,11 @@ class ConversationsViewController: UIViewController { NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadConversationsTableView", name: YepNewMessagesReceivedNotification, object: nil) - YepUserDefaults.nickname.bindListener("ConversationsViewController.Nickname") { _ in + YepUserDefaults.nickname.bindListener("ConversationsViewController.Nickname") { [unowned self] _ in self.reloadConversationsTableView() } - YepUserDefaults.avatarURLString.bindListener("ConversationsViewController.Avatar") { _ in + YepUserDefaults.avatarURLString.bindListener("ConversationsViewController.Avatar") { [unowned self] _ in self.reloadConversationsTableView() } diff --git a/Yep/ViewControllers/EditProfile/EditProfileViewController.swift b/Yep/ViewControllers/EditProfile/EditProfileViewController.swift index 09ad18d1..970bd836 100644 --- a/Yep/ViewControllers/EditProfile/EditProfileViewController.swift +++ b/Yep/ViewControllers/EditProfile/EditProfileViewController.swift @@ -154,7 +154,7 @@ extension EditProfileViewController: UITableViewDataSource, UITableViewDelegate cell.annotationLabel.text = NSLocalizedString("Nickname", comment: "") - YepUserDefaults.nickname.bindAndFireListener("EditProfileLessInfoCell.Nickname") { nickname in + YepUserDefaults.nickname.bindAndFireListener("EditProfileLessInfoCell.Nickname") { [unowned self] nickname in cell.infoLabel.text = nickname } @@ -165,7 +165,7 @@ extension EditProfileViewController: UITableViewDataSource, UITableViewDelegate cell.annotationLabel.text = NSLocalizedString("Introduction", comment: "") - YepUserDefaults.introduction.bindAndFireListener("EditProfileLessInfoCell.Introduction") { introduction in + YepUserDefaults.introduction.bindAndFireListener("EditProfileLessInfoCell.Introduction") { [unowned self] introduction in cell.infoLabel.text = introduction } diff --git a/Yep/ViewControllers/Profile/ProfileViewController.swift b/Yep/ViewControllers/Profile/ProfileViewController.swift index 62ec8a97..8bcde3e6 100644 --- a/Yep/ViewControllers/Profile/ProfileViewController.swift +++ b/Yep/ViewControllers/Profile/ProfileViewController.swift @@ -191,7 +191,7 @@ class ProfileViewController: UIViewController { } if user.friendState == UserFriendState.Me.rawValue { - YepUserDefaults.introduction.bindListener("Profile.introductionText") { introduction in + YepUserDefaults.introduction.bindListener("Profile.introductionText") { [unowned self] introduction in if let introduction = introduction { self.introductionText = introduction self.updateProfileCollectionView() @@ -358,7 +358,7 @@ class ProfileViewController: UIViewController { customNavigationItem.title = user.nickname if user.friendState == UserFriendState.Me.rawValue { - YepUserDefaults.nickname.bindListener("ProfileViewController.Title") { nickname in + YepUserDefaults.nickname.bindListener("ProfileViewController.Title") { [unowned self] nickname in self.customNavigationItem.title = nickname } } diff --git a/Yep/Views/Cells/ProfileHeader/ProfileHeaderCell.swift b/Yep/Views/Cells/ProfileHeader/ProfileHeaderCell.swift index f051984c..fe0f4876 100644 --- a/Yep/Views/Cells/ProfileHeader/ProfileHeaderCell.swift +++ b/Yep/Views/Cells/ProfileHeader/ProfileHeaderCell.swift @@ -59,7 +59,7 @@ class ProfileHeaderCell: UICollectionViewCell { updateAvatarWithAvatarURLString(user.avatarURLString) if user.friendState == UserFriendState.Me.rawValue { - YepUserDefaults.avatarURLString.bindListener("ProfileHeaderCell.Avatar") { avatarURLString in + YepUserDefaults.avatarURLString.bindListener("ProfileHeaderCell.Avatar") { [unowned self] avatarURLString in if let avatarURLString = avatarURLString { self.updateAvatarWithAvatarURLString(avatarURLString) } diff --git a/Yep/Views/Cells/SettingsUser/SettingsUserCell.swift b/Yep/Views/Cells/SettingsUser/SettingsUserCell.swift index 26b33790..4cd8ae83 100644 --- a/Yep/Views/Cells/SettingsUser/SettingsUserCell.swift +++ b/Yep/Views/Cells/SettingsUser/SettingsUserCell.swift @@ -30,15 +30,15 @@ class SettingsUserCell: UITableViewCell { let avatarSize = YepConfig.Settings.userCellAvatarSize avatarImageViewWidthConstraint.constant = avatarSize - YepUserDefaults.avatarURLString.bindAndFireListener("SettingsUserCell.Avatar") { _ in + YepUserDefaults.avatarURLString.bindAndFireListener("SettingsUserCell.Avatar") { [unowned self] _ in self.updateAvatar() } - YepUserDefaults.nickname.bindAndFireListener("SettingsUserCell.Nickname") { nickname in + YepUserDefaults.nickname.bindAndFireListener("SettingsUserCell.Nickname") { [unowned self] nickname in self.nameLabel.text = nickname } - YepUserDefaults.introduction.bindAndFireListener("SettingsUserCell.Introduction") { introduction in + YepUserDefaults.introduction.bindAndFireListener("SettingsUserCell.Introduction") { [unowned self] introduction in self.introLabel.text = introduction } From 946439a4f7b9354679e1face748c004b1790408d Mon Sep 17 00:00:00 2001 From: nixzhu Date: Fri, 12 Jun 2015 13:48:30 +0800 Subject: [PATCH 4/7] more [unowned self] --- .../ConversationViewController.swift | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index de4baab5..617aeaa1 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -271,7 +271,7 @@ class ConversationViewController: BaseViewController { lastTimeMessagesCount = messages.count - messageToolbar.textSendAction = { messageToolbar in + messageToolbar.textSendAction = { [unowned self] messageToolbar in let text = messageToolbar.messageTextView.text! self.cleanTextInput() @@ -294,10 +294,10 @@ class ConversationViewController: BaseViewController { }) } else if let withGroup = self.conversation.withGroup { - sendText(text, toRecipient: withGroup.groupID, recipientType: "Circle", afterCreatedMessage: { message in + sendText(text, toRecipient: withGroup.groupID, recipientType: "Circle", afterCreatedMessage: { [unowned self] message in dispatch_async(dispatch_get_main_queue()) { - self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { success in + self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { _ in }) } @@ -319,7 +319,7 @@ class ConversationViewController: BaseViewController { //println("Update waver") audioRecorder.updateMeters() - var normalizedValue = pow(10, audioRecorder.averagePowerForChannel(0)/40) + let normalizedValue = pow(10, audioRecorder.averagePowerForChannel(0)/40) waver.level = CGFloat(normalizedValue) } @@ -328,7 +328,7 @@ class ConversationViewController: BaseViewController { // MARK: Audio Send - messageToolbar.voiceSendBeginAction = { messageToolbar in + messageToolbar.voiceSendBeginAction = { [unowned self] messageToolbar in self.view.addSubview(self.waverView) self.swipeUpView.hidden = false self.view.bringSubviewToFront(self.swipeUpView) @@ -351,13 +351,13 @@ class ConversationViewController: BaseViewController { } } - messageToolbar.voiceSendCancelAction = { messageToolbar in + messageToolbar.voiceSendCancelAction = { [unowned self] messageToolbar in self.swipeUpView.hidden = true self.waverView.removeFromSuperview() YepAudioService.sharedManager.endRecord() } - messageToolbar.voiceSendEndAction = { messageToolbar in + messageToolbar.voiceSendEndAction = { [unowned self] messageToolbar in self.swipeUpView.hidden = true self.waverView.removeFromSuperview() if YepAudioService.sharedManager.audioRecorder?.currentTime < 0.5 { @@ -398,7 +398,7 @@ class ConversationViewController: BaseViewController { if let fileURL = YepAudioService.sharedManager.audioFileURL { if let withFriend = self.conversation.withFriend { - sendAudioInFilePath(fileURL.path!, orFileData: nil, metaData: metaData, toRecipient: withFriend.userID, recipientType: "User", afterCreatedMessage: { message -> Void in + sendAudioInFilePath(fileURL.path!, orFileData: nil, metaData: metaData, toRecipient: withFriend.userID, recipientType: "User", afterCreatedMessage: { [unowned self] message in dispatch_async(dispatch_get_main_queue()) { if let realm = message.realm { @@ -410,7 +410,7 @@ class ConversationViewController: BaseViewController { } realm.commitWrite() - self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { success in + self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { _ in }) } } @@ -424,7 +424,7 @@ class ConversationViewController: BaseViewController { }) } else if let withGroup = self.conversation.withGroup { - sendAudioInFilePath(fileURL.path!, orFileData: nil, metaData: metaData, toRecipient: withGroup.groupID, recipientType: "Circle", afterCreatedMessage: { (message) -> Void in + sendAudioInFilePath(fileURL.path!, orFileData: nil, metaData: metaData, toRecipient: withGroup.groupID, recipientType: "Circle", afterCreatedMessage: { [unowned self] message in dispatch_async(dispatch_get_main_queue()) { if let realm = message.realm { @@ -436,7 +436,7 @@ class ConversationViewController: BaseViewController { } realm.commitWrite() - self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { success in + self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { _ in }) } } @@ -454,7 +454,7 @@ class ConversationViewController: BaseViewController { // MARK: MessageToolbar State Transitions - messageToolbar.stateTransitionAction = { (messageToolbar, previousState, currentState) in + messageToolbar.stateTransitionAction = { [unowned self] (messageToolbar, previousState, currentState) in switch (previousState, currentState) { @@ -884,7 +884,7 @@ class ConversationViewController: BaseViewController { messageIDs = _messageIDs } - updateConversationCollectionViewWithMessageIDs(messageIDs, scrollToBottom: false, success: { success in + updateConversationCollectionViewWithMessageIDs(messageIDs, scrollToBottom: false, success: { _ in }) } @@ -1001,7 +1001,7 @@ class ConversationViewController: BaseViewController { if (totleMessagesHeight > conversationCollectionView.frame.size.height) { // println("New Message scroll") - UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in + UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { [unowned self] in if (useableSpace > 0) { let contentToScroll = newMessagesTotalHeight - useableSpace @@ -1140,7 +1140,7 @@ class ConversationViewController: BaseViewController { conversationCollectionViewContentOffsetBeforeKeyboardWillShow = conversationCollectionView.contentOffset } - UIView.animateWithDuration(animationDuration, delay: 0, options: UIViewAnimationOptions(animationCurveValue << 16), animations: { () -> Void in + UIView.animateWithDuration(animationDuration, delay: 0, options: UIViewAnimationOptions(animationCurveValue << 16), animations: { [unowned self] in self.messageToolbarBottomConstraint.constant = newHeight @@ -1180,7 +1180,7 @@ class ConversationViewController: BaseViewController { self.view.layoutIfNeeded() - }, completion: { (finished) -> Void in + }, completion: { [unowned self] finished in if keyboard { self.keyboardShowTimes -= 1 } @@ -1207,7 +1207,7 @@ class ConversationViewController: BaseViewController { conversationCollectionViewContentOffsetBeforeKeyboardWillHide = conversationCollectionView.contentOffset } - UIView.animateWithDuration(animationDuration, delay: 0, options: UIViewAnimationOptions(animationCurveValue << 16), animations: { () -> Void in + UIView.animateWithDuration(animationDuration, delay: 0, options: UIViewAnimationOptions(animationCurveValue << 16), animations: { [unowned self] in var contentOffset = self.conversationCollectionViewContentOffsetBeforeKeyboardWillHide @@ -1226,7 +1226,7 @@ class ConversationViewController: BaseViewController { self.view.layoutIfNeeded() - }, completion: { (finished) -> Void in + }, completion: { _ in }) } @@ -1402,14 +1402,14 @@ class ConversationViewController: BaseViewController { let nvc = segue.destinationViewController as! UINavigationController let vc = nvc.topViewController as! PickLocationViewController - vc.sendLocationAction = { coordinate in + vc.sendLocationAction = { [unowned self] coordinate in if let withFriend = self.conversation.withFriend { sendLocationWithCoordinate(coordinate, toRecipient: withFriend.userID, recipientType: "User", afterCreatedMessage: { message in dispatch_async(dispatch_get_main_queue()) { - self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { success in + self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { _ in }) } @@ -1425,7 +1425,7 @@ class ConversationViewController: BaseViewController { sendLocationWithCoordinate(coordinate, toRecipient: withGroup.groupID, recipientType: "Circle", afterCreatedMessage: { message in dispatch_async(dispatch_get_main_queue()) { - self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { success in + self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { _ in }) } @@ -1507,7 +1507,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi case MessageMediaType.Image.rawValue: let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatLeftImageCellIdentifier, forIndexPath: indexPath) as! ChatLeftImageCell - cell.configureWithMessage(message, messageImagePreferredWidth: messageImagePreferredWidth, messageImagePreferredHeight: messageImagePreferredHeight, messageImagePreferredAspectRatio: messageImagePreferredAspectRatio, mediaTapAction: { + cell.configureWithMessage(message, messageImagePreferredWidth: messageImagePreferredWidth, messageImagePreferredHeight: messageImagePreferredHeight, messageImagePreferredAspectRatio: messageImagePreferredAspectRatio, mediaTapAction: { [unowned self] in self.performSegueWithIdentifier("showMessageMedia", sender: message) @@ -1520,7 +1520,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi let audioPlayedDuration = audioPlayedDurationOfMessage(message) - cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration, audioBubbleTapAction: { message in + cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration, audioBubbleTapAction: { [unowned self] message in self.playMessageAudioWithMessage(message) @@ -1531,7 +1531,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi case MessageMediaType.Video.rawValue: let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatLeftVideoCellIdentifier, forIndexPath: indexPath) as! ChatLeftVideoCell - cell.configureWithMessage(message, messageImagePreferredWidth: messageImagePreferredWidth, messageImagePreferredHeight: messageImagePreferredHeight, messageImagePreferredAspectRatio: messageImagePreferredAspectRatio, mediaTapAction: { + cell.configureWithMessage(message, messageImagePreferredWidth: messageImagePreferredWidth, messageImagePreferredHeight: messageImagePreferredHeight, messageImagePreferredAspectRatio: messageImagePreferredAspectRatio, mediaTapAction: { [unowned self] in self.performSegueWithIdentifier("showMessageMedia", sender: message) @@ -1542,7 +1542,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi case MessageMediaType.Location.rawValue: let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatLeftLocationCellIdentifier, forIndexPath: indexPath) as! ChatLeftLocationCell - cell.configureWithMessage(message, mediaTapAction: { + cell.configureWithMessage(message, mediaTapAction: { [unowned self] in if let coordinate = message.coordinate { let locationCoordinate = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude) let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: locationCoordinate, addressDictionary: nil)) @@ -1571,7 +1571,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi case MessageMediaType.Image.rawValue: let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatRightImageCellIdentifier, forIndexPath: indexPath) as! ChatRightImageCell - cell.configureWithMessage(message, messageImagePreferredWidth: messageImagePreferredWidth, messageImagePreferredHeight: messageImagePreferredHeight, messageImagePreferredAspectRatio: messageImagePreferredAspectRatio, mediaTapAction: { + cell.configureWithMessage(message, messageImagePreferredWidth: messageImagePreferredWidth, messageImagePreferredHeight: messageImagePreferredHeight, messageImagePreferredAspectRatio: messageImagePreferredAspectRatio, mediaTapAction: { [unowned self] in if message.sendState == MessageSendState.Failed.rawValue { @@ -1601,7 +1601,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi let audioPlayedDuration = audioPlayedDurationOfMessage(message) - cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration, audioBubbleTapAction: { message in + cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration, audioBubbleTapAction: { [unowned self] message in if let message = message { if message.sendState == MessageSendState.Failed.rawValue { @@ -1632,7 +1632,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi case MessageMediaType.Video.rawValue: let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatRightVideoCellIdentifier, forIndexPath: indexPath) as! ChatRightVideoCell - cell.configureWithMessage(message, messageImagePreferredWidth: messageImagePreferredWidth, messageImagePreferredHeight: messageImagePreferredHeight, messageImagePreferredAspectRatio: messageImagePreferredAspectRatio, mediaTapAction: { + cell.configureWithMessage(message, messageImagePreferredWidth: messageImagePreferredWidth, messageImagePreferredHeight: messageImagePreferredHeight, messageImagePreferredAspectRatio: messageImagePreferredAspectRatio, mediaTapAction: { [unowned self] in if message.sendState == MessageSendState.Failed.rawValue { @@ -1660,7 +1660,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi case MessageMediaType.Location.rawValue: let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatRightLocationCellIdentifier, forIndexPath: indexPath) as! ChatRightLocationCell - cell.configureWithMessage(message, mediaTapAction: { + cell.configureWithMessage(message, mediaTapAction: { [unowned self] in if message.sendState == MessageSendState.Failed.rawValue { @@ -1696,7 +1696,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi default: let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatRightTextCellIdentifier, forIndexPath: indexPath) as! ChatRightTextCell - cell.configureWithMessage(message, textContentLabelWidth: textContentLabelWidthOfMessage(message), mediaTapAction: { + cell.configureWithMessage(message, textContentLabelWidth: textContentLabelWidthOfMessage(message), mediaTapAction: { [unowned self] in if message.sendState == MessageSendState.Failed.rawValue { @@ -1810,7 +1810,7 @@ extension ConversationViewController: PullToRefreshViewDelegate { func pulllToRefreshViewDidRefresh(pulllToRefreshView: PullToRefreshView) { delay(1) { - pulllToRefreshView.endRefreshingAndDoFurtherAction() { + pulllToRefreshView.endRefreshingAndDoFurtherAction() { [unowned self] in let lastDisplayedMessagesRange = self.displayedMessagesRange @@ -1958,7 +1958,7 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat if let withFriend = conversation.withFriend { - sendImageInFilePath(nil, orFileData: imageData, metaData: metaData, toRecipient: withFriend.userID, recipientType: "User", afterCreatedMessage: { message -> Void in + sendImageInFilePath(nil, orFileData: imageData, metaData: metaData, toRecipient: withFriend.userID, recipientType: "User", afterCreatedMessage: { [unowned self] message in dispatch_async(dispatch_get_main_queue()) { @@ -1974,7 +1974,7 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat } } - self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { success in + self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { _ in }) } @@ -1987,7 +1987,7 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat }) } else if let withGroup = conversation.withGroup { - sendImageInFilePath(nil, orFileData: imageData, metaData: nil, toRecipient: withGroup.groupID, recipientType: "Circle", afterCreatedMessage: { message -> Void in + sendImageInFilePath(nil, orFileData: imageData, metaData: nil, toRecipient: withGroup.groupID, recipientType: "Circle", afterCreatedMessage: { [unowned self] message in dispatch_async(dispatch_get_main_queue()) { if let messageImageURL = NSFileManager.saveMessageImageData(imageData, withName: messageImageName) { @@ -2002,7 +2002,7 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat } } - self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { success in + self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { _ in }) } @@ -2037,7 +2037,7 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat let messageVideoName = NSUUID().UUIDString - let afterCreatedMessageAction = { (message: Message) in + let afterCreatedMessageAction = { [unowned self] (message: Message) in dispatch_async(dispatch_get_main_queue()) { if let videoData = NSData(contentsOfURL: videoURL) { @@ -2062,27 +2062,27 @@ extension ConversationViewController: UIImagePickerControllerDelegate, UINavigat } } - self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { success in + self.updateConversationCollectionViewWithMessageIDs(nil, scrollToBottom: true, success: { _ in }) } } } if let withFriend = conversation.withFriend { - sendVideoInFilePath(videoURL.path!, orFileData: nil, metaData: metaData, toRecipient: withFriend.userID, recipientType: "User", afterCreatedMessage: afterCreatedMessageAction, failureHandler: {(reason, errorMessage) -> () in + sendVideoInFilePath(videoURL.path!, orFileData: nil, metaData: metaData, toRecipient: withFriend.userID, recipientType: "User", afterCreatedMessage: afterCreatedMessageAction, failureHandler: { (reason, errorMessage) in defaultFailureHandler(reason, errorMessage) // TODO: sendVideo 错误提醒 - }, completion: { success -> Void in + }, completion: { success in println("sendVideo to friend: \(success)") }) } else if let withGroup = conversation.withGroup { - sendVideoInFilePath(videoURL.path!, orFileData: nil, metaData: nil, toRecipient: withGroup.groupID, recipientType: "Circle", afterCreatedMessage: afterCreatedMessageAction, failureHandler: {(reason, errorMessage) -> () in + sendVideoInFilePath(videoURL.path!, orFileData: nil, metaData: nil, toRecipient: withGroup.groupID, recipientType: "Circle", afterCreatedMessage: afterCreatedMessageAction, failureHandler: { (reason, errorMessage) in defaultFailureHandler(reason, errorMessage) // TODO: sendVideo 错误提醒 - }, completion: { success -> Void in + }, completion: { success in println("sendVideo to group: \(success)") }) } From 2467f3bcc8a0471dfe87f102b691ea56ca3abb64 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Fri, 12 Jun 2015 15:11:11 +0800 Subject: [PATCH 5/7] better update conversation UI logic for self message --- .../ConversationViewController.swift | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 617aeaa1..474d8d2a 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -934,36 +934,35 @@ class ConversationViewController: BaseViewController { if let messageIDs = messageIDs { - if messageIDs.filter({ $0.isEmpty }).count > 0 { - println("have sectionDate") - conversationCollectionView.reloadData() + var indexPaths = [NSIndexPath]() - } else { + for messageID in messageIDs { + if let + message = messageWithMessageID(messageID, inRealm: realm), + index = messages.indexOf(message), + indexPath = NSIndexPath(forItem: index - displayedMessagesRange.location, inSection: 0) { + println("insert item: \(indexPath.item)") - println("why?") + indexPaths.append(indexPath) - var indexPaths = [NSIndexPath]() - - for messageID in messageIDs { - if let - message = messageWithMessageID(messageID, inRealm: realm), - index = messages.indexOf(message), - indexPath = NSIndexPath(forItem: index - displayedMessagesRange.location, inSection: 0) { - println("insert item: \(indexPath.item)") - - indexPaths.append(indexPath) - - } else { - println("unknown message") - } + } else { + println("unknown message") } - - conversationCollectionView.insertItemsAtIndexPaths(indexPaths) } + conversationCollectionView.insertItemsAtIndexPaths(indexPaths) + } else { println("self message") - conversationCollectionView.reloadData() + + var indexPaths = [NSIndexPath]() + + for i in 0.. Date: Fri, 12 Jun 2015 15:14:40 +0800 Subject: [PATCH 6/7] do not updateConversationCollectionViewWithMessageIDs when this VC is not topViewController --- .../Conversation/ConversationViewController.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 474d8d2a..d73f4256 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -889,10 +889,14 @@ class ConversationViewController: BaseViewController { } func updateConversationCollectionViewWithMessageIDs(messageIDs: [String]?, scrollToBottom: Bool, success: (Bool) -> Void) { - let keyboardAndToolBarHeight = messageToolbarBottomConstraint.constant + CGRectGetHeight(messageToolbar.bounds) - adjustConversationCollectionViewWithMessageIDs(messageIDs, adjustHeight: keyboardAndToolBarHeight, scrollToBottom: scrollToBottom) { finished in - success(finished) + if navigationController?.topViewController == self { // 防止 pop/push 后,原来未释放的 VC 也执行这下面的代码 + + let keyboardAndToolBarHeight = messageToolbarBottomConstraint.constant + CGRectGetHeight(messageToolbar.bounds) + + adjustConversationCollectionViewWithMessageIDs(messageIDs, adjustHeight: keyboardAndToolBarHeight, scrollToBottom: scrollToBottom) { finished in + success(finished) + } } } From b8f274320cf51822250f9c9fba02ddf8a41d7487 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Fri, 12 Jun 2015 15:21:44 +0800 Subject: [PATCH 7/7] better fake messageID for SectionDate message --- Yep/Realm/Models.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index 95bd4eb7..210b6883 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -399,10 +399,10 @@ func tryCreateSectionDateMessageInConversation(conversation: Conversation, befor // insert a new SectionDate Message let newSectionDateMessage = Message() - newSectionDateMessage.messageID = "sectionDate-" + NSUUID().UUIDString newSectionDateMessage.conversation = conversation newSectionDateMessage.mediaType = MessageMediaType.SectionDate.rawValue newSectionDateMessage.createdUnixTime = message.createdUnixTime - 1 // 比新消息早一秒 + newSectionDateMessage.messageID = "sectionDate-\(newSectionDateMessage.createdUnixTime)" success(newSectionDateMessage) }