Skip to content

Commit

Permalink
applying detekt on client module. (#2263)
Browse files Browse the repository at this point in the history
MIFOSAC-304
  • Loading branch information
Darkeye14 authored Dec 9, 2024
1 parent 54aa6bc commit 0e31a16
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -104,37 +104,37 @@ internal fun ClientListScreen(

val state = viewModel.clientListUiState.collectAsState().value

val isInSelectionMode = remember { mutableStateOf(false) }
val selectedItems = remember { mutableStateListOf<Client>() }
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
}
}

Scaffold(
modifier = Modifier
.padding(paddingValues),
topBar = {
if (isInSelectionMode.value) {
if (isInSelectionMode) {
SelectionModeTopAppBar(
currentSelectedItems = selectedItems,
currentSelectedItems = selectedItems.selectedItems.value,
syncClicked = { sync.value = true },
resetSelectionMode = resetSelectionMode,
)
Expand Down Expand Up @@ -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 -> {
Expand All @@ -200,7 +204,7 @@ internal fun ClientListScreen(
sync.value = false
},
hide = { sync.value = false },
list = selectedItems.toList(),
list = selectedItems.selectedItems.value,
)
}
}
Expand Down Expand Up @@ -255,20 +259,46 @@ private fun SelectionModeTopAppBar(
)
}

class ClientSelectionState(initialSelectedItems: List<Client> = emptyList()) {
private val _selectedItems = mutableStateListOf<Client>().also { it.addAll(initialSelectedItems) }
var selectedItems: State<List<Client>> = 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<Client> {
return _selectedItems.toList()
}
}

@Composable
private fun LazyColumnForClientListApi(
clientPagingList: LazyPagingItems<Client>,
isInSelectionMode: MutableState<Boolean>,
selectedItems: SnapshotStateList<Client>,
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") {
Expand All @@ -284,17 +314,17 @@ 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(
modifier = Modifier
.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) }
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -214,7 +213,7 @@ internal fun CreateNewClientScreen(
Toast.makeText(
context,
stringResource(id = uiState.message),
Toast.LENGTH_SHORT
Toast.LENGTH_SHORT,
)
.show()
navigateBack.invoke()
Expand All @@ -224,7 +223,7 @@ internal fun CreateNewClientScreen(
Toast.makeText(
context,
stringResource(id = uiState.message),
Toast.LENGTH_SHORT
Toast.LENGTH_SHORT,
)
.show()
navigateBack.invoke()
Expand All @@ -246,7 +245,7 @@ internal fun CreateNewClientScreen(
}
}
}
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -451,7 +450,6 @@ private fun CreateNewClientContent(
.fillMaxSize()
.verticalScroll(state = scrollState),
) {

ClientImageSection(selectedImageUri = selectedImageUri) {
showImagePickerDialog = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun NavGraphBuilder.clientNavGraph(
activateClient: (Int) -> Unit,
hasDatatables: KFunction4<List<DataTable>, Any?, Int, MutableList<List<FormWidget>>, Unit>,
onDocumentClicked: (Int, String) -> Unit,
onCardClicked: (Int, List<Survey>) -> Unit
onCardClicked: (Int, List<Survey>) -> Unit,
) {
navigation(
startDestination = ClientScreens.ClientListScreen.route,
Expand Down Expand Up @@ -165,7 +165,7 @@ fun NavGraphBuilder.clientIdentifierRoute(
) {
ClientIdentifiersScreen(
onBackPressed = onBackPressed,
onDocumentClicked = {onDocumentClicked(it,Constants.ENTITY_TYPE_CLIENTS)},
onDocumentClicked = { onDocumentClicked(it, Constants.ENTITY_TYPE_CLIENTS) },
)
}
}
Expand Down Expand Up @@ -240,16 +240,16 @@ fun NavGraphBuilder.clientSurveyQuestionRoute(

fun NavGraphBuilder.createClientRoute(
onBackPressed: () -> Unit,
hasDatatables: (List<DataTable>, ClientPayload, Int,MutableList<List<FormWidget>>) -> Unit,
hasDatatables: (List<DataTable>, ClientPayload, Int, MutableList<List<FormWidget>>) -> Unit,
) {
composable(
route = ClientScreens.CreateClientScreen.route,
) {
CreateNewClientScreen(
navigateBack = onBackPressed,
hasDatatables ={ datatables, clientPayload ->
hasDatatables = { datatables, clientPayload ->
hasDatatables(datatables, clientPayload, Constants.CREATE_CLIENT, mutableListOf())
} ,
},
)
}
}
Expand Down

0 comments on commit 0e31a16

Please sign in to comment.