RxJava icon indicating copy to clipboard operation
RxJava copied to clipboard

Question: Simplify property binding (Android)

Open Gi-lo opened this issue 12 years ago • 4 comments

We are currently evaluating using RxJava for our Android projects, since our iOS colleagues have had some good experiences with ReactiveCocoa. ReactiveCocoa seems to offer some additional syntactic sugar, and in one case we are wondering if there is an equivalent in RxJava we could use.

If we want to bind, for example, the visibility of a view on the strings emitted by one of our subjects, we currently do it like this:

stringSubject.map(new Func1<String, Integer>() {
            @Override
            public Integer call(String newValue) {
                return newValue == null ? View.GONE : View.VISIBLE;
            }
        }).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer visibility) {
                view.setVisibility(visibility);
            }
        }));

ReactiveCocoa has a nice macro, which in our eyes makes it more obvious that we're trying to do some kind of property binding:

RAC(self, view.hidden) = [RACObserve(self, viewModel.string) map:^id(NSString *value) {
            return @([value length] == 0);
        }];

Is there any syntactic way in RxJava to rewrite our Android code like that? Are there some best practices for cases like this?

Gi-lo avatar Aug 25 '14 17:08 Gi-lo

RxJava doesn't have any kind of built-in way to observe property change notifications on objects, or to pipe them to other properties like the RAC macro (aka the ToProperty method in ReactiveUI).

As the author of ReactiveUI who is writing Java code at the moment, I Think This Idea Is Cool Too™ :smiley_cat:

anaisbetts avatar Aug 25 '14 17:08 anaisbetts

@paulcbetts Thanks for your quick reply.

... have any kind of built-in way to observe property change notifications on objects ...

On iOS there is Key-Value Observing which can be used to get notified whenever a property changes. I haven't found any equivalent for Android yet. Currently we are creating a BehaviorSubject and send next() to it manually whenever we change our property.

... or to pipe them to other properties like the RAC ...

The RAC macro is using object-subscripting to enable the piping. Same as above: I haven't found anything similar on Android yet.

Gi-lo avatar Aug 25 '14 20:08 Gi-lo

Closing out as it is stale. If further discussion on this is warranted please open an issue on the new RxAndroid project: https://github.com/ReactiveX/RxAndroid

benjchristensen avatar Oct 07 '14 23:10 benjchristensen

BTW, thanks @paulcbetts for answering

benjchristensen avatar Oct 07 '14 23:10 benjchristensen