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:
Severiano Jaramillo 2018-12-02 20:17:31 -06:00
parent 5ec8afbdb3
commit cd2f388894
8 changed files with 84 additions and 37 deletions

View file

@ -8,16 +8,16 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SortedList import androidx.recyclerview.widget.SortedList
import cy.agorise.bitsybitshareswallet.R 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) : class BalancesAdapter(private val context: Context) :
RecyclerView.Adapter<BalancesAdapter.ViewHolder>() { RecyclerView.Adapter<BalancesAdapter.ViewHolder>() {
private val mComparator = private val mComparator =
Comparator<Balance> { a, b -> a.assetId.compareTo(b.assetId) } Comparator<BalanceDetail> { a, b -> a.symbol.compareTo(b.symbol) }
private val mSortedList = 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) { override fun onInserted(position: Int, count: Int) {
notifyItemRangeInserted(position, count) notifyItemRangeInserted(position, count)
} }
@ -34,16 +34,16 @@ class BalancesAdapter(private val context: Context) :
notifyItemRangeChanged(position, count) notifyItemRangeChanged(position, count)
} }
override fun compare(a: Balance, b: Balance): Int { override fun compare(a: BalanceDetail, b: BalanceDetail): Int {
return mComparator.compare(a, b) return mComparator.compare(a, b)
} }
override fun areContentsTheSame(oldItem: Balance, newItem: Balance): Boolean { override fun areContentsTheSame(oldItem: BalanceDetail, newItem: BalanceDetail): Boolean {
return oldItem == newItem return oldItem == newItem
} }
override fun areItemsTheSame(item1: Balance, item2: Balance): Boolean { override fun areItemsTheSame(item1: BalanceDetail, item2: BalanceDetail): Boolean {
return item1.assetId == item2.assetId return item1.id == item2.id
} }
}) })
@ -62,23 +62,25 @@ class BalancesAdapter(private val context: Context) :
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val balance = mSortedList.get(position) val balance = mSortedList.get(position)
val amount = "${balance.assetAmount} ${balance.assetId}" val amount = balance.amount.toDouble() / Math.pow(10.0, balance.precision.toDouble())
viewHolder.tvBalance.text = amount
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) mSortedList.add(balance)
} }
fun remove(balance: Balance) { fun remove(balance: BalanceDetail) {
mSortedList.remove(balance) mSortedList.remove(balance)
} }
fun add(balances: List<Balance>) { fun add(balances: List<BalanceDetail>) {
mSortedList.addAll(balances) mSortedList.addAll(balances)
} }
fun remove(balances: List<Balance>) { fun remove(balances: List<BalanceDetail>) {
mSortedList.beginBatchedUpdates() mSortedList.beginBatchedUpdates()
for (balance in balances) { for (balance in balances) {
mSortedList.remove(balance) mSortedList.remove(balance)
@ -86,7 +88,7 @@ class BalancesAdapter(private val context: Context) :
mSortedList.endBatchedUpdates() mSortedList.endBatchedUpdates()
} }
fun replaceAll(balances: List<Balance>) { fun replaceAll(balances: List<BalanceDetail>) {
mSortedList.beginBatchedUpdates() mSortedList.beginBatchedUpdates()
for (i in mSortedList.size() - 1 downTo 0) { for (i in mSortedList.size() - 1 downTo 0) {
val balance = mSortedList.get(i) val balance = mSortedList.get(i)

View file

@ -6,6 +6,8 @@ import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import cy.agorise.bitsybitshareswallet.database.daos.* import cy.agorise.bitsybitshareswallet.database.daos.*
import cy.agorise.bitsybitshareswallet.database.entities.* import cy.agorise.bitsybitshareswallet.database.entities.*
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetailDao
@Database(entities = [ @Database(entities = [
Asset::class, Asset::class,
@ -24,6 +26,7 @@ abstract class BitsyDatabase : RoomDatabase() {
abstract fun equivalentValueDao(): EquivalentValueDao abstract fun equivalentValueDao(): EquivalentValueDao
abstract fun transferDao(): TransferDao abstract fun transferDao(): TransferDao
abstract fun userAccountDao(): UserAccountDao abstract fun userAccountDao(): UserAccountDao
abstract fun balanceDetailDao(): BalanceDetailDao
companion object { companion object {

View file

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

View file

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

View file

@ -14,11 +14,11 @@ import androidx.recyclerview.widget.LinearLayoutManager
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.adapters.BalancesAdapter import cy.agorise.bitsybitshareswallet.adapters.BalancesAdapter
import cy.agorise.bitsybitshareswallet.adapters.TransactionsAdapter 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.Transfer
import cy.agorise.bitsybitshareswallet.database.entities.UserAccount import cy.agorise.bitsybitshareswallet.database.entities.UserAccount
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
import cy.agorise.bitsybitshareswallet.utils.Constants 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.TransactionViewModel
import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel
import kotlinx.android.synthetic.main.fragment_balances.* import kotlinx.android.synthetic.main.fragment_balances.*
@ -26,7 +26,7 @@ import kotlinx.android.synthetic.main.fragment_balances.*
class BalancesFragment : Fragment() { class BalancesFragment : Fragment() {
private lateinit var mUserAccountViewModel: UserAccountViewModel private lateinit var mUserAccountViewModel: UserAccountViewModel
private lateinit var mBalanceViewModel: BalanceViewModel private lateinit var mBalanceDetailViewModel: BalanceDetailViewModel
private lateinit var mTransactionViewModel: TransactionViewModel private lateinit var mTransactionViewModel: TransactionViewModel
override fun onCreateView( override fun onCreateView(
@ -49,15 +49,15 @@ class BalancesFragment : Fragment() {
tvAccountName.text = user.name tvAccountName.text = user.name
}) })
// Configure BalanceViewModel to show the current balances // Configure BalanceDetailViewModel to show the current balances
mBalanceViewModel = ViewModelProviders.of(this).get(BalanceViewModel::class.java) mBalanceDetailViewModel = ViewModelProviders.of(this).get(BalanceDetailViewModel::class.java)
val balancesAdapter = BalancesAdapter(context!!) val balancesAdapter = BalancesAdapter(context!!)
rvBalances.adapter = balancesAdapter rvBalances.adapter = balancesAdapter
rvBalances.layoutManager = GridLayoutManager(context, 2) rvBalances.layoutManager = GridLayoutManager(context, 2)
mBalanceViewModel.getAll().observe(this, Observer<List<Balance>> { balances -> mBalanceDetailViewModel.getAll().observe(this, Observer<List<BalanceDetail>> { balancesDetails ->
balancesAdapter.replaceAll(balances) balancesAdapter.replaceAll(balancesDetails)
}) })
// Configure TransactionViewModel to show the transaction history // Configure TransactionViewModel to show the transaction history

View file

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

View file

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

View file

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