Skip to content

Commit a6b520c

Browse files
committed
feat(feature/client): default to "Active" status and apply filters on initial load
feat(feature/client): default to "Active" status and apply filters on refresh feat(feature/client): default to "Active" status and apply filters on refresh feat(feature/client): default to "Active" status and apply filters on refresh feat(feature/client): default to "Active" status and apply filters on refresh feat(feature/client): default to "Active" status and apply filters on refresh
1 parent fe7938c commit a6b520c

File tree

2 files changed

+108
-78
lines changed

2 files changed

+108
-78
lines changed

feature/client/src/androidMain/kotlin/com/mifos/feature/client/clientsList/ClientListScreen.android.kt

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.compose.foundation.lazy.items
2020
import androidx.compose.material3.Text
2121
import androidx.compose.runtime.Composable
2222
import androidx.compose.runtime.LaunchedEffect
23+
import androidx.compose.runtime.remember
2324
import androidx.compose.ui.Modifier
2425
import androidx.compose.ui.text.style.TextAlign
2526
import androidx.paging.LoadState
@@ -66,92 +67,89 @@ internal actual fun LazyColumnForClientListApi(
6667
is LoadState.NotLoading -> Unit
6768
}
6869

69-
if (sort != null) {
70+
val displayList = remember(clientPagingList.itemSnapshotList, sort) {
7071
val currentItems = clientPagingList.itemSnapshotList.items
7172

72-
val sortedItems = when (sort) {
73-
SortTypes.NAME -> {
74-
currentItems.sortedBy { it.displayName?.lowercase() }
75-
}
76-
SortTypes.ACCOUNT_NUMBER -> {
77-
currentItems.sortedBy { it.accountNo }
78-
}
79-
SortTypes.EXTERNAL_ID -> {
80-
currentItems.sortedBy { it.externalId }
81-
}
82-
else -> currentItems
73+
when (sort) {
74+
SortTypes.NAME -> currentItems.sortedBy { it.displayName?.lowercase() }
75+
SortTypes.ACCOUNT_NUMBER -> currentItems.sortedBy { it.accountNo }
76+
SortTypes.EXTERNAL_ID -> currentItems.sortedBy { it.externalId }
77+
else -> currentItems.sortedBy { getStatusOrder(it.status?.value) }
8378
}
79+
}
8480

85-
LazyColumn(
86-
modifier = modifier,
87-
) {
88-
items(
89-
items = sortedItems,
90-
key = { client -> client.id },
91-
) { client ->
92-
LaunchedEffect(client.id) {
93-
fetchImage(client.id)
94-
}
95-
ClientItem(
96-
client = client,
97-
byteArray = images[client.id],
98-
onClientClick = onClientSelect,
99-
)
81+
if (clientPagingList.itemCount > 0) {
82+
LaunchedEffect(clientPagingList.itemCount, displayList.size) {
83+
val lastRawIndex = clientPagingList.itemCount - 1
84+
if ((displayList.isEmpty() || displayList.size < 10) && lastRawIndex >= 0) {
85+
clientPagingList[lastRawIndex]
10086
}
10187
}
102-
} else {
103-
LazyColumn(
104-
modifier = modifier,
105-
) {
106-
items(
107-
count = clientPagingList.itemCount,
108-
key = { index -> clientPagingList[index]?.id ?: index },
109-
) { index ->
110-
clientPagingList[index]?.let { client ->
111-
LaunchedEffect(client.id) {
112-
fetchImage(client.id)
88+
}
89+
90+
LazyColumn(modifier = modifier) {
91+
items(
92+
items = displayList,
93+
key = { client -> client.id },
94+
) { client ->
95+
LaunchedEffect(client.id) { fetchImage(client.id) }
96+
97+
if (client == displayList.last()) {
98+
LaunchedEffect(Unit) {
99+
val lastIndex = clientPagingList.itemCount - 1
100+
if (lastIndex >= 0) {
101+
clientPagingList[lastIndex]
113102
}
114-
ClientItem(
115-
client = client,
116-
byteArray = images[client.id],
117-
onClientClick = onClientSelect,
118-
)
119103
}
120104
}
121105

122-
when (clientPagingList.loadState.append) {
123-
is LoadState.Error -> {
124-
item {
125-
MifosSweetError(message = stringResource(Res.string.feature_client_failed_to_more_clients)) {
126-
onRefresh()
127-
}
106+
ClientItem(
107+
client = client,
108+
byteArray = images[client.id],
109+
onClientClick = onClientSelect,
110+
)
111+
}
112+
113+
when (clientPagingList.loadState.append) {
114+
is LoadState.Error -> {
115+
item {
116+
MifosSweetError(message = stringResource(Res.string.feature_client_failed_to_more_clients)) {
117+
onRefresh()
128118
}
129119
}
120+
}
130121

131-
is LoadState.Loading -> {
132-
item {
133-
MifosPagingAppendProgress()
134-
}
122+
is LoadState.Loading -> {
123+
item {
124+
MifosPagingAppendProgress()
135125
}
126+
}
136127

137-
is LoadState.NotLoading -> {
138-
if (clientPagingList.loadState.append.endOfPaginationReached &&
139-
clientPagingList.itemCount > 0
140-
) {
141-
item {
142-
Text(
143-
modifier = Modifier
144-
.fillMaxWidth()
145-
.padding(bottom = DesignToken.padding.extraExtraLarge)
146-
.padding(bottom = DesignToken.padding.extraExtraLarge),
147-
text = stringResource(Res.string.feature_client_no_more_clients_available),
148-
style = MifosTypography.bodyMedium,
149-
textAlign = TextAlign.Center,
150-
)
151-
}
128+
is LoadState.NotLoading -> {
129+
if (clientPagingList.loadState.append.endOfPaginationReached &&
130+
clientPagingList.itemCount > 0
131+
) {
132+
item {
133+
Text(
134+
modifier = Modifier
135+
.fillMaxWidth()
136+
.padding(bottom = DesignToken.padding.extraExtraLarge)
137+
.padding(bottom = DesignToken.padding.extraExtraLarge),
138+
text = stringResource(Res.string.feature_client_no_more_clients_available),
139+
style = MifosTypography.bodyMedium,
140+
textAlign = TextAlign.Center,
141+
)
152142
}
153143
}
154144
}
155145
}
156146
}
157147
}
148+
fun getStatusOrder(status: String?): Int {
149+
return when (status?.lowercase()) {
150+
"active" -> 1
151+
"pending" -> 2
152+
"closed" -> 3
153+
else -> 4
154+
}
155+
}

feature/client/src/commonMain/kotlin/com/mifos/feature/client/clientsList/ClientListViewModel.kt

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import com.mifos.core.ui.util.BaseViewModel
2323
import com.mifos.core.ui.util.imageToByteArray
2424
import com.mifos.room.entities.client.ClientEntity
2525
import kotlinx.coroutines.flow.Flow
26-
import kotlinx.coroutines.flow.filter
2726
import kotlinx.coroutines.flow.first
2827
import kotlinx.coroutines.flow.map
2928
import kotlinx.coroutines.flow.update
@@ -149,23 +148,56 @@ internal class ClientListViewModel(
149148
)
150149
}
151150

152-
is DataState.Success -> updateState {
153-
val data = result.data.pageItems
154-
if (data.isEmpty()) {
155-
it.copy(isEmpty = true, dialogState = null)
156-
} else {
157-
it.copy(clients = data, dialogState = null, unfilteredClients = data)
151+
is DataState.Success -> {
152+
updateState { state ->
153+
val data = result.data.pageItems
154+
if (data.isEmpty()) {
155+
state.copy(isEmpty = true, dialogState = null)
156+
} else {
157+
val filteredData = data.filter { client ->
158+
val statusMatch = state.selectedStatus.isEmpty() || client.status?.value in state.selectedStatus
159+
val officeMatch = state.selectedOffices.isEmpty() || (client.officeName ?: "Null") in state.selectedOffices
160+
statusMatch && officeMatch
161+
}
162+
fun getStatusOrder(status: String?): Int {
163+
return when (status?.lowercase()) {
164+
"active" -> 1
165+
"pending" -> 2
166+
"closed" -> 3
167+
else -> 4
168+
}
169+
}
170+
val sortedList = when (state.sort) {
171+
SortTypes.NAME -> filteredData.sortedBy { it.displayName?.lowercase() }
172+
SortTypes.ACCOUNT_NUMBER -> filteredData.sortedBy { it.accountNo }
173+
SortTypes.EXTERNAL_ID -> filteredData.sortedBy { it.externalId }
174+
else -> filteredData.sortedBy { getStatusOrder(it.status?.value) }
175+
}
176+
state.copy(
177+
clients = sortedList,
178+
unfilteredClients = data,
179+
dialogState = null,
180+
)
181+
}
158182
}
159183
}
160184
}
161185
}
162186

163187
private fun handleClientResult(result: Flow<PagingData<ClientEntity>>) {
164-
updateState {
188+
updateState { state ->
189+
val filteredFlow = result.map { pagingData ->
190+
pagingData.filter { client ->
191+
val statusMatch = state.selectedStatus.isEmpty() || client.status?.value in state.selectedStatus
192+
val officeMatch = state.selectedOffices.isEmpty() || (client.officeName ?: "Null") in state.selectedOffices
193+
statusMatch && officeMatch
194+
}
195+
}
196+
165197
state.copy(
166-
clientsFlow = result,
167-
dialogState = null,
198+
clientsFlow = filteredFlow,
168199
unfilteredClientsFlow = result,
200+
dialogState = null,
169201
)
170202
}
171203
}

0 commit comments

Comments
 (0)