Skip to content

Commit

Permalink
fix: show proper empty user search screens [WPB-6257] (#3589)
Browse files Browse the repository at this point in the history
  • Loading branch information
saleniuk authored Nov 7, 2024
1 parent fccf8c8 commit 8041a2d
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@

package com.wire.android.ui.home.conversations.search

import androidx.annotation.StringRes
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
Expand All @@ -37,34 +34,39 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.wire.android.R
import com.wire.android.model.Clickable
import com.wire.android.ui.common.button.WireSecondaryButton
import com.wire.android.ui.common.dimensions
import com.wire.android.ui.common.progress.WireCircularProgressIndicator
import com.wire.android.ui.common.progress.CenteredCircularProgressBarIndicator
import com.wire.android.ui.common.snackbar.LocalSnackbarHostState
import com.wire.android.ui.home.conversations.search.widget.SearchFailureBox
import com.wire.android.ui.home.conversationslist.model.Membership
import com.wire.android.ui.home.newconversation.SendConnectionRequestViewModel
import com.wire.android.ui.home.newconversation.SendConnectionRequestViewModelImpl
import com.wire.android.ui.home.newconversation.SendConnectionRequestViewModelPreview
import com.wire.android.ui.home.newconversation.model.Contact
import com.wire.android.ui.theme.WireTheme
import com.wire.android.util.extension.folderWithElements
import com.wire.android.util.ui.PreviewMultipleThemes
import com.wire.kalium.logic.data.user.ConnectionState
import com.wire.kalium.logic.data.user.UserId
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.ImmutableSet
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.persistentSetOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.launch

private const val DEFAULT_SEARCH_RESULT_ITEM_SIZE = 4

@Composable
fun SearchAllPeopleScreen(
searchQuery: String,
noneSearchSucceed: Boolean,
contactsSearchResult: ImmutableList<Contact>,
publicSearchResult: ImmutableList<Contact>,
contactsAddedToGroup: ImmutableSet<Contact>,
Expand All @@ -74,26 +76,24 @@ fun SearchAllPeopleScreen(
onOpenUserProfile: (Contact) -> Unit,
lazyListState: LazyListState = rememberLazyListState()
) {
if (contactsSearchResult.isEmpty() && publicSearchResult.isEmpty()) {
EmptySearchQueryScreen()
} else {
if (noneSearchSucceed) {
SearchFailureBox(R.string.label_no_results_found)
} else {
Column {
SearchResult(
searchQuery = searchQuery,
publicSearchResult = publicSearchResult,
contactsSearchResult = contactsSearchResult,
contactsAddedToGroup = contactsAddedToGroup,
onChecked = onChecked,
onOpenUserProfile = onOpenUserProfile,
lazyListState = lazyListState,
isSearchActive = isSearchActive,
isLoading = isLoading
)
}
}
val emptyResults = contactsSearchResult.isEmpty() && publicSearchResult.isEmpty()
when {
isLoading -> CenteredCircularProgressBarIndicator()

searchQuery.isBlank() && emptyResults -> EmptySearchQueryScreen()

searchQuery.isNotBlank() && emptyResults -> SearchFailureBox(R.string.label_no_results_found)

else -> SearchResult(
searchQuery = searchQuery,
publicSearchResult = publicSearchResult,
contactsSearchResult = contactsSearchResult,
contactsAddedToGroup = contactsAddedToGroup,
onChecked = onChecked,
onOpenUserProfile = onOpenUserProfile,
lazyListState = lazyListState,
isSearchActive = isSearchActive,
)
}
}

Expand All @@ -102,12 +102,13 @@ private fun SearchResult(
searchQuery: String,
contactsSearchResult: ImmutableList<Contact>,
publicSearchResult: ImmutableList<Contact>,
isLoading: Boolean,
isSearchActive: Boolean,
contactsAddedToGroup: ImmutableSet<Contact>,
onChecked: (Boolean, Contact) -> Unit,
onOpenUserProfile: (Contact) -> Unit,
sendConnectionRequestViewModel: SendConnectionRequestViewModel = hiltViewModel(),
sendConnectionRequestViewModel: SendConnectionRequestViewModel =
if (LocalInspectionMode.current) SendConnectionRequestViewModelPreview
else hiltViewModel<SendConnectionRequestViewModelImpl>(),
lazyListState: LazyListState = rememberLazyListState()
) {
val searchPeopleScreenState = rememberSearchPeopleScreenState()
Expand Down Expand Up @@ -141,10 +142,9 @@ private fun SearchResult(
internalSearchResults(
searchTitle = context.getString(R.string.label_contacts),
searchQuery = searchQuery,
contactsAddedToGroup = contactsAddedToGroup,
onChecked = onChecked,
isLoading = isLoading,
contactSearchResult = contactsSearchResult,
searchResult = contactsSearchResult,
contactsAddedToGroup = contactsAddedToGroup,
showAllItems = !isSearchActive || searchPeopleScreenState.contactsAllResultsCollapsed,
onShowAllButtonClicked = searchPeopleScreenState::toggleShowAllContactsResult,
onOpenUserProfile = onOpenUserProfile,
Expand All @@ -155,8 +155,7 @@ private fun SearchResult(
externalSearchResults(
searchTitle = context.getString(R.string.label_public_wire),
searchQuery = searchQuery,
contactSearchResult = publicSearchResult,
isLoading = isLoading,
searchResult = publicSearchResult,
showAllItems = searchPeopleScreenState.publicResultsCollapsed,
onShowAllButtonClicked = searchPeopleScreenState::toggleShowAllPublicResult,
onOpenUserProfile = onOpenUserProfile,
Expand All @@ -169,68 +168,6 @@ private fun SearchResult(

@Suppress("LongParameterList")
private fun LazyListScope.internalSearchResults(
searchTitle: String,
searchQuery: String,
contactsAddedToGroup: ImmutableSet<Contact>,
onChecked: (Boolean, Contact) -> Unit,
isLoading: Boolean,
contactSearchResult: ImmutableList<Contact>,
showAllItems: Boolean,
onShowAllButtonClicked: () -> Unit,
onOpenUserProfile: (Contact) -> Unit
) {
when {
isLoading -> {
inProgressItem()
}

else -> {
internalSuccessItem(
searchTitle = searchTitle,
showAllItems = showAllItems,
contactsAddedToGroup = contactsAddedToGroup,
onChecked = onChecked,
searchResult = contactSearchResult,
searchQuery = searchQuery,
onShowAllButtonClicked = onShowAllButtonClicked,
onOpenUserProfile = onOpenUserProfile
)
}
}
}

@Suppress("LongParameterList")
private fun LazyListScope.externalSearchResults(
searchTitle: String,
searchQuery: String,
contactSearchResult: ImmutableList<Contact>,
isLoading: Boolean,
showAllItems: Boolean,
onShowAllButtonClicked: () -> Unit,
onOpenUserProfile: (Contact) -> Unit,
onAddContactClicked: (UserId) -> Unit
) {
when {
isLoading -> {
inProgressItem()
}

else -> {
externalSuccessItem(
searchTitle = searchTitle,
showAllItems = showAllItems,
searchResult = contactSearchResult,
searchQuery = searchQuery,
onShowAllButtonClicked = onShowAllButtonClicked,
onOpenUserProfile = onOpenUserProfile,
onAddContactClicked = onAddContactClicked
)
}
}
}

@Suppress("LongParameterList")
private fun LazyListScope.internalSuccessItem(
searchTitle: String,
showAllItems: Boolean,
contactsAddedToGroup: ImmutableSet<Contact>,
Expand Down Expand Up @@ -283,7 +220,7 @@ private fun LazyListScope.internalSuccessItem(
}

@Suppress("LongParameterList")
private fun LazyListScope.externalSuccessItem(
private fun LazyListScope.externalSearchResults(
searchTitle: String,
showAllItems: Boolean,
searchResult: List<Contact>,
Expand Down Expand Up @@ -332,30 +269,6 @@ private fun LazyListScope.externalSuccessItem(
}
}

fun LazyListScope.inProgressItem() {
item {
Box(
Modifier
.fillMaxWidth()
.height(224.dp)
) {
WireCircularProgressIndicator(
progressColor = Color.Black,
modifier = Modifier.align(
Alignment.Center
)
)
}
}
}

fun LazyListScope.failureItem(@StringRes failureMessage: Int) {
item {
SearchFailureBox(failureMessage)
}
}

@OptIn(ExperimentalAnimationApi::class)
@Composable
private fun ShowButton(
isShownAll: Boolean,
Expand All @@ -382,3 +295,47 @@ fun PreviewShowButton() {
ShowButton(isShownAll = false, onShowButtonClicked = {})
}
}

@PreviewMultipleThemes
@Composable
fun PreviewSearchAllPeopleScreen_Loading() = WireTheme {
SearchAllPeopleScreen("Search query", persistentListOf(), persistentListOf(), persistentSetOf(), true, false, { _, _ -> }, {})
}

@PreviewMultipleThemes
@Composable
fun PreviewSearchAllPeopleScreen_InitialResults() = WireTheme {
val contacts = previewContactsList(count = 10, startIndex = 0, isContact = true).toPersistentList()
SearchAllPeopleScreen("", contacts, persistentListOf(), persistentSetOf(), false, false, { _, _ -> }, {})
}

@PreviewMultipleThemes
@Composable
fun PreviewSearchAllPeopleScreen_EmptyInitialResults() = WireTheme {
SearchAllPeopleScreen("", persistentListOf(), persistentListOf(), persistentSetOf(), false, false, { _, _ -> }, {})
}

@PreviewMultipleThemes
@Composable
fun PreviewSearchAllPeopleScreen_SearchResults() = WireTheme {
val contacts = previewContactsList(count = 10, startIndex = 0, isContact = true).toPersistentList()
val public = previewContactsList(count = 10, startIndex = 10, isContact = false).toPersistentList()
SearchAllPeopleScreen("Con", contacts, public, persistentSetOf(), false, true, { _, _ -> }, {})
}

@PreviewMultipleThemes
@Composable
fun PreviewSearchAllPeopleScreen_EmptySearchResults() = WireTheme {
SearchAllPeopleScreen("Con", persistentListOf(), persistentListOf(), persistentSetOf(), false, true, { _, _ -> }, {})
}

private fun previewContact(index: Int, isContact: Boolean) = Contact(
id = index.toString(),
domain = "wire.com",
name = "Contact nr $index",
connectionState = if (isContact) ConnectionState.ACCEPTED else ConnectionState.NOT_CONNECTED,
membership = Membership.Standard,
)

private fun previewContactsList(count: Int, startIndex: Int = 0, isContact: Boolean): List<Contact> =
buildList { repeat(count) { index -> add(previewContact(startIndex + index, isContact)) } }
Loading

0 comments on commit 8041a2d

Please sign in to comment.