Update RxBinding library version to 3.0.0-alpha2 and its usages.

The new version makes use of Kotlin extension functions which are elegant and effective. Also, make use of RxBinding in Transactions Toolbar SearchView, to add a nice debounce effect to wait 500ms after the user stops writing the search query, and actually call the filter method to avoid multiple calls while the user is still typing.
This commit is contained in:
Severiano Jaramillo 2018-12-24 10:55:54 -06:00
parent 2f720bb114
commit a39daaf155
5 changed files with 36 additions and 12 deletions

View file

@ -40,6 +40,7 @@ dependencies {
def lifecycle_version = "2.0.0" def lifecycle_version = "2.0.0"
def room_version = "2.1.0-alpha03" def room_version = "2.1.0-alpha03"
def nav_version = "1.0.0-alpha09" def nav_version = "1.0.0-alpha09"
def rx_bindings_version = "3.0.0-alpha2"
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':graphenejlib:graphenej') implementation project(':graphenejlib:graphenej')
@ -62,6 +63,10 @@ dependencies {
// AAC Navigation // AAC Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version" implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$nav_version" implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
// RxBindings
implementation "com.jakewharton.rxbinding3:rxbinding:$rx_bindings_version"
implementation "com.jakewharton.rxbinding3:rxbinding-material:$rx_bindings_version" // Material Components widgets
implementation "com.jakewharton.rxbinding3:rxbinding-appcompat:$rx_bindings_version" // AndroidX appcompat widgets
// Retrofit // Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0' implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
@ -70,7 +75,6 @@ dependencies {
implementation 'com.google.firebase:firebase-crash:16.2.1' implementation 'com.google.firebase:firebase-crash:16.2.1'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.7' implementation 'com.crashlytics.sdk.android:crashlytics:2.9.7'
// Others // Others
implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
implementation 'org.bitcoinj:bitcoinj-core:0.14.3' implementation 'org.bitcoinj:bitcoinj-core:0.14.3'
implementation 'com.moldedbits.r2d2:r2d2:1.0.1' implementation 'com.moldedbits.r2d2:r2d2:1.0.1'
implementation 'me.dm7.barcodescanner:zxing:1.9.8' implementation 'me.dm7.barcodescanner:zxing:1.9.8'

View file

@ -7,7 +7,7 @@ import android.util.Log
import android.widget.Toast import android.widget.Toast
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItemsSingleChoice import com.afollestad.materialdialogs.list.listItemsSingleChoice
import com.jakewharton.rxbinding2.widget.RxTextView import com.jakewharton.rxbinding3.widget.textChanges
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.database.entities.Authority import cy.agorise.bitsybitshareswallet.database.entities.Authority
import cy.agorise.bitsybitshareswallet.repositories.AuthorityRepository import cy.agorise.bitsybitshareswallet.repositories.AuthorityRepository
@ -66,7 +66,7 @@ class ImportBrainkeyActivity : ConnectedActivity() {
// Use RxJava Debounce to update the PIN error only after the user stops writing for > 500 ms // Use RxJava Debounce to update the PIN error only after the user stops writing for > 500 ms
mDisposables.add( mDisposables.add(
RxTextView.textChanges(tietPin) tietPin.textChanges()
.skipInitialValue() .skipInitialValue()
.debounce(500, TimeUnit.MILLISECONDS) .debounce(500, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
@ -75,7 +75,7 @@ class ImportBrainkeyActivity : ConnectedActivity() {
// Use RxJava Debounce to update the PIN Confirmation error only after the user stops writing for > 500 ms // Use RxJava Debounce to update the PIN Confirmation error only after the user stops writing for > 500 ms
mDisposables.add( mDisposables.add(
RxTextView.textChanges(tietPinConfirmation) tietPinConfirmation.textChanges()
.skipInitialValue() .skipInitialValue()
.debounce(500, TimeUnit.MILLISECONDS) .debounce(500, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
@ -84,7 +84,7 @@ class ImportBrainkeyActivity : ConnectedActivity() {
// Use RxJava Debounce to update the BrainKey error only after the user stops writing for > 500 ms // Use RxJava Debounce to update the BrainKey error only after the user stops writing for > 500 ms
mDisposables.add( mDisposables.add(
RxTextView.textChanges(tietBrainKey) tietBrainKey.textChanges()
.skipInitialValue() .skipInitialValue()
.debounce(500, TimeUnit.MILLISECONDS) .debounce(500, TimeUnit.MILLISECONDS)
.map { it.toString().trim() } .map { it.toString().trim() }

View file

@ -25,7 +25,7 @@ import com.google.zxing.EncodeHintType
import com.google.zxing.MultiFormatWriter import com.google.zxing.MultiFormatWriter
import com.google.zxing.WriterException import com.google.zxing.WriterException
import com.google.zxing.common.BitMatrix import com.google.zxing.common.BitMatrix
import com.jakewharton.rxbinding2.widget.RxTextView import com.jakewharton.rxbinding3.widget.textChanges
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.adapters.AssetsAdapter import cy.agorise.bitsybitshareswallet.adapters.AssetsAdapter
import cy.agorise.bitsybitshareswallet.adapters.AutoSuggestAssetAdapter import cy.agorise.bitsybitshareswallet.adapters.AutoSuggestAssetAdapter
@ -158,7 +158,7 @@ class ReceiveTransactionFragment : Fragment(), ServiceConnection {
// Use RxJava Debounce to create QR code only after the user stopped typing an amount // Use RxJava Debounce to create QR code only after the user stopped typing an amount
mDisposables.add( mDisposables.add(
RxTextView.textChanges(tietAmount) tietAmount.textChanges()
.debounce(1000, TimeUnit.MILLISECONDS) .debounce(1000, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe { updateQR() } .subscribe { updateQR() }
@ -171,7 +171,7 @@ class ReceiveTransactionFragment : Fragment(), ServiceConnection {
// Use RxJava Debounce to avoid making calls to the NetworkService on every text change event and also avoid // Use RxJava Debounce to avoid making calls to the NetworkService on every text change event and also avoid
// the first call when the View is created // the first call when the View is created
mDisposables.add( mDisposables.add(
RxTextView.textChanges(actvAsset) actvAsset.textChanges()
.skipInitialValue() .skipInitialValue()
.debounce(500, TimeUnit.MILLISECONDS) .debounce(500, TimeUnit.MILLISECONDS)
.map { it.toString().trim().toUpperCase() } .map { it.toString().trim().toUpperCase() }

View file

@ -22,7 +22,7 @@ import androidx.lifecycle.ViewModelProviders
import com.google.common.primitives.UnsignedLong import com.google.common.primitives.UnsignedLong
import com.google.zxing.BarcodeFormat import com.google.zxing.BarcodeFormat
import com.google.zxing.Result import com.google.zxing.Result
import com.jakewharton.rxbinding2.widget.RxTextView import com.jakewharton.rxbinding3.widget.textChanges
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.adapters.BalancesDetailsAdapter import cy.agorise.bitsybitshareswallet.adapters.BalancesDetailsAdapter
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
@ -180,7 +180,7 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
// Use RxJava Debounce to avoid making calls to the NetworkService on every text change event // Use RxJava Debounce to avoid making calls to the NetworkService on every text change event
mDisposables.add( mDisposables.add(
RxTextView.textChanges(tietTo) tietTo.textChanges()
.debounce(500, TimeUnit.MILLISECONDS) .debounce(500, TimeUnit.MILLISECONDS)
.map { it.toString().trim() } .map { it.toString().trim() }
.filter { it.length > 1 } .filter { it.length > 1 }
@ -192,7 +192,7 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
// Use RxJava Debounce to update the Amount error only after the user stops writing for > 500 ms // Use RxJava Debounce to update the Amount error only after the user stops writing for > 500 ms
mDisposables.add( mDisposables.add(
RxTextView.textChanges(tietAmount) tietAmount.textChanges()
.debounce(500, TimeUnit.MILLISECONDS) .debounce(500, TimeUnit.MILLISECONDS)
.filter { it.isNotEmpty() } .filter { it.isNotEmpty() }
.map { it.toString().trim().toDouble() } .map { it.toString().trim().toDouble() }

View file

@ -10,14 +10,17 @@ import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.jakewharton.rxbinding3.appcompat.queryTextChanges
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.adapters.TransfersDetailsAdapter import cy.agorise.bitsybitshareswallet.adapters.TransfersDetailsAdapter
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
import cy.agorise.bitsybitshareswallet.utils.BounceTouchListener import cy.agorise.bitsybitshareswallet.utils.BounceTouchListener
import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.viewmodels.TransferDetailViewModel import cy.agorise.bitsybitshareswallet.viewmodels.TransferDetailViewModel
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.fragment_transactions.* import kotlinx.android.synthetic.main.fragment_transactions.*
import java.util.* import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
class TransactionsFragment : Fragment(), SearchView.OnQueryTextListener { class TransactionsFragment : Fragment(), SearchView.OnQueryTextListener {
@ -41,6 +44,8 @@ class TransactionsFragment : Fragment(), SearchView.OnQueryTextListener {
private var filterFromFiatAmount = 0L private var filterFromFiatAmount = 0L
private var filterToFiatAmount = 500L private var filterToFiatAmount = 500L
private var mDisposables = CompositeDisposable()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
setHasOptionsMenu(true) setHasOptionsMenu(true)
@ -83,7 +88,16 @@ class TransactionsFragment : Fragment(), SearchView.OnQueryTextListener {
// Adds listener for the SearchView // Adds listener for the SearchView
val searchItem = menu.findItem(R.id.menuSearch) val searchItem = menu.findItem(R.id.menuSearch)
val searchView = searchItem.actionView as SearchView val searchView = searchItem.actionView as SearchView
searchView.setOnQueryTextListener(this) mDisposables.add(
searchView.queryTextChanges()
.skipInitialValue()
.debounce(500, TimeUnit.MILLISECONDS)
.map { it.toString().toLowerCase() }
.subscribe {
filterQuery = it
applyFilterOptions()
}
)
// Adjust SearchView width to avoid pushing other menu items out of the screen // Adjust SearchView width to avoid pushing other menu items out of the screen
searchView.maxWidth = getScreenWidth(activity) * 3 / 5 searchView.maxWidth = getScreenWidth(activity) * 3 / 5
@ -154,4 +168,10 @@ class TransactionsFragment : Fragment(), SearchView.OnQueryTextListener {
if (scrollToTop) if (scrollToTop)
rvTransactions.scrollToPosition(0) rvTransactions.scrollToPosition(0)
} }
override fun onDestroy() {
super.onDestroy()
if (!mDisposables.isDisposed) mDisposables.dispose()
}
} }