diff --git a/feature/client/src/main/java/com/mifos/feature/client/clientList/presentation/ClientListScreen.kt b/feature/client/src/main/java/com/mifos/feature/client/clientList/presentation/ClientListScreen.kt index 79f149eb1b..6c52a78c16 100644 --- a/feature/client/src/main/java/com/mifos/feature/client/clientList/presentation/ClientListScreen.kt +++ b/feature/client/src/main/java/com/mifos/feature/client/clientList/presentation/ClientListScreen.kt @@ -44,15 +44,15 @@ import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.MutableState +import androidx.compose.runtime.State import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue -import androidx.compose.runtime.snapshots.SnapshotStateList import androidx.compose.runtime.toMutableStateList import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -104,27 +104,27 @@ internal fun ClientListScreen( val state = viewModel.clientListUiState.collectAsState().value - val isInSelectionMode = remember { mutableStateOf(false) } - val selectedItems = remember { mutableStateListOf() } + var isInSelectionMode by remember { mutableStateOf(false) } + val selectedItems = remember { ClientSelectionState() } val resetSelectionMode = { - isInSelectionMode.value = false + isInSelectionMode = false selectedItems.clear() } val sync = rememberSaveable { mutableStateOf(false) } - BackHandler(enabled = isInSelectionMode.value) { + BackHandler(enabled = isInSelectionMode) { resetSelectionMode() } LaunchedEffect( - key1 = isInSelectionMode.value, - key2 = selectedItems.size, + key1 = isInSelectionMode, + key2 = selectedItems.size(), ) { - if (isInSelectionMode.value && selectedItems.isEmpty()) { - isInSelectionMode.value = false + if (isInSelectionMode && selectedItems.isEmpty()) { + isInSelectionMode = false } } @@ -132,9 +132,9 @@ internal fun ClientListScreen( modifier = Modifier .padding(paddingValues), topBar = { - if (isInSelectionMode.value) { + if (isInSelectionMode) { SelectionModeTopAppBar( - currentSelectedItems = selectedItems, + currentSelectedItems = selectedItems.selectedItems.value, syncClicked = { sync.value = true }, resetSelectionMode = resetSelectionMode, ) @@ -172,10 +172,14 @@ internal fun ClientListScreen( clientPagingList = state.list.collectAsLazyPagingItems(), isInSelectionMode = isInSelectionMode, selectedItems = selectedItems, + onClientSelect = { + onClientSelect(it) + }, failedRefresh = { viewModel.refreshClientList() }, - ) { - onClientSelect(it) - } + selectedMode = { + isInSelectionMode = true + }, + ) } is ClientListUiState.ClientListDb -> { @@ -200,7 +204,7 @@ internal fun ClientListScreen( sync.value = false }, hide = { sync.value = false }, - list = selectedItems.toList(), + list = selectedItems.selectedItems.value, ) } } @@ -255,20 +259,46 @@ private fun SelectionModeTopAppBar( ) } +class ClientSelectionState(initialSelectedItems: List = emptyList()) { + private val _selectedItems = mutableStateListOf().also { it.addAll(initialSelectedItems) } + var selectedItems: State> = derivedStateOf { _selectedItems } + + fun add(client: Client) { + _selectedItems.add(client) + } + + fun remove(client: Client) { + _selectedItems.remove(client) + } + + fun contains(client: Client): Boolean { + return _selectedItems.contains(client) + } + fun isEmpty(): Boolean { + return _selectedItems.isEmpty() + } + + fun clear() { + _selectedItems.clear() + } + + fun size(): Int { + return _selectedItems.size + } + fun toList(): List { + return _selectedItems.toList() + } +} + @Composable private fun LazyColumnForClientListApi( clientPagingList: LazyPagingItems, - isInSelectionMode: MutableState, - selectedItems: SnapshotStateList, + isInSelectionMode: Boolean, + selectedItems: ClientSelectionState, failedRefresh: () -> Unit, onClientSelect: (Int) -> Unit, + selectedMode: () -> Unit, ) { -// val selectedItems = currentSelectedItems.toMutableStateList() -// val isInSelectionMode = rememberSaveable { -// mutableStateOf(inSelectionMode) -// } - /*ToDo Remove Snapshot State and Mutable state for the parameters*/ - when (clientPagingList.loadState.refresh) { is LoadState.Error -> { MifosSweetError(message = "Failed to Fetch Clients") { @@ -284,7 +314,7 @@ private fun LazyColumnForClientListApi( LazyColumn { items(clientPagingList.itemCount) { index -> - val isSelected = selectedItems.contains(clientPagingList[index]) + val isSelected = clientPagingList[index]?.let { selectedItems.contains(it) } var cardColor by remember { mutableStateOf(White) } OutlinedCard( @@ -292,9 +322,9 @@ private fun LazyColumnForClientListApi( .padding(6.dp) .combinedClickable( onClick = { - if (isInSelectionMode.value) { - cardColor = if (isSelected) { - selectedItems.remove(clientPagingList[index]) + if (isInSelectionMode) { + cardColor = if (isSelected == true) { + clientPagingList[index]?.let { selectedItems.remove(it) } White } else { clientPagingList[index]?.let { selectedItems.add(it) } @@ -305,16 +335,16 @@ private fun LazyColumnForClientListApi( } }, onLongClick = { - if (isInSelectionMode.value) { - cardColor = if (isSelected) { - selectedItems.remove(clientPagingList[index]) + if (isInSelectionMode) { + cardColor = if (isSelected == true) { + clientPagingList[index]?.let { selectedItems.remove(it) } White } else { clientPagingList[index]?.let { selectedItems.add(it) } LightGray } } else { - isInSelectionMode.value = true + selectedMode() clientPagingList[index]?.let { selectedItems.add(it) } cardColor = LightGray } diff --git a/feature/client/src/main/java/com/mifos/feature/client/createNewClient/CreateNewClientScreen.kt b/feature/client/src/main/java/com/mifos/feature/client/createNewClient/CreateNewClientScreen.kt index 2ca4212811..8ad9346ae0 100644 --- a/feature/client/src/main/java/com/mifos/feature/client/createNewClient/CreateNewClientScreen.kt +++ b/feature/client/src/main/java/com/mifos/feature/client/createNewClient/CreateNewClientScreen.kt @@ -52,7 +52,6 @@ import androidx.compose.material3.CheckboxDefaults import androidx.compose.material3.DatePicker import androidx.compose.material3.DatePickerDialog import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.MaterialTheme import androidx.compose.material3.SelectableDates import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.Text @@ -214,7 +213,7 @@ internal fun CreateNewClientScreen( Toast.makeText( context, stringResource(id = uiState.message), - Toast.LENGTH_SHORT + Toast.LENGTH_SHORT, ) .show() navigateBack.invoke() @@ -224,7 +223,7 @@ internal fun CreateNewClientScreen( Toast.makeText( context, stringResource(id = uiState.message), - Toast.LENGTH_SHORT + Toast.LENGTH_SHORT, ) .show() navigateBack.invoke() @@ -246,7 +245,7 @@ internal fun CreateNewClientScreen( } } } - } + } } @OptIn(ExperimentalMaterial3Api::class) @@ -451,7 +450,6 @@ private fun CreateNewClientContent( .fillMaxSize() .verticalScroll(state = scrollState), ) { - ClientImageSection(selectedImageUri = selectedImageUri) { showImagePickerDialog = true } diff --git a/feature/client/src/main/java/com/mifos/feature/client/navigation/ClientNavigation.kt b/feature/client/src/main/java/com/mifos/feature/client/navigation/ClientNavigation.kt index d0b5515d30..30165b93c2 100644 --- a/feature/client/src/main/java/com/mifos/feature/client/navigation/ClientNavigation.kt +++ b/feature/client/src/main/java/com/mifos/feature/client/navigation/ClientNavigation.kt @@ -46,7 +46,7 @@ fun NavGraphBuilder.clientNavGraph( activateClient: (Int) -> Unit, hasDatatables: KFunction4, Any?, Int, MutableList>, Unit>, onDocumentClicked: (Int, String) -> Unit, - onCardClicked: (Int, List) -> Unit + onCardClicked: (Int, List) -> Unit, ) { navigation( startDestination = ClientScreens.ClientListScreen.route, @@ -165,7 +165,7 @@ fun NavGraphBuilder.clientIdentifierRoute( ) { ClientIdentifiersScreen( onBackPressed = onBackPressed, - onDocumentClicked = {onDocumentClicked(it,Constants.ENTITY_TYPE_CLIENTS)}, + onDocumentClicked = { onDocumentClicked(it, Constants.ENTITY_TYPE_CLIENTS) }, ) } } @@ -240,16 +240,16 @@ fun NavGraphBuilder.clientSurveyQuestionRoute( fun NavGraphBuilder.createClientRoute( onBackPressed: () -> Unit, - hasDatatables: (List, ClientPayload, Int,MutableList>) -> Unit, + hasDatatables: (List, ClientPayload, Int, MutableList>) -> Unit, ) { composable( route = ClientScreens.CreateClientScreen.route, ) { CreateNewClientScreen( navigateBack = onBackPressed, - hasDatatables ={ datatables, clientPayload -> + hasDatatables = { datatables, clientPayload -> hasDatatables(datatables, clientPayload, Constants.CREATE_CLIENT, mutableListOf()) - } , + }, ) } }