-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2199 from PratyushSingh07/recentTransaction-mvvm
feat: recent transaction fragment to mvvm
- Loading branch information
Showing
8 changed files
with
384 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/src/main/java/org/mifos/mobile/repositories/RecentTransactionRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?>?>? | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/org/mifos/mobile/repositories/RecentTransactionRepositoryImp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
app/src/main/java/org/mifos/mobile/utils/RecentTransactionUiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
68 changes: 68 additions & 0 deletions
68
app/src/main/java/org/mifos/mobile/viewModels/RecentTransactionViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.