Create AssetsAdapter and use it to populate the asset spinner in the ReceiveTransactionFragment, so the user the asset he wants to receive.

This commit is contained in:
Severiano Jaramillo 2018-12-20 08:48:48 -06:00
parent 49f920877a
commit e8ec0e6f89
4 changed files with 110 additions and 42 deletions

View file

@ -16,7 +16,6 @@ import androidx.lifecycle.ViewModelProviders
import cy.agorise.bitsybitshareswallet.database.entities.Balance
import cy.agorise.bitsybitshareswallet.processors.TransfersLoader
import cy.agorise.bitsybitshareswallet.repositories.AssetRepository
import cy.agorise.bitsybitshareswallet.repositories.BalanceRepository
import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.viewmodels.BalanceViewModel
import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel
@ -43,7 +42,6 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
private lateinit var mUserAccountViewModel: UserAccountViewModel
private lateinit var mBalanceViewModel: BalanceViewModel
private lateinit var mBalanceRepository: BalanceRepository
private lateinit var mAssetRepository: AssetRepository
/* Current user account */
@ -75,7 +73,6 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
if (userId != "")
mCurrentAccount = UserAccount(userId)
mBalanceRepository = BalanceRepository(this)
mAssetRepository = AssetRepository(this)
// Configure UserAccountViewModel to obtain the missing account ids
@ -199,7 +196,8 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
balances.add(balance)
}
mBalanceRepository.insertAll(balances)
mBalanceViewModel.insertAll(balances)
}
/**

View file

@ -0,0 +1,40 @@
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.entities.Asset
class AssetsAdapter(context: Context, resource: Int, data: List<Asset>) :
ArrayAdapter<Asset>(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 asset = getItem(position)
text.text = asset!!.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 asset = getItem(position)
text.text = asset!!.symbol
return v
}
}

View file

@ -8,7 +8,7 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AdapterView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
@ -20,6 +20,7 @@ import com.google.zxing.WriterException
import com.google.zxing.common.BitMatrix
import com.jakewharton.rxbinding2.widget.RxTextView
import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.adapters.AssetsAdapter
import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.viewmodels.AssetViewModel
import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel
@ -47,6 +48,12 @@ class ReceiveTransactionFragment : Fragment() {
private var mAsset: Asset? = null
private var mAssetsAdapter: AssetsAdapter? = null
private var mAssets: List<cy.agorise.bitsybitshareswallet.database.entities.Asset>? = null
private var selectedAssetSymbol = ""
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_receive_transaction, container, false)
}
@ -69,15 +76,29 @@ class ReceiveTransactionFragment : Fragment() {
mAssetViewModel.getAll().observe(this,
Observer<List<cy.agorise.bitsybitshareswallet.database.entities.Asset>> { assets ->
val adapter = ArrayAdapter<cy.agorise.bitsybitshareswallet.database.entities.Asset>(context!!,
android.R.layout.simple_dropdown_item_1line, assets)
actvAsset.setAdapter(adapter)
mAssets = assets
mAssetsAdapter = AssetsAdapter(context!!, android.R.layout.simple_spinner_item, mAssets!!)
spAsset.adapter = mAssetsAdapter
// Try to select the selectedAssetSymbol
for (i in 0 until mAssetsAdapter!!.count) {
if (mAssetsAdapter!!.getItem(i)!!.symbol == selectedAssetSymbol) {
spAsset.setSelection(i)
break
}
}
})
actvAsset.setOnItemClickListener { parent, _, position, _ ->
val asset = parent.adapter.getItem(position) as cy.agorise.bitsybitshareswallet.database.entities.Asset
mAsset = Asset(asset.id, asset.symbol, asset.precision)
updateQR()
spAsset.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) { }
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val asset = mAssetsAdapter!!.getItem(position)!!
selectedAssetSymbol = asset.symbol
mAsset = Asset(asset.id, asset.symbol, asset.precision)
updateQR()
}
}
// Use RxJava Debounce to create QR code only after the user stopped typing an amount
@ -87,18 +108,6 @@ class ReceiveTransactionFragment : Fragment() {
.observeOn(AndroidSchedulers.mainThread())
.subscribe { updateQR() }
)
// Use RxJava Debounce to avoid making calls to the NetworkService on every text change event
mDisposables.add(
RxTextView.textChanges(actvAsset)
.debounce(500, TimeUnit.MILLISECONDS)
.filter { it.toString().length < 3 }
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
mAsset = null
updateQR()
}
)
}
private fun updateQR() {

View file

@ -38,28 +38,49 @@
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.Bitsy.TextInputLayout"
<Spinner
android:id="@+id/spAsset"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginStart="12dp"
android:hint="@string/text__asset"
app:layout_constraintTop_toTopOf="@id/tilAmount"
app:layout_constraintBottom_toBottomOf="@id/tilAmount"
app:layout_constraintStart_toEndOf="@id/centeredVerticalGuideline"
app:layout_constraintEnd_toEndOf="parent">
android:layout_marginEnd="@dimen/activity_horizontal_margin"
app:layout_constraintTop_toTopOf="@+id/tilAmount"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/centeredVerticalGuideline"/>
<cy.agorise.bitsybitshareswallet.views.MyTextInputAutoCompleteTextView
android:id="@+id/actvAsset"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="12dp"
android:paddingEnd="8dp"
android:maxLines="1"
android:lines="1"
android:imeOptions="actionDone"/>
<View
android:id="@+id/vSpinner"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="12dp"
android:background="@color/darkGray"
app:layout_constraintTop_toBottomOf="@+id/spAsset"
app:layout_constraintEnd_toEndOf="@+id/spAsset"
app:layout_constraintStart_toStartOf="@+id/spAsset" />
</com.google.android.material.textfield.TextInputLayout>
<!--<com.google.android.material.textfield.TextInputLayout-->
<!--style="@style/Widget.Bitsy.TextInputLayout"-->
<!--android:layout_width="0dp"-->
<!--android:layout_height="0dp"-->
<!--android:layout_marginStart="12dp"-->
<!--android:hint="@string/text__asset"-->
<!--app:layout_constraintTop_toTopOf="@id/tilAmount"-->
<!--app:layout_constraintBottom_toBottomOf="@id/tilAmount"-->
<!--app:layout_constraintStart_toEndOf="@id/centeredVerticalGuideline"-->
<!--app:layout_constraintEnd_toEndOf="parent">-->
<!--<cy.agorise.bitsybitshareswallet.views.MyTextInputAutoCompleteTextView-->
<!--android:id="@+id/actvAsset"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:paddingStart="12dp"-->
<!--android:paddingEnd="8dp"-->
<!--android:maxLines="1"-->
<!--android:lines="1"-->
<!--android:imeOptions="actionDone"/>-->
<!--</com.google.android.material.textfield.TextInputLayout>-->
<ImageView
android:id="@+id/ivQR"