Skip to content

Commit

Permalink
Add PutDataMap.toObservable(). Add convenience methods for listening/…
Browse files Browse the repository at this point in the history
…getting items from a path. Improved sample project.
  • Loading branch information
patloew committed May 23, 2016
1 parent 0423120 commit 0723fe1
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
.DS_Store
/build
/captures
.idea
*.apk
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
40 changes: 40 additions & 0 deletions library/src/main/java/com/patloew/rxwear/RxWear.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,14 @@ public static Observable<DataEvent> listen(@NonNull Uri uri, int filterType, lon
return listenInternal(uri, filterType, timeout, timeUnit);
}

public static Observable<DataEvent> 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<DataEvent> 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<DataEvent> listenInternal(Uri uri, Integer filterType, Long timeout, TimeUnit timeUnit) {
return Observable.create(new DataListenerObservable(RxWear.get(), uri, filterType, timeout, timeUnit));
}
Expand Down Expand Up @@ -419,10 +427,14 @@ private static Single<DataItem> putInternal(PutDataRequest putDataRequest, Long

// getSingle

@Deprecated
// use get() instead
public static Single<DataItem> getSingle(@NonNull Uri uri) {
return getSingleInternal(uri, null, null);
}

@Deprecated
// use get() instead
public static Single<DataItem> getSingle(@NonNull Uri uri, long timeout, @NonNull TimeUnit timeUnit) {
return getSingleInternal(uri, timeout, timeUnit);
}
Expand All @@ -441,6 +453,14 @@ public static Observable<DataItem> get(@NonNull Uri uri, int filterType, long ti
return getInternal(uri, filterType, timeout, timeUnit);
}

public static Observable<DataItem> 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<DataItem> 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<DataItem> get(@NonNull Uri uri) {
return getInternal(uri, null, null, null);
}
Expand All @@ -449,6 +469,14 @@ public static Observable<DataItem> get(@NonNull Uri uri, long timeout, @NonNull
return getInternal(uri, null, timeout, timeUnit);
}

public static Observable<DataItem> get(@NonNull String path) {
return getInternal(new Uri.Builder().scheme(PutDataRequest.WEAR_URI_SCHEME).path(path).build(), null, null, null);
}

public static Observable<DataItem> 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<DataItem> get() {
return getInternal(null, null, null, null);
}
Expand Down Expand Up @@ -663,6 +691,10 @@ public PutDataMap putStringArray(String key, String[] value) {
public Single<DataItem> toSingle() {
return putInternal(request.asPutDataRequest(), null, null);
}

public Observable<DataItem> toObservable() {
return putInternal(request.asPutDataRequest(), null, null).toObservable();
}
}

}
Expand All @@ -689,6 +721,14 @@ public static Observable<MessageEvent> listen(@NonNull Uri uri, int filterType,
return listenInternal(uri, filterType, timeout, timeUnit);
}

public static Observable<MessageEvent> 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<MessageEvent> 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<MessageEvent> listenInternal(Uri uri, Integer filterType, Long timeout, TimeUnit timeUnit) {
return Observable.create(new MessageListenerObservable(RxWear.get(), uri, filterType, timeout, timeUnit));
}
Expand Down
10 changes: 5 additions & 5 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -81,8 +81,9 @@ protected void onCreate(Bundle savedInstanceState) {
}
}));

subscription.add(RxWear.Data.get()
.compose(DataItemGetSerializable.<String>filterByPath("/persistentText"))
subscription.add(RxWear.Data.get("/persistentText")
.compose(DataItemGetDataMap.noFilter())
.map(dataMap -> dataMap.getString("text"))
.subscribe(text -> persistentEditText.setText(text)));
}

Expand Down
4 changes: 2 additions & 2 deletions wearsample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
26 changes: 14 additions & 12 deletions wearsample/src/main/java/com/patloew/rxwearsample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.<String>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.<String>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
Expand Down

0 comments on commit 0723fe1

Please sign in to comment.