Skip to content

Commit

Permalink
Merge pull request #2199 from PratyushSingh07/recentTransaction-mvvm
Browse files Browse the repository at this point in the history
feat: recent transaction fragment to mvvm
  • Loading branch information
jawidMuhammadi authored Jul 16, 2023
2 parents 9fe9976 + e3ed04d commit 1be8efe
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.repositories.RecentTransactionRepository
import org.mifos.mobile.repositories.RecentTransactionRepositoryImp
import org.mifos.mobile.repositories.UserAuthRepository
import org.mifos.mobile.repositories.UserAuthRepositoryImp

Expand All @@ -16,4 +18,9 @@ class RepositoryModule {
fun providesUserAuthRepository(dataManager: DataManager): UserAuthRepository {
return UserAuthRepositoryImp(dataManager)
}

@Provides
fun providesRecentTransactionRepository(dataManager: DataManager): RecentTransactionRepository {
return RecentTransactionRepositoryImp(dataManager)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mifos.mobile.repositories


import io.reactivex.Observable
import org.mifos.mobile.models.Page
import org.mifos.mobile.models.Transaction

interface RecentTransactionRepository {

fun recentTransactions(
offset: Int?,
limit: Int?
): Observable<Page<Transaction?>?>?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.Page
import org.mifos.mobile.models.Transaction
import javax.inject.Inject

class RecentTransactionRepositoryImp @Inject constructor(private val dataManager: DataManager) :
RecentTransactionRepository {

override fun recentTransactions(offset: Int?, limit: Int?): Observable<Page<Transaction?>?>? {
return limit?.let { offset?.let { it1 -> dataManager.getRecentTransactions(it1, it) } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
Expand All @@ -14,35 +15,30 @@ import dagger.hilt.android.AndroidEntryPoint
import org.mifos.mobile.R
import org.mifos.mobile.databinding.FragmentRecentTransactionsBinding
import org.mifos.mobile.models.Transaction
import org.mifos.mobile.presenters.RecentTransactionsPresenter
import org.mifos.mobile.ui.activities.base.BaseActivity
import org.mifos.mobile.ui.adapters.RecentTransactionListAdapter
import org.mifos.mobile.ui.fragments.base.BaseFragment
import org.mifos.mobile.ui.views.RecentTransactionsView
import org.mifos.mobile.utils.Constants
import org.mifos.mobile.utils.DividerItemDecoration
import org.mifos.mobile.utils.EndlessRecyclerViewScrollListener
import org.mifos.mobile.utils.*
import org.mifos.mobile.utils.Network.isConnected
import org.mifos.mobile.utils.Toaster
import org.mifos.mobile.viewModels.RecentTransactionViewModel
import javax.inject.Inject

/**
* @author Vishwwajeet
* @since 09/08/16
*/
@AndroidEntryPoint
class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRefreshListener {
class RecentTransactionsFragment : BaseFragment(), OnRefreshListener {

private var _binding: FragmentRecentTransactionsBinding? = null
private val binding get() = _binding!!

@JvmField
@Inject
var recentTransactionsPresenter: RecentTransactionsPresenter? = null

@JvmField
@Inject
var recentTransactionsListAdapter: RecentTransactionListAdapter? = null

private lateinit var recentTransactionViewModel: RecentTransactionViewModel

private var sweetUIErrorHandler: SweetUIErrorHandler? = null
private var recentTransactionList: MutableList<Transaction?>? = null
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -56,18 +52,41 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
savedInstanceState: Bundle?,
): View {
_binding = FragmentRecentTransactionsBinding.inflate(inflater, container, false)
recentTransactionsPresenter?.attachView(this)
recentTransactionViewModel = ViewModelProvider(this)[RecentTransactionViewModel::class.java]
sweetUIErrorHandler = SweetUIErrorHandler(activity, binding.root)
showUserInterface()
setToolbarTitle(getString(R.string.recent_transactions))
if (savedInstanceState == null) {
recentTransactionsPresenter?.loadRecentTransactions(false, 0)
recentTransactionViewModel.loadRecentTransactions(false, 0)
}
return binding.root
}

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)
}
}
}

binding.layoutError.btnTryAgain.setOnClickListener {
retryClicked()
}
Expand Down Expand Up @@ -95,7 +114,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
/**
* Setting up `rvRecentTransactions`
*/
override fun showUserInterface() {
fun showUserInterface() {
val layoutManager = LinearLayoutManager(activity)
layoutManager.orientation = LinearLayoutManager.VERTICAL
binding.rvRecentTransactions.layoutManager = layoutManager
Expand All @@ -111,7 +130,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
binding.rvRecentTransactions.addOnScrollListener(
object : EndlessRecyclerViewScrollListener(layoutManager) {
override fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView?) {
recentTransactionsPresenter?.loadRecentTransactions(true, totalItemsCount)
recentTransactionViewModel.loadRecentTransactions(true, totalItemsCount)
}

override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
Expand All @@ -138,13 +157,13 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
if (binding.layoutError.root.visibility == View.VISIBLE) {
resetUI()
}
recentTransactionsPresenter?.loadRecentTransactions(false, 0)
recentTransactionViewModel.loadRecentTransactions(false, 0)
}

/**
* Shows a Toast
*/
override fun showMessage(message: String?) {
fun showMessage(message: String?) {
(activity as BaseActivity?)?.showToast(message!!)
}

Expand All @@ -154,7 +173,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
*
* @param recentTransactionList List of [Transaction]
*/
override fun showRecentTransactions(recentTransactionList: List<Transaction?>?) {
fun showRecentTransactions(recentTransactionList: List<Transaction?>?) {
this.recentTransactionList = recentTransactionList as MutableList<Transaction?>?
recentTransactionsListAdapter?.setTransactions(recentTransactionList)
}
Expand All @@ -164,12 +183,12 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
*
* @param transactions List of [Transaction]
*/
override fun showLoadMoreRecentTransactions(transactions: List<Transaction?>?) {
fun showLoadMoreRecentTransactions(transactions: List<Transaction?>?) {
this.recentTransactionList?.addAll(recentTransactionList!!)
recentTransactionsListAdapter?.notifyDataSetChanged()
}

override fun resetUI() {
fun resetUI() {
sweetUIErrorHandler?.hideSweetErrorLayoutUI(
binding.rvRecentTransactions,
binding.layoutError.root,
Expand All @@ -179,7 +198,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
/**
* Hides `rvRecentTransactions` and shows a textview prompting no transactions
*/
override fun showEmptyTransaction() {
fun showEmptyTransaction() {
sweetUIErrorHandler?.showSweetEmptyUI(
getString(R.string.recent_transactions),
R.drawable.ic_error_black_24dp,
Expand All @@ -193,7 +212,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
*
* @param message Error message that tells the user about the problem.
*/
override fun showErrorFetchingRecentTransactions(message: String?) {
fun showErrorFetchingRecentTransactions(message: String?) {
if (!isConnected(requireActivity())) {
sweetUIErrorHandler?.showSweetNoInternetUI(
binding.rvRecentTransactions,
Expand All @@ -214,7 +233,7 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
binding.rvRecentTransactions,
binding.layoutError.root,
)
recentTransactionsPresenter?.loadRecentTransactions(false, 0)
recentTransactionViewModel.loadRecentTransactions(false, 0)
} else {
Toast.makeText(
context,
Expand All @@ -224,23 +243,22 @@ class RecentTransactionsFragment : BaseFragment(), RecentTransactionsView, OnRef
}
}

override fun showProgress() {
fun showProgress() {
showSwipeRefreshLayout(true)
}

override fun hideProgress() {
fun hideProgress() {
showSwipeRefreshLayout(false)
}

override fun showSwipeRefreshLayout(show: Boolean) {
fun showSwipeRefreshLayout(show: Boolean) {
binding.swipeTransactionContainer.post {
binding.swipeTransactionContainer.isRefreshing = show
}
}

override fun onDestroyView() {
super.onDestroyView()
recentTransactionsPresenter?.detachView()
_binding = null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mifos.mobile.utils

import org.mifos.mobile.models.Transaction

sealed class 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()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.mifos.mobile.viewModels

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import org.mifos.mobile.R
import org.mifos.mobile.models.Page
import org.mifos.mobile.models.Transaction
import org.mifos.mobile.repositories.RecentTransactionRepository
import org.mifos.mobile.utils.RecentTransactionUiState
import javax.inject.Inject

@HiltViewModel
class RecentTransactionViewModel @Inject constructor(private val recentTransactionRepositoryImp: RecentTransactionRepository) :
ViewModel() {

private val compositeDisposables: CompositeDisposable = CompositeDisposable()
private val limit = 50
private var loadmore = false

private val _recentTransactionUiState = MutableLiveData<RecentTransactionUiState>()
val recentTransactionUiState: LiveData<RecentTransactionUiState> = _recentTransactionUiState

fun loadRecentTransactions(loadmore: Boolean, offset: Int) {
this.loadmore = loadmore
loadRecentTransactions(offset, limit)
}

private fun loadRecentTransactions(offset: Int, limit: Int) {
_recentTransactionUiState.value = RecentTransactionUiState.Loading
recentTransactionRepositoryImp.recentTransactions(offset, limit)
?.observeOn( AndroidSchedulers.mainThread())
?.subscribeOn(Schedulers.io())
?.subscribeWith(object : DisposableObserver<Page<Transaction?>?>() {
override fun onNext(transactions: Page<Transaction?>) {
if (transactions.totalFilteredRecords == 0) {
_recentTransactionUiState.value = RecentTransactionUiState.EmptyTransaction
} else if (loadmore && transactions.pageItems.isNotEmpty()) {
_recentTransactionUiState.value = RecentTransactionUiState.LoadMoreRecentTransactions(transactions.pageItems)
} else if (transactions.pageItems.isNotEmpty()) {
_recentTransactionUiState.value = RecentTransactionUiState.RecentTransactions(transactions.pageItems)
}
}

override fun onError(e: Throwable) {
_recentTransactionUiState.value = RecentTransactionUiState.Error(R.string.recent_transactions)
}

override fun onComplete() {}
}).let {
if (it != null) {
compositeDisposables.add(
it,
)
}
}
}

override fun onCleared() {
super.onCleared()
compositeDisposables.clear()
}
}
Loading

0 comments on commit 1be8efe

Please sign in to comment.