Skip to content

Commit

Permalink
refactor: refactor client list screen to compose with realm and paging3
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-gupta99 committed Feb 21, 2024
1 parent 8a8b2d4 commit 56825eb
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

/**
* Created by Aditya Gupta on 21/02/24.
*/

class DatabaseClientQuery @Inject constructor(private val realm: Realm) {

fun getClientListFromDb(): Flow<Page<ClientDb>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.mifos.core.model

import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey

/**
* Created by Aditya Gupta on 21/02/24.
*/

class ClientDateDb : RealmObject {

Expand Down
4 changes: 4 additions & 0 deletions core/datastore/src/main/java/com/mifos/core/model/ClientDb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.Ignore
import io.realm.kotlin.types.annotations.PrimaryKey

/**
* Created by Aditya Gupta on 21/02/24.
*/

class ClientDb : RealmObject {

@PrimaryKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package com.mifos.core.model
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey

/**
* Created by Aditya Gupta on 21/02/24.
*/

class GroupDateDb : RealmObject {

@PrimaryKey
Expand Down
4 changes: 4 additions & 0 deletions core/datastore/src/main/java/com/mifos/core/model/GroupDb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.Ignore
import io.realm.kotlin.types.annotations.PrimaryKey

/**
* Created by Aditya Gupta on 21/02/24.
*/

class GroupDb : RealmObject {

@PrimaryKey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.mifos.core.model

import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey

/**
* Created by Aditya Gupta on 21/02/24.
*/

class StatusDb : RealmObject {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.mifos.core.model

/**
* Created by Aditya Gupta on 21/02/24.
*/

class TimelineDb(
var submittedOnDate: MutableList<Int> = ArrayList(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp

/**
* Created by Aditya Gupta on 21/02/24.
*/

@Composable
fun MifosAndroidClientIcon(id: Int) {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import com.mifos.core.designsystem.theme.BluePrimaryDark
import com.mifos.core.designsystem.theme.DarkGray
import com.mifos.core.designsystem.theme.White

/**
* Created by Aditya Gupta on 21/02/24.
*/

@Composable
fun MifosOutlinedTextField(
value: TextFieldValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mifos.core.designsystem.component

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
Expand All @@ -11,6 +12,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.mifos.core.designsystem.theme.DarkGray

/**
* Created by Aditya Gupta on 21/02/24.
*/

@Composable
fun MifosPagingAppendProgress() {
Expand All @@ -29,4 +35,22 @@ fun MifosPagingAppendProgress() {
)
}

}

@Composable
fun MifosCircularProgress() {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator(
modifier = Modifier
.width(60.dp)
.height(60.dp)
.padding(8.dp),
strokeWidth = 4.dp,
color = DarkGray
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.mifos.core.common.utils.Page
import com.mifos.core.data.model.client.Client
import com.mifos.core.model.ClientDb
import com.mifos.core.network.datamanger.DataManagerClient
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.suspendCancellableCoroutine
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

/**
* Created by Aditya Gupta on 21/02/24.
*/

class ClientListPagingSource(private val dataManagerClient: DataManagerClient) :
PagingSource<Int, Client>() {

override fun getRefreshKey(state: PagingState<Int, Client>): Int? {
return state.anchorPosition?.let { position ->
state.closestPageToPosition(position)?.prevKey?.plus(10) ?: state.closestPageToPosition(
Expand All @@ -31,8 +38,10 @@ class ClientListPagingSource(private val dataManagerClient: DataManagerClient) :
val getClients = getClientList(position)
val clientList = getClients.first
val totalClients = getClients.second
val clientDbList = getClientDbList()
val clientListWithSync = getClientListWithSync(clientList, clientDbList)
LoadResult.Page(
data = clientList,
data = clientListWithSync,
prevKey = if (position <= 0) null else position - 10,
nextKey = if (position >= totalClients) null else position + 10
)
Expand Down Expand Up @@ -61,4 +70,24 @@ class ClientListPagingSource(private val dataManagerClient: DataManagerClient) :
})
}
}

private suspend fun getClientDbList(): List<ClientDb> {
return dataManagerClient.allDatabaseClients().first().pageItems
}

private fun getClientListWithSync(
clientList: List<Client>,
clientDbList: List<ClientDb>
): List<Client> {
if (clientDbList.isEmpty()) {
clientList.forEach { client ->
clientDbList.forEach { clientDb ->
if (client.id == clientDb._id) {
client.sync = true
}
}
}
}
return clientList
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.activity.compose.BackHandler
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.border
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
Expand Down Expand Up @@ -58,6 +59,7 @@ import coil.compose.AsyncImage
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import com.mifos.core.data.model.client.Client
import com.mifos.core.designsystem.component.MifosCircularProgress
import com.mifos.core.designsystem.component.MifosPagingAppendProgress
import com.mifos.core.designsystem.theme.Black
import com.mifos.core.designsystem.theme.BlueSecondary
Expand All @@ -67,6 +69,10 @@ import com.mifos.core.designsystem.theme.White
import com.mifos.core.model.ClientDb
import com.mifos.feature.client.R

/**
* Created by Aditya Gupta on 21/02/24.
*/

@Composable
fun ClientListScreen(
createNewClient: () -> Unit,
Expand Down Expand Up @@ -128,17 +134,19 @@ fun ClientListScreen(
containerColor = White,
snackbarHost = { SnackbarHost(snackbarHostState) }
) { padding ->
SwipeRefresh(
state = swipeRefreshState,
onRefresh = {
viewModel.refreshClientList()
}) {
Column(
modifier = Modifier
.padding(padding)
) {
when (state) {
is ClientListUiState.ClientListApi -> {
when (state) {
is ClientListUiState.ClientListApi -> {

SwipeRefresh(
state = swipeRefreshState,
onRefresh = {
viewModel.refreshClientList()
}) {
Column(
modifier = Modifier
.padding(padding),
verticalArrangement = Arrangement.Center
) {
LazyColumnForClientListApi(
clientPagingList = state.list.collectAsLazyPagingItems(),
isInSelectionMode,
Expand All @@ -147,14 +155,17 @@ fun ClientListScreen(
onClientSelect(it)
}
}
}
}

is ClientListUiState.Empty -> {}
is ClientListUiState.ClientListDb -> {
LazyColumnForClientListDb(clientList = state.list)
}

is ClientListUiState.ClientListDb -> {
LazyColumnForClientListDb(clientList = state.list)
}
}
ClientListUiState.Empty -> {
}


}
}
}
Expand Down Expand Up @@ -213,6 +224,14 @@ fun LazyColumnForClientListApi(
onClientSelect: (Client) -> Unit
) {

when (clientPagingList.loadState.refresh) {
is LoadState.Error -> {}

is LoadState.Loading -> MifosCircularProgress()

is LoadState.NotLoading -> Unit
}


LazyColumn {
items(clientPagingList.itemCount) { index ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
* Created by Aditya Gupta on 21/02/24.
*/

@HiltViewModel
class ClientListViewModel @Inject constructor(
private val repository: ClientListRepository,
private val prefManager: PrefManager
) : ViewModel() {

private val _clientListUiState = MutableStateFlow<ClientListUiState>(ClientListUiState.Empty)
val clientListUiState = _clientListUiState.asStateFlow()

init {
getClientList()
}

private val _clientListUiState = MutableStateFlow<ClientListUiState>(ClientListUiState.Empty)
val clientListUiState = _clientListUiState.asStateFlow()

// for refresh feature
private val _isRefreshing = MutableStateFlow(false)
val isRefreshing = _isRefreshing.asStateFlow()
Expand All @@ -40,7 +44,8 @@ class ClientListViewModel @Inject constructor(
}

private fun loadClientsFromApi() = viewModelScope.launch(Dispatchers.IO) {
_clientListUiState.value = ClientListUiState.ClientListApi(repository.getAllClients())
val response = repository.getAllClients()
_clientListUiState.value = ClientListUiState.ClientListApi(response)
}

private fun loadClientsFromDb() = viewModelScope.launch(Dispatchers.IO) {
Expand Down

0 comments on commit 56825eb

Please sign in to comment.