Add direction to TransferDetail and use it to display the correct left bar color and direction arrow in the Transactions list..

This commit is contained in:
Severiano Jaramillo 2018-12-03 13:51:36 -06:00
parent de75c12197
commit 70ebea75b0
6 changed files with 22 additions and 9 deletions

View file

@ -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<TransfersAdapter.ViewHolder>() {
val userId = PreferenceManager.getDefaultSharedPreferences(context)
.getString(Constants.KEY_CURRENT_ACCOUNT_ID, "")!!
private val mComparator =
Comparator<TransferDetail> { 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) {

View file

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

View file

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

View file

@ -67,7 +67,7 @@ class BalancesFragment : Fragment() {
rvTransactions.adapter = transfersAdapter
rvTransactions.layoutManager = LinearLayoutManager(context)
mTransferDetailViewModel.getAll().observe(this, Observer<List<TransferDetail>> { transfersDetails ->
mTransferDetailViewModel.getAll(userId).observe(this, Observer<List<TransferDetail>> { transfersDetails ->
transfersAdapter.replaceAll(transfersDetails)
})
}

View file

@ -15,8 +15,8 @@ class TransferDetailRepository internal constructor(context: Context) {
mTransferDetailDao = db!!.transferDetailDao()
}
fun getAll(): LiveData<List<TransferDetail>> {
return mTransferDetailDao.getAll()
fun getAll(userId: String): LiveData<List<TransferDetail>> {
return mTransferDetailDao.getAll(userId)
}
}

View file

@ -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<List<TransferDetail>> {
return mRepository.getAll()
internal fun getAll(userId: String): LiveData<List<TransferDetail>> {
return mRepository.getAll(userId)
}
}