|
7 | 7 | import android.view.ViewGroup; |
8 | 8 | import android.widget.EditText; |
9 | 9 | import android.widget.TextView; |
10 | | - |
11 | | -import com.jakewharton.rxbinding.widget.RxTextView; |
12 | | -import com.morihacky.android.rxjava.R; |
13 | | - |
14 | 10 | import butterknife.Bind; |
15 | 11 | import butterknife.ButterKnife; |
16 | | -import rx.Observable; |
17 | | -import rx.Observer; |
18 | | -import rx.Subscription; |
| 12 | +import com.jakewharton.rxbinding.widget.RxTextView; |
| 13 | +import com.morihacky.android.rxjava.R; |
| 14 | +import hu.akarnokd.rxjava.interop.RxJavaInterop; |
| 15 | +import io.reactivex.Flowable; |
| 16 | +import io.reactivex.subscribers.DisposableSubscriber; |
19 | 17 | import timber.log.Timber; |
20 | 18 |
|
| 19 | + |
21 | 20 | import static android.text.TextUtils.isEmpty; |
22 | 21 | import static android.util.Patterns.EMAIL_ADDRESS; |
23 | 22 |
|
@@ -29,24 +28,27 @@ public class FormValidationCombineLatestFragment |
29 | 28 | @Bind(R.id.demo_combl_password) EditText _password; |
30 | 29 | @Bind(R.id.demo_combl_num) EditText _number; |
31 | 30 |
|
32 | | - private Observable<CharSequence> _emailChangeObservable; |
33 | | - private Observable<CharSequence> _passwordChangeObservable; |
34 | | - private Observable<CharSequence> _numberChangeObservable; |
35 | | - |
36 | | - private Subscription _subscription = null; |
| 31 | + private DisposableSubscriber<Boolean> _disposableObserver = null; |
| 32 | + private Flowable<CharSequence> _emailChangeObservable; |
| 33 | + private Flowable<CharSequence> _numberChangeObservable; |
| 34 | + private Flowable<CharSequence> _passwordChangeObservable; |
37 | 35 |
|
38 | 36 | @Override |
39 | 37 | public View onCreateView(LayoutInflater inflater, |
40 | 38 | @Nullable ViewGroup container, |
41 | 39 | @Nullable Bundle savedInstanceState) { |
42 | | - View layout = inflater.inflate(R.layout.fragment_form_validation_comb_latest, |
43 | | - container, |
44 | | - false); |
| 40 | + View layout = inflater.inflate(R.layout.fragment_form_validation_comb_latest, container, false); |
45 | 41 | ButterKnife.bind(this, layout); |
46 | 42 |
|
47 | | - _emailChangeObservable = RxTextView.textChanges(_email).skip(1); |
48 | | - _passwordChangeObservable = RxTextView.textChanges(_password).skip(1); |
49 | | - _numberChangeObservable = RxTextView.textChanges(_number).skip(1); |
| 43 | + _emailChangeObservable = RxJavaInterop.toV2Flowable(RxTextView |
| 44 | + .textChanges(_email) |
| 45 | + .skip(1)); |
| 46 | + _passwordChangeObservable = RxJavaInterop.toV2Flowable(RxTextView |
| 47 | + .textChanges(_password) |
| 48 | + .skip(1)); |
| 49 | + _numberChangeObservable = RxJavaInterop.toV2Flowable(RxTextView |
| 50 | + .textChanges(_number) |
| 51 | + .skip(1)); |
50 | 52 |
|
51 | 53 | _combineLatestEvents(); |
52 | 54 |
|
@@ -57,57 +59,63 @@ public View onCreateView(LayoutInflater inflater, |
57 | 59 | public void onDestroyView() { |
58 | 60 | super.onDestroyView(); |
59 | 61 | ButterKnife.unbind(this); |
60 | | - _subscription.unsubscribe(); |
| 62 | + _disposableObserver.dispose(); |
61 | 63 | } |
62 | 64 |
|
63 | 65 | private void _combineLatestEvents() { |
64 | | - _subscription = Observable.combineLatest(_emailChangeObservable, |
65 | | - _passwordChangeObservable, |
66 | | - _numberChangeObservable, |
67 | | - (newEmail, newPassword, newNumber) -> { |
68 | | - |
69 | | - boolean emailValid = !isEmpty(newEmail) && |
70 | | - EMAIL_ADDRESS.matcher(newEmail).matches(); |
71 | | - if (!emailValid) { |
72 | | - _email.setError("Invalid Email!"); |
73 | | - } |
74 | | - |
75 | | - boolean passValid = !isEmpty(newPassword) && newPassword.length() > 8; |
76 | | - if (!passValid) { |
77 | | - _password.setError("Invalid Password!"); |
78 | | - } |
79 | | - |
80 | | - boolean numValid = !isEmpty(newNumber); |
81 | | - if (numValid) { |
82 | | - int num = Integer.parseInt(newNumber.toString()); |
83 | | - numValid = num > 0 && num <= 100; |
84 | | - } |
85 | | - if (!numValid) { |
86 | | - _number.setError("Invalid Number!"); |
87 | | - } |
88 | | - |
89 | | - return emailValid && passValid && numValid; |
90 | | - |
91 | | - })// |
92 | | - .subscribe(new Observer<Boolean>() { |
93 | | - @Override |
94 | | - public void onCompleted() { |
95 | | - Timber.d("completed"); |
96 | | - } |
97 | | - |
98 | | - @Override |
99 | | - public void onError(Throwable e) { |
100 | | - Timber.e(e, "there was an error"); |
101 | | - } |
102 | | - |
103 | | - @Override |
104 | | - public void onNext(Boolean formValid) { |
105 | | - if (formValid) { |
106 | | - _btnValidIndicator.setBackgroundColor(getResources().getColor(R.color.blue)); |
107 | | - } else { |
108 | | - _btnValidIndicator.setBackgroundColor(getResources().getColor(R.color.gray)); |
109 | | - } |
110 | | - } |
111 | | - }); |
| 66 | + |
| 67 | + _disposableObserver = new DisposableSubscriber<Boolean>() { |
| 68 | + @Override |
| 69 | + public void onNext(Boolean formValid) { |
| 70 | + if (formValid) { |
| 71 | + _btnValidIndicator.setBackgroundColor(getResources().getColor(R.color.blue)); |
| 72 | + } |
| 73 | + else { |
| 74 | + _btnValidIndicator.setBackgroundColor(getResources().getColor(R.color.gray)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onError(Throwable e) { |
| 80 | + Timber.e(e, "there was an error"); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onComplete() { |
| 85 | + Timber.d("completed"); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + Flowable |
| 90 | + .combineLatest(_emailChangeObservable, |
| 91 | + _passwordChangeObservable, |
| 92 | + _numberChangeObservable, |
| 93 | + (newEmail, newPassword, newNumber) -> { |
| 94 | + |
| 95 | + boolean emailValid = !isEmpty(newEmail) && |
| 96 | + EMAIL_ADDRESS |
| 97 | + .matcher(newEmail) |
| 98 | + .matches(); |
| 99 | + if (!emailValid) { |
| 100 | + _email.setError("Invalid Email!"); |
| 101 | + } |
| 102 | + |
| 103 | + boolean passValid = !isEmpty(newPassword) && newPassword.length() > 8; |
| 104 | + if (!passValid) { |
| 105 | + _password.setError("Invalid Password!"); |
| 106 | + } |
| 107 | + |
| 108 | + boolean numValid = !isEmpty(newNumber); |
| 109 | + if (numValid) { |
| 110 | + int num = Integer.parseInt(newNumber.toString()); |
| 111 | + numValid = num > 0 && num <= 100; |
| 112 | + } |
| 113 | + if (!numValid) { |
| 114 | + _number.setError("Invalid Number!"); |
| 115 | + } |
| 116 | + |
| 117 | + return emailValid && passValid && numValid; |
| 118 | + }) |
| 119 | + .subscribe(_disposableObserver); |
112 | 120 | } |
113 | 121 | } |