-
Notifications
You must be signed in to change notification settings - Fork 684
feat(feature/client): default to "Active" status and apply filters on refresh #2582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,7 @@ import androidx.compose.foundation.lazy.items | |||||
| import androidx.compose.material3.Text | ||||||
| import androidx.compose.runtime.Composable | ||||||
| import androidx.compose.runtime.LaunchedEffect | ||||||
| import androidx.compose.runtime.remember | ||||||
| import androidx.compose.ui.Modifier | ||||||
| import androidx.compose.ui.text.style.TextAlign | ||||||
| import androidx.paging.LoadState | ||||||
|
|
@@ -66,92 +67,89 @@ internal actual fun LazyColumnForClientListApi( | |||||
| is LoadState.NotLoading -> Unit | ||||||
| } | ||||||
|
|
||||||
| if (sort != null) { | ||||||
| val displayList = remember(clientPagingList.itemSnapshotList, sort) { | ||||||
| val currentItems = clientPagingList.itemSnapshotList.items | ||||||
|
|
||||||
| val sortedItems = when (sort) { | ||||||
| SortTypes.NAME -> { | ||||||
| currentItems.sortedBy { it.displayName?.lowercase() } | ||||||
| } | ||||||
| SortTypes.ACCOUNT_NUMBER -> { | ||||||
| currentItems.sortedBy { it.accountNo } | ||||||
| } | ||||||
| SortTypes.EXTERNAL_ID -> { | ||||||
| currentItems.sortedBy { it.externalId } | ||||||
| } | ||||||
| else -> currentItems | ||||||
| when (sort) { | ||||||
| SortTypes.NAME -> currentItems.sortedBy { it.displayName?.lowercase() } | ||||||
| SortTypes.ACCOUNT_NUMBER -> currentItems.sortedBy { it.accountNo } | ||||||
| SortTypes.EXTERNAL_ID -> currentItems.sortedBy { it.externalId } | ||||||
| else -> currentItems.sortedBy { getStatusOrder(it.status?.value) } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| LazyColumn( | ||||||
| modifier = modifier, | ||||||
| ) { | ||||||
| items( | ||||||
| items = sortedItems, | ||||||
| key = { client -> client.id }, | ||||||
| ) { client -> | ||||||
| LaunchedEffect(client.id) { | ||||||
| fetchImage(client.id) | ||||||
| } | ||||||
| ClientItem( | ||||||
| client = client, | ||||||
| byteArray = images[client.id], | ||||||
| onClientClick = onClientSelect, | ||||||
| ) | ||||||
| if (clientPagingList.itemCount > 0) { | ||||||
| LaunchedEffect(clientPagingList.itemCount, displayList.size) { | ||||||
| val lastRawIndex = clientPagingList.itemCount - 1 | ||||||
| if ((displayList.isEmpty() || displayList.size < 10) && lastRawIndex >= 0) { | ||||||
| clientPagingList[lastRawIndex] | ||||||
| } | ||||||
| } | ||||||
| } else { | ||||||
| LazyColumn( | ||||||
| modifier = modifier, | ||||||
| ) { | ||||||
| items( | ||||||
| count = clientPagingList.itemCount, | ||||||
| key = { index -> clientPagingList[index]?.id ?: index }, | ||||||
| ) { index -> | ||||||
| clientPagingList[index]?.let { client -> | ||||||
| LaunchedEffect(client.id) { | ||||||
| fetchImage(client.id) | ||||||
| } | ||||||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| LazyColumn(modifier = modifier) { | ||||||
| items( | ||||||
| items = displayList, | ||||||
| key = { client -> client.id }, | ||||||
| ) { client -> | ||||||
| LaunchedEffect(client.id) { fetchImage(client.id) } | ||||||
|
|
||||||
| if (client == displayList.last()) { | ||||||
| LaunchedEffect(Unit) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use this:
Suggested change
This ensures the effect only runs when the specific item changes. |
||||||
| val lastIndex = clientPagingList.itemCount - 1 | ||||||
| if (lastIndex >= 0) { | ||||||
| clientPagingList[lastIndex] | ||||||
| } | ||||||
| ClientItem( | ||||||
| client = client, | ||||||
| byteArray = images[client.id], | ||||||
| onClientClick = onClientSelect, | ||||||
| ) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| when (clientPagingList.loadState.append) { | ||||||
| is LoadState.Error -> { | ||||||
| item { | ||||||
| MifosSweetError(message = stringResource(Res.string.feature_client_failed_to_more_clients)) { | ||||||
| onRefresh() | ||||||
| } | ||||||
| ClientItem( | ||||||
| client = client, | ||||||
| byteArray = images[client.id], | ||||||
| onClientClick = onClientSelect, | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| when (clientPagingList.loadState.append) { | ||||||
| is LoadState.Error -> { | ||||||
| item { | ||||||
| MifosSweetError(message = stringResource(Res.string.feature_client_failed_to_more_clients)) { | ||||||
| onRefresh() | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| is LoadState.Loading -> { | ||||||
| item { | ||||||
| MifosPagingAppendProgress() | ||||||
| } | ||||||
| is LoadState.Loading -> { | ||||||
| item { | ||||||
| MifosPagingAppendProgress() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| is LoadState.NotLoading -> { | ||||||
| if (clientPagingList.loadState.append.endOfPaginationReached && | ||||||
| clientPagingList.itemCount > 0 | ||||||
| ) { | ||||||
| item { | ||||||
| Text( | ||||||
| modifier = Modifier | ||||||
| .fillMaxWidth() | ||||||
| .padding(bottom = DesignToken.padding.extraExtraLarge) | ||||||
| .padding(bottom = DesignToken.padding.extraExtraLarge), | ||||||
| text = stringResource(Res.string.feature_client_no_more_clients_available), | ||||||
| style = MifosTypography.bodyMedium, | ||||||
| textAlign = TextAlign.Center, | ||||||
| ) | ||||||
| } | ||||||
| is LoadState.NotLoading -> { | ||||||
| if (clientPagingList.loadState.append.endOfPaginationReached && | ||||||
| clientPagingList.itemCount > 0 | ||||||
| ) { | ||||||
| item { | ||||||
| Text( | ||||||
| modifier = Modifier | ||||||
| .fillMaxWidth() | ||||||
| .padding(bottom = DesignToken.padding.extraExtraLarge) | ||||||
| .padding(bottom = DesignToken.padding.extraExtraLarge), | ||||||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double padding. Please review. |
||||||
| text = stringResource(Res.string.feature_client_no_more_clients_available), | ||||||
| style = MifosTypography.bodyMedium, | ||||||
| textAlign = TextAlign.Center, | ||||||
| ) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| fun getStatusOrder(status: String?): Int { | ||||||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| return when (status?.lowercase()) { | ||||||
| "active" -> 1 | ||||||
| "pending" -> 2 | ||||||
| "closed" -> 3 | ||||||
| else -> 4 | ||||||
| } | ||||||
| } | ||||||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,6 @@ import com.mifos.core.ui.util.BaseViewModel | |
| import com.mifos.core.ui.util.imageToByteArray | ||
| import com.mifos.room.entities.client.ClientEntity | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.filter | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.flow.update | ||
|
|
@@ -149,23 +148,56 @@ internal class ClientListViewModel( | |
| ) | ||
| } | ||
|
|
||
| is DataState.Success -> updateState { | ||
| val data = result.data.pageItems | ||
| if (data.isEmpty()) { | ||
| it.copy(isEmpty = true, dialogState = null) | ||
| } else { | ||
| it.copy(clients = data, dialogState = null, unfilteredClients = data) | ||
| is DataState.Success -> { | ||
| updateState { state -> | ||
| val data = result.data.pageItems | ||
| if (data.isEmpty()) { | ||
| state.copy(isEmpty = true, dialogState = null) | ||
| } else { | ||
| val filteredData = data.filter { client -> | ||
| val statusMatch = state.selectedStatus.isEmpty() || client.status?.value in state.selectedStatus | ||
| val officeMatch = state.selectedOffices.isEmpty() || (client.officeName ?: "Null") in state.selectedOffices | ||
|
Comment on lines
+158
to
+159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These have been duplicated in three places:
You may do something like this: fun ClientEntity.matchesFilter(
statuses: List<String>,
offices: List<String>
): Boolean {
val statusMatch = statuses.isEmpty() || status?.value in statuses
val officeMatch = offices.isEmpty() || (officeName ?: "") in offices
return statusMatch && officeMatch
}Then, use it like this: filteredData.filter { it.matchesFilter(state.selectedStatus, state.selectedOffices) }
Comment on lines
+158
to
+159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use empty string Instead of "Null". |
||
| statusMatch && officeMatch | ||
| } | ||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fun getStatusOrder(status: String?): Int { | ||
| return when (status?.lowercase()) { | ||
| "active" -> 1 | ||
| "pending" -> 2 | ||
| "closed" -> 3 | ||
| else -> 4 | ||
| } | ||
| } | ||
| val sortedList = when (state.sort) { | ||
| SortTypes.NAME -> filteredData.sortedBy { it.displayName?.lowercase() } | ||
| SortTypes.ACCOUNT_NUMBER -> filteredData.sortedBy { it.accountNo } | ||
| SortTypes.EXTERNAL_ID -> filteredData.sortedBy { it.externalId } | ||
| else -> filteredData.sortedBy { getStatusOrder(it.status?.value) } | ||
| } | ||
| state.copy( | ||
| clients = sortedList, | ||
| unfilteredClients = data, | ||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dialogState = null, | ||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| } | ||
| } | ||
| } | ||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private fun handleClientResult(result: Flow<PagingData<ClientEntity>>) { | ||
| updateState { | ||
| updateState { state -> | ||
| val filteredFlow = result.map { pagingData -> | ||
| pagingData.filter { client -> | ||
| val statusMatch = state.selectedStatus.isEmpty() || client.status?.value in state.selectedStatus | ||
| val officeMatch = state.selectedOffices.isEmpty() || (client.officeName ?: "Null") in state.selectedOffices | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, please review. |
||
| statusMatch && officeMatch | ||
| } | ||
| } | ||
|
|
||
| state.copy( | ||
| clientsFlow = result, | ||
| dialogState = null, | ||
| clientsFlow = filteredFlow, | ||
| unfilteredClientsFlow = result, | ||
| dialogState = null, | ||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| } | ||
| } | ||
Kartikey15dem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we have sorting logic in three places:
ClientListViewModel.handleClientResultFromDbClientListViewModel.handleSortClickLazyColumnForClientListApicomposableIdeally, ViewModel should handle the sorting and UI displays it. Please review.