Add functionality to eReceipts to fetch the Transaction information from the database and correctly format and display it on the screen.

This commit is contained in:
Severiano Jaramillo 2019-02-05 17:17:00 -06:00
parent 49b8ce1cb5
commit 032c5bcb5f
8 changed files with 104 additions and 6 deletions

View file

@ -7,6 +7,9 @@ import androidx.room.Query
@Dao
interface TransferDetailDao {
@Query("SELECT transfers.id, (SELECT name FROM user_accounts WHERE user_accounts.id=transfers.source) AS `from`, (SELECT name FROM user_accounts WHERE user_accounts.id=transfers.destination) AS `to`, (CASE WHEN destination=:userId THEN 1 ELSE 0 END) AS `direction`, transfers.memo AS `memo`, transfers.timestamp AS `date`, transfers.transfer_amount AS `assetAmount`, assets.precision AS `assetPrecision`, assets.symbol AS `assetSymbol`, assets.issuer as `assetIssuer` FROM transfers INNER JOIN assets WHERE transfers.id=:transferId AND transfers.transfer_asset_id = assets.id")
fun get(userId: String, transferId: String): LiveData<TransferDetail>
@Query("SELECT transfers.id, (SELECT name FROM user_accounts WHERE user_accounts.id=transfers.source) AS `from`, (SELECT name FROM user_accounts WHERE user_accounts.id=transfers.destination) AS `to`, (CASE WHEN destination=:userId THEN 1 ELSE 0 END) AS `direction`, transfers.memo AS `memo`, transfers.timestamp AS `date`, transfers.transfer_amount AS `assetAmount`, assets.precision AS `assetPrecision`, assets.symbol AS `assetSymbol`, assets.issuer as `assetIssuer` FROM transfers INNER JOIN assets WHERE transfers.transfer_asset_id = assets.id")
fun getAll(userId: String): LiveData<List<TransferDetail>>
}

View file

@ -1,19 +1,35 @@
package cy.agorise.bitsybitshareswallet.fragments
import android.os.Bundle
import android.preference.PreferenceManager
import android.text.Html
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.fragment.navArgs
import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.utils.toast
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.viewmodels.EReceiptViewModel
import kotlinx.android.synthetic.main.fragment_e_receipt.*
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.text.SimpleDateFormat
import java.util.*
class EReceiptFragment : Fragment() {
private val args: EReceiptFragmentArgs by navArgs()
private lateinit var mEReceiptViewModel: EReceiptViewModel
private lateinit var mLocale: Locale
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
setHasOptionsMenu(true)
@ -23,7 +39,62 @@ class EReceiptFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mLocale = resources.configuration.locale
val userId = PreferenceManager.getDefaultSharedPreferences(context)
.getString(Constants.KEY_CURRENT_ACCOUNT_ID, "") ?: ""
val transferId = args.transferId
context?.toast("Transfer ID: $transferId")
mEReceiptViewModel = ViewModelProviders.of(this).get(EReceiptViewModel::class.java)
mEReceiptViewModel.get(userId, transferId).observe(this, Observer<TransferDetail> { transferDetail ->
bindTransferDetail(transferDetail)
})
}
private fun bindTransferDetail(transferDetail: TransferDetail) {
vPaymentDirection.setBackgroundColor(resources.getColor(
if(transferDetail.direction) R.color.colorReceive else R.color.colorSend
))
tvFrom.text = transferDetail.from ?: ""
tvTo.text = transferDetail.to ?: ""
// Show the crypto amount correctly formatted
val df = DecimalFormat("####."+("#".repeat(transferDetail.assetPrecision)))
df.roundingMode = RoundingMode.CEILING
df.decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault())
val amount = transferDetail.assetAmount.toDouble() /
Math.pow(10.toDouble(), transferDetail.assetPrecision.toDouble())
val assetAmount = "${df.format(amount)} ${transferDetail.getUIAssetSymbol()}"
tvAmount.text = assetAmount
// TODO show the equivalent value
tvEquivalentValue.text = "-"
// Memo
if (transferDetail.memo != "")
tvMemo.text = getString(R.string.template__memo, transferDetail.memo)
else
tvMemo.visibility = View.GONE
// Date
val dateFormat = SimpleDateFormat("dd MMM HH:mm:ss z", mLocale)
tvDate.text = getString(R.string.template__date, dateFormat.format(transferDetail.date * 1000))
// Transaction #
formatTransferTextView(transferDetail.id)
}
/** Formats the transfer TextView to show a link to explore the given transfer
* in a BitShares explorer */
private fun formatTransferTextView(transferId: String) {
val tx = Html.fromHtml(getString(R.string.template__tx,
"<a href=\"http://bitshares-explorer.io/#/operations/$transferId\">$transferId</a>"
))
tvTransferID.text = tx
tvTransferID.movementMethod = LinkMovementMethod.getInstance()
}
}

View file

@ -29,7 +29,6 @@ import java.io.File
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.collections.ArrayList
import kotlin.math.exp
class TransactionsFragment : Fragment(), FilterOptionsDialog.OnFilterOptionsSelectedListener {

View file

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

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 EReceiptViewModel(application: Application) : AndroidViewModel(application) {
private var mTransferDetailRepository = TransferDetailRepository(application)
internal fun get(userId: String, transferId: String): LiveData<TransferDetail> {
return mTransferDetailRepository.get(userId, transferId)
}
}

View file

@ -23,6 +23,7 @@
android:background="@android:color/white">
<View
android:id="@+id/vPaymentDirection"
android:layout_width="8dp"
android:layout_height="0dp"
android:background="@color/colorReceive"
@ -176,7 +177,7 @@
app:layout_constraintTop_toBottomOf="@id/tvMemo"/>
<TextView
android:id="@+id/tvTransactionID"
android:id="@+id/tvTransferID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"

View file

@ -73,7 +73,9 @@
<!-- eReceipt -->
<string name="title_e_receipt">Recibo electrónico</string>
<string name="template__memo">Memo: %1$d</string>
<string name="template__memo">Memo: %1$s</string>
<string name="template__date">Fecha: %1$s</string>
<string name="template__tx">Tx: %1$s</string>
<!-- Merchants & Tellers -->
<string name="title_merchants">Comerciantes</string>

View file

@ -73,7 +73,10 @@
<!-- eReceipt -->
<string name="title_e_receipt">eReceipt</string>
<string name="template__memo">Memo: %1$d</string>
<string name="template__memo">Memo: %1$s</string>
<string name="template__date">Date: %1$s</string>
<string name="template__tx">Tx: %1$s</string>
<!-- Merchants & Tellers -->
<string name="title_merchants">Merchants</string>