mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 10:33:45 +00:00
Improve user search using the GET profile (#754)
* Add getProfile in ClientProxyProtocol * Add fetchProfileIfNeeded * Refactor search * Update StartChatSuggestedUserCell UI * Add showProfilePopover action * Remove popover code * Add UTs in StartChatViewModelTests * Add UI tests * Cleanup code * Refine design * Fix design * Localise StartChatSuggestedUserCell * Update localisations * Update StartChatSuggestedUserCell design * Fix UT * Fix PR comments * Apply suggestions * Fix typo
This commit is contained in:
@@ -24,9 +24,6 @@ class StartChatViewModel: StartChatViewModelType, StartChatViewModelProtocol {
|
||||
|
||||
var callback: ((StartChatViewModelAction) -> Void)?
|
||||
weak var userIndicatorController: UserIndicatorControllerProtocol?
|
||||
var searchTask: Task<Void, Error>? {
|
||||
didSet { oldValue?.cancel() }
|
||||
}
|
||||
|
||||
init(userSession: UserSessionProtocol, userIndicatorController: UserIndicatorControllerProtocol?) {
|
||||
self.userSession = userSession
|
||||
@@ -50,7 +47,7 @@ class StartChatViewModel: StartChatViewModelType, StartChatViewModelProtocol {
|
||||
case .selectUser(let user):
|
||||
showLoadingIndicator()
|
||||
Task {
|
||||
let currentDirectRoom = await userSession.clientProxy.directRoomForUserID(user.userID)
|
||||
let currentDirectRoom = await clientProxy.directRoomForUserID(user.userID)
|
||||
switch currentDirectRoom {
|
||||
case .success(.some(let roomId)):
|
||||
self.hideLoadingIndicator()
|
||||
@@ -65,7 +62,9 @@ class StartChatViewModel: StartChatViewModelType, StartChatViewModelProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
func displayError(_ type: ClientProxyError) {
|
||||
// MARK: - Private
|
||||
|
||||
private func displayError(_ type: ClientProxyError) {
|
||||
switch type {
|
||||
case .failedRetrievingDirectRoom:
|
||||
state.bindings.alertInfo = AlertInfo(id: type,
|
||||
@@ -80,8 +79,6 @@ class StartChatViewModel: StartChatViewModelType, StartChatViewModelProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func setupBindings() {
|
||||
context.$viewState
|
||||
.map(\.bindings.searchQuery)
|
||||
@@ -92,22 +89,56 @@ class StartChatViewModel: StartChatViewModelType, StartChatViewModelProtocol {
|
||||
}
|
||||
.switchToLatest()
|
||||
.removeDuplicates()
|
||||
.sink { [weak self] query in
|
||||
self?.updateState(searchQuery: query)
|
||||
.sink { [weak self] _ in
|
||||
self?.fetchData()
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
private func updateState(searchQuery: String) {
|
||||
searchTask = nil
|
||||
|
||||
if searchQuery.count < 3 {
|
||||
private func fetchData() {
|
||||
guard searchQuery.count >= 3 else {
|
||||
fetchSuggestions()
|
||||
} else if MatrixEntityRegex.isMatrixUserIdentifier(searchQuery) {
|
||||
state.usersSection = .init(type: .searchResult, users: [UserProfile(userID: searchQuery)])
|
||||
} else {
|
||||
searchUsers(searchTerm: searchQuery)
|
||||
return
|
||||
}
|
||||
|
||||
Task {
|
||||
await searchProfiles()
|
||||
}
|
||||
}
|
||||
|
||||
private func searchProfiles() async {
|
||||
// copies the current query to check later if fetched data must be shown or not
|
||||
let committedQuery = searchQuery
|
||||
|
||||
async let queriedProfile = getProfileIfPossible()
|
||||
async let searchedUsers = clientProxy.searchUsers(searchTerm: committedQuery, limit: 5)
|
||||
|
||||
await updateState(committedQuery: committedQuery,
|
||||
queriedProfile: queriedProfile,
|
||||
searchResults: try? searchedUsers.get())
|
||||
}
|
||||
|
||||
private func updateState(committedQuery: String, queriedProfile: UserProfile?, searchResults: SearchUsersResults?) {
|
||||
guard committedQuery == searchQuery else {
|
||||
return
|
||||
}
|
||||
|
||||
let localProfile = queriedProfile ?? UserProfile(searchQuery: searchQuery)
|
||||
let allResults = merge(localProfile: localProfile, searchResults: searchResults?.results)
|
||||
|
||||
state.usersSection = .init(type: .searchResult, users: allResults)
|
||||
}
|
||||
|
||||
private func merge(localProfile: UserProfile?, searchResults: [UserProfile]?) -> [UserProfile] {
|
||||
guard let localProfile else {
|
||||
return searchResults ?? []
|
||||
}
|
||||
|
||||
let filteredSearchResult = searchResults?.filter {
|
||||
$0.userID != localProfile.userID
|
||||
} ?? []
|
||||
|
||||
return [localProfile] + filteredSearchResult
|
||||
}
|
||||
|
||||
private func fetchSuggestions() {
|
||||
@@ -116,7 +147,7 @@ class StartChatViewModel: StartChatViewModelType, StartChatViewModelProtocol {
|
||||
|
||||
private func createDirectRoom(with user: UserProfile) async {
|
||||
showLoadingIndicator()
|
||||
let result = await userSession.clientProxy.createDirectRoom(with: user.userID)
|
||||
let result = await clientProxy.createDirectRoom(with: user.userID)
|
||||
hideLoadingIndicator()
|
||||
switch result {
|
||||
case .success(let roomId):
|
||||
@@ -126,17 +157,20 @@ class StartChatViewModel: StartChatViewModelType, StartChatViewModelProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
private func searchUsers(searchTerm: String) {
|
||||
searchTask = Task { @MainActor in
|
||||
guard
|
||||
case let .success(result) = await userSession.clientProxy.searchUsers(searchTerm: searchTerm, limit: 5),
|
||||
!Task.isCancelled
|
||||
else {
|
||||
return
|
||||
}
|
||||
|
||||
state.usersSection = .init(type: .searchResult, users: result.results)
|
||||
private func getProfileIfPossible() async -> UserProfile? {
|
||||
guard searchQuery.isMatrixIdentifier else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return try? await clientProxy.getProfile(for: searchQuery).get()
|
||||
}
|
||||
|
||||
private var clientProxy: ClientProxyProtocol {
|
||||
userSession.clientProxy
|
||||
}
|
||||
|
||||
private var searchQuery: String {
|
||||
context.searchQuery
|
||||
}
|
||||
|
||||
// MARK: Loading indicator
|
||||
@@ -154,3 +188,18 @@ class StartChatViewModel: StartChatViewModelType, StartChatViewModelProtocol {
|
||||
userIndicatorController?.retractIndicatorWithId(Self.loadingIndicatorIdentifier)
|
||||
}
|
||||
}
|
||||
|
||||
private extension String {
|
||||
var isMatrixIdentifier: Bool {
|
||||
MatrixEntityRegex.isMatrixUserIdentifier(self)
|
||||
}
|
||||
}
|
||||
|
||||
private extension UserProfile {
|
||||
init?(searchQuery: String) {
|
||||
guard searchQuery.isMatrixIdentifier else {
|
||||
return nil
|
||||
}
|
||||
self.init(userID: searchQuery)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,4 @@ import Foundation
|
||||
protocol StartChatViewModelProtocol {
|
||||
var callback: ((StartChatViewModelAction) -> Void)? { get set }
|
||||
var context: StartChatViewModelType.Context { get }
|
||||
|
||||
/// Display an error to the user.
|
||||
/// - Parameter type: The type of error to be displayed.
|
||||
func displayError(_ type: ClientProxyError)
|
||||
}
|
||||
|
||||
@@ -29,15 +29,29 @@ struct StartChatSuggestedUserCell: View {
|
||||
imageProvider: imageProvider)
|
||||
.padding(.vertical, 10)
|
||||
.accessibilityHidden(true)
|
||||
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(user.displayName ?? user.userID)
|
||||
.font(.element.title3)
|
||||
.foregroundColor(.element.primaryContent)
|
||||
|
||||
if user.displayName != nil {
|
||||
Text(user.userID)
|
||||
.font(.element.subheadline)
|
||||
.foregroundColor(.element.tertiaryContent)
|
||||
}
|
||||
|
||||
if !user.isVerified {
|
||||
HStack(alignment: .firstTextBaseline, spacing: 4) {
|
||||
Text(Image(systemName: "exclamationmark.circle.fill"))
|
||||
.foregroundColor(.compound.textActionCritical)
|
||||
|
||||
Text(L10n.screenStartChatUnknownProfile)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
.font(.compound.bodyXS)
|
||||
.padding(.top, 4)
|
||||
}
|
||||
}
|
||||
.accessibilityElement(children: .combine)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user