Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BottomNavigationView itemReselections #540

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,20 @@ public final class RxBottomNavigationViewTest {
menu.performIdentifierAction(R.id.menu_item_two, 0);
o.assertNoMoreEvents();
}

@Test @UiThreadTest public void itemReselections() {
RecordingObserver<MenuItem> o = new RecordingObserver<>();
RxBottomNavigationView.itemReselections(view).subscribe(o);

menu.performIdentifierAction(R.id.menu_item_one, 0);
assertEquals(R.id.menu_item_one, o.takeNext().getItemId());

menu.performIdentifierAction(R.id.menu_item_one, 0);
assertEquals(R.id.menu_item_one, o.takeNext().getItemId());

o.dispose();

menu.performIdentifierAction(R.id.menu_item_one, 0);
o.assertNoMoreEvents();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
@file:JvmName("RxBottomNavigationView")
@file:JvmMultifileClass

package com.jakewharton.rxbinding3.material

import android.view.MenuItem
import androidx.annotation.CheckResult
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.jakewharton.rxbinding3.internal.checkMainThread
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.MainThreadDisposable

/**
* Create an observable which emits the reselected item in `view`.
*
* *Warning:* The created observable keeps a strong reference to `view`. Unsubscribe
* to free this reference.
*
*/
@CheckResult
fun BottomNavigationView.itemReselections(): Observable<MenuItem> {
return BottomNavigationViewItemReselectionsObservable(this)
}

private class BottomNavigationViewItemReselectionsObservable(
private val view: BottomNavigationView
) : Observable<MenuItem>() {

override fun subscribeActual(observer: Observer<in MenuItem>) {
if (!checkMainThread(observer)) {
return
}
val listener = Listener(view, observer)
observer.onSubscribe(listener)
view.setOnNavigationItemReselectedListener(listener)
}

private class Listener(
private val bottomNavigationView: BottomNavigationView,
private val observer: Observer<in MenuItem>
) : MainThreadDisposable(), BottomNavigationView.OnNavigationItemReselectedListener {

override fun onNavigationItemReselected(item: MenuItem) {
if (!isDisposed) {
observer.onNext(item)
}
}

override fun onDispose() {
bottomNavigationView.setOnNavigationItemReselectedListener(null)
}
}
}