Skip to content

Instantly share code, notes, and snippets.

@tehmou
Created February 21, 2017 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tehmou/3c5bc523cf7086043e6342ecbb008daf to your computer and use it in GitHub Desktop.
Save tehmou/3c5bc523cf7086043e6342ecbb008daf to your computer and use it in GitHub Desktop.
RxJava Android example of .withLatestFrom usage
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.tehmou.book.chapter7coffeebreak.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"/>
<EditText
android:id="@+id/title_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message"/>
<EditText
android:id="@+id/message_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Alert"/>
</LinearLayout>
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.tehmou.book.chapter7coffeebreak"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
jackOptions {
enabled true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
// RxJava
compile 'io.reactivex:rxjava:1.1.3'
compile 'io.reactivex:rxandroid:1.1.0'
// RxBinding wrappers
compile 'com.jakewharton.rxbinding:rxbinding:0.3.0'
}
package com.tehmou.book.chapter7coffeebreak;
import android.support.v4.util.Pair;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Observable<String> titleObservable =
RxTextView
.textChanges((TextView) findViewById(R.id.title_edit_text))
.map(Object::toString);
final Observable<String> messageObservable =
RxTextView
.textChanges((TextView) findViewById(R.id.message_edit_text))
.map(Object::toString);
final Observable<Void> clickEvents =
RxView.clicks(findViewById(R.id.action_button));
final Observable<Pair<String, String>> dialogContentsObservable =
Observable.combineLatest(titleObservable, messageObservable, Pair::new);
final Observable<Pair<String, String>> showDialogEventObservable =
clickEvents.withLatestFrom(dialogContentsObservable,
(ignore, dialogContents) -> dialogContents);
showDialogEventObservable
.observeOn(AndroidSchedulers.mainThread())
.subscribe(dialogContents ->
new AlertDialog.Builder(this)
.setTitle(dialogContents.first)
.setMessage(dialogContents.second)
.show()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment