Skip to content

Commit 59b8195

Browse files
author
Kaushik Gopal
committed
feat: port example Form validation (using combineLatest)
1 parent d83a666 commit 59b8195

File tree

2 files changed

+77
-70
lines changed

2 files changed

+77
-70
lines changed

app/src/main/java/com/morihacky/android/rxjava/MainActivity.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import android.os.Bundle;
44
import android.support.v4.app.Fragment;
55
import android.support.v4.app.FragmentActivity;
6+
import com.morihacky.android.rxjava.fragments.MainFragment;
67
import com.morihacky.android.rxjava.fragments.RotationPersist1WorkerFragment;
78
import com.morihacky.android.rxjava.fragments.RotationPersist2WorkerFragment;
8-
import com.morihacky.android.rxjava.fragments.TimeoutDemoFragment;
99
import com.morihacky.android.rxjava.rxbus.RxBus;
1010

1111
public class MainActivity
@@ -25,8 +25,7 @@ protected void onCreate(Bundle savedInstanceState) {
2525

2626
if (savedInstanceState == null) {
2727
getSupportFragmentManager().beginTransaction()
28-
//.replace(android.R.id.content, new MainFragment(), this.toString())
29-
.replace(android.R.id.content, new TimeoutDemoFragment(), this.toString())
28+
.replace(android.R.id.content, new MainFragment(), this.toString())
3029
.commit();
3130
}
3231
}

app/src/main/java/com/morihacky/android/rxjava/fragments/FormValidationCombineLatestFragment.java

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
import android.view.ViewGroup;
88
import android.widget.EditText;
99
import android.widget.TextView;
10-
11-
import com.jakewharton.rxbinding.widget.RxTextView;
12-
import com.morihacky.android.rxjava.R;
13-
1410
import butterknife.Bind;
1511
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;
1917
import timber.log.Timber;
2018

19+
2120
import static android.text.TextUtils.isEmpty;
2221
import static android.util.Patterns.EMAIL_ADDRESS;
2322

@@ -29,24 +28,27 @@ public class FormValidationCombineLatestFragment
2928
@Bind(R.id.demo_combl_password) EditText _password;
3029
@Bind(R.id.demo_combl_num) EditText _number;
3130

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;
3735

3836
@Override
3937
public View onCreateView(LayoutInflater inflater,
4038
@Nullable ViewGroup container,
4139
@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);
4541
ButterKnife.bind(this, layout);
4642

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));
5052

5153
_combineLatestEvents();
5254

@@ -57,57 +59,63 @@ public View onCreateView(LayoutInflater inflater,
5759
public void onDestroyView() {
5860
super.onDestroyView();
5961
ButterKnife.unbind(this);
60-
_subscription.unsubscribe();
62+
_disposableObserver.dispose();
6163
}
6264

6365
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);
112120
}
113121
}

0 commit comments

Comments
 (0)