From 0723fe198f8522766c245743ce4e06a18e6d575c Mon Sep 17 00:00:00 2001 From: patloew Date: Mon, 23 May 2016 13:41:54 +0200 Subject: [PATCH] Add PutDataMap.toObservable(). Add convenience methods for listening/getting items from a path. Improved sample project. --- .gitignore | 2 + build.gradle | 2 +- gradle.properties | 1 + library/build.gradle | 2 +- .../main/java/com/patloew/rxwear/RxWear.java | 40 +++++++++++++++++++ sample/build.gradle | 10 ++--- .../patloew/rxwearsample/MainActivity.java | 9 +++-- wearsample/build.gradle | 4 +- .../patloew/rxwearsample/MainActivity.java | 26 ++++++------ 9 files changed, 71 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index c6cbe56..64ae266 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ .DS_Store /build /captures +.idea +*.apk diff --git a/build.gradle b/build.gradle index ac01455..e1e1231 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.0.0-beta6' + classpath 'com.android.tools.build:gradle:2.1.0' classpath 'me.tatarka:gradle-retrolambda:3.3.0-beta4' classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2' diff --git a/gradle.properties b/gradle.properties index 1d3591c..a741f6e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,6 +11,7 @@ # 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 +org.gradle.jvmargs=-Xmx2048M # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit diff --git a/library/build.gradle b/library/build.gradle index e938348..4752a2d 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -29,7 +29,7 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'io.reactivex:rxjava:1.1.1' + compile 'io.reactivex:rxjava:1.1.5' compile 'com.google.android.gms:play-services-wearable:8.4.0' testCompile 'junit:junit:4.12' diff --git a/library/src/main/java/com/patloew/rxwear/RxWear.java b/library/src/main/java/com/patloew/rxwear/RxWear.java index c04a0cf..7940457 100644 --- a/library/src/main/java/com/patloew/rxwear/RxWear.java +++ b/library/src/main/java/com/patloew/rxwear/RxWear.java @@ -368,6 +368,14 @@ public static Observable listen(@NonNull Uri uri, int filterType, lon return listenInternal(uri, filterType, timeout, timeUnit); } + public static Observable listen(@NonNull String path, int filterType) { + return listenInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, null, null); + } + + public static Observable listen(@NonNull String path, int filterType, long timeout, @NonNull TimeUnit timeUnit) { + return listenInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, timeout, timeUnit); + } + private static Observable listenInternal(Uri uri, Integer filterType, Long timeout, TimeUnit timeUnit) { return Observable.create(new DataListenerObservable(RxWear.get(), uri, filterType, timeout, timeUnit)); } @@ -419,10 +427,14 @@ private static Single putInternal(PutDataRequest putDataRequest, Long // getSingle + @Deprecated + // use get() instead public static Single getSingle(@NonNull Uri uri) { return getSingleInternal(uri, null, null); } + @Deprecated + // use get() instead public static Single getSingle(@NonNull Uri uri, long timeout, @NonNull TimeUnit timeUnit) { return getSingleInternal(uri, timeout, timeUnit); } @@ -441,6 +453,14 @@ public static Observable get(@NonNull Uri uri, int filterType, long ti return getInternal(uri, filterType, timeout, timeUnit); } + public static Observable get(@NonNull String path, int filterType) { + return getInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, null, null); + } + + public static Observable get(@NonNull String path, int filterType, long timeout, @NonNull TimeUnit timeUnit) { + return getInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, timeout, timeUnit); + } + public static Observable get(@NonNull Uri uri) { return getInternal(uri, null, null, null); } @@ -449,6 +469,14 @@ public static Observable get(@NonNull Uri uri, long timeout, @NonNull return getInternal(uri, null, timeout, timeUnit); } + public static Observable get(@NonNull String path) { + return getInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), null, null, null); + } + + public static Observable get(@NonNull String path, long timeout, @NonNull TimeUnit timeUnit) { + return getInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), null, timeout, timeUnit); + } + public static Observable get() { return getInternal(null, null, null, null); } @@ -663,6 +691,10 @@ public PutDataMap putStringArray(String key, String[] value) { public Single toSingle() { return putInternal(request.asPutDataRequest(), null, null); } + + public Observable toObservable() { + return putInternal(request.asPutDataRequest(), null, null).toObservable(); + } } } @@ -689,6 +721,14 @@ public static Observable listen(@NonNull Uri uri, int filterType, return listenInternal(uri, filterType, timeout, timeUnit); } + public static Observable listen(@NonNull String path, int filterType) { + return listenInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, null, null); + } + + public static Observable listen(@NonNull String path, int filterType, long timeout, @NonNull TimeUnit timeUnit) { + return listenInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), filterType, timeout, timeUnit); + } + private static Observable listenInternal(Uri uri, Integer filterType, Long timeout, TimeUnit timeUnit) { return Observable.create(new MessageListenerObservable(RxWear.get(), uri, filterType, timeout, timeUnit)); } diff --git a/sample/build.gradle b/sample/build.gradle index 92bda16..2486e99 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -33,15 +33,15 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.2.0' - compile "com.android.support:design:23.2.0" - compile "com.android.support:gridlayout-v7:23.2.0" + compile 'com.android.support:appcompat-v7:23.4.0' + compile "com.android.support:design:23.4.0" + compile "com.android.support:gridlayout-v7:23.4.0" compile project(':library') //compile 'com.patloew.rxwear:rxwear:1.1.0' - compile 'io.reactivex:rxjava:1.1.1' - compile 'io.reactivex:rxandroid:1.1.0' + compile 'io.reactivex:rxjava:1.1.5' + compile 'io.reactivex:rxandroid:1.2.0' compile 'com.jakewharton.rxbinding:rxbinding:0.4.0' compile 'com.google.android.gms:play-services-wearable:8.4.0' diff --git a/sample/src/main/java/com/patloew/rxwearsample/MainActivity.java b/sample/src/main/java/com/patloew/rxwearsample/MainActivity.java index f545366..6377919 100644 --- a/sample/src/main/java/com/patloew/rxwearsample/MainActivity.java +++ b/sample/src/main/java/com/patloew/rxwearsample/MainActivity.java @@ -14,7 +14,7 @@ import com.jakewharton.rxbinding.view.RxView; import com.patloew.rxwear.GoogleAPIConnectionException; import com.patloew.rxwear.RxWear; -import com.patloew.rxwear.transformers.DataItemGetSerializable; +import com.patloew.rxwear.transformers.DataItemGetDataMap; import rx.Observable; import rx.subscriptions.CompositeSubscription; @@ -69,7 +69,7 @@ protected void onCreate(Bundle savedInstanceState) { subscription.add(RxView.clicks(setPersistentButton) .doOnNext(click -> hideKeyboard()) - .flatMap(click2 -> RxWear.Data.PutSerializable.urgentTo("/persistentText", persistentEditText.getText().toString()).toObservable()) + .flatMap(click2 -> RxWear.Data.PutDataMap.to("/persistentText").setUrgent().putString("text", persistentEditText.getText().toString()).toObservable()) .subscribe(dataItem1 -> Snackbar.make(coordinatorLayout, "Set persistent text", Snackbar.LENGTH_LONG).show(), throwable -> { Log.e("MainActivity", "Error on setting persistent text", throwable); @@ -81,8 +81,9 @@ protected void onCreate(Bundle savedInstanceState) { } })); - subscription.add(RxWear.Data.get() - .compose(DataItemGetSerializable.filterByPath("/persistentText")) + subscription.add(RxWear.Data.get("/persistentText") + .compose(DataItemGetDataMap.noFilter()) + .map(dataMap -> dataMap.getString("text")) .subscribe(text -> persistentEditText.setText(text))); } diff --git a/wearsample/build.gradle b/wearsample/build.gradle index cdb4571..9e5bb2c 100644 --- a/wearsample/build.gradle +++ b/wearsample/build.gradle @@ -37,7 +37,7 @@ dependencies { compile project(':library') //compile 'com.patloew.rxwear:rxwear:1.1.0' - compile 'io.reactivex:rxjava:1.1.1' - compile 'io.reactivex:rxandroid:1.1.0' + compile 'io.reactivex:rxjava:1.1.5' + compile 'io.reactivex:rxandroid:1.2.0' compile 'com.jakewharton.rxbinding:rxbinding:0.4.0' } diff --git a/wearsample/src/main/java/com/patloew/rxwearsample/MainActivity.java b/wearsample/src/main/java/com/patloew/rxwearsample/MainActivity.java index fb3d979..fe5ee23 100644 --- a/wearsample/src/main/java/com/patloew/rxwearsample/MainActivity.java +++ b/wearsample/src/main/java/com/patloew/rxwearsample/MainActivity.java @@ -6,12 +6,15 @@ import android.widget.TextView; import android.widget.Toast; +import com.google.android.gms.wearable.DataApi; import com.google.android.gms.wearable.DataEvent; +import com.google.android.gms.wearable.MessageApi; import com.patloew.rxwear.RxWear; -import com.patloew.rxwear.transformers.DataEventGetSerializable; -import com.patloew.rxwear.transformers.DataItemGetSerializable; +import com.patloew.rxwear.transformers.DataEventGetDataMap; +import com.patloew.rxwear.transformers.DataItemGetDataMap; import com.patloew.rxwear.transformers.MessageEventGetDataMap; +import rx.Observable; import rx.subscriptions.CompositeSubscription; public class MainActivity extends WearableActivity { @@ -36,22 +39,21 @@ protected void onCreate(Bundle savedInstanceState) { RxWear.init(this); - subscription.add(RxWear.Message.listen() - .compose(MessageEventGetDataMap.filterByPath("/message")) + subscription.add(RxWear.Message.listen("/message", MessageApi.FILTER_LITERAL) + .compose(MessageEventGetDataMap.noFilter()) .subscribe(dataMap -> { mTitleText.setText(dataMap.getString("title", getString(R.string.no_message))); mMessageText.setText(dataMap.getString("message", getString(R.string.no_message_info))); }, throwable -> Toast.makeText(this, "Error on message listen", Toast.LENGTH_LONG))); - subscription.add(RxWear.Data.listen() - .compose(DataEventGetSerializable.filterByPathAndType("/persistentText", DataEvent.TYPE_CHANGED)) + subscription.add( + Observable.concat( + RxWear.Data.get("/persistentText").compose(DataItemGetDataMap.noFilter()), + RxWear.Data.listen("/persistentText", DataApi.FILTER_LITERAL).compose(DataEventGetDataMap.filterByType(DataEvent.TYPE_CHANGED)) + ).map(dataMap -> dataMap.getString("text")) .subscribe(text -> mPersistentText.setText(text), - throwable -> Toast.makeText(this, "Error on data listen", Toast.LENGTH_LONG))); - - subscription.add(RxWear.Data.get() - .compose(DataItemGetSerializable.filterByPath("/persistentText")) - .subscribe(text -> mPersistentText.setText(text), - throwable -> Toast.makeText(this, "Error on getting DataItem", Toast.LENGTH_LONG))); + throwable -> Toast.makeText(this, "Error on data listen", Toast.LENGTH_LONG)) + ); } @Override