diff --git a/app/src/main/java/org/mifos/mobile/api/DataManager.kt b/app/src/main/java/org/mifos/mobile/api/DataManager.kt index 259d60bf4..301b01220 100644 --- a/app/src/main/java/org/mifos/mobile/api/DataManager.kt +++ b/app/src/main/java/org/mifos/mobile/api/DataManager.kt @@ -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 @@ -74,7 +73,7 @@ class DataManager @Inject constructor( return baseApiManager.clientsApi.getAccounts(clientId, accountType) } - suspend fun getRecentTransactions(offset: Int, limit: Int): Response?>? { + suspend fun getRecentTransactions(offset: Int, limit: Int): Page { return baseApiManager.recentTransactionsApi .getRecentTransactionsList(clientId, offset, limit) } diff --git a/app/src/main/java/org/mifos/mobile/api/services/RecentTransactionsService.kt b/app/src/main/java/org/mifos/mobile/api/services/RecentTransactionsService.kt index 68aad7067..10507e325 100644 --- a/app/src/main/java/org/mifos/mobile/api/services/RecentTransactionsService.kt +++ b/app/src/main/java/org/mifos/mobile/api/services/RecentTransactionsService.kt @@ -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 @@ -18,5 +17,5 @@ interface RecentTransactionsService { @Path("clientId") clientId: Long?, @Query("offset") offset: Int?, @Query("limit") limit: Int?, - ): Response?>? + ): Page } diff --git a/app/src/main/java/org/mifos/mobile/repositories/RecentTransactionRepository.kt b/app/src/main/java/org/mifos/mobile/repositories/RecentTransactionRepository.kt index bdd004df4..6a4fc7200 100644 --- a/app/src/main/java/org/mifos/mobile/repositories/RecentTransactionRepository.kt +++ b/app/src/main/java/org/mifos/mobile/repositories/RecentTransactionRepository.kt @@ -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?>? + ): Flow> } \ No newline at end of file diff --git a/app/src/main/java/org/mifos/mobile/repositories/RecentTransactionRepositoryImp.kt b/app/src/main/java/org/mifos/mobile/repositories/RecentTransactionRepositoryImp.kt index a43f978b9..87fa6e408 100644 --- a/app/src/main/java/org/mifos/mobile/repositories/RecentTransactionRepositoryImp.kt +++ b/app/src/main/java/org/mifos/mobile/repositories/RecentTransactionRepositoryImp.kt @@ -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) : @@ -12,7 +13,10 @@ class RecentTransactionRepositoryImp @Inject constructor(private val dataManager override suspend fun recentTransactions( offset: Int?, limit: Int? - ): Response?>? { - return limit?.let { offset?.let { it1 -> dataManager.getRecentTransactions(it1, it) } } + ): Flow> { + return flow { + offset?.let { limit?.let { it1 -> dataManager.getRecentTransactions(it, it1) } } + ?.let { emit(it) } + } } } \ No newline at end of file diff --git a/app/src/main/java/org/mifos/mobile/ui/fragments/RecentTransactionsFragment.kt b/app/src/main/java/org/mifos/mobile/ui/fragments/RecentTransactionsFragment.kt index fa64eacc9..7f98534f0 100644 --- a/app/src/main/java/org/mifos/mobile/ui/fragments/RecentTransactionsFragment.kt +++ b/app/src/main/java/org/mifos/mobile/ui/fragments/RecentTransactionsFragment.kt @@ -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 @@ -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? = null @@ -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)) @@ -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 -> {} + } } } } @@ -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!!) } @@ -173,7 +183,7 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener { * * @param recentTransactionList List of [Transaction] */ - fun showRecentTransactions(recentTransactionList: List?) { + private fun showRecentTransactions(recentTransactionList: List?) { this.recentTransactionList = recentTransactionList as MutableList? recentTransactionsListAdapter?.setTransactions(recentTransactionList) } @@ -188,7 +198,7 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener { recentTransactionsListAdapter?.notifyDataSetChanged() } - fun resetUI() { + private fun resetUI() { sweetUIErrorHandler?.hideSweetErrorLayoutUI( binding.rvRecentTransactions, binding.layoutError.root, @@ -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, @@ -227,7 +237,7 @@ class RecentTransactionsFragment : BaseFragment(), OnRefreshListener { } } - fun retryClicked() { + private fun retryClicked() { if (isConnected(requireContext())) { sweetUIErrorHandler?.hideSweetErrorLayoutUI( binding.rvRecentTransactions, @@ -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 } diff --git a/app/src/main/java/org/mifos/mobile/utils/RecentTransactionUiState.kt b/app/src/main/java/org/mifos/mobile/utils/RecentTransactionUiState.kt index 2838e9512..5fa15e671 100644 --- a/app/src/main/java/org/mifos/mobile/utils/RecentTransactionUiState.kt +++ b/app/src/main/java/org/mifos/mobile/utils/RecentTransactionUiState.kt @@ -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) : RecentTransactionUiState() - data class LoadMoreRecentTransactions(val transactions: List) : RecentTransactionUiState() + data class LoadMoreRecentTransactions(val transactions: List) : + RecentTransactionUiState() } diff --git a/app/src/main/java/org/mifos/mobile/viewModels/RecentTransactionViewModel.kt b/app/src/main/java/org/mifos/mobile/viewModels/RecentTransactionViewModel.kt index 5c00319cb..e6cf3a80c 100644 --- a/app/src/main/java/org/mifos/mobile/viewModels/RecentTransactionViewModel.kt +++ b/app/src/main/java/org/mifos/mobile/viewModels/RecentTransactionViewModel.kt @@ -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 @@ -18,8 +19,9 @@ class RecentTransactionViewModel @Inject constructor(private val recentTransacti private val limit = 50 private var loadmore = false - private val _recentTransactionUiState = MutableLiveData() - val recentTransactionUiState: LiveData = _recentTransactionUiState + private val _recentTransactionUiState = + MutableStateFlow(RecentTransactionUiState.Initial) + val recentTransactionUiState: StateFlow = _recentTransactionUiState fun loadRecentTransactions(loadmore: Boolean, offset: Int) { this.loadmore = loadmore @@ -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) } } }