user3201520 user3201520 - 1 year ago 49
Android Question

how to listen to the original observable after it is flatmaped to a new observable?

public Observable<Void> onFabClicked() {
return RxView.clicks(a_button);
}

{
view.onOutBoundBtClicked()
.observeOn(computationScheduler)
.map(new Func1<Void, ViewModelAction>() {
@Override public ViewModelAction call(Void aVoid) {
return new OutboundInputFieldTappedAction();
}
}).mergeWith(view.onFabClicked().flatMap(new Func1<Void, Observable<ListOfResult>>() {
@Override
public Observable<ListOfResult> call(Void avoid) {
return listRepository.getListResult("UK",
"en-GB",
).toObservable();
}
}).map(new Func1<ListOfResult, ViewViewModelAction>() {
@Override
public ViewViewModelAction call(ListOfResult ListOfResult) {

return new ListOfResultsReceivedAction(ListOfResult);
}
}))
.mergeWith(view.onFabClicked()
.observeOn(computationScheduler)
.map(new Func1<Void, ViewModelAction>() {
@Override
public ViewModelAction call(Void aVoid) {
return new SearchPanelAnimationAction();
}
}))


I have an observable1 which is come from RxView.clicks(a_button), how can I create another observable2 from this click observable1, then emit observable1 and observable2 to a same subscriber? I tried flatmap that convert observable1 to ovservable2 but then observalbe1 is never heard, the code is

{
view.onOutBoundBtClicked().observeOn(computationScheduler).map(new Func1() {
@Override
public ViewModelAction call(Void aVoid) {
return new OutboundInputFieldTappedAction();
}
}).mergeWith.


, and why I always return an XXXAction is that I will change some fields in the viewModel, and I will use

.withLatestFrom(viewModels, new Func2() {
@Override
public ActivityViewModel call(ViewModelAction action, ActivityViewModel activityViewModel) {
return action.modifyViewModel(activityViewModel);
}


so when ever something changed in the viewmodel I will get an event emitted and do some thing upon it, thanks, guys.

Answer Source

You need to create an observable field that is shared, and expose that instead of returning RxView.clicks() directly.

Observable<Void> fabClicks;

public void onCreate(Bundle bundle) {
    ...

    fabClicks = RxView.clicks(a_button).share();
}

public Observable<Void> onFabClicked() {
    return fabClicks;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download