From fa4544ac1bf930d4df95ea2ac8d40258a5e5d466 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Wed, 2 Dec 2015 14:46:54 +0800 Subject: [PATCH] update countOfUnreadMessagesInRealm (consider ConversationType & Group.includeMe) --- Yep/Realm/Models.swift | 31 ++++++++++++++++--- .../ConversationsViewController.swift | 4 +-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index 27a74a60..918588ab 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -885,14 +885,37 @@ func countOfConversationsInRealm(realm: Realm, withConversationType conversation return realm.objects(Conversation).filter(predicate).count } -func countOfUnreadMessagesInRealm(realm: Realm) -> Int { - let predicate = NSPredicate(format: "readed = false AND fromFriend != nil AND fromFriend.friendState != %d", UserFriendState.Me.rawValue) - return realm.objects(Message).filter(predicate).count -} +//func countOfUnreadMessagesInRealm(realm: Realm) -> Int { +// let predicate = NSPredicate(format: "readed = false AND fromFriend != nil AND fromFriend.friendState != %d", UserFriendState.Me.rawValue) +// return realm.objects(Message).filter(predicate).count +// +// //return realm.objects(Conversation).map({ $0.unreadMessagesCount }).reduce(0, combine: +) +//} func countOfUnreadMessagesInRealm(realm: Realm, withConversationType conversationType: ConversationType) -> Int { + + switch conversationType { + + case .OneToOne: + let predicate = NSPredicate(format: "readed = false AND fromFriend != nil AND fromFriend.friendState != %d AND conversation != nil AND conversation.type = %d", UserFriendState.Me.rawValue, conversationType.rawValue) + return realm.objects(Message).filter(predicate).count + + case .Group: + let count = realm.objects(Group).filter("includeMe = true").map({ $0.conversation }).flatMap({ $0 }).map({ countOfUnreadMessagesInConversation($0) }).reduce(0, combine: +) + + return count + } + + /* let predicate = NSPredicate(format: "readed = false AND fromFriend != nil AND fromFriend.friendState != %d AND conversation != nil AND conversation.type = %d", UserFriendState.Me.rawValue, conversationType.rawValue) +// let messages = realm.objects(Message).filter(predicate) +// messages.forEach { +// println("X unread message.textContent: \($0.textContent), \($0.fromFriend?.nickname), \($0.conversation?.withGroup?.withFeed?.body)") +// } return realm.objects(Message).filter(predicate).count + + //return realm.objects(Conversation).filter("type = %d", conversationType.rawValue).map({ $0.unreadMessagesCount }).reduce(0, combine: +) + */ } func countOfUnreadMessagesInConversation(conversation: Conversation) -> Int { diff --git a/Yep/ViewControllers/Conversations/ConversationsViewController.swift b/Yep/ViewControllers/Conversations/ConversationsViewController.swift index aa063560..ab090c27 100644 --- a/Yep/ViewControllers/Conversations/ConversationsViewController.swift +++ b/Yep/ViewControllers/Conversations/ConversationsViewController.swift @@ -127,9 +127,9 @@ class ConversationsViewController: UIViewController { realmNotificationToken = realm.addNotificationBlock { [weak self] notification, realm in if let strongSelf = self { - let haveOneToOneUnreadMessages = countOfUnreadMessagesInRealm(realm, withConversationType: ConversationType.OneToOne) > 0 + let haveOneToOneUnreadMessages = countOfUnreadMessagesInRealm(realm, withConversationType: .OneToOne) > 0 - strongSelf.haveUnreadMessages = haveOneToOneUnreadMessages || (countOfUnreadMessagesInRealm(realm) > 0) + strongSelf.haveUnreadMessages = haveOneToOneUnreadMessages || (countOfUnreadMessagesInRealm(realm, withConversationType: .Group) > 0) strongSelf.noConversation = countOfConversationsInRealm(realm) == 0 }