From 57894e8d5458d3e09fc1004fea0cc3083985a1da Mon Sep 17 00:00:00 2001 From: kevinzhow Date: Tue, 21 Apr 2015 19:48:35 +0800 Subject: [PATCH 1/3] Fix scroll to bottom bug --- .../Conversation/ConversationViewController.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index e1b2c454..1596c937 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -18,7 +18,7 @@ class ConversationViewController: UIViewController { return messagesInConversation(self.conversation) }() - let messagesBunchCount = 12 // TODO: 分段载入的“一束”消息的数量 + let messagesBunchCount = 50 // TODO: 分段载入的“一束”消息的数量 var displayedMessagesRange = NSRange() @@ -401,11 +401,11 @@ class ConversationViewController: UIViewController { // 初始时移动一次到底部 if !conversationCollectionViewHasBeenMovedToBottomOnce { - conversationCollectionViewHasBeenMovedToBottomOnce = true + // 先调整一下初次的 contentInset setConversaitonCollectionViewOriginalContentInset() - + if displayedMessagesRange.length > 0 { conversationCollectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: displayedMessagesRange.length - 1, inSection: 0), atScrollPosition: UICollectionViewScrollPosition.Bottom, animated: false) } @@ -969,11 +969,17 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi } } - + + override func viewDidAppear(animated: Bool) { + super.viewDidAppear(animated) + conversationCollectionViewHasBeenMovedToBottomOnce = true + } + // MARK: UIScrollViewDelegate func scrollViewDidScroll(scrollView: UIScrollView) { pullToRefreshView.scrollViewDidScroll(scrollView) + println("Scrolled\(scrollView.contentOffset.y)") } func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) { From 4684c9e58bc75247f54367f215b8e5d17b616f6f Mon Sep 17 00:00:00 2001 From: kevinzhow Date: Tue, 21 Apr 2015 20:50:19 +0800 Subject: [PATCH 2/3] load unread messages by notification --- Yep/AppDelegate.swift | 8 +++++--- Yep/Services/YepServiceSync.swift | 3 ++- .../ConversationViewController.swift | 1 - .../Cells/Conversation/ConversationCell.swift | 16 ++++++++++++++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Yep/AppDelegate.swift b/Yep/AppDelegate.swift index 5c0f32cf..e5106321 100644 --- a/Yep/AppDelegate.swift +++ b/Yep/AppDelegate.swift @@ -93,9 +93,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { + syncUnreadMessages() { completionHandler(UIBackgroundFetchResult.NewData) } + } // MARK: APNs @@ -119,12 +121,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { println("didReceiveRemoteNotification: \(userInfo)") if let v1AccessToken = YepUserDefaults.v1AccessToken() { - APService.handleRemoteNotification(userInfo) - + if let type = userInfo["type"] as? String { if type == "message" { syncUnreadMessages() { - + completionHandler(UIBackgroundFetchResult.NewData) + APService.handleRemoteNotification(userInfo) } } } diff --git a/Yep/Services/YepServiceSync.swift b/Yep/Services/YepServiceSync.swift index b9699fce..d150ba29 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -365,7 +365,8 @@ private func syncGroupWithGroupInfo(groupInfo: JSONDictionary, inRealm realm: RL func syncUnreadMessagesAndDoFurtherAction(furtherAction: () -> Void) { unreadMessages { allUnreadMessages in //println("\n allUnreadMessages: \(allUnreadMessages)") - + println("Got unread message \(allUnreadMessages.count)") + dispatch_async(realmQueue) { let realm = RLMRealm.defaultRealm() diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 1596c937..452fe0ed 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -979,7 +979,6 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi func scrollViewDidScroll(scrollView: UIScrollView) { pullToRefreshView.scrollViewDidScroll(scrollView) - println("Scrolled\(scrollView.contentOffset.y)") } func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) { diff --git a/Yep/Views/Cells/Conversation/ConversationCell.swift b/Yep/Views/Cells/Conversation/ConversationCell.swift index c900452d..9e375129 100644 --- a/Yep/Views/Cells/Conversation/ConversationCell.swift +++ b/Yep/Views/Cells/Conversation/ConversationCell.swift @@ -87,8 +87,20 @@ class ConversationCell: UITableViewCell { } } } - - self.chatLabel.text = latestMessage.textContent + switch latestMessage.mediaType { + + case MessageMediaType.Audio.rawValue: + self.chatLabel.text = "[声音]" + case MessageMediaType.Video.rawValue: + self.chatLabel.text = "[视频]" + case MessageMediaType.Image.rawValue: + self.chatLabel.text = "[图片]" + case MessageMediaType.Text.rawValue: + self.chatLabel.text = latestMessage.textContent + default: + break + + } self.timeAgoLabel.text = latestMessage.createdAt.timeAgo } else { From 3b4e6edfe0b66a6dc27ea42684d2f889956994fc Mon Sep 17 00:00:00 2001 From: kevinzhow Date: Tue, 21 Apr 2015 21:10:20 +0800 Subject: [PATCH 3/3] Fix mark as readed --- Yep/Services/YepService.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index 740367f8..1fad2ef5 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -961,7 +961,7 @@ func sendMessageWithMediaType(mediaType: MessageMediaType, inFilePath filePath: func markAsReadMessage(message: Message ,#failureHandler: ((Reason, String?) -> Void)?, #completion: (Bool) -> Void) { - if message.readed || message.messageID.isEmpty || message.downloadState != MessageDownloadState.Downloaded.rawValue { + if message.readed || message.messageID.isEmpty { return }