Created a new TransferDetail model and DAO to create a object composed form the transfers, assets, accounts, and equivalent_values db tables and used such classes to correctly show the accounts involved in transactions in the BalancesFragment.

This commit is contained in:
Severiano Jaramillo 2018-12-03 13:34:13 -06:00
parent cd2f388894
commit de75c12197
8 changed files with 101 additions and 52 deletions

View file

@ -11,16 +11,16 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SortedList
import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.database.entities.Transfer
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
class TransactionsAdapter(private val context: Context) :
RecyclerView.Adapter<TransactionsAdapter.ViewHolder>() {
class TransfersAdapter(private val context: Context) :
RecyclerView.Adapter<TransfersAdapter.ViewHolder>() {
private val mComparator =
Comparator<Transfer> { a, b -> a.id.compareTo(b.id) }
Comparator<TransferDetail> { a, b -> a.id.compareTo(b.id) }
private val mSortedList =
SortedList<Transfer>(Transfer::class.java, object : SortedList.Callback<Transfer>() {
SortedList<TransferDetail>(TransferDetail::class.java, object : SortedList.Callback<TransferDetail>() {
override fun onInserted(position: Int, count: Int) {
notifyItemRangeInserted(position, count)
}
@ -37,15 +37,15 @@ class TransactionsAdapter(private val context: Context) :
notifyItemRangeChanged(position, count)
}
override fun compare(a: Transfer, b: Transfer): Int {
override fun compare(a: TransferDetail, b: TransferDetail): Int {
return mComparator.compare(a, b)
}
override fun areContentsTheSame(oldItem: Transfer, newItem: Transfer): Boolean {
override fun areContentsTheSame(oldItem: TransferDetail, newItem: TransferDetail): Boolean {
return oldItem == newItem
}
override fun areItemsTheSame(item1: Transfer, item2: Transfer): Boolean {
override fun areItemsTheSame(item1: TransferDetail, item2: TransferDetail): Boolean {
return item1.id == item2.id
}
})
@ -64,7 +64,7 @@ class TransactionsAdapter(private val context: Context) :
val tvFiatEquivalent: TextView = itemView.findViewById(R.id.tvFiatEquivalent)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionsAdapter.ViewHolder {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransfersAdapter.ViewHolder {
val inflater = LayoutInflater.from(context)
val transactionView = inflater.inflate(R.layout.item_transaction, parent, false)
@ -73,41 +73,41 @@ class TransactionsAdapter(private val context: Context) :
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val transaction = mSortedList.get(position)
val transferDetail = mSortedList.get(position)
viewHolder.tvFrom.text = transaction.source
viewHolder.tvTo.text = transaction.destination
viewHolder.tvFrom.text = transferDetail.from
viewHolder.tvTo.text = transferDetail.to
}
fun add(transaction: Transfer) {
mSortedList.add(transaction)
fun add(transferDetail: TransferDetail) {
mSortedList.add(transferDetail)
}
fun remove(transaction: Transfer) {
mSortedList.remove(transaction)
fun remove(transferDetail: TransferDetail) {
mSortedList.remove(transferDetail)
}
fun add(transactions: List<Transfer>) {
mSortedList.addAll(transactions)
fun add(transfersDetails: List<TransferDetail>) {
mSortedList.addAll(transfersDetails)
}
fun remove(transactions: List<Transfer>) {
fun remove(transfersDetails: List<TransferDetail>) {
mSortedList.beginBatchedUpdates()
for (transaction in transactions) {
mSortedList.remove(transaction)
for (transferDetail in transfersDetails) {
mSortedList.remove(transferDetail)
}
mSortedList.endBatchedUpdates()
}
fun replaceAll(transactions: List<Transfer>) {
fun replaceAll(transfersDetails: List<TransferDetail>) {
mSortedList.beginBatchedUpdates()
for (i in mSortedList.size() - 1 downTo 0) {
val transaction = mSortedList.get(i)
if (!transactions.contains(transaction)) {
mSortedList.remove(transaction)
val transferDetail = mSortedList.get(i)
if (!transfersDetails.contains(transferDetail)) {
mSortedList.remove(transferDetail)
}
}
mSortedList.addAll(transactions)
mSortedList.addAll(transfersDetails)
mSortedList.endBatchedUpdates()
}

View file

@ -6,8 +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
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetailDao
@Database(entities = [
Asset::class,
@ -27,6 +27,7 @@ abstract class BitsyDatabase : RoomDatabase() {
abstract fun transferDao(): TransferDao
abstract fun userAccountDao(): UserAccountDao
abstract fun balanceDetailDao(): BalanceDetailDao
abstract fun transferDetailDao(): TransferDetailDao
companion object {

View file

@ -0,0 +1,14 @@
package cy.agorise.bitsybitshareswallet.database.joins
data class TransferDetail(
val id: String,
val from: String,
val to: String
// val direction: Boolean, // True -> Received, False -> Sent
// val date: Long,
// val cryptoAmount: Long,
// val cryptoPrecision: Int,
// val cryptoSymbol: String,
// val fiatAmount: Long,
// val fiatCurrency: 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 TransferDetailDao {
@Query("SELECT id, IFNULL((SELECT name FROM user_accounts WHERE user_accounts.id=transfers.source), '') AS `from`, IFNULL((SELECT name FROM user_accounts WHERE user_accounts.id=transfers.destination), '') AS `to` FROM transfers")
fun getAll(): LiveData<List<TransferDetail>>
}

View file

@ -13,13 +13,13 @@ 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.Transfer
import cy.agorise.bitsybitshareswallet.adapters.TransfersAdapter
import cy.agorise.bitsybitshareswallet.database.entities.UserAccount
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.viewmodels.BalanceDetailViewModel
import cy.agorise.bitsybitshareswallet.viewmodels.TransactionViewModel
import cy.agorise.bitsybitshareswallet.viewmodels.TransferDetailViewModel
import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel
import kotlinx.android.synthetic.main.fragment_balances.*
@ -27,7 +27,7 @@ class BalancesFragment : Fragment() {
private lateinit var mUserAccountViewModel: UserAccountViewModel
private lateinit var mBalanceDetailViewModel: BalanceDetailViewModel
private lateinit var mTransactionViewModel: TransactionViewModel
private lateinit var mTransferDetailViewModel: TransferDetailViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
@ -60,15 +60,15 @@ class BalancesFragment : Fragment() {
balancesAdapter.replaceAll(balancesDetails)
})
// Configure TransactionViewModel to show the transaction history
mTransactionViewModel = ViewModelProviders.of(this).get(TransactionViewModel::class.java)
// Configure TransferDetailViewModel to show the transaction history
mTransferDetailViewModel = ViewModelProviders.of(this).get(TransferDetailViewModel::class.java)
val transactionsAdapter = TransactionsAdapter(context!!)
rvTransactions.adapter = transactionsAdapter
val transfersAdapter = TransfersAdapter(context!!)
rvTransactions.adapter = transfersAdapter
rvTransactions.layoutManager = LinearLayoutManager(context)
mTransactionViewModel.getAll().observe(this, Observer<List<Transfer>> { transfers ->
transactionsAdapter.replaceAll(transfers)
mTransferDetailViewModel.getAll().observe(this, Observer<List<TransferDetail>> { transfersDetails ->
transfersAdapter.replaceAll(transfersDetails)
})
}
}

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.TransferDetail
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetailDao
class TransferDetailRepository internal constructor(context: Context) {
private val mTransferDetailDao: TransferDetailDao
init {
val db = BitsyDatabase.getDatabase(context)
mTransferDetailDao = db!!.transferDetailDao()
}
fun getAll(): LiveData<List<TransferDetail>> {
return mTransferDetailDao.getAll()
}
}

View file

@ -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.Transfer
import cy.agorise.bitsybitshareswallet.repositories.TransferRepository
class TransactionViewModel(application: Application) : AndroidViewModel(application) {
private var mRepository = TransferRepository(application)
internal fun getAll(): LiveData<List<Transfer>> {
return mRepository.getAll()
}
}

View file

@ -0,0 +1,15 @@
package cy.agorise.bitsybitshareswallet.viewmodels
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
import cy.agorise.bitsybitshareswallet.repositories.TransferDetailRepository
class TransferDetailViewModel(application: Application) : AndroidViewModel(application) {
private var mRepository = TransferDetailRepository(application)
internal fun getAll(): LiveData<List<TransferDetail>> {
return mRepository.getAll()
}
}