Commit df3a0e7c authored by Kateryna Kostiuk's avatar Kateryna Kostiuk

smart list: enable search by id

Change-Id: I2e8e19d35a297c4ed44f69a37d13c71f299b4507
parent bf018b15
...@@ -57,6 +57,8 @@ class SmartlistViewModel: Stateable, ViewModel { ...@@ -57,6 +57,8 @@ class SmartlistViewModel: Stateable, ViewModel {
return self.networkService.connectionState.value return self.networkService.connectionState.value
} }
let injectionBag: InjectionBag
// swiftlint:disable function_body_length // swiftlint:disable function_body_length
// swiftlint:disable cyclomatic_complexity // swiftlint:disable cyclomatic_complexity
required init(with injectionBag: InjectionBag) { required init(with injectionBag: InjectionBag) {
...@@ -65,6 +67,7 @@ class SmartlistViewModel: Stateable, ViewModel { ...@@ -65,6 +67,7 @@ class SmartlistViewModel: Stateable, ViewModel {
self.accountsService = injectionBag.accountService self.accountsService = injectionBag.accountService
self.contactsService = injectionBag.contactsService self.contactsService = injectionBag.contactsService
self.networkService = injectionBag.networkService self.networkService = injectionBag.networkService
self.injectionBag = injectionBag
// Observe connectivity changes // Observe connectivity changes
self.networkService.connectionStateObservable self.networkService.connectionStateObservable
...@@ -147,8 +150,7 @@ class SmartlistViewModel: Stateable, ViewModel { ...@@ -147,8 +150,7 @@ class SmartlistViewModel: Stateable, ViewModel {
//Observe username lookup //Observe username lookup
self.nameService.usernameLookupStatus.observeOn(MainScheduler.instance).subscribe(onNext: { [unowned self, unowned injectionBag] usernameLookupStatus in self.nameService.usernameLookupStatus.observeOn(MainScheduler.instance).subscribe(onNext: { [unowned self, unowned injectionBag] usernameLookupStatus in
if usernameLookupStatus.state == .found && usernameLookupStatus.name == self.searchBarText.value { if usernameLookupStatus.state == .found && (usernameLookupStatus.name == self.searchBarText.value || usernameLookupStatus.address == self.searchBarText.value) {
if let conversation = self.conversationViewModels.filter({ conversationViewModel in if let conversation = self.conversationViewModels.filter({ conversationViewModel in
conversationViewModel.conversation.value.recipientRingId == usernameLookupStatus.address conversationViewModel.conversation.value.recipientRingId == usernameLookupStatus.address
}).first { }).first {
...@@ -191,26 +193,35 @@ class SmartlistViewModel: Stateable, ViewModel { ...@@ -191,26 +193,35 @@ class SmartlistViewModel: Stateable, ViewModel {
self.filteredResults.value.removeAll() self.filteredResults.value.removeAll()
self.searchStatus.onNext("") self.searchStatus.onNext("")
if !text.isEmpty { if text.isEmpty {return}
//Filter conversations by user name or RingId
let filteredConversations = self.conversationViewModels.filter({ [unowned self] conversationViewModel in
let contact = self.contactsService.contact(withRingId: conversationViewModel.conversation.value.recipientRingId) //Filter conversations
let filteredConversations = self.conversationViewModels
if let recipientUserName = contact?.userName { .filter({conversationViewModel in
return recipientUserName.lowercased().hasPrefix(text.lowercased()) conversationViewModel.conversation.value.recipientRingId == text
} else {
return false
}
}) })
if !filteredConversations.isEmpty { if !filteredConversations.isEmpty {
self.filteredResults.value = filteredConversations self.filteredResults.value = filteredConversations
} }
let regexId = try? NSRegularExpression(pattern: "[0-9a-f]{40}", options: [])
if ((regexId?.firstMatch(in: text, options: NSRegularExpression.MatchingOptions.reportCompletion,
range: NSRange(location: 0, length: text.count))) == nil) {
self.nameService.lookupName(withAccount: "", nameserver: "", name: text) self.nameService.lookupName(withAccount: "", nameserver: "", name: text)
self.searchStatus.onNext(L10n.Smartlist.searching) self.searchStatus.onNext(L10n.Smartlist.searching)
return
}
if self.contactFoundConversation.value?.conversation.value.recipientRingId != text {
let accountId = self.accountsService.currentAccount?.id ?? ""
let jamiId = AccountModelHelper(withAccount: self.accountsService.currentAccount?).ringId ?? ""
//Create new converation
let conversation = ConversationModel(withRecipientRingId: text, accountId: accountId, accountUri: jamiId)
let newConversation = ConversationViewModel(with: self.injectionBag)
newConversation.conversation = Variable<ConversationModel>(conversation)
self.contactFoundConversation.value = newConversation
} }
} }
......
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