|
16 | 16 | import android.support.v7.widget.LinearLayoutManager; |
17 | 17 | import android.support.v7.widget.RecyclerView; |
18 | 18 | import android.support.v7.widget.SwitchCompat; |
| 19 | +import android.text.Editable; |
| 20 | +import android.text.TextWatcher; |
19 | 21 | import android.text.util.Linkify; |
20 | 22 | import android.text.util.Linkify.TransformFilter; |
21 | 23 | import android.util.Log; |
|
45 | 47 | import com.flutterwave.raveandroid.responses.ChargeResponse; |
46 | 48 | import com.flutterwave.raveandroid.responses.RequeryResponse; |
47 | 49 |
|
| 50 | +import java.text.ParseException; |
| 51 | +import java.text.SimpleDateFormat; |
| 52 | +import java.util.Calendar; |
48 | 53 | import java.util.List; |
49 | 54 | import java.util.regex.Matcher; |
50 | 55 | import java.util.regex.Pattern; |
@@ -161,29 +166,7 @@ public void onClick(View v) { |
161 | 166 | } |
162 | 167 | }); |
163 | 168 |
|
164 | | - cardExpiryTv.setOnClickListener(new View.OnClickListener() { |
165 | | - @Override |
166 | | - public void onClick(View v) { |
167 | | - ExpirationPickerBuilder epb = new ExpirationPickerBuilder() |
168 | | - .setFragmentManager(getActivity().getSupportFragmentManager()) |
169 | | - .setStyleResId(R.style.BetterPickersDialogFragment) |
170 | | - .setMinYear(2000); |
171 | | - epb.show(); |
172 | | - |
173 | | - epb.addExpirationPickerDialogHandler(new ExpirationPickerDialogFragment.ExpirationPickerDialogHandler() { |
174 | | - @Override |
175 | | - public void onDialogExpirationSet(int reference, int year, int monthOfYear) { |
176 | | - String month = String.valueOf(monthOfYear); |
177 | | - if (month.length() != 2) { |
178 | | - month = "0" + month; |
179 | | - } |
180 | | - |
181 | | - String year_str = String.valueOf(year).substring(2, 4); |
182 | | - cardExpiryTv.setText(month + "/" + year_str); |
183 | | - } |
184 | | - }); |
185 | | - } |
186 | | - }); |
| 169 | + cardExpiryTv.addTextChangedListener(new ExpiryWatcher()); |
187 | 170 |
|
188 | 171 |
|
189 | 172 | payButton.setOnClickListener(this); |
@@ -851,4 +834,63 @@ public void onPageFinished(WebView view, String url) { |
851 | 834 | } |
852 | 835 | } |
853 | 836 |
|
| 837 | + private class ExpiryWatcher implements TextWatcher { |
| 838 | + |
| 839 | + private final Calendar calendar; |
| 840 | + private final SimpleDateFormat simpleDateFormat; |
| 841 | + private String lastInput = ""; |
| 842 | + |
| 843 | + public ExpiryWatcher() { |
| 844 | + calendar = Calendar.getInstance(); |
| 845 | + simpleDateFormat = new SimpleDateFormat("MM/yy"); |
| 846 | + } |
| 847 | + |
| 848 | + @Override |
| 849 | + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { |
| 850 | + |
| 851 | + } |
| 852 | + |
| 853 | + @Override |
| 854 | + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { |
| 855 | + |
| 856 | + } |
| 857 | + |
| 858 | + @Override |
| 859 | + public void afterTextChanged(Editable editable) { |
| 860 | + String input = editable.toString(); |
| 861 | + |
| 862 | + try { |
| 863 | + calendar.setTime(simpleDateFormat.parse(input)); |
| 864 | + } catch (ParseException e) { |
| 865 | + if (editable.length() == 2 && !lastInput.endsWith("/")) { |
| 866 | + int month = Integer.parseInt(input); |
| 867 | + if (month <= 12) { |
| 868 | + cardExpiryTv.setText(cardExpiryTv.getText().toString() + "/"); |
| 869 | + cardExpiryTv.setSelection(cardExpiryTv.getText().toString().length()); |
| 870 | + } else { |
| 871 | + cardExpiryTv.setText("12"); |
| 872 | + cardExpiryTv.setSelection(cardExpiryTv.getText().toString().length()); |
| 873 | + } |
| 874 | + } else if (editable.length() == 2 && lastInput.endsWith("/")) { |
| 875 | + int month = Integer.parseInt(input); |
| 876 | + if (month <= 12) { |
| 877 | + cardExpiryTv.setText(cardExpiryTv.getText().toString().substring(0,1)); |
| 878 | + cardExpiryTv.setSelection(cardExpiryTv.getText().toString().length()); |
| 879 | + } else { |
| 880 | + cardExpiryTv.setText("12"); |
| 881 | + cardExpiryTv.setSelection(cardExpiryTv.getText().toString().length()); |
| 882 | + } |
| 883 | + } else if (editable.length() == 1) { |
| 884 | + int month = Integer.parseInt(input); |
| 885 | + if (month > 1) { |
| 886 | + cardExpiryTv.setText("0" + cardExpiryTv.getText().toString() + "/"); |
| 887 | + cardExpiryTv.setSelection(cardExpiryTv.getText().toString().length()); |
| 888 | + } |
| 889 | + } |
| 890 | + |
| 891 | + lastInput = cardExpiryTv.getText().toString(); |
| 892 | + } |
| 893 | + } |
| 894 | + } |
| 895 | + |
854 | 896 | } |
0 commit comments