22
33import android .content .Context ;
44import androidx .annotation .NonNull ;
5- import com .google .android .material .textfield .TextInputLayout ;
6- import androidx .appcompat .widget .AppCompatSpinner ;
7- import android .view .View ;
8- import android .widget .EditText ;
95import android .widget .Toast ;
106
117import com .blogspot .e_kanivets .moneytracker .R ;
8+ import com .blogspot .e_kanivets .moneytracker .databinding .ActivityAddExchangeRateBinding ;
129import com .blogspot .e_kanivets .moneytracker .entity .ExchangeRatePair ;
1310
14- import butterknife .BindView ;
15- import butterknife .ButterKnife ;
16-
17- /**
18- * Util class for Transfer validation.
19- * Created on 13.12.2016.
20- *
21- * @author Evgenii Kanivets
22- */
23-
2411@ SuppressWarnings ("WeakerAccess" )
2512public class ExchangeRatePairValidator implements IValidator <ExchangeRatePair > {
2613
2714 @ NonNull
2815 private final Context context ;
2916
30- @ BindView (R .id .spinner_from_currency )
31- AppCompatSpinner spinnerFromCurrency ;
32- @ BindView (R .id .spinner_to_currency )
33- AppCompatSpinner spinnerToCurrency ;
34- @ BindView (R .id .til_buy )
35- TextInputLayout tilBuy ;
36- @ BindView (R .id .et_buy )
37- EditText etBuy ;
38- @ BindView (R .id .til_sell )
39- TextInputLayout tilSell ;
40- @ BindView (R .id .et_sell )
41- EditText etSell ;
42-
43- public ExchangeRatePairValidator (@ NonNull Context context , @ NonNull View view ) {
17+ private ActivityAddExchangeRateBinding binding ;
18+
19+ public ExchangeRatePairValidator (
20+ @ NonNull Context context ,
21+ @ NonNull ActivityAddExchangeRateBinding binding
22+ ) {
4423 this .context = context ;
45- ButterKnife . bind ( this , view ) ;
24+ this . binding = binding ;
4625 initTextWatchers ();
4726 }
4827
@@ -51,15 +30,15 @@ public boolean validate() {
5130 boolean valid = true ;
5231
5332 String fromCurrency = null ;
54- if (spinnerFromCurrency .isEnabled ()) {
55- fromCurrency = (String ) spinnerFromCurrency .getSelectedItem ();
33+ if (binding . spinnerFromCurrency .isEnabled ()) {
34+ fromCurrency = (String ) binding . spinnerFromCurrency .getSelectedItem ();
5635 } else {
5736 valid = false ;
5837 }
5938
6039 String toCurrency = null ;
61- if (spinnerToCurrency .isEnabled ()) {
62- toCurrency = (String ) spinnerToCurrency .getSelectedItem ();
40+ if (binding . spinnerToCurrency .isEnabled ()) {
41+ toCurrency = (String ) binding . spinnerToCurrency .getSelectedItem ();
6342 } else {
6443 valid = false ;
6544 }
@@ -71,45 +50,45 @@ public boolean validate() {
7150
7251 double amountBuy = Double .MAX_VALUE ;
7352 try {
74- amountBuy = Double .parseDouble (etBuy .getText ().toString ().trim ());
53+ amountBuy = Double .parseDouble (binding . etBuy .getText ().toString ().trim ());
7554 } catch (Exception e ) {
7655 e .printStackTrace ();
7756 }
7857
7958 if (amountBuy == Double .MAX_VALUE ) {
80- tilBuy .setError (context .getString (R .string .field_cant_be_empty ));
59+ binding . tilBuy .setError (context .getString (R .string .field_cant_be_empty ));
8160 amountBuy = 0 ;
8261 valid = false ;
8362 }
8463
8564 if (amountBuy > MAX_ABS_VALUE ) {
86- tilBuy .setError (context .getString (R .string .too_much_for_exchange ));
65+ binding . tilBuy .setError (context .getString (R .string .too_much_for_exchange ));
8766 valid = false ;
8867 }
8968
9069 double amountSell = Double .MAX_VALUE ;
9170 try {
92- amountSell = Double .parseDouble (etSell .getText ().toString ().trim ());
71+ amountSell = Double .parseDouble (binding . etSell .getText ().toString ().trim ());
9372 } catch (Exception e ) {
9473 e .printStackTrace ();
9574 }
9675
9776 if (amountSell == Double .MAX_VALUE ) {
98- tilSell .setError (context .getString (R .string .field_cant_be_empty ));
77+ binding . tilSell .setError (context .getString (R .string .field_cant_be_empty ));
9978 amountSell = 0 ;
10079 valid = false ;
10180 }
10281
10382 if (amountSell > MAX_ABS_VALUE ) {
104- tilSell .setError (context .getString (R .string .too_much_for_exchange ));
83+ binding . tilSell .setError (context .getString (R .string .too_much_for_exchange ));
10584 valid = false ;
10685 }
10786
10887 return valid ;
10988 }
11089
11190 private void initTextWatchers () {
112- etBuy .addTextChangedListener (new ClearErrorTextWatcher (tilBuy ));
113- etSell .addTextChangedListener (new ClearErrorTextWatcher (tilSell ));
91+ binding . etBuy .addTextChangedListener (new ClearErrorTextWatcher (binding . tilBuy ));
92+ binding . etSell .addTextChangedListener (new ClearErrorTextWatcher (binding . tilSell ));
11493 }
11594}
0 commit comments