forked from kaushikgopal/RxJava-Android-Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormValidationCombineLatestFragment.java
165 lines (140 loc) · 5.69 KB
/
FormValidationCombineLatestFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.morihacky.android.rxjava.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;
import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.functions.Func1;
import rx.functions.Func3;
import timber.log.Timber;
import static android.text.TextUtils.isEmpty;
import static android.util.Patterns.EMAIL_ADDRESS;
public class FormValidationCombineLatestFragment
extends BaseFragment {
@Bind(R.id.btn_demo_form_valid) TextView _btnValidIndicator;
@Bind(R.id.demo_combl_email) EditText _email;
@Bind(R.id.demo_combl_password) EditText _password;
@Bind(R.id.demo_combl_num) EditText _number;
private Observable<CharSequence> _emailChangeObservable;
private Observable<CharSequence> _passwordChangeObservable;
private Observable<CharSequence> _numberChangeObservable;
private Subscription _subscription = null;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_form_validation_comb_latest,
container,
false);
ButterKnife.bind(this, layout);
_emailChangeObservable = RxTextView.textChanges(_email);
_passwordChangeObservable = RxTextView.textChanges(_password);
_numberChangeObservable = RxTextView.textChanges(_number);
return layout;
}
@Override
public void onStart() {
super.onStart();
_combineLatestEvents();
}
@Override
public void onStop() {
super.onStop();
RxUtils.unsubscribeIfNotNull(_subscription);
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
private void _combineLatestEvents() {
_subscription = Observable.combineLatest(
_emailChangeObservable,
_passwordChangeObservable,
_numberChangeObservable,
new Func3<CharSequence, CharSequence, CharSequence, Form>() {
@Override
public Form call(CharSequence newEmail,
CharSequence newPassword,
CharSequence newNumber) {
return new Form(newEmail, newPassword, newNumber);
}
})
.skipWhile(new FormIsClean())
.map(new Func1<Form, Boolean>() {
@Override
public Boolean call(Form form) {
boolean emailValid = !isEmpty(form.email) && EMAIL_ADDRESS.matcher(form.email).matches();
boolean passwordValid = !isEmpty(form.password) && form.password.length() > 8;
boolean numberValid = !isEmpty(form.number);
if (numberValid) {
int num = Integer.parseInt(form.number.toString());
numberValid = num > 0 && num <= 100;
}
if (!emailValid) {
_email.setError("Invalid Email!");
}
if (!passwordValid) {
_password.setError("Invalid Password!");
}
if (!numberValid) {
_number.setError("Invalid Number!");
}
return emailValid && passwordValid && numberValid;
}
})
.subscribe(new Observer<Boolean>() {
@Override
public void onCompleted() {
Timber.d("completed");
}
@Override
public void onError(Throwable e) {
Timber.e(e, "there was an error");
}
@Override
public void onNext(Boolean valid) {
if (valid) {
_btnValidIndicator.setBackgroundColor(
ContextCompat.getColor(getActivity(), R.color.blue)
);
} else {
_btnValidIndicator.setBackgroundColor(
ContextCompat.getColor(getActivity(), R.color.gray)
);
}
}
});
}
private static class FormIsClean implements Func1<Form, Boolean> {
private boolean isDirty = false;
@Override
public Boolean call(Form form) {
if (!isDirty) {
isDirty = !isEmpty(form.email) && !isEmpty(form.password) && !isEmpty(form.number);
}
return !isDirty;
}
}
private static class Form {
private final CharSequence email;
private final CharSequence password;
private final CharSequence number;
public Form(CharSequence newEmail, CharSequence newPassword, CharSequence newNumber) {
this.email = newEmail;
this.password = newPassword;
this.number = newNumber;
}
}
}