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

RxBinding for ChipGroup checked events #461

Open
wants to merge 2 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
ext {
androidPlugin = 'com.android.tools.build:gradle:3.0.1'
minSdkVersion = 14
compileSdkVersion = 27
compileSdkVersion = 28

kotlinPlugin = 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.21'
kotlinStdlib = 'org.jetbrains.kotlin:kotlin-stdlib:1.2.21'
kotlinPoet = 'com.squareup:kotlinpoet:0.6.0'

supportVersion = '27.0.2'
supportVersion = '28.0.0-alpha1'
supportAnnotations = "com.android.support:support-annotations:$supportVersion"
supportV4CoreUi = "com.android.support:support-core-ui:$supportVersion"
supportRecyclerView = "com.android.support:recyclerview-v7:$supportVersion"
supportAppCompat = "com.android.support:appcompat-v7:$supportVersion"
supportDesign = "com.android.support:design:$supportVersion"
supportDesignChip = "com.android.support:design-chip:$supportVersion"
supportLeanback = "com.android.support:leanback-v17:$supportVersion"

autoValue = 'com.google.auto.value:auto-value:1.5.2'
Expand Down
1 change: 1 addition & 0 deletions rxbinding-design-chip/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
57 changes: 57 additions & 0 deletions rxbinding-design-chip/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
buildscript {
dependencies {
classpath rootProject.ext.androidPlugin
}
}

apply plugin: 'com.android.library'
apply plugin: 'rxbinding-module'

dependencies {
api project(':rxbinding')
api rootProject.ext.supportDesignChip
implementation rootProject.ext.rxAndroid
implementation rootProject.ext.supportAnnotations

compileOnly rootProject.ext.autoValueAnnotations
annotationProcessor rootProject.ext.autoValue

androidTestImplementation project(':testing-utils')
androidTestImplementation rootProject.ext.supportTestRunner
androidTestImplementation rootProject.ext.supportTestRules
androidTestImplementation rootProject.ext.supportTestEspresso
androidTestImplementation rootProject.ext.rxAndroid
}

android {
compileSdkVersion rootProject.ext.compileSdkVersion

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion

testInstrumentationRunner 'com.jakewharton.rxbinding.RxBindingTestRunner'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

lintOptions {
textReport true
textOutput 'stdout'
}

buildTypes {
debug {
testCoverageEnabled true
}
}

// TODO replace with https://issuetracker.google.com/issues/72050365 once released.
libraryVariants.all {
it.generateBuildConfig.enabled = false
}
}

apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
3 changes: 3 additions & 0 deletions rxbinding-design-chip/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POM_ARTIFACT_ID=rxbinding-design-chip
POM_NAME=RxBinding (design-chip)
POM_PACKAGING=aar
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.jakewharton.rxbinding2.support.design.chip;

import android.content.Context;
import android.support.design.chip.Chip;
import android.support.design.chip.ChipGroup;
import android.support.test.InstrumentationRegistry;
import android.support.test.annotation.UiThreadTest;
import android.support.test.rule.UiThreadTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v7.view.ContextThemeWrapper;
import android.view.View;

import com.jakewharton.rxbinding2.RecordingObserver;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import io.reactivex.functions.Consumer;

import static org.junit.Assert.assertEquals;

@RunWith(AndroidJUnit4.class)
@SuppressWarnings("ResourceType") // Don't need real IDs for test case.
public final class RxChipGroupTest {
@Rule public final UiThreadTestRule uiThread = new UiThreadTestRule();

private final Context context = new ContextThemeWrapper(InstrumentationRegistry.getContext(), R.style.Theme_AppCompat);
private final ChipGroup view = new ChipGroup(context);

@Before public void setUp() {
view.setSingleSelection(true);
Chip button1 = new Chip(context);
button1.setId(1);
view.addView(button1);
Chip button2 = new Chip(context);
button2.setId(2);
view.addView(button2);
}

@Test @UiThreadTest public void checkedChanges() {
RecordingObserver<Integer> o = new RecordingObserver<>();
RxChipGroup.checkedChanges(view).subscribe(o);
assertEquals(View.NO_ID, o.takeNext().intValue());

view.check(1);
assertEquals(1, o.takeNext().intValue());

view.clearCheck();
assertEquals(View.NO_ID, o.takeNext().intValue());

view.check(2);
assertEquals(2, o.takeNext().intValue());

o.dispose();

view.check(1);
o.assertNoMoreEvents();
}

@Test @UiThreadTest public void checked() throws Exception {
Consumer<? super Integer> action = RxChipGroup.checked(view);
assertEquals(View.NO_ID, view.getCheckedChipId());
action.accept(1);
assertEquals(1, view.getCheckedChipId());
action.accept(View.NO_ID);
assertEquals(View.NO_ID, view.getCheckedChipId());
action.accept(2);
assertEquals(2, view.getCheckedChipId());
}
}
1 change: 1 addition & 0 deletions rxbinding-design-chip/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="com.jakewharton.rxbinding2.support.design.chip" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.jakewharton.rxbinding2.support.design.chip;

import android.support.design.chip.ChipGroup;
import android.support.design.chip.ChipGroup.OnCheckedChangeListener;
import android.view.View;

import com.jakewharton.rxbinding2.InitialValueObservable;
import io.reactivex.Observer;
import io.reactivex.android.MainThreadDisposable;

import static com.jakewharton.rxbinding2.internal.Preconditions.checkMainThread;

final class ChipGroupCheckedChangeObservable extends InitialValueObservable<Integer> {
private final ChipGroup view;

ChipGroupCheckedChangeObservable(ChipGroup view) {
this.view = view;
}

@Override
protected void subscribeListener(Observer<? super Integer> observer) {
if (!checkMainThread(observer)) {
return;
}
Listener listener = new Listener(view, observer);
view.setOnCheckedChangeListener(listener);
observer.onSubscribe(listener);
}

@Override
protected Integer getInitialValue() {
return view.getCheckedChipId();
}

static final class Listener extends MainThreadDisposable implements OnCheckedChangeListener {
private final ChipGroup view;
private final Observer<? super Integer> observer;
private int lastChecked = View.NO_ID;

Listener(ChipGroup view, Observer<? super Integer> observer) {
this.view = view;
this.observer = observer;
}

@Override
public void onCheckedChanged(ChipGroup chipGroup, int checkedId) {
if (!isDisposed() && checkedId != lastChecked) {
lastChecked = checkedId;
observer.onNext(checkedId);
}
}

@Override
protected void onDispose() {
view.setOnCheckedChangeListener(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.jakewharton.rxbinding2.support.design.chip;

import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.design.chip.ChipGroup;
import android.view.View;
import com.jakewharton.rxbinding2.InitialValueObservable;
import io.reactivex.functions.Consumer;

import static com.jakewharton.rxbinding2.internal.Preconditions.checkNotNull;

public final class RxChipGroup {
/**
* Create an observable of the checked chip ID changes in {@code view}.
* <p>
* <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
* to free this reference.
* </p>
* <p>
* <em>Note:</em> A value will be emitted immediately on subscribe
* </p>
*/
@CheckResult @NonNull
public static InitialValueObservable<Integer> checkedChanges(@NonNull ChipGroup view) {
checkNotNull(view, "view == null");
return new ChipGroupCheckedChangeObservable(view);
}

/**
* An action which sets the checked child of {@code view} with ID. Passing {@code View.NO_ID} will clear
* any checked view.
* <p>
* <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
* to free this reference
* </p>
*/
@CheckResult @NonNull
public static Consumer<? super Integer> checked(@NonNull final ChipGroup view) {
checkNotNull(view, "view == null");
return new Consumer<Integer>() {
@Override
public void accept(Integer value) {
if (value == View.NO_ID) {
view.clearCheck();
} else {
view.check(value);
}
}
};
}

private RxChipGroup() {
throw new AssertionError("No instances.");
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include ':rxbinding-appcompat-v7-kotlin'

include ':rxbinding-design'
include ':rxbinding-design-kotlin'
include ':rxbinding-design-chip'

include ':rxbinding-support-v4'
include ':rxbinding-support-v4-kotlin'
Expand Down