diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/AssetsAdapter.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/AssetsAdapter.kt new file mode 100644 index 0000000..3911159 --- /dev/null +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/AssetsAdapter.kt @@ -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) : + ArrayAdapter(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 + } +} \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt index 57578c5..c424c47 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt @@ -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? = 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> { 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() { diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/BalanceDetailRepository.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/BalanceDetailRepository.kt index 28c8772..edecb7d 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/BalanceDetailRepository.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/BalanceDetailRepository.kt @@ -18,5 +18,4 @@ class BalanceDetailRepository internal constructor(context: Context) { fun getAll(): LiveData> { return mBalanceDetailDao.getAll() } - } \ No newline at end of file diff --git a/app/src/main/res/drawable/send_fab_background.xml b/app/src/main/res/drawable/send_fab_background.xml index 6b31518..a8c2324 100644 --- a/app/src/main/res/drawable/send_fab_background.xml +++ b/app/src/main/res/drawable/send_fab_background.xml @@ -1,7 +1,7 @@ + android:bottomLeftRadius="60dp" + android:topLeftRadius="60dp" /> \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_send_transaction.xml b/app/src/main/res/layout/fragment_send_transaction.xml index 74fe56b..a665e66 100644 --- a/app/src/main/res/layout/fragment_send_transaction.xml +++ b/app/src/main/res/layout/fragment_send_transaction.xml @@ -157,8 +157,8 @@