|  | 
|  | 1 | +package com.buzzbil.rxandroidsample.activity; | 
|  | 2 | + | 
|  | 3 | +import android.os.Bundle; | 
|  | 4 | +import android.support.v7.app.AppCompatActivity; | 
|  | 5 | +import android.widget.Button; | 
|  | 6 | +import android.widget.CheckBox; | 
|  | 7 | +import android.widget.Toast; | 
|  | 8 | + | 
|  | 9 | +import com.buzzbil.rxandroidsample.R; | 
|  | 10 | +import com.jakewharton.rxbinding2.view.RxView; | 
|  | 11 | +import com.jakewharton.rxbinding2.widget.RxCompoundButton; | 
|  | 12 | + | 
|  | 13 | +import io.reactivex.Observable; | 
|  | 14 | +import io.reactivex.disposables.Disposable; | 
|  | 15 | +import io.reactivex.functions.BiFunction; | 
|  | 16 | +import io.reactivex.functions.Consumer; | 
|  | 17 | +import io.reactivex.functions.Predicate; | 
|  | 18 | + | 
|  | 19 | +public class RxViewActivity extends AppCompatActivity { | 
|  | 20 | + | 
|  | 21 | +    @Override | 
|  | 22 | +    protected void onCreate(Bundle savedInstanceState) { | 
|  | 23 | +        super.onCreate(savedInstanceState); | 
|  | 24 | +        setContentView(R.layout.activity_rx_view); | 
|  | 25 | + | 
|  | 26 | +        Button button = findViewById(R.id.rx_view_btn); | 
|  | 27 | +        Observable<Object> clickObs = RxView.clicks(button); | 
|  | 28 | + | 
|  | 29 | + | 
|  | 30 | +        CheckBox checkBox = findViewById(R.id.rx_view_checkbox); | 
|  | 31 | +        Observable<Boolean> checkedTrueObs = RxCompoundButton.checkedChanges(checkBox) | 
|  | 32 | +                .skipInitialValue(); | 
|  | 33 | + | 
|  | 34 | +        Disposable subscribe = clickObs | 
|  | 35 | +                // 클릭 스트림에 체크 스트림의 마지막 값을 조합해서 새로운 스트림을 만든다. | 
|  | 36 | +                .withLatestFrom(checkedTrueObs, new BiFunction<Object, Boolean, Boolean>() { | 
|  | 37 | +                    @Override | 
|  | 38 | +                    public Boolean apply(Object o, Boolean checked) throws Exception { | 
|  | 39 | +                        // 새로운 스트림의 데이터는 체크 상태인지 아닌지를 나타낸다. | 
|  | 40 | +                        return checked; | 
|  | 41 | +                    } | 
|  | 42 | +                }) | 
|  | 43 | +                // 체크 상태인 경우만 필터링 한다. | 
|  | 44 | +                .filter(new Predicate<Boolean>() { | 
|  | 45 | +                    @Override | 
|  | 46 | +                    public boolean test(Boolean aBoolean) throws Exception { | 
|  | 47 | +                        return aBoolean == true; | 
|  | 48 | +                    } | 
|  | 49 | +                }) | 
|  | 50 | +                // 여기까지 오면 클릭 시점에 마지막 체크 상태가 true 인 경우인 스트림을 나타낸다. | 
|  | 51 | +                .subscribe(new Consumer<Object>() { | 
|  | 52 | +                    @Override | 
|  | 53 | +                    public void accept(Object o) throws Exception { | 
|  | 54 | +                        Toast.makeText(RxViewActivity.this, "result : " + o, Toast.LENGTH_SHORT).show(); | 
|  | 55 | +                    } | 
|  | 56 | +                }); | 
|  | 57 | +    } | 
|  | 58 | +} | 
0 commit comments