Commit 6d3a34a4 authored by Kateryna Kostiuk's avatar Kateryna Kostiuk

smartlist: check if conversation exists

When user search for a new contact and than starts a conversation
the conversation model created two times and all subscriptions to
the services doubled. This patch uses existing conversation model
instead of creating a new one

Change-Id: I1d5f3ef1894377fc060e3a03da15a7046940f4d2
parent cb26c2d1
...@@ -72,7 +72,7 @@ class SmartlistViewModel: Stateable, ViewModel { ...@@ -72,7 +72,7 @@ class SmartlistViewModel: Stateable, ViewModel {
.disposed(by: self.disposeBag) .disposed(by: self.disposeBag)
//Create observable from sorted conversations and flatMap them to view models //Create observable from sorted conversations and flatMap them to view models
let conversationsObservable: Observable<[ConversationViewModel]> = self.conversationsService.conversationsForCurrentAccount.map({ conversations in let conversationsObservable: Observable<[ConversationViewModel]> = self.conversationsService.conversationsForCurrentAccount.map({ [weak self] conversations in
return conversations return conversations
.sorted(by: { conversation1, conversations2 in .sorted(by: { conversation1, conversations2 in
...@@ -83,22 +83,25 @@ class SmartlistViewModel: Stateable, ViewModel { ...@@ -83,22 +83,25 @@ class SmartlistViewModel: Stateable, ViewModel {
return lastMessage1.receivedDate > lastMessage2.receivedDate return lastMessage1.receivedDate > lastMessage2.receivedDate
}) })
.filter({ self.contactsService.contact(withRingId: $0.recipientRingId) != nil .filter({ self?.contactsService.contact(withRingId: $0.recipientRingId) != nil
|| (!$0.messages.isEmpty && (self.contactsService.contactRequest(withRingId: $0.recipientRingId) == nil)) || (!$0.messages.isEmpty && (self?.contactsService.contactRequest(withRingId: $0.recipientRingId) == nil))
}) })
.flatMap({ conversationModel in .flatMap({ conversationModel in
var conversationViewModel: ConversationViewModel? var conversationViewModel: ConversationViewModel?
//Get the current ConversationViewModel if exists or create it //Get the current ConversationViewModel if exists or create it
if let foundConversationViewModel = self.conversationViewModels.filter({ conversationViewModel in if let foundConversationViewModel = self?.conversationViewModels.filter({ conversationViewModel in
return conversationViewModel.conversation.value == conversationModel return conversationViewModel.conversation.value == conversationModel
}).first { }).first {
conversationViewModel = foundConversationViewModel conversationViewModel = foundConversationViewModel
} else if let contactFound = self?.contactFoundConversation.value, contactFound.conversation.value == conversationModel {
conversationViewModel = contactFound
self?.conversationViewModels.append(contactFound)
} else { } else {
conversationViewModel = ConversationViewModel(with: injectionBag) conversationViewModel = ConversationViewModel(with: injectionBag)
conversationViewModel?.conversation = Variable<ConversationModel>(conversationModel) conversationViewModel?.conversation = Variable<ConversationModel>(conversationModel)
self.conversationViewModels.append(conversationViewModel!) self?.conversationViewModels.append(conversationViewModel!)
} }
return conversationViewModel return conversationViewModel
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment