Skip to content

Commit

Permalink
refactor #2415: client charge repository migrated to stateflow
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyushSingh07 authored and therajanmaurya committed Dec 28, 2023
1 parent e07e300 commit ec9a256
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 86 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/org/mifos/mobile/api/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ class DataManager @Inject constructor(
.getRecentTransactionsList(clientId, offset, limit)
}

suspend fun getClientCharges(clientId: Long): Response<Page<Charge?>?>? {
suspend fun getClientCharges(clientId: Long): Page<Charge> {
return baseApiManager.clientChargeApi.getClientChargeList(clientId).apply {
databaseHelper.syncCharges(this?.body())
databaseHelper.syncCharges(this)
}
}

suspend fun getLoanCharges(loanId: Long): Response<List<Charge?>?>? {
suspend fun getLoanCharges(loanId: Long): List<Charge> {
return baseApiManager.clientChargeApi.getLoanAccountChargeList(loanId)
}

suspend fun getSavingsCharges(savingsId: Long): Response<List<Charge?>?>? {
suspend fun getSavingsCharges(savingsId: Long): List<Charge> {
return baseApiManager.clientChargeApi.getSavingsAccountChargeList(savingsId)
}

Expand Down Expand Up @@ -205,7 +205,7 @@ class DataManager @Inject constructor(
return baseApiManager.registrationApi.verifyUser(userVerify)
}

suspend fun clientLocalCharges(): Response<Page<Charge?>?> = databaseHelper.clientCharges()
suspend fun clientLocalCharges(): Page<Charge?> = databaseHelper.clientCharges()

fun notifications(): List<MifosNotification> = databaseHelper.notifications()

Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/org/mifos/mobile/api/local/DatabaseHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.mifos.mobile.api.local

import com.raizlabs.android.dbflow.sql.language.SQLite
import io.reactivex.Observable
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.mifos.mobile.models.Charge
import org.mifos.mobile.models.Page
import org.mifos.mobile.models.notification.MifosNotification
Expand All @@ -17,21 +19,21 @@ import javax.inject.Singleton
*/
@Singleton
class DatabaseHelper @Inject constructor() {
fun syncCharges(charges: Page<Charge?>?): Response<Page<Charge?>?> {
fun syncCharges(charges: Page<Charge>?): Page<Charge>? {
if (charges != null) {
for (charge in charges.pageItems)
charge?.save()
charge.save()
}
return Response.success(charges)
return charges
}

fun clientCharges(): Response<Page<Charge?>?> {
fun clientCharges(): Page<Charge?> {
val charges = SQLite.select()
.from(Charge::class.java)
.queryList()
val chargePage = Page<Charge?>()
chargePage.pageItems = charges
return Response.success(chargePage)
return chargePage
}

fun notifications(): List<MifosNotification> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import retrofit2.http.Path
interface ClientChargeService {

@GET(ApiEndPoints.CLIENTS + "/{clientId}/charges")
suspend fun getClientChargeList(@Path("clientId") clientId: Long?): Response<Page<Charge?>?>?
suspend fun getClientChargeList(@Path("clientId") clientId: Long?): Page<Charge>

@GET(ApiEndPoints.LOANS + "/{loanId}/charges")
suspend fun getLoanAccountChargeList(@Path("loanId") loanId: Long?): Response<List<Charge?>?>?
suspend fun getLoanAccountChargeList(@Path("loanId") loanId: Long?): List<Charge>

@GET(ApiEndPoints.SAVINGS_ACCOUNTS + "/{savingsId}/charges")
suspend fun getSavingsAccountChargeList(@Path("savingsId") savingsId: Long?): Response<List<Charge?>?>?
suspend fun getSavingsAccountChargeList(@Path("savingsId") savingsId: Long?): List<Charge>
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.mifos.mobile.repositories

import kotlinx.coroutines.flow.Flow
import org.mifos.mobile.models.Charge
import org.mifos.mobile.models.Page
import retrofit2.Response

interface ClientChargeRepository {

suspend fun getClientCharges(clientId: Long): Response<Page<Charge?>?>?
suspend fun getLoanCharges(loanId: Long): Response<List<Charge?>?>?
suspend fun getSavingsCharges(savingsId: Long): Response<List<Charge?>?>?
suspend fun clientLocalCharges(): Response<Page<Charge?>?>
suspend fun getClientCharges(clientId: Long): Flow<Page<Charge>>
suspend fun getLoanCharges(loanId: Long): Flow<List<Charge>>
suspend fun getSavingsCharges(savingsId: Long): Flow<List<Charge>>
suspend fun clientLocalCharges(): Flow<Page<Charge?>>
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.mifos.mobile.repositories

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.Charge
import org.mifos.mobile.models.Page
Expand All @@ -9,19 +11,27 @@ import javax.inject.Inject
class ClientChargeRepositoryImp @Inject constructor(private val dataManager: DataManager) :
ClientChargeRepository {

override suspend fun getClientCharges(clientId: Long): Response<Page<Charge?>?>? {
return dataManager.getClientCharges(clientId)
override suspend fun getClientCharges(clientId: Long): Flow<Page<Charge>> {
return flow {
emit(dataManager.getClientCharges(clientId))
}
}

override suspend fun getLoanCharges(loanId: Long): Response<List<Charge?>?>? {
return dataManager.getLoanCharges(loanId)
override suspend fun getLoanCharges(loanId: Long): Flow<List<Charge>> {
return flow {
emit(dataManager.getLoanCharges(loanId))
}
}

override suspend fun getSavingsCharges(savingsId: Long): Response<List<Charge?>?>? {
return dataManager.getSavingsCharges(savingsId)
override suspend fun getSavingsCharges(savingsId: Long): Flow<List<Charge>> {
return flow {
emit(dataManager.getSavingsCharges(savingsId))
}
}

override suspend fun clientLocalCharges(): Response<Page<Charge?>?> {
return dataManager.clientLocalCharges()
override suspend fun clientLocalCharges(): Flow<Page<Charge?>> {
return flow {
emit(dataManager.clientLocalCharges())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.therajanmaurya.sweeterror.SweetUIErrorHandler
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.mifos.mobile.R
import org.mifos.mobile.databinding.FragmentClientChargeBinding
import org.mifos.mobile.models.Charge
Expand All @@ -21,7 +25,6 @@ import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.Network
import org.mifos.mobile.utils.Toaster
import org.mifos.mobile.viewModels.ClientChargeViewModel
import java.util.*

/**
* @author Vishwajeet
Expand All @@ -33,7 +36,7 @@ class ClientChargeFragment : BaseFragment() {
private var _binding: FragmentClientChargeBinding? = null
private val binding get() = _binding!!

private lateinit var viewModel: ClientChargeViewModel
private val viewModel: ClientChargeViewModel by viewModels()

private var clientChargeAdapter: ClientChargeAdapter? = null
private var id: Long? = 0
Expand All @@ -55,7 +58,6 @@ class ClientChargeFragment : BaseFragment() {
savedInstanceState: Bundle?,
): View {
_binding = FragmentClientChargeBinding.inflate(inflater, container, false)
viewModel = ViewModelProvider(this)[ClientChargeViewModel::class.java]
clientChargeAdapter = ClientChargeAdapter(::onItemClick)
setToolbarTitle(getString(R.string.charges))
sweetUIErrorHandler = SweetUIErrorHandler(activity, binding.root)
Expand All @@ -78,16 +80,23 @@ class ClientChargeFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.clientChargeUiState.observe(viewLifecycleOwner) {
when (it) {
is ClientChargeUiState.Loading -> showProgress()
is ClientChargeUiState.ShowError -> {
hideProgress()
showErrorFetchingClientCharges(getString(it.message))
}
is ClientChargeUiState.ShowClientCharges -> {
hideProgress()
showClientCharges(it.charges)
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.clientChargeUiState.collect {
when (it) {
is ClientChargeUiState.Loading -> showProgress()
is ClientChargeUiState.ShowError -> {
hideProgress()
showErrorFetchingClientCharges(getString(it.message))
}

is ClientChargeUiState.ShowClientCharges -> {
hideProgress()
showClientCharges(it.charges)
}

is ClientChargeUiState.Initial -> {}
}
}
}
}
Expand All @@ -101,9 +110,11 @@ class ClientChargeFragment : BaseFragment() {
super.onSaveInstanceState(outState)
outState.putParcelableArrayList(
Constants.CHARGES,
ArrayList<Parcelable>(
clientChargeList,
),
clientChargeList?.let {
ArrayList(
it,
)
},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.mifos.mobile.utils
import org.mifos.mobile.models.Charge

sealed class ClientChargeUiState {
object Initial : ClientChargeUiState()
object Loading : ClientChargeUiState()
data class ShowError(val message: Int) : ClientChargeUiState()
data class ShowClientCharges(val charges: List<Charge?>) : ClientChargeUiState()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.mifos.mobile.viewModels

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import org.mifos.mobile.R
import org.mifos.mobile.repositories.ClientChargeRepository
Expand All @@ -14,81 +16,55 @@ import javax.inject.Inject
class ClientChargeViewModel @Inject constructor(private val clientChargeRepositoryImp: ClientChargeRepository) :
ViewModel() {

private val _clientChargeUiState = MutableLiveData<ClientChargeUiState>()
val clientChargeUiState get() = _clientChargeUiState
private val _clientChargeUiState =
MutableStateFlow<ClientChargeUiState>(ClientChargeUiState.Initial)

val clientChargeUiState: StateFlow<ClientChargeUiState> get() = _clientChargeUiState

fun loadClientCharges(clientId: Long) {
viewModelScope.launch {
_clientChargeUiState.value = ClientChargeUiState.Loading
try {
val response = clientChargeRepositoryImp.getClientCharges(clientId)
if (response?.isSuccessful == true) {
_clientChargeUiState.value =
response.body()?.pageItems?.let { ClientChargeUiState.ShowClientCharges(it) }
} else {
_clientChargeUiState.value =
ClientChargeUiState.ShowError(R.string.client_charges)
}
} catch (e: Throwable) {
clientChargeRepositoryImp.getClientCharges(clientId).catch {
_clientChargeUiState.value =
ClientChargeUiState.ShowError(R.string.client_charges)
}.collect {
_clientChargeUiState.value = ClientChargeUiState.ShowClientCharges(it.pageItems)
}
}
}

fun loadLoanAccountCharges(loanId: Long) {
viewModelScope.launch {
_clientChargeUiState.value = ClientChargeUiState.Loading
try {
val response = clientChargeRepositoryImp.getLoanCharges(loanId)
if (response?.isSuccessful == true) {
_clientChargeUiState.value = response.body()
?.let { ClientChargeUiState.ShowClientCharges(it) }
} else {
_clientChargeUiState.value =
ClientChargeUiState.ShowError(R.string.client_charges)
}
} catch (e: Throwable) {
clientChargeRepositoryImp.getLoanCharges(loanId).catch {
_clientChargeUiState.value =
ClientChargeUiState.ShowError(R.string.client_charges)
}.collect {
_clientChargeUiState.value = ClientChargeUiState.ShowClientCharges(it)
}
}
}

fun loadSavingsAccountCharges(savingsId: Long) {
viewModelScope.launch {
_clientChargeUiState.value = ClientChargeUiState.Loading
try {
val response = clientChargeRepositoryImp.getSavingsCharges(savingsId)
if (response?.isSuccessful == true) {
_clientChargeUiState.value = response.body()
?.let { ClientChargeUiState.ShowClientCharges(it) }
} else {
_clientChargeUiState.value =
ClientChargeUiState.ShowError(R.string.client_charges)
}
} catch (e: Throwable) {
clientChargeRepositoryImp.getSavingsCharges(savingsId).catch {
_clientChargeUiState.value =
ClientChargeUiState.ShowError(R.string.client_charges)
}.collect {
_clientChargeUiState.value = ClientChargeUiState.ShowClientCharges(it)
}
}
}

fun loadClientLocalCharges() {
viewModelScope.launch {
_clientChargeUiState.value = ClientChargeUiState.Loading
try {
val response = clientChargeRepositoryImp.clientLocalCharges()
if (response.isSuccessful) {
_clientChargeUiState.value =
response.body()?.pageItems?.let { ClientChargeUiState.ShowClientCharges(it) }
} else {
_clientChargeUiState.value =
ClientChargeUiState.ShowError(R.string.client_charges)
}
} catch (e: Throwable) {
clientChargeRepositoryImp.clientLocalCharges().catch {
_clientChargeUiState.value =
ClientChargeUiState.ShowError(R.string.client_charges)
}.collect {
_clientChargeUiState.value = ClientChargeUiState.ShowClientCharges(it.pageItems)
}
}
}
Expand Down

0 comments on commit ec9a256

Please sign in to comment.