From 70ebea75b0963a64d415523c7851edbbe85846f0 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Mon, 3 Dec 2018 13:51:36 -0600 Subject: [PATCH] Add direction to TransferDetail and use it to display the correct left bar color and direction arrow in the Transactions list.. --- .../adapters/TransfersAdapter.kt | 13 +++++++++++++ .../database/joins/TransferDetail.kt | 4 ++-- .../database/joins/TransferDetailDao.kt | 4 ++-- .../fragments/BalancesFragment.kt | 2 +- .../repositories/TransferDetailRepository.kt | 4 ++-- .../viewmodels/TransferDetailViewModel.kt | 4 ++-- 6 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersAdapter.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersAdapter.kt index 422a980..631e1bc 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersAdapter.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersAdapter.kt @@ -1,6 +1,7 @@ package cy.agorise.bitsybitshareswallet.adapters import android.content.Context +import android.preference.PreferenceManager import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -12,10 +13,14 @@ import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.SortedList import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail +import cy.agorise.bitsybitshareswallet.utils.Constants class TransfersAdapter(private val context: Context) : RecyclerView.Adapter() { + val userId = PreferenceManager.getDefaultSharedPreferences(context) + .getString(Constants.KEY_CURRENT_ACCOUNT_ID, "")!! + private val mComparator = Comparator { a, b -> a.id.compareTo(b.id) } @@ -75,8 +80,16 @@ class TransfersAdapter(private val context: Context) : override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { val transferDetail = mSortedList.get(position) + viewHolder.vPaymentDirection.setBackgroundColor(context.resources.getColor( + if(transferDetail.direction) R.color.colorReceive else R.color.colorSend + )) + viewHolder.tvFrom.text = transferDetail.from viewHolder.tvTo.text = transferDetail.to + + viewHolder.ivDirectionArrow.setImageDrawable(context.getDrawable( + if(transferDetail.direction) R.drawable.ic_arrow_receive else R.drawable.ic_arrow_send + )) } fun add(transferDetail: TransferDetail) { diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetail.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetail.kt index fb46432..a490111 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetail.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetail.kt @@ -3,8 +3,8 @@ 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 to: String, + val direction: Boolean // True -> Received, False -> Sent // val date: Long, // val cryptoAmount: Long, // val cryptoPrecision: Int, diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetailDao.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetailDao.kt index e027b66..7322a19 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetailDao.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetailDao.kt @@ -7,6 +7,6 @@ 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> + @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`, (CASE WHEN destination=:userId THEN 1 ELSE 0 END) AS `direction` FROM transfers") + fun getAll(userId: String): LiveData> } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt index 64a42e3..ef1687a 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt @@ -67,7 +67,7 @@ class BalancesFragment : Fragment() { rvTransactions.adapter = transfersAdapter rvTransactions.layoutManager = LinearLayoutManager(context) - mTransferDetailViewModel.getAll().observe(this, Observer> { transfersDetails -> + mTransferDetailViewModel.getAll(userId).observe(this, Observer> { transfersDetails -> transfersAdapter.replaceAll(transfersDetails) }) } diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TransferDetailRepository.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TransferDetailRepository.kt index 915289d..cd86011 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TransferDetailRepository.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TransferDetailRepository.kt @@ -15,8 +15,8 @@ class TransferDetailRepository internal constructor(context: Context) { mTransferDetailDao = db!!.transferDetailDao() } - fun getAll(): LiveData> { - return mTransferDetailDao.getAll() + fun getAll(userId: String): LiveData> { + return mTransferDetailDao.getAll(userId) } } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/TransferDetailViewModel.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/TransferDetailViewModel.kt index 4521f4d..39ff1b6 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/TransferDetailViewModel.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/TransferDetailViewModel.kt @@ -9,7 +9,7 @@ import cy.agorise.bitsybitshareswallet.repositories.TransferDetailRepository class TransferDetailViewModel(application: Application) : AndroidViewModel(application) { private var mRepository = TransferDetailRepository(application) - internal fun getAll(): LiveData> { - return mRepository.getAll() + internal fun getAll(userId: String): LiveData> { + return mRepository.getAll(userId) } } \ No newline at end of file