Skip to content
Open
Show file tree
Hide file tree
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 @@ -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
Expand Down Expand Up @@ -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) }
Comment on lines +73 to +77
Copy link
Contributor

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:

  1. ClientListViewModel.handleClientResultFromDb
  2. ClientListViewModel.handleSortClick
  3. LazyColumnForClientListApi composable

Ideally, ViewModel should handle the sorting and UI displays it. Please review.

}
}

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)
}

LazyColumn(modifier = modifier) {
items(
items = displayList,
key = { client -> client.id },
) { client ->
LaunchedEffect(client.id) { fetchImage(client.id) }

if (client == displayList.last()) {
LaunchedEffect(Unit) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use this:

Suggested change
LaunchedEffect(Unit) {
LaunchedEffect(clientId) {

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),
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
return when (status?.lowercase()) {
"active" -> 1
"pending" -> 2
"closed" -> 3
else -> 4
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor

@biplab1 biplab1 Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have been duplicated in three places:

  1. handleClientResultFromDb
  2. handleClientResult
  3. handleFilterClick

You may do something like this:
First extract

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use empty string Instead of "Null".

statusMatch && officeMatch
}
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,
dialogState = null,
)
}
}
}
}
}

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
)
}
}
Expand Down
Loading