From 6cde0706be7d990f64e546b8fb890cae69fd87dc Mon Sep 17 00:00:00 2001 From: Jorge Martin Espinosa Date: Thu, 1 Oct 2026 18:17:18 +0200 Subject: [PATCH] Add remove item and clear history buttons to search history (#7816) * Add remove and clear history buttons to search * Rename `SearchHistoryResultItem` to `SearchHistoryListItem` and `MessagesSearchResultItem` to `MessageSearchListItem` Also, add some docs. * Update screenshots --------- Co-authored-by: ElementBot --- .../home/impl/search/GlobalSearchEvent.kt | 8 +- .../home/impl/search/GlobalSearchPresenter.kt | 31 +++++-- .../home/impl/search/GlobalSearchState.kt | 39 +++++++-- .../search/GlobalSearchStatePreviewParam.kt | 18 ++-- .../home/impl/search/GlobalSearchView.kt | 83 ++++++++++++++----- .../history/DefaultSearchHistoryStore.kt | 11 +++ .../search/history/SearchHistoryResult.kt | 12 ++- .../impl/search/history/SearchHistoryStore.kt | 5 ++ .../impl/search/GlobalSearchPresenterTest.kt | 73 +++++++++++++++- .../home/impl/search/GlobalSearchViewTest.kt | 43 +++++++++- .../history/DefaultSearchHistoryStoreTest.kt | 25 ++++++ .../search/history/FakeSearchHistoryStore.kt | 4 + .../designsystem/theme/components/Button.kt | 4 +- .../src/main/res/values/localazy.xml | 1 + ....impl.search_GlobalSearchView_Day_1_en.png | 4 +- ....impl.search_GlobalSearchView_Day_2_en.png | 4 +- ....impl.search_GlobalSearchView_Day_3_en.png | 4 +- ....impl.search_GlobalSearchView_Day_4_en.png | 4 +- ....impl.search_GlobalSearchView_Day_5_en.png | 4 +- ....impl.search_GlobalSearchView_Day_6_en.png | 4 +- ...mpl.search_GlobalSearchView_Night_1_en.png | 4 +- ...mpl.search_GlobalSearchView_Night_2_en.png | 4 +- ...mpl.search_GlobalSearchView_Night_3_en.png | 4 +- ...mpl.search_GlobalSearchView_Night_4_en.png | 4 +- ...mpl.search_GlobalSearchView_Night_5_en.png | 4 +- ...mpl.search_GlobalSearchView_Night_6_en.png | 4 +- 26 files changed, 323 insertions(+), 82 deletions(-) diff --git a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchEvent.kt b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchEvent.kt index 1e051bd807..ef4d031540 100644 --- a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchEvent.kt +++ b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchEvent.kt @@ -29,5 +29,11 @@ sealed interface GlobalSearchEvent { data class SaveRoomToHistory(val roomId: RoomId) : GlobalSearchEvent /** The user selected a search result from the search history. */ - data class SearchHistoryResultSelected(val resultItem: SearchHistoryResultItem) : GlobalSearchEvent + data class SearchHistoryResultSelected(val resultItem: SearchHistoryListItem) : GlobalSearchEvent + + /** The user removed a search history entry. */ + data class RemoveSearchHistoryResult(val result: SearchHistoryListItem) : GlobalSearchEvent + + /** The user cleared the search history. */ + data object ClearSearchHistory : GlobalSearchEvent } diff --git a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchPresenter.kt b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchPresenter.kt index 7b8b21be38..f07a169589 100644 --- a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchPresenter.kt +++ b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchPresenter.kt @@ -57,6 +57,7 @@ import kotlinx.coroutines.awaitAll import kotlinx.coroutines.delay import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import timber.log.Timber import kotlin.time.Duration.Companion.milliseconds @@ -87,14 +88,14 @@ class GlobalSearchPresenter( var currentTarget: GlobalSearchTarget by remember { mutableStateOf(GlobalSearchTarget.ROOMS) } val currentMessageSearch: MessageSearch = remember { messageSearchService.createMessageSearch(scope = coroutineScope) } - val searchHistory by produceState>>(initialValue = AsyncData.Uninitialized) { + val searchHistory by produceState>>(initialValue = AsyncData.Uninitialized) { searchHistoryStore.history.collectLatest { history -> val mappedHistoryItems = history.mapNotNull { result -> when (result) { - is SearchHistoryResult.Query -> SearchHistoryResultItem.Query(term = result.term) + is SearchHistoryResult.Query -> SearchHistoryListItem.Query(term = result.term) is SearchHistoryResult.Room -> { val roomInfo = matrixClient.getRoomInfo(result.roomId).getOrNull() ?: return@mapNotNull null - SearchHistoryResultItem.Room(roomId = result.roomId, roomInfo = roomInfo) + SearchHistoryListItem.Room(roomId = result.roomId, roomInfo = roomInfo) } } } @@ -236,7 +237,7 @@ class GlobalSearchPresenter( } is GlobalSearchEvent.SearchHistoryResultSelected -> { when (event.resultItem) { - is SearchHistoryResultItem.Query -> coroutineScope.launch { + is SearchHistoryListItem.Query -> coroutineScope.launch { delay(300.milliseconds) queryState.edit { replace(0, length, event.resultItem.term) @@ -244,9 +245,21 @@ class GlobalSearchPresenter( searchResults = AsyncData.Loading() currentTarget = GlobalSearchTarget.ROOMS } - is SearchHistoryResultItem.Room -> Unit + is SearchHistoryListItem.Room -> Unit } } + is GlobalSearchEvent.RemoveSearchHistoryResult -> coroutineScope.launch { + val currentHistory = searchHistoryStore.history.first() + val id = when (event.result) { + is SearchHistoryListItem.Query -> event.result.term + is SearchHistoryListItem.Room -> event.result.roomId.value + } + val result = currentHistory.find { it.id == id } ?: return@launch + searchHistoryStore.remove(result) + } + GlobalSearchEvent.ClearSearchHistory -> coroutineScope.launch { + searchHistoryStore.clear() + } } } @@ -265,7 +278,7 @@ class GlobalSearchPresenter( result: MessageSearchResult, roomInfo: RoomInfo, formattedTimestamp: String, - ): MessageSearchResultItem.Message? { + ): MessageSearchListItem.Message? { val body = latestEventFormatter.format( latestEvent = LatestEventValue.Remote( timestamp = result.timestamp, @@ -276,7 +289,7 @@ class GlobalSearchPresenter( ), isDmRoom = false, ) - return MessageSearchResultItem.Message( + return MessageSearchListItem.Message( messageSearchResult = result, body = body?.toString() ?: "", formattedTimestamp = formattedTimestamp, @@ -288,7 +301,7 @@ class GlobalSearchPresenter( result: MessageSearchResult, roomInfo: RoomInfo, formattedTimestamp: String, - ): MessageSearchResultItem.Media? { + ): MessageSearchListItem.Media? { val messageType = when (val content = result.content) { is MessageContent if content.type is MessageTypeWithAttachment -> content.type as MessageTypeWithAttachment else -> return null @@ -342,7 +355,7 @@ class GlobalSearchPresenter( blurhash = blurhash ) - return MessageSearchResultItem.Media( + return MessageSearchListItem.Media( messageSearchResult = result, mediaContent = mediaContent, formattedTimestamp = formattedTimestamp, diff --git a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchState.kt b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchState.kt index 44b94bd512..d60290b62e 100644 --- a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchState.kt +++ b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchState.kt @@ -28,20 +28,26 @@ data class GlobalSearchState( val queryState: TextFieldState, val currentTarget: GlobalSearchTarget, val results: AsyncData, - val history: AsyncData>, + val history: AsyncData>, val eventSink: (GlobalSearchEvent) -> Unit ) +/** + * The target of the global search: either rooms or messages. + */ @Immutable enum class GlobalSearchTarget { ROOMS, MESSAGES, } +/** + * The results of a global search, either a list of rooms or a list of messages. + */ @Immutable sealed interface GlobalSearchResults { data class RoomListResults(val results: ImmutableList) : GlobalSearchResults - data class MessageSearchResults(val results: ImmutableList) : GlobalSearchResults + data class MessageSearchResults(val results: ImmutableList) : GlobalSearchResults fun isEmpty(): Boolean = when (this) { is RoomListResults -> results.isEmpty() @@ -49,7 +55,10 @@ sealed interface GlobalSearchResults { } } -sealed interface MessageSearchResultItem { +/** + * A single item in the message search results. It's the UI representation of [MessageSearchResult], which is the mapped model returned by the Matrix SDK. + */ +sealed interface MessageSearchListItem { val roomId: RoomId val eventId: EventId val senderId: UserId @@ -57,24 +66,30 @@ sealed interface MessageSearchResultItem { val roomInfo: RoomInfo val formattedTimestamp: String + /** + * A message item in the message search results. + */ data class Message( val messageSearchResult: MessageSearchResult, val body: String, override val roomInfo: RoomInfo, override val formattedTimestamp: String, - ) : MessageSearchResultItem { + ) : MessageSearchListItem { override val roomId: RoomId = messageSearchResult.roomId override val eventId: EventId = messageSearchResult.eventId override val senderId: UserId = messageSearchResult.senderId override val senderName: String = messageSearchResult.senderProfile.getDisambiguatedDisplayName(senderId) } + /** + * A message with media item in the message search results. + */ data class Media( val messageSearchResult: MessageSearchResult, val mediaContent: MediaSearchResultContent, override val roomInfo: RoomInfo, override val formattedTimestamp: String, - ) : MessageSearchResultItem { + ) : MessageSearchListItem { override val roomId: RoomId = messageSearchResult.roomId override val eventId: EventId = messageSearchResult.eventId override val senderId: UserId = messageSearchResult.senderId @@ -82,20 +97,28 @@ sealed interface MessageSearchResultItem { } } -sealed interface SearchHistoryResultItem { +/** + * A single entry in the global search history, displayed in the list of recent searches. + * + * It's the UI representation of [io.element.android.features.home.impl.search.history.SearchHistoryResult], which is the persisted model. + */ +sealed interface SearchHistoryListItem { val id: String - data class Query(val term: String) : SearchHistoryResultItem { + data class Query(val term: String) : SearchHistoryListItem { override val id: String = "query:$term" } data class Room( val roomId: RoomId, val roomInfo: RoomInfo, - ) : SearchHistoryResultItem { + ) : SearchHistoryListItem { override val id: String = "room:${roomId.value}" } } +/** + * Represents the media content of a message search result. + */ data class MediaSearchResultContent( val filename: String, val extension: String?, diff --git a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchStatePreviewParam.kt b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchStatePreviewParam.kt index f9cc105a77..ab1efca019 100644 --- a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchStatePreviewParam.kt +++ b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchStatePreviewParam.kt @@ -79,13 +79,13 @@ class GlobalSearchStatePreviewParam : PreviewParameterProvider = AsyncData.Uninitialized, - history: AsyncData> = AsyncData.Uninitialized, + history: AsyncData> = AsyncData.Uninitialized, eventSink: (GlobalSearchEvent) -> Unit = {}, ) = GlobalSearchState( isEnabled = isEnabled, diff --git a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchView.kt b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchView.kt index cce0620f3f..5154289770 100644 --- a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchView.kt +++ b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/GlobalSearchView.kt @@ -12,12 +12,14 @@ import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeOut import androidx.compose.foundation.background +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width @@ -31,9 +33,11 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.input.KeyboardActionHandler import androidx.compose.foundation.text.input.TextFieldLineLimits +import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.IconButtonDefaults import androidx.compose.material3.SingleChoiceSegmentedButtonRow import androidx.compose.material3.TextFieldDefaults +import androidx.compose.material3.minimumInteractiveComponentSize import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember @@ -67,6 +71,8 @@ import io.element.android.libraries.designsystem.modifiers.niceClickable import io.element.android.libraries.designsystem.preview.ElementPreview import io.element.android.libraries.designsystem.preview.PreviewsDayNight import io.element.android.libraries.designsystem.text.roundToPx +import io.element.android.libraries.designsystem.text.toDp +import io.element.android.libraries.designsystem.theme.components.ButtonSize import io.element.android.libraries.designsystem.theme.components.FilledTextField import io.element.android.libraries.designsystem.theme.components.HorizontalDivider import io.element.android.libraries.designsystem.theme.components.Icon @@ -75,6 +81,7 @@ import io.element.android.libraries.designsystem.theme.components.LinearProgress import io.element.android.libraries.designsystem.theme.components.Scaffold import io.element.android.libraries.designsystem.theme.components.SegmentedButton import io.element.android.libraries.designsystem.theme.components.Text +import io.element.android.libraries.designsystem.theme.components.TextButton import io.element.android.libraries.designsystem.theme.components.TopAppBar import io.element.android.libraries.designsystem.utils.OnVisibleRangeChangeEffect import io.element.android.libraries.designsystem.utils.lazyColumnContentPadding @@ -155,10 +162,14 @@ private fun GlobalSearchContent( val historyResults = state.history.dataOrNull()?.toImmutableList() ?: persistentListOf() when { state.results.isUninitialized() -> if (state.queryState.text.isBlank() && historyResults.isNotEmpty()) { - searchHistory(historyResults) { result -> + searchHistory( + historyResults = historyResults, + onRemoveResult = { item -> state.eventSink(GlobalSearchEvent.RemoveSearchHistoryResult(item)) }, + onClearAllResults = { state.eventSink(GlobalSearchEvent.ClearSearchHistory) }, + ) { result -> when (result) { - is SearchHistoryResultItem.Query -> Unit - is SearchHistoryResultItem.Room -> onSelectSearchResult(result.roomInfo.id, null) + is SearchHistoryListItem.Query -> Unit + is SearchHistoryListItem.Room -> onSelectSearchResult(result.roomInfo.id, null) } state.eventSink(GlobalSearchEvent.SearchHistoryResultSelected(result)) @@ -214,7 +225,7 @@ private fun SearchTopAppBarContent(state: GlobalSearchState) { modifier = Modifier.clip(CircleShape).size(20.dp), onClick = { state.eventSink(GlobalSearchEvent.ClearQuery) }, colors = IconButtonDefaults.iconButtonColors( - containerColor = ElementTheme.colors.borderInteractivePrimary, + containerColor = ElementTheme.colors.iconSecondary, contentColor = ElementTheme.colors.iconOnSolidPrimary ) ) { @@ -279,31 +290,52 @@ private fun LazyListScope.startSearching() { } private fun LazyListScope.searchHistory( - historyResults: ImmutableList, - onSelectSearchHistoryResult: (SearchHistoryResultItem) -> Unit, + historyResults: ImmutableList, + onRemoveResult: (SearchHistoryListItem) -> Unit, + onClearAllResults: () -> Unit, + onSelectSearchHistoryResult: (SearchHistoryListItem) -> Unit, ) { item { - Text( - modifier = Modifier.padding(start = 16.dp, end = 16.dp, top = 16.dp, bottom = 8.dp), - text = stringResource(R.string.screen_search_recent_searches_section_title), - style = ElementTheme.typography.fontBodyLgMedium, - color = ElementTheme.colors.textPrimary, - ) + Row( + modifier = Modifier.padding(top = 16.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + Text( + modifier = Modifier.padding(start = 16.dp).weight(1f), + text = stringResource(R.string.screen_search_recent_searches_section_title), + style = ElementTheme.typography.fontBodyLgMedium, + color = ElementTheme.colors.textPrimary, + ) + + TextButton( + text = stringResource(CommonStrings.action_clear_all), + size = ButtonSize.Small, + colors = ButtonDefaults.textButtonColors(contentColor = ElementTheme.colors.textActionAccent), + onClick = onClearAllResults, + ) + } } items( items = historyResults, key = { it.id }, ) { result -> + val twoLineHeight = + // Vertical padding + 12.dp * 2 + + // Text line heights + ElementTheme.typography.fontBodyMdRegular.lineHeight.toDp() + + ElementTheme.typography.fontBodyLgRegular.lineHeight.toDp() Row( modifier = Modifier - .niceClickable(onClick = { onSelectSearchHistoryResult(result) }) + .clickable(onClick = { onSelectSearchHistoryResult(result) }) .fillMaxWidth() - .padding(horizontal = 16.dp, vertical = 12.dp), + .padding(start = 16.dp, end = 2.dp) + .height(twoLineHeight), horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically, ) { when (result) { - is SearchHistoryResultItem.Query -> { + is SearchHistoryListItem.Query -> { Icon( modifier = Modifier.size(32.dp) .clip(RoundedCornerShape(8.dp)) @@ -315,13 +347,14 @@ private fun LazyListScope.searchHistory( ) Text( + modifier = Modifier.weight(1f, fill = true), text = result.term, maxLines = 1, overflow = TextOverflow.Ellipsis, style = ElementTheme.typography.fontBodyLgRegular, ) } - is SearchHistoryResultItem.Room -> { + is SearchHistoryListItem.Room -> { val heroAvatars = remember(result.roomInfo.heroes) { result.roomInfo.heroes.map { it.getAvatarData(AvatarSize.SearchRoomListItem) }.toImmutableList() } @@ -333,7 +366,7 @@ private fun LazyListScope.searchHistory( ), ) - Column(modifier = Modifier.fillMaxWidth()) { + Column(modifier = Modifier.weight(1f, fill = true)) { Text( text = result.roomInfo.name.orEmpty(), maxLines = 1, @@ -365,6 +398,16 @@ private fun LazyListScope.searchHistory( } } } + + Icon( + modifier = Modifier.minimumInteractiveComponentSize() + .niceClickable { + onRemoveResult(result) + }, + imageVector = CompoundIcons.Close(), + contentDescription = stringResource(CommonStrings.action_remove), + tint = ElementTheme.colors.iconSecondary, + ) } } } @@ -451,7 +494,7 @@ private fun LazyListScope.roomListResults( } private fun LazyListScope.messageSearchResults( - results: ImmutableList, + results: ImmutableList, onSearchResultSelected: (RoomId, EventId?) -> Unit, ) { if (results.isNotEmpty()) { @@ -471,7 +514,7 @@ private fun LazyListScope.messageSearchResults( ) { index, result -> val clickableModifier = Modifier.niceClickable(onClick = { onSearchResultSelected(result.roomId, result.eventId) }) when (result) { - is MessageSearchResultItem.Message -> TextMessageSearchResultItemView( + is MessageSearchListItem.Message -> TextMessageSearchResultItemView( modifier = clickableModifier.addSeparatorLine(color = ElementTheme.colors.separatorSecondary, isLastItem = index == results.lastIndex), avatarData = result.roomInfo.getAvatarData(AvatarSize.RoomListItem), avatarType = AvatarType.Room( @@ -482,7 +525,7 @@ private fun LazyListScope.messageSearchResults( body = result.body, formattedTimestamp = result.formattedTimestamp, ) - is MessageSearchResultItem.Media -> { + is MessageSearchListItem.Media -> { MediaMessageSearchResultItemView( modifier = clickableModifier.addSeparatorLine(color = ElementTheme.colors.separatorSecondary, isLastItem = index == results.lastIndex), avatarData = result.roomInfo.getAvatarData(AvatarSize.RoomListItem), diff --git a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/DefaultSearchHistoryStore.kt b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/DefaultSearchHistoryStore.kt index 4da3456043..b8ab5a385c 100644 --- a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/DefaultSearchHistoryStore.kt +++ b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/DefaultSearchHistoryStore.kt @@ -54,6 +54,17 @@ class DefaultSearchHistoryStore( } } + override suspend fun remove(result: SearchHistoryResult) { + withContext(dispatchers.io) { + mutex.withLock { + ensureLoadedLocked() + val updated = cache.value.filterNot { it == result } + cache.value = updated + persist(updated) + } + } + } + override suspend fun clear() = withContext(dispatchers.io) { mutex.withLock { isLoaded = true diff --git a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/SearchHistoryResult.kt b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/SearchHistoryResult.kt index 1b883fa24e..2c37c1038d 100644 --- a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/SearchHistoryResult.kt +++ b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/SearchHistoryResult.kt @@ -18,9 +18,15 @@ import kotlinx.serialization.Serializable */ @Serializable sealed interface SearchHistoryResult { - @Serializable - data class Query(val term: String) : SearchHistoryResult + val id: String @Serializable - data class Room(val roomId: RoomId) : SearchHistoryResult + data class Query(val term: String) : SearchHistoryResult { + override val id: String = term + } + + @Serializable + data class Room(val roomId: RoomId) : SearchHistoryResult { + override val id: String = roomId.value + } } diff --git a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/SearchHistoryStore.kt b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/SearchHistoryStore.kt index 7718d32f77..f2c41f26f6 100644 --- a/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/SearchHistoryStore.kt +++ b/features/home/impl/src/main/kotlin/io/element/android/features/home/impl/search/history/SearchHistoryStore.kt @@ -30,6 +30,11 @@ interface SearchHistoryStore { */ suspend fun add(result: SearchHistoryResult) + /** + * Remove a [result] from the history. + */ + suspend fun remove(result: SearchHistoryResult) + /** * Remove every entry from the history. */ diff --git a/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/GlobalSearchPresenterTest.kt b/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/GlobalSearchPresenterTest.kt index 6d54c59912..0dda72b0fb 100644 --- a/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/GlobalSearchPresenterTest.kt +++ b/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/GlobalSearchPresenterTest.kt @@ -224,7 +224,7 @@ class GlobalSearchPresenterTest { val successState = consumeItemsUntilPredicate { it.results is AsyncData.Success }.last() val results = (successState.results.dataOrNull() as GlobalSearchResults.MessageSearchResults).results assertThat(results).hasSize(1) - assertThat(results.first()).isInstanceOf(MessageSearchResultItem.Message::class.java) + assertThat(results.first()).isInstanceOf(MessageSearchListItem.Message::class.java) cancelAndIgnoreRemainingEvents() } } @@ -251,7 +251,7 @@ class GlobalSearchPresenterTest { val successState = consumeItemsUntilPredicate { it.results is AsyncData.Success }.last() val results = (successState.results.dataOrNull() as GlobalSearchResults.MessageSearchResults).results assertThat(results).hasSize(1) - assertThat(results.first()).isInstanceOf(MessageSearchResultItem.Message::class.java) + assertThat(results.first()).isInstanceOf(MessageSearchListItem.Message::class.java) // Remove the query successState.queryState.edit { replace(0, length, "") } @@ -337,6 +337,71 @@ class GlobalSearchPresenterTest { } } + @Test + fun `present - RemoveSearchHistoryResult removes a query entry`() = runTest { + val searchHistoryStore = FakeSearchHistoryStore().apply { + add(SearchHistoryResult.Query("keep")) + add(SearchHistoryResult.Query("drop")) + } + val presenter = createGlobalSearchPresenter(searchHistoryStore = searchHistoryStore) + presenter.test { + val initialState = awaitItem() + initialState.eventSink(GlobalSearchEvent.RemoveSearchHistoryResult(SearchHistoryListItem.Query("drop"))) + testScheduler.advanceUntilIdle() + + assertThat(searchHistoryStore.history.first()).containsExactly(SearchHistoryResult.Query("keep")) + cancelAndIgnoreRemainingEvents() + } + } + + @Test + fun `present - RemoveSearchHistoryResult removes a room entry`() = runTest { + val searchHistoryStore = FakeSearchHistoryStore().apply { + add(SearchHistoryResult.Query("keep")) + add(SearchHistoryResult.Room(A_ROOM_ID)) + } + val presenter = createGlobalSearchPresenter(searchHistoryStore = searchHistoryStore) + presenter.test { + val initialState = awaitItem() + initialState.eventSink(GlobalSearchEvent.RemoveSearchHistoryResult(SearchHistoryListItem.Room(A_ROOM_ID, aRoomInfo()))) + testScheduler.advanceUntilIdle() + + assertThat(searchHistoryStore.history.first()).containsExactly(SearchHistoryResult.Query("keep")) + cancelAndIgnoreRemainingEvents() + } + } + + @Test + fun `present - RemoveSearchHistoryResult with an unknown id does nothing`() = runTest { + val searchHistoryStore = FakeSearchHistoryStore().apply { add(SearchHistoryResult.Query("keep")) } + val presenter = createGlobalSearchPresenter(searchHistoryStore = searchHistoryStore) + presenter.test { + val initialState = awaitItem() + initialState.eventSink(GlobalSearchEvent.RemoveSearchHistoryResult(SearchHistoryListItem.Query("query:unknown"))) + testScheduler.advanceUntilIdle() + + assertThat(searchHistoryStore.history.first()).containsExactly(SearchHistoryResult.Query("keep")) + cancelAndIgnoreRemainingEvents() + } + } + + @Test + fun `present - ClearSearchHistory removes every entry`() = runTest { + val searchHistoryStore = FakeSearchHistoryStore().apply { + add(SearchHistoryResult.Query("a")) + add(SearchHistoryResult.Room(A_ROOM_ID)) + } + val presenter = createGlobalSearchPresenter(searchHistoryStore = searchHistoryStore) + presenter.test { + val initialState = awaitItem() + initialState.eventSink(GlobalSearchEvent.ClearSearchHistory) + testScheduler.advanceUntilIdle() + + assertThat(searchHistoryStore.history.first()).isEmpty() + cancelAndIgnoreRemainingEvents() + } + } + @Test fun `present - SearchHistoryResultSelected does nothing for a room result`() = runTest { val searchHistoryStore = FakeSearchHistoryStore() @@ -345,7 +410,7 @@ class GlobalSearchPresenterTest { val initialState = awaitItem() skipItems(1) - initialState.eventSink(GlobalSearchEvent.SearchHistoryResultSelected(SearchHistoryResultItem.Room(A_ROOM_ID, aRoomInfo()))) + initialState.eventSink(GlobalSearchEvent.SearchHistoryResultSelected(SearchHistoryListItem.Room(A_ROOM_ID, aRoomInfo()))) testScheduler.advanceUntilIdle() ensureAllEventsConsumed() @@ -361,7 +426,7 @@ class GlobalSearchPresenterTest { // Skip loading search results state skipItems(1) - initialState.eventSink(GlobalSearchEvent.SearchHistoryResultSelected(SearchHistoryResultItem.Query("Test"))) + initialState.eventSink(GlobalSearchEvent.SearchHistoryResultSelected(SearchHistoryListItem.Query("Test"))) assertThat(awaitItem().queryState.text.toString()).isEqualTo("Test") assertThat(awaitItem().results.isLoading()).isTrue() diff --git a/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/GlobalSearchViewTest.kt b/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/GlobalSearchViewTest.kt index f63e21496e..45b3e52acf 100644 --- a/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/GlobalSearchViewTest.kt +++ b/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/GlobalSearchViewTest.kt @@ -162,7 +162,7 @@ class GlobalSearchViewTest : RobolectricTest() { results = AsyncData.Success( GlobalSearchResults.MessageSearchResults( persistentListOf( - MessageSearchResultItem.Message( + MessageSearchListItem.Message( messageSearchResult = aMessageSearchResult(roomId = roomId, eventId = eventId), body = "A message body", roomInfo = aRoomInfo(id = roomId, name = "A message room"), @@ -184,7 +184,7 @@ class GlobalSearchViewTest : RobolectricTest() { @Test fun `clicking on a history query result - emits the expected Event`() = runAndroidComposeUiTest { - val queryResult = SearchHistoryResultItem.Query("Recent term") + val queryResult = SearchHistoryListItem.Query("Recent term") val eventsRecorder = EventsRecorder() setGlobalSearchView( state = aGlobalSearchState( @@ -203,10 +203,47 @@ class GlobalSearchViewTest : RobolectricTest() { eventsRecorder.assertSingle(GlobalSearchEvent.SearchHistoryResultSelected(queryResult)) } + @Test + fun `clicking on the remove button of a history result - emits the expected Event`() = runAndroidComposeUiTest { + val queryResult = SearchHistoryListItem.Query("Recent term") + val eventsRecorder = EventsRecorder() + setGlobalSearchView( + state = aGlobalSearchState( + isSearchActive = true, + queryState = TextFieldState(), + results = AsyncData.Uninitialized, + history = AsyncData.Success(persistentListOf(queryResult)), + eventSink = eventsRecorder, + ), + ) + // Remove automatic initial events + eventsRecorder.clear() + onNodeWithContentDescription(activity!!.getString(CommonStrings.action_remove)).performClick() + eventsRecorder.assertSingle(GlobalSearchEvent.RemoveSearchHistoryResult(queryResult)) + } + + @Test + fun `clicking on clear all in the history - emits the expected Event`() = runAndroidComposeUiTest { + val eventsRecorder = EventsRecorder() + setGlobalSearchView( + state = aGlobalSearchState( + isSearchActive = true, + queryState = TextFieldState(), + results = AsyncData.Uninitialized, + history = AsyncData.Success(persistentListOf(SearchHistoryListItem.Query("Recent term"))), + eventSink = eventsRecorder, + ), + ) + // Remove automatic initial events + eventsRecorder.clear() + onNodeWithText("Clear all").performClick() + eventsRecorder.assertSingle(GlobalSearchEvent.ClearSearchHistory) + } + @Test fun `clicking on a history room result - invokes the callback and emits the expected Events`() = runAndroidComposeUiTest { val roomId = RoomId("!aHistoryRoom:server.org") - val roomResult = SearchHistoryResultItem.Room( + val roomResult = SearchHistoryListItem.Room( roomId = roomId, roomInfo = aRoomInfo(id = roomId, name = "A history room"), ) diff --git a/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/history/DefaultSearchHistoryStoreTest.kt b/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/history/DefaultSearchHistoryStoreTest.kt index a0a63755dc..fccd1bda93 100644 --- a/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/history/DefaultSearchHistoryStoreTest.kt +++ b/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/history/DefaultSearchHistoryStoreTest.kt @@ -89,6 +89,31 @@ class DefaultSearchHistoryStoreTest { ).inOrder() } + @Test + fun `remove deletes only the given entry and persists the change`() = runTest { + val fileStore = FakeSearchHistoryFileStore() + val store = createStore(fileStore) + store.add(SearchHistoryResult.Query("a")) + store.add(SearchHistoryResult.Room(A_ROOM_ID)) + store.add(SearchHistoryResult.Query("b")) + + store.remove(SearchHistoryResult.Room(A_ROOM_ID)) + + val expected = listOf(SearchHistoryResult.Query("b"), SearchHistoryResult.Query("a")) + assertThat(store.history.first()).containsExactlyElementsIn(expected).inOrder() + assertThat(createStore(fileStore).history.first()).containsExactlyElementsIn(expected).inOrder() + } + + @Test + fun `remove of an unknown entry leaves the history unchanged`() = runTest { + val store = createStore() + store.add(SearchHistoryResult.Query("a")) + + store.remove(SearchHistoryResult.Query("unknown")) + + assertThat(store.history.first()).containsExactly(SearchHistoryResult.Query("a")) + } + @Test fun `clear removes every entry and deletes the persisted content`() = runTest { val fileStore = FakeSearchHistoryFileStore() diff --git a/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/history/FakeSearchHistoryStore.kt b/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/history/FakeSearchHistoryStore.kt index 14d6a7ce65..d79f9002c6 100644 --- a/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/history/FakeSearchHistoryStore.kt +++ b/features/home/impl/src/test/kotlin/io/element/android/features/home/impl/search/history/FakeSearchHistoryStore.kt @@ -22,6 +22,10 @@ class FakeSearchHistoryStore : SearchHistoryStore { }.take(MAX_SEARCH_HISTORY_SIZE) } + override suspend fun remove(result: SearchHistoryResult) { + state.value = state.value.filterNot { it == result } + } + override suspend fun clear() { state.value = emptyList() } diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/Button.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/Button.kt index 299744cebf..a33bedc3c8 100644 --- a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/Button.kt +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/Button.kt @@ -109,6 +109,7 @@ fun TextButton( showProgress: Boolean = false, destructive: Boolean = false, leadingIcon: IconSource? = null, + colors: ButtonColors = ButtonStyle.Text.getColors(destructive), ) = ButtonInternal( text = text, onClick = onClick, @@ -118,7 +119,8 @@ fun TextButton( size = size, showProgress = showProgress, destructive = destructive, - leadingIcon = leadingIcon + leadingIcon = leadingIcon, + colors = colors, ) @Composable diff --git a/libraries/ui-strings/src/main/res/values/localazy.xml b/libraries/ui-strings/src/main/res/values/localazy.xml index 7a0ce8fd4e..e6ba1d5fc3 100644 --- a/libraries/ui-strings/src/main/res/values/localazy.xml +++ b/libraries/ui-strings/src/main/res/values/localazy.xml @@ -98,6 +98,7 @@ "Choose file" "Choose photo" "Clear" + "Clear all" "Close" "Complete verification" "Confirm" diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_1_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_1_en.png index c98a95267b..3a7e0ebef9 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_1_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_1_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bc4f291efc45cec7361e11b1981db69a962d37416a79dc460a0607a6bb4536aa -size 22961 +oid sha256:7690fa2bd7837112cd002d0ccbd9181a7e1fbb0ab53a47f317c5da1d82663fd7 +size 22941 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_2_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_2_en.png index 9f61ba64d8..4396b5f8f0 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_2_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_2_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ecdc3be22cbdbb5886d6e1e56d8054a538e3d84b37998398aeed618e1847fba2 -size 12605 +oid sha256:c6e2116d529443a0862815b36b9727a2d3ccbce5787c8ef7817367ad45093616 +size 12582 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_3_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_3_en.png index 601d2e6823..a85ed59b93 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_3_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_3_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:afc7a28ad19df48ece0f058599022b1d89e11c9eafe7d392e00ae1cb4be24cf1 -size 36369 +oid sha256:542668094780959aa0f0ffc57d4308aa88122b8bc9a314a5d8e1461d706e2b6c +size 36346 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_4_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_4_en.png index 8df455cc58..d9c5a98ff8 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_4_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_4_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:73264974cfae3cd80629bbd0e046dd9fe195997e2fe92beb42fae7d642171595 -size 77494 +oid sha256:4eb20fa2292fd683b1c4d4b22c4d9dd8fc58db9634ef92449d4a459f6d66673a +size 77483 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_5_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_5_en.png index 80e30284bc..82dc2f9440 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_5_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_5_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6a3db641f3074e582e5e7762e811f1ad6b6ac9f3b3de452466a39d7755ac4d1e -size 23910 +oid sha256:5a03168e5ab9b8a2f8d1436b6127b9d9802ed8249cb16a0efb6d6b8f37e17eab +size 23889 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_6_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_6_en.png index 2b232b1b57..fa8cca7bd0 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_6_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Day_6_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:46b6afded292b0ed12f943642974c75b3143b5ac6449bd0c83aeb1c998cccdd6 -size 53076 +oid sha256:2619c41c1c197eae28a7fb4bd09ccc4e10ab6e591450092a7810a6d697534aa4 +size 57108 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_1_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_1_en.png index 0e819d43bb..da25cdadf7 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_1_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_1_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:23ca745fa3b96db6d9d51575dbcd8a84f7cfc21bb714a55c925fd56a2fd549e9 -size 22343 +oid sha256:57d79b6d37f2ac347121456ece36b56b6414487356dbb6b839212ff74f86d235 +size 22391 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_2_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_2_en.png index fabd426934..2f54a0148c 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_2_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_2_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c84e890f4cb364ec0c450ade4daf16848f7fc1961d9d57d98acdc77474f042da -size 12103 +oid sha256:bf9a9faba2db7dbfba29f17d13bb68e66ebf761924fdb8b66b4ffe9f74db7dc1 +size 12146 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_3_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_3_en.png index 5f7a1c2a05..e3d8d867dc 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_3_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_3_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d7551aa6d84010695a2828828ca07977ecd9323289264ae3eaef22093102a1c8 -size 35953 +oid sha256:bb51e322215a812d987890e0d9e76769a1b7b97e0182b6cc0b239a48c044f551 +size 35990 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_4_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_4_en.png index 5330f9be34..fd0c18d5e4 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_4_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_4_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:8a40ad67305f16235b54d700975e3b0929b3cfbe3a0af3aa8f6756034c137696 -size 76067 +oid sha256:70bd1fd0debce99eebdecaee57bd3c1287575e53a5a16c8310e6a9805c24e221 +size 76088 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_5_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_5_en.png index 65429c1dc5..2ea51c60f8 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_5_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_5_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:07ea67ea3d87eb8227f0bb45d5129dd675eb8fdf272c2492370e62d23cf3e45b -size 23410 +oid sha256:923bd2ff7389c971ee578e519c9b99920007dbaee63a0134aed62f29996044c0 +size 23449 diff --git a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_6_en.png b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_6_en.png index 9eb44a0c9e..65c2f0b3df 100644 --- a/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_6_en.png +++ b/tests/uitests/src/test/snapshots/images/features.home.impl.search_GlobalSearchView_Night_6_en.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:dc84b79da7defdd738665606603cf84b72a4acf4de521c86ab715ae8460d416b -size 51588 +oid sha256:6b8437c10c51a1dcb80f0e32dc7618f00f5c59d44e9e1a339b515a8ca78a25a6 +size 55259