Skip to content

Commit

Permalink
refactor #2415: recent transaction viewmodel from to stateflow
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyushSingh07 committed Nov 3, 2023
1 parent 956d443 commit 4b7229f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 54 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/org/mifos/mobile/api/DataManager.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.mifos.mobile.api

import io.reactivex.Observable
import kotlinx.coroutines.flow.Flow
import okhttp3.ResponseBody
import org.mifos.mobile.api.local.DatabaseHelper
import org.mifos.mobile.api.local.PreferencesHelper
Expand Down Expand Up @@ -74,7 +73,7 @@ class DataManager @Inject constructor(
return baseApiManager.clientsApi.getAccounts(clientId, accountType)
}

suspend fun getRecentTransactions(offset: Int, limit: Int): Response<Page<Transaction?>?>? {
suspend fun getRecentTransactions(offset: Int, limit: Int): Page<Transaction> {
return baseApiManager.recentTransactionsApi
.getRecentTransactionsList(clientId, offset, limit)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.mifos.mobile.api.services
import org.mifos.mobile.api.ApiEndPoints
import org.mifos.mobile.models.Page
import org.mifos.mobile.models.Transaction
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
Expand All @@ -18,5 +17,5 @@ interface RecentTransactionsService {
@Path("clientId") clientId: Long?,
@Query("offset") offset: Int?,
@Query("limit") limit: Int?,
): Response<Page<Transaction?>?>?
): Page<Transaction>
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.mifos.mobile.repositories


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

interface RecentTransactionRepository {

suspend fun recentTransactions(
offset: Int?,
limit: Int?
): Response<Page<Transaction?>?>?
): Flow<Page<Transaction>>
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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.Page
import org.mifos.mobile.models.Transaction
import retrofit2.Response
import javax.inject.Inject

class RecentTransactionRepositoryImp @Inject constructor(private val dataManager: DataManager) :
Expand All @@ -12,7 +13,10 @@ class RecentTransactionRepositoryImp @Inject constructor(private val dataManager
override suspend fun recentTransactions(
offset: Int?,
limit: Int?
): Response<Page<Transaction?>?>? {
return limit?.let { offset?.let { it1 -> dataManager.getRecentTransactions(it1, it) } }
): Flow<Page<Transaction>> {
return flow {
offset?.let { limit?.let { it1 -> dataManager.getRecentTransactions(it, it1) } }
?.let { emit(it) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
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.FragmentRecentTransactionsBinding
import org.mifos.mobile.models.Transaction
Expand All @@ -37,7 +42,7 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {
@Inject
var recentTransactionsListAdapter: RecentTransactionListAdapter? = null

private lateinit var recentTransactionViewModel: RecentTransactionViewModel
private val recentTransactionViewModel: RecentTransactionViewModel by viewModels()

private var sweetUIErrorHandler: SweetUIErrorHandler? = null
private var recentTransactionList: MutableList<Transaction?>? = null
Expand All @@ -52,7 +57,6 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {
savedInstanceState: Bundle?,
): View {
_binding = FragmentRecentTransactionsBinding.inflate(inflater, container, false)
recentTransactionViewModel = ViewModelProvider(this)[RecentTransactionViewModel::class.java]
sweetUIErrorHandler = SweetUIErrorHandler(activity, binding.root)
showUserInterface()
setToolbarTitle(getString(R.string.recent_transactions))
Expand All @@ -65,24 +69,30 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

recentTransactionViewModel.recentTransactionUiState.observe(viewLifecycleOwner) {
when (it) {
is RecentTransactionUiState.Loading -> showProgress()
is RecentTransactionUiState.RecentTransactions -> {
hideProgress()
showRecentTransactions(it.transactions)
}
is RecentTransactionUiState.Error -> {
hideProgress()
showMessage(getString(it.message))
}
is RecentTransactionUiState.EmptyTransaction -> {
hideProgress()
showEmptyTransaction()
}
is RecentTransactionUiState.LoadMoreRecentTransactions -> {
hideProgress()
showLoadMoreRecentTransactions(it.transactions)
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
recentTransactionViewModel.recentTransactionUiState.collect{
when (it) {
is RecentTransactionUiState.Loading -> showProgress()
is RecentTransactionUiState.RecentTransactions -> {
hideProgress()
showRecentTransactions(it.transactions)
}
is RecentTransactionUiState.Error -> {
hideProgress()
showMessage(getString(it.message))
}
is RecentTransactionUiState.EmptyTransaction -> {
hideProgress()
showEmptyTransaction()
}
is RecentTransactionUiState.LoadMoreRecentTransactions -> {
hideProgress()
showLoadMoreRecentTransactions(it.transactions)
}

RecentTransactionUiState.Initial -> {}
}
}
}
}
Expand Down Expand Up @@ -163,7 +173,7 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {
/**
* Shows a Toast
*/
fun showMessage(message: String?) {
private fun showMessage(message: String?) {
(activity as BaseActivity?)?.showToast(message!!)
}

Expand All @@ -173,7 +183,7 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {
*
* @param recentTransactionList List of [Transaction]
*/
fun showRecentTransactions(recentTransactionList: List<Transaction?>?) {
private fun showRecentTransactions(recentTransactionList: List<Transaction?>?) {
this.recentTransactionList = recentTransactionList as MutableList<Transaction?>?
recentTransactionsListAdapter?.setTransactions(recentTransactionList)
}
Expand All @@ -188,7 +198,7 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {
recentTransactionsListAdapter?.notifyDataSetChanged()
}

fun resetUI() {
private fun resetUI() {
sweetUIErrorHandler?.hideSweetErrorLayoutUI(
binding.rvRecentTransactions,
binding.layoutError.root,
Expand All @@ -198,7 +208,7 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {
/**
* Hides `rvRecentTransactions` and shows a textview prompting no transactions
*/
fun showEmptyTransaction() {
private fun showEmptyTransaction() {
sweetUIErrorHandler?.showSweetEmptyUI(
getString(R.string.recent_transactions),
R.drawable.ic_error_black_24dp,
Expand Down Expand Up @@ -227,7 +237,7 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {
}
}

fun retryClicked() {
private fun retryClicked() {
if (isConnected(requireContext())) {
sweetUIErrorHandler?.hideSweetErrorLayoutUI(
binding.rvRecentTransactions,
Expand All @@ -251,7 +261,7 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {
showSwipeRefreshLayout(false)
}

fun showSwipeRefreshLayout(show: Boolean) {
private fun showSwipeRefreshLayout(show: Boolean) {
binding.swipeTransactionContainer.post {
binding.swipeTransactionContainer.isRefreshing = show
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package org.mifos.mobile.utils
import org.mifos.mobile.models.Transaction

sealed class RecentTransactionUiState {
object Initial : RecentTransactionUiState()
object Loading : RecentTransactionUiState()
object EmptyTransaction : RecentTransactionUiState()
data class Error(val message: Int) : RecentTransactionUiState()
data class RecentTransactions(val transactions: List<Transaction?>) : RecentTransactionUiState()
data class LoadMoreRecentTransactions(val transactions: List<Transaction?>) : RecentTransactionUiState()
data class LoadMoreRecentTransactions(val transactions: List<Transaction?>) :
RecentTransactionUiState()
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.mifos.mobile.viewModels

import androidx.lifecycle.LiveData
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.RecentTransactionRepository
Expand All @@ -18,8 +19,9 @@ class RecentTransactionViewModel @Inject constructor(private val recentTransacti
private val limit = 50
private var loadmore = false

private val _recentTransactionUiState = MutableLiveData<RecentTransactionUiState>()
val recentTransactionUiState: LiveData<RecentTransactionUiState> = _recentTransactionUiState
private val _recentTransactionUiState =
MutableStateFlow<RecentTransactionUiState>(RecentTransactionUiState.Initial)
val recentTransactionUiState: StateFlow<RecentTransactionUiState> = _recentTransactionUiState

fun loadRecentTransactions(loadmore: Boolean, offset: Int) {
this.loadmore = loadmore
Expand All @@ -29,23 +31,19 @@ class RecentTransactionViewModel @Inject constructor(private val recentTransacti
private fun loadRecentTransactions(offset: Int, limit: Int) {
viewModelScope.launch {
_recentTransactionUiState.value = RecentTransactionUiState.Loading
val response = recentTransactionRepositoryImp.recentTransactions(offset, limit)
if (response?.isSuccessful == true) {
if (response.body()?.totalFilteredRecords == 0) {
recentTransactionRepositoryImp.recentTransactions(offset, limit).catch {
_recentTransactionUiState.value =
RecentTransactionUiState.Error(R.string.recent_transactions)
}.collect {
if (it.totalFilteredRecords == 0) {
_recentTransactionUiState.value = RecentTransactionUiState.EmptyTransaction
} else if (loadmore && response.body()?.pageItems?.isNotEmpty() == true) {
} else if (loadmore && it.pageItems.isNotEmpty()) {
_recentTransactionUiState.value =
RecentTransactionUiState.LoadMoreRecentTransactions(it.pageItems)
} else if (it.pageItems.isNotEmpty()) {
_recentTransactionUiState.value =
RecentTransactionUiState.LoadMoreRecentTransactions(
response.body()!!.pageItems
)
} else if (response.body()?.pageItems?.isNotEmpty() == true) {
_recentTransactionUiState.value = RecentTransactionUiState.RecentTransactions(
response.body()?.pageItems!!
)
RecentTransactionUiState.RecentTransactions(it.pageItems)
}
} else {
_recentTransactionUiState.value =
RecentTransactionUiState.Error(R.string.recent_transactions)
}
}
}
Expand Down

0 comments on commit 4b7229f

Please sign in to comment.