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 {
.disposed(by: self.disposeBag)
//Create observable from sorted conversations and flatMap them to view models
let conversationsObservable: Observable<[ConversationViewModel]> ={ conversations in
let conversationsObservable: Observable<[ConversationViewModel]> ={ [weak self] conversations in
return conversations
.sorted(by: { conversation1, conversations2 in
......@@ -83,22 +83,25 @@ class SmartlistViewModel: Stateable, ViewModel {
return lastMessage1.receivedDate > lastMessage2.receivedDate
.filter({ $0.recipientRingId) != nil
|| (!$0.messages.isEmpty && (self.contactsService.contactRequest(withRingId: $0.recipientRingId) == nil))
.filter({ self? $0.recipientRingId) != nil
|| (!$0.messages.isEmpty && (self?.contactsService.contactRequest(withRingId: $0.recipientRingId) == nil))
.flatMap({ conversationModel in
var conversationViewModel: ConversationViewModel?
//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
}).first {
conversationViewModel = foundConversationViewModel
} else if let contactFound = self?.contactFoundConversation.value, contactFound.conversation.value == conversationModel {
conversationViewModel = contactFound
} else {
conversationViewModel = ConversationViewModel(with: injectionBag)
conversationViewModel?.conversation = Variable<ConversationModel>(conversationModel)
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