- Create AssetsAdapter to populate the Assets Spinner in SendTransactionFragment.

- Use BalanceDetailViewModel to fetch the Balance details from the database and pass them to the AssetsAdapter.
This commit is contained in:
Severiano Jaramillo 2018-12-04 15:49:22 -06:00
parent fa2c7a07d8
commit 2aa685d1da
5 changed files with 104 additions and 8 deletions

View file

@ -0,0 +1,42 @@
package cy.agorise.bitsybitshareswallet.adapters
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
class AssetsAdapter(context: Context, resource: Int, data: List<BalanceDetail>) :
ArrayAdapter<BalanceDetail>(context, resource, data) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var cv = convertView
if (cv == null) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
cv = inflater.inflate(android.R.layout.simple_spinner_item, parent, false)
}
val text: TextView = cv!!.findViewById(android.R.id.text1)
val balanceDetail = getItem(position)
text.text = balanceDetail!!.symbol
return cv
}
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val v = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false)
val text: TextView = v.findViewById(android.R.id.text1)
val balanceDetail = getItem(position)
text.text = balanceDetail!!.symbol
return v
}
}

View file

@ -3,25 +3,44 @@ package cy.agorise.bitsybitshareswallet.fragments
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.google.zxing.BarcodeFormat
import com.google.zxing.Result
import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.adapters.AssetsAdapter
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
import cy.agorise.bitsybitshareswallet.viewmodels.BalanceDetailViewModel
import cy.agorise.graphenej.Invoice
import kotlinx.android.synthetic.main.fragment_send_transaction.*
import me.dm7.barcodescanner.zxing.ZXingScannerView
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.Locale
class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler {
private val TAG = this.javaClass.simpleName
// Camera Permission
private val REQUEST_CAMERA_PERMISSION = 1
private var isCameraPreviewVisible = false
private var mBalancesDetails: List<BalanceDetail>? = null
private lateinit var mBalanceDetailViewModel: BalanceDetailViewModel
private var mAssetsAdapter: AssetsAdapter? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_send_transaction, container, false)
}
@ -32,6 +51,15 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler {
verifyCameraPermission()
fabOpenCamera.setOnClickListener { if (isCameraPreviewVisible) stopCameraPreview() else verifyCameraPermission() }
// Configure BalanceDetailViewModel to show the current balances
mBalanceDetailViewModel = ViewModelProviders.of(this).get(BalanceDetailViewModel::class.java)
mBalanceDetailViewModel.getAll().observe(this, Observer<List<BalanceDetail>> { balancesDetails ->
mBalancesDetails = balancesDetails
mAssetsAdapter = AssetsAdapter(context!!, android.R.layout.simple_spinner_item, mBalancesDetails!!)
spAsset.adapter = mAssetsAdapter
})
}
private fun verifyCameraPermission() {
@ -82,7 +110,34 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler {
}
override fun handleResult(result: Result?) {
Toast.makeText(context!!, result!!.text, Toast.LENGTH_SHORT).show()
try {
val invoice = Invoice.fromQrCode(result!!.text)
Log.d(TAG, "QR Code read: " + invoice.toJsonString())
tietTo.setText(invoice.to)
for (i in 0 until mAssetsAdapter!!.count) {
if (mAssetsAdapter!!.getItem(i)!!.symbol == invoice.currency.toUpperCase()) {
spAsset.setSelection(i)
break
}
}
tietMemo.setText(invoice.memo)
var amount = 0.0
for (nextItem in invoice.lineItems) {
amount += nextItem.quantity * nextItem.price
}
val df = DecimalFormat("####.#####")
df.roundingMode = RoundingMode.CEILING
df.decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault())
tietAmount.setText(df.format(amount))
}catch (e: Exception) {
Log.d(TAG, "Invoice error: " + e.message)
}
}
override fun onResume() {

View file

@ -18,5 +18,4 @@ class BalanceDetailRepository internal constructor(context: Context) {
fun getAll(): LiveData<List<BalanceDetail>> {
return mBalanceDetailDao.getAll()
}
}

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp" />
android:bottomLeftRadius="60dp"
android:topLeftRadius="60dp" />
<solid android:color="#5fcaff" />
</shape>

View file

@ -157,8 +157,8 @@
<View
android:id="@+id/vSend"
android:layout_width="130dp"
android:layout_height="140dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginEnd="0dp"
android:background="@drawable/send_fab_background"
app:layout_constraintEnd_toEndOf="parent"
@ -169,8 +169,8 @@
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
app:fabCustomSize="90dp"
app:maxImageSize="70dp"
app:srcCompat="@drawable/ic_arrow_forward"