diff --git a/.gitignore b/.gitignore index f69630c23c..52b0ae57e2 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ Thumbs.db # Gradle Files # ################ .gradle +.gradletasknamecache .m2 # Build output directies diff --git a/rxjava-contrib/rxjava-android-samples-build-wrapper/build.gradle b/rxjava-contrib/rxjava-android-samples-build-wrapper/build.gradle new file mode 100644 index 0000000000..03513b751f --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples-build-wrapper/build.gradle @@ -0,0 +1,11 @@ +tasks.build.doLast { + def androidHome = System.getenv("ANDROID_HOME") + if (project.hasProperty('buildAndroidSamples') && !androidHome.isEmpty()) { + println("Android SDK detected at $androidHome, running samples build") + project.exec { + workingDir '../rxjava-android-samples' + + commandLine "./gradlew", "clean", "packageDebug" + } + } +} \ No newline at end of file diff --git a/rxjava-contrib/rxjava-android-samples/.gitignore b/rxjava-contrib/rxjava-android-samples/.gitignore new file mode 100644 index 0000000000..d6bfc95b18 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/.gitignore @@ -0,0 +1,4 @@ +.gradle +/local.properties +/.idea/workspace.xml +.DS_Store diff --git a/rxjava-contrib/rxjava-android-samples/build.gradle b/rxjava-contrib/rxjava-android-samples/build.gradle new file mode 100644 index 0000000000..0ba4fcdb79 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/build.gradle @@ -0,0 +1,17 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath 'com.android.tools.build:gradle:0.9.+' + } +} + +allprojects { + repositories { + mavenLocal() + mavenCentral() + } +} diff --git a/rxjava-contrib/rxjava-android-samples/gradle.properties b/rxjava-contrib/rxjava-android-samples/gradle.properties new file mode 100644 index 0000000000..5d08ba75bb --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/gradle.properties @@ -0,0 +1,18 @@ +# Project-wide Gradle settings. + +# IDE (e.g. Android Studio) users: +# Settings specified in this file will override any Gradle settings +# configured through the IDE. + +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html + +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +# Default value: -Xmx10248m -XX:MaxPermSize=256m +# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 + +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true \ No newline at end of file diff --git a/rxjava-contrib/rxjava-android-samples/gradle/wrapper/gradle-wrapper.properties b/rxjava-contrib/rxjava-android-samples/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..a4b6756f97 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Mar 12 12:09:46 CET 2014 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=http\://services.gradle.org/distributions/gradle-1.10-all.zip diff --git a/rxjava-contrib/rxjava-android-samples/gradlew b/rxjava-contrib/rxjava-android-samples/gradlew new file mode 100755 index 0000000000..91a7e269e1 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# For Cygwin, ensure paths are in UNIX format before anything is touched. +if $cygwin ; then + [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` +fi + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >&- +APP_HOME="`pwd -P`" +cd "$SAVED" >&- + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/rxjava-contrib/rxjava-android-samples/gradlew.bat b/rxjava-contrib/rxjava-android-samples/gradlew.bat new file mode 100644 index 0000000000..8a0b282aa6 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/rxjava-contrib/rxjava-android-samples/samples/.gitignore b/rxjava-contrib/rxjava-android-samples/samples/.gitignore new file mode 100644 index 0000000000..796b96d1c4 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/.gitignore @@ -0,0 +1 @@ +/build diff --git a/rxjava-contrib/rxjava-android-samples/samples/build.gradle b/rxjava-contrib/rxjava-android-samples/samples/build.gradle new file mode 100644 index 0000000000..9a080128a8 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/build.gradle @@ -0,0 +1,32 @@ +apply plugin: 'android' + +android { + compileSdkVersion 19 + buildToolsVersion "19.0.0" + + defaultConfig { + minSdkVersion 14 + targetSdkVersion 19 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + runProguard false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' + } + } +} + +// make sure we always compile against the latest version of RxJava +def rootProjectProperties = new Properties() +file("../../../gradle.properties").withReader { reader -> + rootProjectProperties.load(reader) + properties.putAll(rootProjectProperties) +} + +dependencies { + def rxjVersion = rootProjectProperties.get("version") + compile "com.netflix.rxjava:rxjava-android:$rxjVersion" + compile fileTree(dir: 'libs', include: ['*.jar']) +} diff --git a/rxjava-contrib/rxjava-android-samples/samples/proguard-rules.txt b/rxjava-contrib/rxjava-android-samples/samples/proguard-rules.txt new file mode 100644 index 0000000000..c423b03cb2 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/proguard-rules.txt @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /usr/local/opt/android-sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the ProGuard +# include property in project.properties. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} \ No newline at end of file diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/AndroidManifest.xml b/rxjava-contrib/rxjava-android-samples/samples/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..f307d55bb0 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/AndroidManifest.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/ListFragmentActivity.java b/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/ListFragmentActivity.java new file mode 100644 index 0000000000..446bffa55d --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/ListFragmentActivity.java @@ -0,0 +1,75 @@ +package com.netflix.rxjava.android.samples; + +import android.app.Activity; +import android.app.ListFragment; +import android.os.Bundle; +import android.widget.ArrayAdapter; + +import rx.Observable; +import rx.Subscriber; + +import static rx.android.schedulers.AndroidSchedulers.mainThread; + +/** + * Problem: + * You have an asynchronous sequence that emits items to be displayed in a list. You want the data + * to survive rotation changes. + *

