- Created Balances adapter and ViewModel to show the Account Balances in the BalancesFragment.

- Simplified the Balances db table.
This commit is contained in:
Severiano Jaramillo 2018-12-01 08:48:19 -06:00
parent fc98208c30
commit dd770fdb5b
7 changed files with 156 additions and 38 deletions

View file

@ -150,7 +150,6 @@ class MainActivity : ConnectedActivity() {
val balances = ArrayList<Balance>() val balances = ArrayList<Balance>()
for (assetBalance in assetBalances) { for (assetBalance in assetBalances) {
val balance = Balance( val balance = Balance(
mCurrentAccount!!.objectId,
assetBalance.asset.objectId, assetBalance.asset.objectId,
assetBalance.amount.toLong(), assetBalance.amount.toLong(),
now now

View file

@ -0,0 +1,104 @@
package cy.agorise.bitsybitshareswallet.adapters
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SortedList
import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.entities.Balance
class BalancesAdapter(private val context: Context) :
RecyclerView.Adapter<BalancesAdapter.ViewHolder>() {
private val mComparator =
Comparator<Balance> { a, b -> a.assetId.compareTo(b.assetId) }
private val mSortedList =
SortedList<Balance>(Balance::class.java, object : SortedList.Callback<Balance>() {
override fun onInserted(position: Int, count: Int) {
notifyItemRangeInserted(position, count)
}
override fun onRemoved(position: Int, count: Int) {
notifyItemRangeRemoved(position, count)
}
override fun onMoved(fromPosition: Int, toPosition: Int) {
notifyItemMoved(fromPosition, toPosition)
}
override fun onChanged(position: Int, count: Int) {
notifyItemRangeChanged(position, count)
}
override fun compare(a: Balance, b: Balance): Int {
return mComparator.compare(a, b)
}
override fun areContentsTheSame(oldItem: Balance, newItem: Balance): Boolean {
return oldItem == newItem
}
override fun areItemsTheSame(item1: Balance, item2: Balance): Boolean {
return item1.assetId == item2.assetId
}
})
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvBalance: TextView = itemView.findViewById(R.id.tvBalance)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BalancesAdapter.ViewHolder {
val inflater = LayoutInflater.from(context)
val balanceView = inflater.inflate(R.layout.item_balance, parent, false)
return ViewHolder(balanceView)
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val balance = mSortedList.get(position)
val amount = "${balance.assetAmount} ${balance.assetId}"
viewHolder.tvBalance.text = amount
}
fun add(balance: Balance) {
mSortedList.add(balance)
}
fun remove(balance: Balance) {
mSortedList.remove(balance)
}
fun add(balances: List<Balance>) {
mSortedList.addAll(balances)
}
fun remove(balances: List<Balance>) {
mSortedList.beginBatchedUpdates()
for (balance in balances) {
mSortedList.remove(balance)
}
mSortedList.endBatchedUpdates()
}
fun replaceAll(balances: List<Balance>) {
mSortedList.beginBatchedUpdates()
for (i in mSortedList.size() - 1 downTo 0) {
val balance = mSortedList.get(i)
if (!balances.contains(balance)) {
mSortedList.remove(balance)
}
}
mSortedList.addAll(balances)
mSortedList.endBatchedUpdates()
}
override fun getItemCount(): Int {
return mSortedList.size()
}
}

View file

@ -13,9 +13,12 @@ import androidx.recyclerview.widget.SortedList
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.entities.Transfer import cy.agorise.bitsybitshareswallet.entities.Transfer
class TransactionsAdapter(private val context: Context, private val mComparator: Comparator<Transfer>) : class TransactionsAdapter(private val context: Context) :
RecyclerView.Adapter<TransactionsAdapter.ViewHolder>() { RecyclerView.Adapter<TransactionsAdapter.ViewHolder>() {
private val mComparator =
Comparator<Transfer> { a, b -> a.id.compareTo(b.id) }
private val mSortedList = private val mSortedList =
SortedList<Transfer>(Transfer::class.java, object : SortedList.Callback<Transfer>() { SortedList<Transfer>(Transfer::class.java, object : SortedList.Callback<Transfer>() {
override fun onInserted(position: Int, count: Int) { override fun onInserted(position: Int, count: Int) {
@ -48,21 +51,17 @@ class TransactionsAdapter(private val context: Context, private val mComparator:
}) })
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val rootView = itemView.findViewById<ConstraintLayout>(R.id.rootView) val rootView: ConstraintLayout = itemView.findViewById(R.id.rootView)
val vPaymentDirection = itemView.findViewById<View>(R.id.vPaymentDirection) val vPaymentDirection: View = itemView.findViewById(R.id.vPaymentDirection)
val tvFrom = itemView.findViewById<TextView>(R.id.tvFrom) val tvFrom: TextView = itemView.findViewById(R.id.tvFrom)
val ivDirectionArrow = itemView.findViewById<ImageView>(R.id.ivDirectionArrow) val ivDirectionArrow: ImageView = itemView.findViewById(R.id.ivDirectionArrow)
val tvTo = itemView.findViewById<TextView>(R.id.tvTo) val tvTo: TextView = itemView.findViewById(R.id.tvTo)
val llMemo = itemView.findViewById<LinearLayout>(R.id.llMemo) val llMemo: LinearLayout = itemView.findViewById(R.id.llMemo)
val tvMemo = itemView.findViewById<TextView>(R.id.tvMemo) val tvMemo: TextView = itemView.findViewById(R.id.tvMemo)
val tvDate = itemView.findViewById<TextView>(R.id.tvDate) val tvDate: TextView = itemView.findViewById(R.id.tvDate)
val tvTime = itemView.findViewById<TextView>(R.id.tvTime) val tvTime: TextView = itemView.findViewById(R.id.tvTime)
val tvCryptoAmount = itemView.findViewById<TextView>(R.id.tvCryptoAmount) val tvCryptoAmount: TextView = itemView.findViewById(R.id.tvCryptoAmount)
val tvFiatEquivalent = itemView.findViewById<TextView>(R.id.tvFiatEquivalent) val tvFiatEquivalent: TextView = itemView.findViewById(R.id.tvFiatEquivalent)
}
init {
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionsAdapter.ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionsAdapter.ViewHolder {

View file

@ -2,12 +2,12 @@ package cy.agorise.bitsybitshareswallet.entities
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "balances", primaryKeys = ["user_account_id", "asset_id"]) @Entity(tableName = "balances")
// TODO make userAccountId and assetId be ForeignKeys
data class Balance( data class Balance(
@ColumnInfo(name = "user_account_id") val userAccountId: String, @PrimaryKey
@ColumnInfo(name = "asset_id") val assetId: String, @ColumnInfo(name = "asset_id") val assetId: String, // TODO should be foreign key?
@ColumnInfo(name = "asset_amount") val assetAmount: Long, @ColumnInfo(name = "asset_amount") val assetAmount: Long,
@ColumnInfo(name = "last_update") val lastUpdate: Long @ColumnInfo(name = "last_update") val lastUpdate: Long
) )

View file

@ -8,28 +8,27 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager 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.TransactionsAdapter import cy.agorise.bitsybitshareswallet.adapters.TransactionsAdapter
import cy.agorise.bitsybitshareswallet.entities.Balance
import cy.agorise.bitsybitshareswallet.entities.Transfer import cy.agorise.bitsybitshareswallet.entities.Transfer
import cy.agorise.bitsybitshareswallet.entities.UserAccount import cy.agorise.bitsybitshareswallet.entities.UserAccount
import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.viewmodels.BalancesViewModel import cy.agorise.bitsybitshareswallet.viewmodels.BalanceViewModel
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.*
import java.util.Comparator
class BalancesFragment : Fragment() { class BalancesFragment : Fragment() {
private lateinit var mUserAccountViewModel: UserAccountViewModel private lateinit var mUserAccountViewModel: UserAccountViewModel
private lateinit var mBalancesViewModel: BalancesViewModel private lateinit var mBalanceViewModel: BalanceViewModel
private lateinit var mTransactionViewModel: TransactionViewModel private lateinit var mTransactionViewModel: TransactionViewModel
private val mComparator =
Comparator<Transfer> { a, b -> a.id.compareTo(b.id) }
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
@ -40,6 +39,7 @@ class BalancesFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
// Configure UserAccountViewModel to show the current account
mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java) mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java)
val userId = PreferenceManager.getDefaultSharedPreferences(context) val userId = PreferenceManager.getDefaultSharedPreferences(context)
@ -49,13 +49,21 @@ class BalancesFragment : Fragment() {
tvAccountName.text = user.name tvAccountName.text = user.name
}) })
// Configure BalanceViewModel to show the current balances
mBalanceViewModel = ViewModelProviders.of(this).get(BalanceViewModel::class.java)
mBalancesViewModel = ViewModelProviders.of(this).get(BalancesViewModel::class.java) val balancesAdapter = BalancesAdapter(context!!)
// TODO: Use the ViewModel rvBalances.adapter = balancesAdapter
rvBalances.layoutManager = GridLayoutManager(context, 2)
mBalanceViewModel.getAll().observe(this, Observer<List<Balance>> { balances ->
balancesAdapter.replaceAll(balances)
})
// Configure TransactionViewModel to show the transaction history
mTransactionViewModel = ViewModelProviders.of(this).get(TransactionViewModel::class.java) mTransactionViewModel = ViewModelProviders.of(this).get(TransactionViewModel::class.java)
val transactionsAdapter = TransactionsAdapter(context!!, mComparator) val transactionsAdapter = TransactionsAdapter(context!!)
rvTransactions.adapter = transactionsAdapter rvTransactions.adapter = transactionsAdapter
rvTransactions.layoutManager = LinearLayoutManager(context) rvTransactions.layoutManager = LinearLayoutManager(context)

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

View file

@ -1,8 +0,0 @@
package cy.agorise.bitsybitshareswallet.viewmodels
import android.app.Application
import androidx.lifecycle.AndroidViewModel
class BalancesViewModel(application: Application) : AndroidViewModel(application) {
}