From 64f4bcc6fcd0767aa4baf242122af23415b604c7 Mon Sep 17 00:00:00 2001 From: Diogo Cunha Date: Wed, 17 Jul 2024 23:12:54 +0100 Subject: [PATCH] android, desktop: made full new chat sheet scrollable --- .../common/views/contacts/ContactsView.kt | 111 +++++++++++------- .../common/views/newchat/NewChatSheet.kt | 10 +- 2 files changed, 75 insertions(+), 46 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/contacts/ContactsView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/contacts/ContactsView.kt index e699f9bc7e..4b99f8b697 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/contacts/ContactsView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/contacts/ContactsView.kt @@ -13,6 +13,7 @@ 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.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width @@ -46,6 +47,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.text.input.TextFieldValue +import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import chat.simplex.common.model.Chat @@ -60,6 +62,7 @@ import chat.simplex.common.platform.chatModel import chat.simplex.common.platform.getKeyboardState import chat.simplex.common.ui.theme.DEFAULT_PADDING import chat.simplex.common.ui.theme.DEFAULT_PADDING_HALF +import chat.simplex.common.views.chatlist.ScrollDirection import chat.simplex.common.views.helpers.AppBarTitle import chat.simplex.common.views.helpers.KeyChangeEffect import chat.simplex.common.views.helpers.KeyboardState @@ -170,33 +173,13 @@ private fun ContactsLayout( contactActions: @Composable () -> Unit, contactTypes: List, contactListTitle: String? = null) { - val listState = rememberLazyListState(lazyListState.first, lazyListState.second) - var searchFocused by remember { mutableStateOf(false) } - val searchText = rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf( - TextFieldValue("") - ) } SectionView { - Divider() - ContactsSearchBar( - listState = listState, - searchText = searchText, - focused = searchFocused, - onFocusChanged = { - searchFocused = it - } + ContactsList( + contactTypes = contactTypes, + contactActions = contactActions, + contactListTitle = contactListTitle ) - Divider() - } - - if (!searchFocused) { - contactActions() - } - - Spacer(Modifier.height(DEFAULT_PADDING)) - - SectionView(title = contactListTitle, padding = PaddingValues(DEFAULT_PADDING)) { - ContactsList(listState = listState, chatModel = chatModel, searchText = searchText, contactTypes = contactTypes) // TODO: Empty list states. } } @@ -286,8 +269,19 @@ fun ToggleFilterButton() { private var lazyListState = 0 to 0 @Composable -fun ContactsList(listState: LazyListState, chatModel: ChatModel, searchText: MutableState, contactTypes: List) { +fun ContactsList( + contactActions: @Composable () -> Unit, + contactTypes: List, + contactListTitle: String ? = null +) { val oneHandUI = remember { chatModel.controller.appPrefs.oneHandUI } + val listState = rememberLazyListState(lazyListState.first, lazyListState.second) + val searchText = rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf( + TextFieldValue("") + ) } + + var searchFocused by remember { mutableStateOf(false) } + DisposableEffect(Unit) { onDispose { lazyListState = @@ -296,9 +290,11 @@ fun ContactsList(listState: LazyListState, chatModel: ChatModel, searchText: Mut } val showUnreadAndFavorites = remember { ChatController.appPrefs.showUnreadAndFavorites.state }.value - val allChats = remember { contactChats( - chatModel.chats, - contactTypes) + val allChats = remember { + contactChats( + chatModel.chats, + contactTypes + ) } val filteredContactChats = filteredContactChats( @@ -307,29 +303,58 @@ fun ContactsList(listState: LazyListState, chatModel: ChatModel, searchText: Mut contactChats = allChats ) + LazyColumn( + Modifier.fillMaxWidth(), + listState + ) { + item { + SectionView { + Divider() + ContactsSearchBar( + listState = listState, + searchText = searchText, + focused = searchFocused, + onFocusChanged = { + searchFocused = it + } + ) + Divider() + } + + if (!searchFocused) { + contactActions() + } + + Spacer(Modifier.height(DEFAULT_PADDING)) + + if (contactListTitle != null) { + Text( + contactListTitle, color = MaterialTheme.colors.secondary, style = MaterialTheme.typography.body2, + modifier = Modifier.padding(start = DEFAULT_PADDING, bottom = 5.dp), fontSize = 12.sp + ) + } + } + itemsIndexed(filteredContactChats) { index, chat -> + val nextChatSelected = remember(chat.id, filteredContactChats) { + derivedStateOf { + chatModel.chatId.value != null && filteredContactChats.getOrNull(index + 1)?.id == chatModel.chatId.value + } + } + SectionItemView(padding = PaddingValues(horizontal = DEFAULT_PADDING)) { + ContactListNavLinkView(chat, nextChatSelected, oneHandUI.state) + } + } + } + if (filteredContactChats.isEmpty() && allChats.isNotEmpty()) { Column(Modifier.fillMaxSize().padding(DEFAULT_PADDING)) { - Box(Modifier.fillMaxWidth() ,contentAlignment = Alignment.Center) { + Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) { Text( generalGetString(MR.strings.no_filtered_contacts), color = MaterialTheme.colors.secondary ) } } - } else { - LazyColumn( - Modifier.fillMaxWidth(), - listState - ) { - itemsIndexed(filteredContactChats) { index, chat -> - val nextChatSelected = remember(chat.id, filteredContactChats) { - derivedStateOf { - chatModel.chatId.value != null && filteredContactChats.getOrNull(index + 1)?.id == chatModel.chatId.value - } - } - ContactListNavLinkView(chat, nextChatSelected, oneHandUI.state) - } - } } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/newchat/NewChatSheet.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/newchat/NewChatSheet.kt index e94721d07f..d2a45dac42 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/newchat/NewChatSheet.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/newchat/NewChatSheet.kt @@ -89,9 +89,13 @@ fun NewChatButton(icon: Painter, text: String, click: () -> Unit, textColor: Col fun NewChatOptions(addContact: () -> Unit, scanPaste: () -> Unit, createGroup: () -> Unit) { val actions = remember { listOf(addContact, scanPaste, createGroup) } - LazyColumn { - items(actions.size) { index -> - NewChatButton(icon = painterResource(icons[index]), text = stringResource(titles[index]), click = actions[index], extraPadding = true) + Column { + actions.forEachIndexed { index, _ -> + NewChatButton( + icon = painterResource(icons[index]), + text = stringResource(titles[index]), + click = actions[index], + extraPadding = true) } } }