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

View file

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