Create ReceiveTransactionViewModel.
- The ReceiveTransactionFragment was using two ViewModels, which is not recommended. According to Google's MVVM guidelines, each activity/fragment should have only one ViewModel and that ViewModel should be coupled to only one activity/fragment. For that reason the methods used in the two ViewModels in ReceiveTransactionFragment were merged into ReceiveTransactionViewModel.
This commit is contained in:
parent
7603546424
commit
2a88821710
3 changed files with 29 additions and 26 deletions
|
@ -29,8 +29,7 @@ import cy.agorise.bitsybitshareswallet.adapters.AutoSuggestAssetAdapter
|
|||
import cy.agorise.bitsybitshareswallet.utils.Constants
|
||||
import cy.agorise.bitsybitshareswallet.utils.Helper
|
||||
import cy.agorise.bitsybitshareswallet.utils.toast
|
||||
import cy.agorise.bitsybitshareswallet.viewmodels.AssetViewModel
|
||||
import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel
|
||||
import cy.agorise.bitsybitshareswallet.viewmodels.ReceiveTransactionViewModel
|
||||
import cy.agorise.graphenej.*
|
||||
import cy.agorise.graphenej.api.ConnectionStatusUpdate
|
||||
import cy.agorise.graphenej.api.calls.ListAssets
|
||||
|
@ -59,8 +58,7 @@ class ReceiveTransactionFragment : ConnectedFragment() {
|
|||
private const val OTHER_ASSET = "other_asset"
|
||||
}
|
||||
|
||||
private lateinit var mUserAccountViewModel: UserAccountViewModel
|
||||
private lateinit var mAssetViewModel: AssetViewModel
|
||||
private lateinit var mViewModel: ReceiveTransactionViewModel
|
||||
|
||||
/** Current user account */
|
||||
private var mUserAccount: UserAccount? = null
|
||||
|
@ -109,21 +107,18 @@ class ReceiveTransactionFragment : ConnectedFragment() {
|
|||
|
||||
Crashlytics.setString(Constants.CRASHLYTICS_KEY_LAST_SCREEN, TAG)
|
||||
|
||||
// Configure UserAccountViewModel to show the current account
|
||||
mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java)
|
||||
// Configure ViewModel
|
||||
mViewModel = ViewModelProviders.of(this).get(ReceiveTransactionViewModel::class.java)
|
||||
|
||||
val userId = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
.getString(Constants.KEY_CURRENT_ACCOUNT_ID, "")
|
||||
|
||||
mUserAccountViewModel.getUserAccount(userId!!).observe(this,
|
||||
mViewModel.getUserAccount(userId!!).observe(this,
|
||||
Observer<cy.agorise.bitsybitshareswallet.database.entities.UserAccount>{ user ->
|
||||
mUserAccount = UserAccount(user.id, user.name)
|
||||
})
|
||||
|
||||
// Configure Assets spinner to show Assets already saved into the db
|
||||
mAssetViewModel = ViewModelProviders.of(this).get(AssetViewModel::class.java)
|
||||
|
||||
mAssetViewModel.getAllNonZero().observe(this,
|
||||
mViewModel.getAllNonZero().observe(this,
|
||||
Observer<List<cy.agorise.bitsybitshareswallet.database.entities.Asset>> { assets ->
|
||||
mAssets.clear()
|
||||
mAssets.addAll(assets)
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
package cy.agorise.bitsybitshareswallet.viewmodels
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import cy.agorise.bitsybitshareswallet.database.entities.Asset
|
||||
import cy.agorise.bitsybitshareswallet.repositories.AssetRepository
|
||||
|
||||
class AssetViewModel(application: Application) : AndroidViewModel(application) {
|
||||
private var mRepository = AssetRepository(application)
|
||||
|
||||
internal fun getAllNonZero(): LiveData<List<Asset>> {
|
||||
return mRepository.getAllNonZero()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package cy.agorise.bitsybitshareswallet.viewmodels
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import cy.agorise.bitsybitshareswallet.database.entities.Asset
|
||||
import cy.agorise.bitsybitshareswallet.database.entities.UserAccount
|
||||
import cy.agorise.bitsybitshareswallet.repositories.AssetRepository
|
||||
import cy.agorise.bitsybitshareswallet.repositories.UserAccountRepository
|
||||
|
||||
class ReceiveTransactionViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
private var mUserAccountRepository = UserAccountRepository(application)
|
||||
private var mAssetRepository = AssetRepository(application)
|
||||
|
||||
internal fun getUserAccount(id: String): LiveData<UserAccount> {
|
||||
return mUserAccountRepository.getUserAccount(id)
|
||||
}
|
||||
|
||||
internal fun getAllNonZero(): LiveData<List<Asset>> {
|
||||
return mAssetRepository.getAllNonZero()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue