Created a new BalancesDetail model and DAO to create a object composed form the balances and assets db tables and used such classes to correctly show the balances in the BalancesFragment.
This commit is contained in:
parent
5ec8afbdb3
commit
cd2f388894
8 changed files with 84 additions and 37 deletions
|
@ -8,16 +8,16 @@ import android.widget.TextView
|
|||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.recyclerview.widget.SortedList
|
||||
import cy.agorise.bitsybitshareswallet.R
|
||||
import cy.agorise.bitsybitshareswallet.database.entities.Balance
|
||||
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
|
||||
|
||||
class BalancesAdapter(private val context: Context) :
|
||||
RecyclerView.Adapter<BalancesAdapter.ViewHolder>() {
|
||||
|
||||
private val mComparator =
|
||||
Comparator<Balance> { a, b -> a.assetId.compareTo(b.assetId) }
|
||||
Comparator<BalanceDetail> { a, b -> a.symbol.compareTo(b.symbol) }
|
||||
|
||||
private val mSortedList =
|
||||
SortedList<Balance>(Balance::class.java, object : SortedList.Callback<Balance>() {
|
||||
SortedList<BalanceDetail>(BalanceDetail::class.java, object : SortedList.Callback<BalanceDetail>() {
|
||||
override fun onInserted(position: Int, count: Int) {
|
||||
notifyItemRangeInserted(position, count)
|
||||
}
|
||||
|
@ -34,16 +34,16 @@ class BalancesAdapter(private val context: Context) :
|
|||
notifyItemRangeChanged(position, count)
|
||||
}
|
||||
|
||||
override fun compare(a: Balance, b: Balance): Int {
|
||||
override fun compare(a: BalanceDetail, b: BalanceDetail): Int {
|
||||
return mComparator.compare(a, b)
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: Balance, newItem: Balance): Boolean {
|
||||
override fun areContentsTheSame(oldItem: BalanceDetail, newItem: BalanceDetail): Boolean {
|
||||
return oldItem == newItem
|
||||
}
|
||||
|
||||
override fun areItemsTheSame(item1: Balance, item2: Balance): Boolean {
|
||||
return item1.assetId == item2.assetId
|
||||
override fun areItemsTheSame(item1: BalanceDetail, item2: BalanceDetail): Boolean {
|
||||
return item1.id == item2.id
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -62,23 +62,25 @@ class BalancesAdapter(private val context: Context) :
|
|||
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
|
||||
val balance = mSortedList.get(position)
|
||||
|
||||
val amount = "${balance.assetAmount} ${balance.assetId}"
|
||||
viewHolder.tvBalance.text = amount
|
||||
val amount = balance.amount.toDouble() / Math.pow(10.0, balance.precision.toDouble())
|
||||
|
||||
viewHolder.tvBalance.text =
|
||||
String.format("%." + Math.min(balance.precision, 8) + "f %s", amount, balance.symbol)
|
||||
}
|
||||
|
||||
fun add(balance: Balance) {
|
||||
fun add(balance: BalanceDetail) {
|
||||
mSortedList.add(balance)
|
||||
}
|
||||
|
||||
fun remove(balance: Balance) {
|
||||
fun remove(balance: BalanceDetail) {
|
||||
mSortedList.remove(balance)
|
||||
}
|
||||
|
||||
fun add(balances: List<Balance>) {
|
||||
fun add(balances: List<BalanceDetail>) {
|
||||
mSortedList.addAll(balances)
|
||||
}
|
||||
|
||||
fun remove(balances: List<Balance>) {
|
||||
fun remove(balances: List<BalanceDetail>) {
|
||||
mSortedList.beginBatchedUpdates()
|
||||
for (balance in balances) {
|
||||
mSortedList.remove(balance)
|
||||
|
@ -86,7 +88,7 @@ class BalancesAdapter(private val context: Context) :
|
|||
mSortedList.endBatchedUpdates()
|
||||
}
|
||||
|
||||
fun replaceAll(balances: List<Balance>) {
|
||||
fun replaceAll(balances: List<BalanceDetail>) {
|
||||
mSortedList.beginBatchedUpdates()
|
||||
for (i in mSortedList.size() - 1 downTo 0) {
|
||||
val balance = mSortedList.get(i)
|
||||
|
|
|
@ -6,6 +6,8 @@ import androidx.room.Room
|
|||
import androidx.room.RoomDatabase
|
||||
import cy.agorise.bitsybitshareswallet.database.daos.*
|
||||
import cy.agorise.bitsybitshareswallet.database.entities.*
|
||||
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
|
||||
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetailDao
|
||||
|
||||
@Database(entities = [
|
||||
Asset::class,
|
||||
|
@ -24,6 +26,7 @@ abstract class BitsyDatabase : RoomDatabase() {
|
|||
abstract fun equivalentValueDao(): EquivalentValueDao
|
||||
abstract fun transferDao(): TransferDao
|
||||
abstract fun userAccountDao(): UserAccountDao
|
||||
abstract fun balanceDetailDao(): BalanceDetailDao
|
||||
|
||||
companion object {
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package cy.agorise.bitsybitshareswallet.database.joins
|
||||
|
||||
data class BalanceDetail(
|
||||
val id: String,
|
||||
val amount: Long,
|
||||
val precision: Int,
|
||||
val symbol: String
|
||||
)
|
|
@ -0,0 +1,12 @@
|
|||
package cy.agorise.bitsybitshareswallet.database.joins
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
|
||||
@Dao
|
||||
interface BalanceDetailDao {
|
||||
@Query("SELECT assets.id AS id, balances.asset_amount AS amount, assets.precision, assets.symbol " +
|
||||
"FROM balances INNER JOIN assets on balances.asset_id = assets.id")
|
||||
fun getAll(): LiveData<List<BalanceDetail>>
|
||||
}
|
|
@ -14,11 +14,11 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
|||
import cy.agorise.bitsybitshareswallet.R
|
||||
import cy.agorise.bitsybitshareswallet.adapters.BalancesAdapter
|
||||
import cy.agorise.bitsybitshareswallet.adapters.TransactionsAdapter
|
||||
import cy.agorise.bitsybitshareswallet.database.entities.Balance
|
||||
import cy.agorise.bitsybitshareswallet.database.entities.Transfer
|
||||
import cy.agorise.bitsybitshareswallet.database.entities.UserAccount
|
||||
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
|
||||
import cy.agorise.bitsybitshareswallet.utils.Constants
|
||||
import cy.agorise.bitsybitshareswallet.viewmodels.BalanceViewModel
|
||||
import cy.agorise.bitsybitshareswallet.viewmodels.BalanceDetailViewModel
|
||||
import cy.agorise.bitsybitshareswallet.viewmodels.TransactionViewModel
|
||||
import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel
|
||||
import kotlinx.android.synthetic.main.fragment_balances.*
|
||||
|
@ -26,7 +26,7 @@ import kotlinx.android.synthetic.main.fragment_balances.*
|
|||
class BalancesFragment : Fragment() {
|
||||
|
||||
private lateinit var mUserAccountViewModel: UserAccountViewModel
|
||||
private lateinit var mBalanceViewModel: BalanceViewModel
|
||||
private lateinit var mBalanceDetailViewModel: BalanceDetailViewModel
|
||||
private lateinit var mTransactionViewModel: TransactionViewModel
|
||||
|
||||
override fun onCreateView(
|
||||
|
@ -49,15 +49,15 @@ class BalancesFragment : Fragment() {
|
|||
tvAccountName.text = user.name
|
||||
})
|
||||
|
||||
// Configure BalanceViewModel to show the current balances
|
||||
mBalanceViewModel = ViewModelProviders.of(this).get(BalanceViewModel::class.java)
|
||||
// Configure BalanceDetailViewModel to show the current balances
|
||||
mBalanceDetailViewModel = ViewModelProviders.of(this).get(BalanceDetailViewModel::class.java)
|
||||
|
||||
val balancesAdapter = BalancesAdapter(context!!)
|
||||
rvBalances.adapter = balancesAdapter
|
||||
rvBalances.layoutManager = GridLayoutManager(context, 2)
|
||||
|
||||
mBalanceViewModel.getAll().observe(this, Observer<List<Balance>> { balances ->
|
||||
balancesAdapter.replaceAll(balances)
|
||||
mBalanceDetailViewModel.getAll().observe(this, Observer<List<BalanceDetail>> { balancesDetails ->
|
||||
balancesAdapter.replaceAll(balancesDetails)
|
||||
})
|
||||
|
||||
// Configure TransactionViewModel to show the transaction history
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package cy.agorise.bitsybitshareswallet.repositories
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.LiveData
|
||||
import cy.agorise.bitsybitshareswallet.database.BitsyDatabase
|
||||
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
|
||||
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetailDao
|
||||
|
||||
class BalanceDetailRepository internal constructor(context: Context) {
|
||||
|
||||
private val mBalanceDetailDao: BalanceDetailDao
|
||||
|
||||
init {
|
||||
val db = BitsyDatabase.getDatabase(context)
|
||||
mBalanceDetailDao = db!!.balanceDetailDao()
|
||||
}
|
||||
|
||||
fun getAll(): LiveData<List<BalanceDetail>> {
|
||||
return mBalanceDetailDao.getAll()
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package cy.agorise.bitsybitshareswallet.viewmodels
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
|
||||
import cy.agorise.bitsybitshareswallet.repositories.BalanceDetailRepository
|
||||
|
||||
|
||||
class BalanceDetailViewModel(application: Application) : AndroidViewModel(application) {
|
||||
private var mRepository = BalanceDetailRepository(application)
|
||||
|
||||
internal fun getAll(): LiveData<List<BalanceDetail>> {
|
||||
return mRepository.getAll()
|
||||
}
|
||||
}
|
|
@ -1,16 +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.Balance
|
||||
import cy.agorise.bitsybitshareswallet.repositories.BalanceRepository
|
||||
|
||||
|
||||
class BalanceViewModel(application: Application) : AndroidViewModel(application) {
|
||||
private var mRepository = BalanceRepository(application)
|
||||
|
||||
internal fun getAll(): LiveData<List<Balance>> {
|
||||
return mRepository.getAll()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue