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 f614b29..af7907f 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,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 + @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> } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt index 3062a97..26bc73a 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt @@ -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 -> + 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, + "$transferId" + )) + tvTransferID.text = tx + tvTransferID.movementMethod = LinkMovementMethod.getInstance() } } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/TransactionsFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/TransactionsFragment.kt index a9b3a1a..0ea86b4 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/TransactionsFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/TransactionsFragment.kt @@ -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 { 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 cd86011..91466da 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TransferDetailRepository.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TransferDetailRepository.kt @@ -15,6 +15,10 @@ class TransferDetailRepository internal constructor(context: Context) { mTransferDetailDao = db!!.transferDetailDao() } + fun get(userId: String, transferId: String): LiveData { + return mTransferDetailDao.get(userId, transferId) + } + fun getAll(userId: String): LiveData> { return mTransferDetailDao.getAll(userId) } diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/EReceiptViewModel.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/EReceiptViewModel.kt new file mode 100644 index 0000000..9124f2d --- /dev/null +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/EReceiptViewModel.kt @@ -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 { + return mTransferDetailRepository.get(userId, transferId) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_e_receipt.xml b/app/src/main/res/layout/fragment_e_receipt.xml index 257ad92..67630ff 100644 --- a/app/src/main/res/layout/fragment_e_receipt.xml +++ b/app/src/main/res/layout/fragment_e_receipt.xml @@ -23,6 +23,7 @@ android:background="@android:color/white"> Recibo electrónico - Memo: %1$d + Memo: %1$s + Fecha: %1$s + Tx: %1$s Comerciantes diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c171ae0..273db75 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -73,7 +73,10 @@ eReceipt - Memo: %1$d + Memo: %1$s + Date: %1$s + Tx: %1$s + Merchants