mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 12:07:28 +00:00
Merge pull request #7266 from element-hq/feature/fga/searchbar-result-state-refactor
Refactor SearchBarResultState.Initial/NoResultsFound to data objects
This commit is contained in:
+4
-4
@@ -88,7 +88,7 @@ class DefaultInvitePeoplePresenter(
|
||||
override fun present(): InvitePeopleState {
|
||||
val roomMembers = remember { mutableStateOf<AsyncData<ImmutableList<RoomMember>>>(AsyncData.Loading()) }
|
||||
val selectedUsers = remember { mutableStateOf<ImmutableList<MatrixUser>>(persistentListOf()) }
|
||||
val searchResults = remember { mutableStateOf<SearchBarResultState<ImmutableList<InvitableUser>>>(SearchBarResultState.Initial()) }
|
||||
val searchResults = remember { mutableStateOf<SearchBarResultState<ImmutableList<InvitableUser>>>(SearchBarResultState.Initial) }
|
||||
val queryState = rememberTextFieldState()
|
||||
var searchActive by rememberSaveable { mutableStateOf(false) }
|
||||
val showSearchLoader = rememberSaveable { mutableStateOf(false) }
|
||||
@@ -332,15 +332,15 @@ class DefaultInvitePeoplePresenter(
|
||||
showSearchLoader: MutableState<Boolean>,
|
||||
searchQuery: String,
|
||||
) = withContext(coroutineDispatchers.io) {
|
||||
searchResults.value = SearchBarResultState.Initial()
|
||||
searchResults.value = SearchBarResultState.Initial
|
||||
showSearchLoader.value = false
|
||||
val joinedMembers = roomMembers.value.dataOrNull().orEmpty()
|
||||
|
||||
userRepository.search(searchQuery).onEach { state ->
|
||||
showSearchLoader.value = state.isSearching
|
||||
searchResults.value = when {
|
||||
state.results.isEmpty() && state.isSearching -> SearchBarResultState.Initial()
|
||||
state.results.isEmpty() && !state.isSearching -> SearchBarResultState.NoResultsFound()
|
||||
state.results.isEmpty() && state.isSearching -> SearchBarResultState.Initial
|
||||
state.results.isEmpty() && !state.isSearching -> SearchBarResultState.NoResultsFound
|
||||
else -> SearchBarResultState.Results(state.results.map { result ->
|
||||
val existingMembership = joinedMembers.firstOrNull { j -> j.userId == result.matrixUser.userId }?.membership
|
||||
val isJoined = existingMembership == RoomMembershipState.JOIN
|
||||
|
||||
+2
-2
@@ -33,7 +33,7 @@ internal class DefaultInvitePeopleStateProvider : PreviewParameterProvider<Defau
|
||||
aDefaultInvitePeopleState(canInvite = true, selectedUsers = aMatrixUserList().toImmutableList()),
|
||||
aDefaultInvitePeopleState(isSearchActive = true, searchQuery = "some query"),
|
||||
aDefaultInvitePeopleState(isSearchActive = true, searchQuery = "some query", selectedUsers = aMatrixUserList().toImmutableList()),
|
||||
aDefaultInvitePeopleState(isSearchActive = true, searchQuery = "some query", searchResults = SearchBarResultState.NoResultsFound()),
|
||||
aDefaultInvitePeopleState(isSearchActive = true, searchQuery = "some query", searchResults = SearchBarResultState.NoResultsFound),
|
||||
aDefaultInvitePeopleState(
|
||||
isSearchActive = true,
|
||||
canInvite = true,
|
||||
@@ -115,7 +115,7 @@ private fun aDefaultInvitePeopleState(
|
||||
room: AsyncData<Unit> = AsyncData.Success(Unit),
|
||||
canInvite: Boolean = false,
|
||||
searchQuery: String = "",
|
||||
searchResults: SearchBarResultState<ImmutableList<InvitableUser>> = SearchBarResultState.Initial(),
|
||||
searchResults: SearchBarResultState<ImmutableList<InvitableUser>> = SearchBarResultState.Initial,
|
||||
selectedUsers: ImmutableList<MatrixUser> = persistentListOf(),
|
||||
isSearchActive: Boolean = false,
|
||||
showSearchLoader: Boolean = false,
|
||||
|
||||
+4
-8
@@ -95,7 +95,7 @@ internal class DefaultInvitePeoplePresenterTest {
|
||||
resultState.searchQuery.setTextAndPlaceCursorAtEnd("some query")
|
||||
assertThat(awaitItemAsDefault().searchQuery.text.toString()).isEqualTo("some query")
|
||||
resultState.eventSink(InvitePeopleEvents.CloseSearch)
|
||||
skipItems(2)
|
||||
skipItems(1)
|
||||
awaitItemAsDefault().also {
|
||||
assertThat(it.isSearchActive).isFalse()
|
||||
assertThat(it.searchQuery.text.toString()).isEmpty()
|
||||
@@ -116,7 +116,7 @@ internal class DefaultInvitePeoplePresenterTest {
|
||||
initialState.searchQuery.setTextAndPlaceCursorAtEnd("some query")
|
||||
assertThat(repository.providedQuery).isEqualTo("some query")
|
||||
repository.emitState(UserSearchResultState(results = emptyList(), isSearching = true))
|
||||
skipItems(3)
|
||||
skipItems(2)
|
||||
awaitItemAsDefault().also { state ->
|
||||
assertThat(state.searchResults).isInstanceOf(SearchBarResultState.Initial::class.java)
|
||||
assertThat(state.showSearchLoader).isTrue()
|
||||
@@ -145,7 +145,6 @@ internal class DefaultInvitePeoplePresenterTest {
|
||||
|
||||
assertThat(repository.providedQuery).isEqualTo("some query")
|
||||
repository.emitStateWithUsers(users = aMatrixUserList())
|
||||
skipItems(1)
|
||||
|
||||
val resultState = awaitItemAsDefault()
|
||||
assertThat(resultState.searchResults).isInstanceOf(SearchBarResultState.Results::class.java)
|
||||
@@ -198,7 +197,6 @@ internal class DefaultInvitePeoplePresenterTest {
|
||||
|
||||
assertThat(repository.providedQuery).isEqualTo("some query")
|
||||
repository.emitStateWithUsers(users = aMatrixUserList())
|
||||
skipItems(1)
|
||||
|
||||
val resultState = awaitItemAsDefault()
|
||||
assertThat(resultState.searchResults).isInstanceOf(SearchBarResultState.Results::class.java)
|
||||
@@ -265,7 +263,6 @@ internal class DefaultInvitePeoplePresenterTest {
|
||||
it
|
||||
)
|
||||
})
|
||||
skipItems(1)
|
||||
|
||||
val resultState = awaitItemAsDefault()
|
||||
assertThat(resultState.searchResults).isInstanceOf(SearchBarResultState.Results::class.java)
|
||||
@@ -329,7 +326,7 @@ internal class DefaultInvitePeoplePresenterTest {
|
||||
|
||||
assertThat(repository.providedQuery).isEqualTo("some query")
|
||||
repository.emitStateWithUsers(users = aMatrixUserList() + selectedUser)
|
||||
skipItems(2)
|
||||
skipItems(1)
|
||||
|
||||
val resultState = awaitItemAsDefault()
|
||||
assertThat(resultState.searchResults).isInstanceOf(SearchBarResultState.Results::class.java)
|
||||
@@ -366,7 +363,6 @@ internal class DefaultInvitePeoplePresenterTest {
|
||||
|
||||
assertThat(repository.providedQuery).isEqualTo("some query")
|
||||
repository.emitStateWithUsers(users = aMatrixUserList() + selectedUser)
|
||||
skipItems(1)
|
||||
awaitItemAsDefault().also { state ->
|
||||
// selectedUser is not selected
|
||||
assertThat(state.searchResults).isInstanceOf(SearchBarResultState.Results::class.java)
|
||||
@@ -732,7 +728,7 @@ internal class DefaultInvitePeoplePresenterTest {
|
||||
isSearching = true
|
||||
)
|
||||
)
|
||||
skipItems(3)
|
||||
skipItems(2)
|
||||
|
||||
awaitItemAsDefault().run {
|
||||
assertThat(selectedUsers).containsExactly(alice, bob, charlie)
|
||||
|
||||
+2
-2
@@ -77,7 +77,7 @@ class ChangeRolesPresenter(
|
||||
val queryState = rememberTextFieldState()
|
||||
var searchActive by rememberSaveable { mutableStateOf(false) }
|
||||
var searchResults by remember {
|
||||
mutableStateOf<SearchBarResultState<MembersByRole>>(SearchBarResultState.Initial())
|
||||
mutableStateOf<SearchBarResultState<MembersByRole>>(SearchBarResultState.Initial)
|
||||
}
|
||||
val selectedUsers = remember {
|
||||
mutableStateOf<ImmutableList<MatrixUser>>(persistentListOf())
|
||||
@@ -118,7 +118,7 @@ class ChangeRolesPresenter(
|
||||
.groupedByRole()
|
||||
|
||||
searchResults = if (results.isEmpty()) {
|
||||
SearchBarResultState.NoResultsFound()
|
||||
SearchBarResultState.NoResultsFound
|
||||
} else {
|
||||
SearchBarResultState.Results(results)
|
||||
}
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ internal fun aChangeRolesState(
|
||||
role: RoomMember.Role = RoomMember.Role.Admin,
|
||||
searchQuery: String = "",
|
||||
isSearchActive: Boolean = false,
|
||||
searchResults: SearchBarResultState<MembersByRole> = SearchBarResultState.NoResultsFound(),
|
||||
searchResults: SearchBarResultState<MembersByRole> = SearchBarResultState.NoResultsFound,
|
||||
selectedUsers: ImmutableList<MatrixUser> = persistentListOf(),
|
||||
hasPendingChanges: Boolean = false,
|
||||
savingState: AsyncAction<Boolean> = AsyncAction.Uninitialized,
|
||||
|
||||
+2
-2
@@ -66,8 +66,8 @@ class AddRoomToSpacePresenter(
|
||||
derivedStateOf {
|
||||
when {
|
||||
filteredRooms.isNotEmpty() -> SearchBarResultState.Results(filteredRooms)
|
||||
isSearchActive && searchQuery.text.isNotEmpty() -> SearchBarResultState.NoResultsFound<ImmutableList<SelectRoomInfo>>()
|
||||
else -> SearchBarResultState.Initial<ImmutableList<SelectRoomInfo>>()
|
||||
isSearchActive && searchQuery.text.isNotEmpty() -> SearchBarResultState.NoResultsFound
|
||||
else -> SearchBarResultState.Initial
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -40,7 +40,7 @@ internal class AddRoomToSpaceStateProvider : PreviewParameterProvider<AddRoomToS
|
||||
anAddRoomToSpaceState(
|
||||
isSearchActive = true,
|
||||
searchQuery = "unknown",
|
||||
searchResults = SearchBarResultState.NoResultsFound(),
|
||||
searchResults = SearchBarResultState.NoResultsFound,
|
||||
),
|
||||
// With selected rooms
|
||||
anAddRoomToSpaceState(
|
||||
@@ -62,7 +62,7 @@ internal class AddRoomToSpaceStateProvider : PreviewParameterProvider<AddRoomToS
|
||||
|
||||
internal fun anAddRoomToSpaceState(
|
||||
searchQuery: String = "",
|
||||
searchResults: SearchBarResultState<ImmutableList<SelectRoomInfo>> = SearchBarResultState.Initial(),
|
||||
searchResults: SearchBarResultState<ImmutableList<SelectRoomInfo>> = SearchBarResultState.Initial,
|
||||
selectedRooms: ImmutableList<SelectRoomInfo> = persistentListOf(),
|
||||
isSearchActive: Boolean = false,
|
||||
saveAction: AsyncAction<Unit> = AsyncAction.Uninitialized,
|
||||
|
||||
+4
-4
@@ -67,19 +67,19 @@ class DefaultUserListPresenter(
|
||||
val selectedUsers by userListDataStore.selectedUsers.collectAsState(emptyList())
|
||||
val queryState = rememberTextFieldState()
|
||||
var searchResults: SearchBarResultState<ImmutableList<UserSearchResult>> by remember {
|
||||
mutableStateOf(SearchBarResultState.Initial())
|
||||
mutableStateOf(SearchBarResultState.Initial)
|
||||
}
|
||||
var showSearchLoader by remember { mutableStateOf(false) }
|
||||
|
||||
val searchQuery = queryState.text.toString()
|
||||
LaunchedEffect(searchQuery) {
|
||||
searchResults = SearchBarResultState.Initial()
|
||||
searchResults = SearchBarResultState.Initial
|
||||
showSearchLoader = false
|
||||
userRepository.search(searchQuery).onEach { state ->
|
||||
showSearchLoader = state.isSearching
|
||||
searchResults = when {
|
||||
state.results.isEmpty() && state.isSearching -> SearchBarResultState.Initial()
|
||||
state.results.isEmpty() && !state.isSearching -> SearchBarResultState.NoResultsFound()
|
||||
state.results.isEmpty() && state.isSearching -> SearchBarResultState.Initial
|
||||
state.results.isEmpty() && !state.isSearching -> SearchBarResultState.NoResultsFound
|
||||
else -> SearchBarResultState.Results(state.results.toImmutableList())
|
||||
}
|
||||
}.launchIn(this)
|
||||
|
||||
+2
-2
@@ -47,7 +47,7 @@ open class UserListStateProvider : PreviewParameterProvider<UserListState> {
|
||||
aUserListState(
|
||||
isSearchActive = true,
|
||||
searchQuery = "something-with-no-results",
|
||||
searchResults = SearchBarResultState.NoResultsFound()
|
||||
searchResults = SearchBarResultState.NoResultsFound
|
||||
),
|
||||
aUserListState(
|
||||
isSearchActive = true,
|
||||
@@ -63,7 +63,7 @@ open class UserListStateProvider : PreviewParameterProvider<UserListState> {
|
||||
fun aUserListState(
|
||||
searchQuery: String = "",
|
||||
isSearchActive: Boolean = false,
|
||||
searchResults: SearchBarResultState<ImmutableList<UserSearchResult>> = SearchBarResultState.Initial(),
|
||||
searchResults: SearchBarResultState<ImmutableList<UserSearchResult>> = SearchBarResultState.Initial,
|
||||
selectedUsers: List<MatrixUser> = emptyList(),
|
||||
showSearchLoader: Boolean = false,
|
||||
selectionMode: SelectionMode = SelectionMode.Single,
|
||||
|
||||
+2
-10
@@ -40,7 +40,6 @@ class DefaultUserListPresenterTest {
|
||||
FakeMatrixClient(),
|
||||
)
|
||||
presenter.test {
|
||||
skipItems(1)
|
||||
val initialState = awaitItem()
|
||||
assertThat(initialState.searchQuery.text.toString()).isEmpty()
|
||||
assertThat(initialState.isMultiSelectionEnabled).isFalse()
|
||||
@@ -60,7 +59,6 @@ class DefaultUserListPresenterTest {
|
||||
FakeMatrixClient(),
|
||||
)
|
||||
presenter.test {
|
||||
skipItems(1)
|
||||
val initialState = awaitItem()
|
||||
assertThat(initialState.searchQuery.text.toString()).isEmpty()
|
||||
assertThat(initialState.isMultiSelectionEnabled).isTrue()
|
||||
@@ -80,7 +78,6 @@ class DefaultUserListPresenterTest {
|
||||
FakeMatrixClient(),
|
||||
)
|
||||
presenter.test {
|
||||
skipItems(1)
|
||||
val initialState = awaitItem()
|
||||
|
||||
initialState.eventSink(UserListEvents.OnSearchActiveChanged(true))
|
||||
@@ -90,13 +87,11 @@ class DefaultUserListPresenterTest {
|
||||
initialState.searchQuery.setTextAndPlaceCursorAtEnd(matrixIdQuery)
|
||||
assertThat(awaitItem().searchQuery.text.toString()).isEqualTo(matrixIdQuery)
|
||||
assertThat(userRepository.providedQuery).isEqualTo(matrixIdQuery)
|
||||
skipItems(1)
|
||||
|
||||
val notMatrixIdQuery = "name"
|
||||
initialState.searchQuery.setTextAndPlaceCursorAtEnd(notMatrixIdQuery)
|
||||
assertThat(awaitItem().searchQuery.text.toString()).isEqualTo(notMatrixIdQuery)
|
||||
assertThat(userRepository.providedQuery).isEqualTo(notMatrixIdQuery)
|
||||
skipItems(1)
|
||||
|
||||
initialState.eventSink(UserListEvents.OnSearchActiveChanged(false))
|
||||
assertThat(awaitItem().isSearchActive).isFalse()
|
||||
@@ -115,13 +110,12 @@ class DefaultUserListPresenterTest {
|
||||
FakeMatrixClient(),
|
||||
)
|
||||
presenter.test {
|
||||
skipItems(1)
|
||||
val initialState = awaitItem()
|
||||
|
||||
initialState.searchQuery.setTextAndPlaceCursorAtEnd("alice")
|
||||
assertThat(initialState.searchResults).isInstanceOf(SearchBarResultState.Initial::class.java)
|
||||
assertThat(userRepository.providedQuery).isEqualTo("alice")
|
||||
skipItems(2)
|
||||
skipItems(1)
|
||||
|
||||
// When the user repository emits a result, it's copied to the state
|
||||
val result = UserSearchResultState(
|
||||
@@ -166,13 +160,12 @@ class DefaultUserListPresenterTest {
|
||||
FakeMatrixClient(),
|
||||
)
|
||||
presenter.test {
|
||||
skipItems(1)
|
||||
val initialState = awaitItem()
|
||||
|
||||
initialState.searchQuery.setTextAndPlaceCursorAtEnd("alice")
|
||||
assertThat(initialState.searchResults).isInstanceOf(SearchBarResultState.Initial::class.java)
|
||||
assertThat(userRepository.providedQuery).isEqualTo("alice")
|
||||
skipItems(2)
|
||||
skipItems(1)
|
||||
|
||||
// When the results list is empty, the state is set to NoResults
|
||||
userRepository.emitState(UserSearchResultState(results = emptyList(), isSearching = false))
|
||||
@@ -190,7 +183,6 @@ class DefaultUserListPresenterTest {
|
||||
FakeMatrixClient(),
|
||||
)
|
||||
presenter.test {
|
||||
skipItems(1)
|
||||
val initialState = awaitItem()
|
||||
|
||||
val userA = aMatrixUser("@userA:domain", "A")
|
||||
|
||||
Reference in New Issue
Block a user