Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ package com.nextcloud.talk.conversationlist.ui

import android.content.res.Configuration
import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.expandHorizontally
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkHorizontally
import androidx.compose.foundation.clickable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -41,7 +47,6 @@ import androidx.compose.material3.BadgedBox
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
Expand All @@ -54,6 +59,7 @@ import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -129,15 +135,9 @@ fun ConversationListTopBar(
.windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal))
) {
when (val mode = state.mode) {
is TopBarMode.SearchBarIdle -> TopBarIdleContent(
is TopBarMode.SearchBarIdle, is TopBarMode.SearchActive -> TopBarSearchContent(
state = state,
actions = actions
)
is TopBarMode.SearchActive -> TopBarSearchActiveContent(
query = mode.query,
isLoading = state.isSearchLoading,
onQueryChange = actions.onSearchQueryChange,
onSearchClose = actions.onSearchClose,
actions = actions,
focusRequester = focusRequester
)
is TopBarMode.TitleBar -> TopBarTitleContent(
Expand All @@ -153,63 +153,163 @@ fun ConversationListTopBar(
}

@Composable
private fun TopBarIdleContent(state: ConversationListTopBarState, actions: ConversationListTopBarActions) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(start = 16.dp, end = 4.dp, top = 4.dp, bottom = 4.dp),
verticalAlignment = Alignment.CenterVertically
) {
IdleSearchBarCard(
state = state,
actions = actions,
private fun TopBarSearchContent(
state: ConversationListTopBarState,
actions: ConversationListTopBarActions,
focusRequester: FocusRequester
) {
val activeMode = state.mode as? TopBarMode.SearchActive
val isActive = activeMode != null
val endPadding by animateDpAsState(if (isActive) 16.dp else 4.dp, label = "searchBarEndPadding")
Box {
Row(
modifier = Modifier
.weight(1f)
.height(50.dp)
)
Spacer(modifier = Modifier.width(8.dp))
AvatarButton(
avatarUrl = state.avatarUrl,
credentials = state.credentials,
showBadge = state.showAvatarBadge,
onClick = actions.onAvatarClick
)
.fillMaxWidth()
.padding(start = 16.dp, end = endPadding, top = 4.dp, bottom = 4.dp),
verticalAlignment = Alignment.CenterVertically
) {
Card(
modifier = Modifier
.weight(1f)
.height(50.dp),
shape = RoundedCornerShape(25.dp),
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
) {
SearchBarCardContent(
state = state,
actions = actions,
query = activeMode?.query,
focusRequester = focusRequester
)
}
AnimatedVisibility(
visible = !isActive,
enter = fadeIn() + expandHorizontally(),
exit = fadeOut() + shrinkHorizontally()
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Spacer(modifier = Modifier.width(8.dp))
AvatarButton(
avatarUrl = state.avatarUrl,
credentials = state.credentials,
showBadge = state.showAvatarBadge,
onClick = actions.onAvatarClick
)
}
}
}
// Overlays the bottom edge so that it doesn't change the height of the top bar.
if (isActive && state.isSearchLoading) {
LinearProgressIndicator(
modifier = Modifier
.fillMaxWidth()
.align(Alignment.BottomStart),
color = MaterialTheme.colorScheme.primary,
trackColor = MaterialTheme.colorScheme.surface
)
}
}
}

/**
* Content of the rounded search card. [query] is null while the search is idle.
*/
@Composable
private fun IdleSearchBarCard(
private fun SearchBarCardContent(
state: ConversationListTopBarState,
actions: ConversationListTopBarActions,
modifier: Modifier = Modifier
query: String?,
focusRequester: FocusRequester
) {
Card(
modifier = modifier,
shape = RoundedCornerShape(25.dp),
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
val isActive = query != null
val showClearButton = !query.isNullOrEmpty()
val textStartPadding by animateDpAsState(if (isActive) 0.dp else 16.dp, label = "searchTextStartPadding")
val textEndPadding by animateDpAsState(
if (isActive && !showClearButton) 16.dp else 0.dp,
label = "searchTextEndPadding"
)
Row(
modifier = Modifier.fillMaxSize(),
verticalAlignment = Alignment.CenterVertically
) {
Box(modifier = Modifier.fillMaxSize()) {
Text(
text = stringResource(R.string.appbar_search_in, stringResource(R.string.nc_app_product_name)),
modifier = Modifier
.fillMaxWidth()
.align(Alignment.CenterStart)
.padding(start = 16.dp, end = if (state.showThreadsButton) 80.dp else 48.dp)
.clickable { actions.onSearchActivate() },
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
IdleSearchBarActions(
state = state,
actions = actions,
modifier = Modifier.align(Alignment.CenterEnd)
AnimatedVisibility(
visible = isActive,
enter = fadeIn() + expandHorizontally(),
exit = fadeOut() + shrinkHorizontally()
) {
SearchBackButton(onClick = {
actions.onSearchQueryChange("")
actions.onSearchClose()
})
}
val textModifier = Modifier
.weight(1f)
.padding(start = textStartPadding, end = textEndPadding)
if (query != null) {
SearchTextField(
query = query,
onQueryChange = actions.onSearchQueryChange,
focusRequester = focusRequester,
modifier = textModifier
)
LaunchedEffect(Unit) { focusRequester.requestFocus() }
} else {
SearchPlaceholder(modifier = textModifier.clickable { actions.onSearchActivate() })
}
AnimatedVisibility(
visible = showClearButton,
enter = fadeIn(),
exit = fadeOut()
) {
SearchClearButton(onClick = {
actions.onSearchQueryChange("")
focusRequester.requestFocus()
})
}
AnimatedVisibility(
visible = !isActive,
enter = fadeIn() + expandHorizontally(),
exit = fadeOut() + shrinkHorizontally()
) {
IdleSearchBarActions(state = state, actions = actions)
}
}
}

@Composable
private fun SearchPlaceholder(modifier: Modifier = Modifier) {
Text(
text = stringResource(R.string.appbar_search_in, stringResource(R.string.nc_app_product_name)),
modifier = modifier,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}

@Composable
private fun SearchBackButton(onClick: () -> Unit) {
IconButton(onClick = onClick) {
Icon(
imageVector = Icons.AutoMirrored.Outlined.ArrowBack,
contentDescription = stringResource(R.string.nc_cancel),
tint = MaterialTheme.colorScheme.onSurface
)
}
}

@Composable
private fun SearchClearButton(onClick: () -> Unit) {
IconButton(onClick = onClick) {
Icon(
imageVector = ImageVector.vectorResource(R.drawable.ic_close_search),
contentDescription = stringResource(R.string.nc_search_clear),
tint = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}

@Composable
private fun IdleSearchBarActions(
state: ConversationListTopBarState,
Expand Down Expand Up @@ -250,68 +350,6 @@ private fun IdleSearchBarActions(
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun TopBarSearchActiveContent(
query: String,
isLoading: Boolean,
onQueryChange: (String) -> Unit,
onSearchClose: () -> Unit,
focusRequester: FocusRequester
) {
Column {
TopAppBar(
navigationIcon = {
IconButton(onClick = {
onQueryChange("")
onSearchClose()
}) {
Icon(
imageVector = Icons.AutoMirrored.Outlined.ArrowBack,
contentDescription = stringResource(R.string.nc_cancel),
tint = MaterialTheme.colorScheme.onSurface
)
}
},
title = {
SearchTextField(
query = query,
onQueryChange = onQueryChange,
focusRequester = focusRequester,
modifier = Modifier.fillMaxWidth()
)
},
actions = {
if (query.isNotEmpty()) {
IconButton(onClick = {
onQueryChange("")
focusRequester.requestFocus()
}) {
Icon(
imageVector = ImageVector.vectorResource(R.drawable.ic_close_search),
contentDescription = stringResource(R.string.nc_search_clear),
tint = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.surface
),
windowInsets = WindowInsets(0)
)
if (isLoading) {
LinearProgressIndicator(
modifier = Modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.primary
)
} else {
HorizontalDivider()
}
}
LaunchedEffect(Unit) { focusRequester.requestFocus() }
}

@Composable
private fun SearchTextField(
query: String,
Expand All @@ -335,14 +373,7 @@ private fun SearchTextField(
decorationBox = { innerTextField ->
Box(contentAlignment = Alignment.CenterStart) {
if (query.isEmpty()) {
Text(
text = stringResource(
R.string.appbar_search_in,
stringResource(R.string.nc_app_product_name)
),
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
SearchPlaceholder()
}
innerTextField()
}
Expand Down
Loading