+ * Solution: + * Combine {@link android.app.Fragment#setRetainInstance(boolean)} in a ListFragment with + * {@link rx.android.schedulers.AndroidSchedulers#mainThread()} and an {@link rx.Observable.Operator} + * that binds to the list adapter. + */ +public class ListFragmentActivity extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setTitle("Lists"); + setContentView(R.layout.list_fragment_activity); + } + + @SuppressWarnings("ConstantConditions") + public static class RetainedListFragment extends ListFragment { + + private ArrayAdapter adapter; + + public RetainedListFragment() { + setRetainInstance(true); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1); + setListAdapter(adapter); + SampleObservables.numberStrings(1, 20, 250) + .observeOn(mainThread()) + .lift(new BindAdapter()) + .subscribe(); + } + + private final class BindAdapter implements Observable.Operator { + @Override + public Subscriber call(Subscriber subscriber) { + return new Subscriber() { + @Override + public void onCompleted() { + adapter.notifyDataSetChanged(); + } + + @Override + public void onError(Throwable throwable) { + + } + + @Override + public void onNext(String strings) { + adapter.add(strings); + } + }; + } + } + } +} diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/ListeningFragmentActivity.java b/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/ListeningFragmentActivity.java new file mode 100644 index 0000000000..b402135bc1 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/ListeningFragmentActivity.java @@ -0,0 +1,96 @@ +package com.netflix.rxjava.android.samples; + +import android.app.Activity; +import android.app.Fragment; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import rx.Subscriber; +import rx.Subscription; +import rx.observables.ConnectableObservable; +import rx.subscriptions.Subscriptions; + +import static rx.android.schedulers.AndroidSchedulers.mainThread; + +/** + * Problem: + * You have a background sequence which keeps emitting items (either a limited or unlimited number) + * and your UI component should be able to "listen in" to the sequence, i.e. it's okay to miss + * in-flight items when going e.g. through a screen rotation or being otherwise detached from the + * screen for a limited period of time. (Another example is a "page out" in a fragment ViewPager.) + *

+ * This is useful if you need behavior that mimics event buses. Think of a publishing + * Observable as a channel or queue on an event bus. + *

+ * Solution: + * Combine {@link android.app.Fragment#setRetainInstance(boolean)} with + * {@link rx.android.schedulers.AndroidSchedulers#mainThread()} and {@link rx.Observable#publish()} + */ +public class ListeningFragmentActivity extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.listening_fragment_activity); + } + + @SuppressWarnings("ConstantConditions") + public static class ListeningFragment extends Fragment { + + private ConnectableObservable strings; + private Subscription subscription = Subscriptions.empty(); + + public ListeningFragment() { + setRetainInstance(true); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + strings = SampleObservables.numberStrings(1, 50, 250).observeOn(mainThread()).publish(); + strings.connect(); // trigger the sequence + } + + @Override + public void onDestroyView() { + subscription.unsubscribe(); // stop listening + super.onDestroyView(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.retained_fragment, container, false); + } + + @Override + public void onViewCreated(final View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + final TextView textView = (TextView) view.findViewById(android.R.id.text1); + + // re-connect to sequence + subscription = strings.subscribe(new Subscriber() { + + @Override + public void onCompleted() { + Toast.makeText(getActivity(), "Done!", Toast.LENGTH_SHORT).show(); + } + + @Override + public void onError(Throwable throwable) { + + } + + @Override + public void onNext(String s) { + textView.setText(s); + } + }); + } + } +} diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/RetainedFragmentActivity.java b/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/RetainedFragmentActivity.java new file mode 100644 index 0000000000..82c5225101 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/RetainedFragmentActivity.java @@ -0,0 +1,105 @@ +package com.netflix.rxjava.android.samples; + +import android.app.Activity; +import android.app.Fragment; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.widget.TextView; + +import org.json.JSONException; +import org.json.JSONObject; + +import rx.Observable; +import rx.Subscription; +import rx.android.schedulers.AndroidSchedulers; +import rx.functions.Action1; +import rx.functions.Func1; +import rx.subscriptions.Subscriptions; + +/** + * Problem: + * You have a data source (where that data is potentially expensive to obtain), and you want to + * emit this data into a fragment. However, you want to gracefully deal with rotation changes and + * not lose any data already emitted. + *

+ * Solution: + * Combine {@link android.app.Fragment#setRetainInstance(boolean)} with + * {@link rx.android.schedulers.AndroidSchedulers#mainThread()} and {@link rx.Observable#cache()} + */ +public class RetainedFragmentActivity extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); + setTitle("Fake API call"); + setContentView(R.layout.retained_fragment_activity); + } + + @SuppressWarnings("ConstantConditions") + public static class RetainedFragment extends Fragment { + + // in a production app, you don't want to have JSON parser code in your fragment, + // but we'll simplify a little here + private static final Func1 PARSE_JSON = new Func1() { + @Override + public String call(String json) { + try { + JSONObject jsonObject = new JSONObject(json); + return String.valueOf(jsonObject.getInt("result")); + } catch (JSONException e) { + throw new RuntimeException(e); + } + } + }; + + private Observable strings; + private Subscription subscription = Subscriptions.empty(); + + public RetainedFragment() { + setRetainInstance(true); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // simulate fetching a JSON document with a latency of 2 seconds + strings = SampleObservables.fakeApiCall(2000).map(PARSE_JSON) + .observeOn(AndroidSchedulers.mainThread()) + .cache(); + } + + @Override + public void onDestroyView() { + subscription.unsubscribe(); + super.onDestroyView(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + getActivity().setProgressBarIndeterminateVisibility(true); + return inflater.inflate(R.layout.retained_fragment, container, false); + } + + @Override + public void onViewCreated(final View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + final TextView textView = (TextView) view.findViewById(android.R.id.text1); + + // (re-)subscribe to the sequence, which either emits the cached result or simply re- + // attaches the subscriber to wait for it to arrive + subscription = strings.subscribe(new Action1() { + @Override + public void call(String result) { + textView.setText(result); + getActivity().setProgressBarIndeterminateVisibility(false); + } + }); + } + } +} diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/SampleObservables.java b/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/SampleObservables.java new file mode 100644 index 0000000000..7a17bedad5 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/SampleObservables.java @@ -0,0 +1,42 @@ +package com.netflix.rxjava.android.samples; + +import android.os.SystemClock; + +import rx.Observable; +import rx.Subscriber; +import rx.functions.Action1; +import rx.functions.Func1; +import rx.schedulers.Schedulers; + +public class SampleObservables { + + /** + * Emits numbers as strings, where these numbers a generated on a background thread. + */ + public static Observable numberStrings(int from, int to, final long delay) { + return Observable.range(from, to).map(new Func1() { + @Override + public String call(Integer integer) { + return integer.toString(); + } + }).doOnNext(new Action1() { + @Override + public void call(String s) { + SystemClock.sleep(delay); + } + }).subscribeOn(Schedulers.newThread()); + } + + public static Observable fakeApiCall(final long delay) { + return Observable.create(new Observable.OnSubscribe() { + @Override + public void call(Subscriber subscriber) { + // simulate I/O latency + SystemClock.sleep(delay); + final String fakeJson = "{\"result\": 42}"; + subscriber.onNext(fakeJson); + subscriber.onCompleted(); + } + }).subscribeOn(Schedulers.io()); + } +} diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/SamplesApplication.java b/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/SamplesApplication.java new file mode 100644 index 0000000000..e91e7d543e --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/java/com/netflix/rxjava/android/samples/SamplesApplication.java @@ -0,0 +1,13 @@ +package com.netflix.rxjava.android.samples; + +import android.app.Application; +import android.os.StrictMode; + +public class SamplesApplication extends Application { + + @Override + public void onCreate() { + super.onCreate(); + StrictMode.enableDefaults(); + } +} diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-hdpi/ic_launcher.png b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000000..96a442e5b8 Binary files /dev/null and b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-hdpi/ic_launcher.png differ diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-mdpi/ic_launcher.png b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 0000000000..359047dfa4 Binary files /dev/null and b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-mdpi/ic_launcher.png differ diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-xhdpi/ic_launcher.png b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000000..71c6d760f0 Binary files /dev/null and b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-xhdpi/ic_launcher.png differ diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-xxhdpi/ic_launcher.png b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 0000000000..4df1894644 Binary files /dev/null and b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/list_fragment_activity.xml b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/list_fragment_activity.xml new file mode 100644 index 0000000000..f741ee62dc --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/list_fragment_activity.xml @@ -0,0 +1,13 @@ + + + + + diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/listening_fragment_activity.xml b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/listening_fragment_activity.xml new file mode 100644 index 0000000000..ecfc325d20 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/listening_fragment_activity.xml @@ -0,0 +1,14 @@ + + + + + diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/retained_fragment.xml b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/retained_fragment.xml new file mode 100644 index 0000000000..0fc012b481 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/retained_fragment.xml @@ -0,0 +1,13 @@ + + + + + + + \ No newline at end of file diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/retained_fragment_activity.xml b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/retained_fragment_activity.xml new file mode 100644 index 0000000000..e5eb51fb86 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/layout/retained_fragment_activity.xml @@ -0,0 +1,14 @@ + + + + + diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/menu/menu.xml b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/menu/menu.xml new file mode 100644 index 0000000000..3c1d36c3d1 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/menu/menu.xml @@ -0,0 +1,7 @@ +

+ + + diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values-w820dp/dimens.xml b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values-w820dp/dimens.xml new file mode 100644 index 0000000000..63fc816444 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values-w820dp/dimens.xml @@ -0,0 +1,6 @@ + + + 64dp + diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values/dimens.xml b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values/dimens.xml new file mode 100644 index 0000000000..a0171a705d --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values/dimens.xml @@ -0,0 +1,6 @@ + + + 16dp + 16dp + + diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values/strings.xml b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values/strings.xml new file mode 100644 index 0000000000..79ce6bb997 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values/strings.xml @@ -0,0 +1,9 @@ + + + + RxJava Android Samples + RetainedFragmentActivity + Hello world! + Settings + + diff --git a/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values/styles.xml b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values/styles.xml new file mode 100644 index 0000000000..b07e679474 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/samples/src/main/res/values/styles.xml @@ -0,0 +1,12 @@ + + + + + + + diff --git a/rxjava-contrib/rxjava-android-samples/settings.gradle b/rxjava-contrib/rxjava-android-samples/settings.gradle new file mode 100644 index 0000000000..f3a182b826 --- /dev/null +++ b/rxjava-contrib/rxjava-android-samples/settings.gradle @@ -0,0 +1 @@ +include ':samples' diff --git a/settings.gradle b/settings.gradle index 5a9e3e8cb0..e9d3b54392 100644 --- a/settings.gradle +++ b/settings.gradle @@ -7,6 +7,7 @@ include 'rxjava-core', \ 'language-adaptors:rxjava-kotlin', \ 'rxjava-contrib:rxjava-swing', \ 'rxjava-contrib:rxjava-android', \ +'rxjava-contrib:rxjava-android-samples-build-wrapper', \ 'rxjava-contrib:rxjava-apache-http', \ 'rxjava-contrib:rxjava-string', \ 'rxjava-contrib:rxjava-math', \