]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/DatePickerFragment.java
bf674ff74730811ad910af210e87c390e83bf39a
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / DatePickerFragment.java
1 package net.ktnx.mobileledger;
2
3 import android.annotation.TargetApi;
4 import android.app.DatePickerDialog;
5 import android.app.Dialog;
6 import android.os.Build;
7 import android.os.Bundle;
8 import android.support.annotation.NonNull;
9 import android.support.v7.app.AppCompatDialogFragment;
10 import android.widget.DatePicker;
11 import android.widget.TextView;
12
13 import java.util.Calendar;
14 import java.util.GregorianCalendar;
15 import java.util.Locale;
16 import java.util.Objects;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 public class DatePickerFragment extends AppCompatDialogFragment
21 implements DatePickerDialog.OnDateSetListener, DatePicker.OnDateChangedListener
22 {
23     @NonNull
24     @Override
25     public Dialog onCreateDialog(Bundle savedInstanceState) {
26         final Calendar c = GregorianCalendar.getInstance();
27         int year = c.get(GregorianCalendar.YEAR);
28         int month = c.get(GregorianCalendar.MONTH);
29         int day = c.get(GregorianCalendar.DAY_OF_MONTH);
30         TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
31
32         CharSequence present = date.getText();
33
34         Pattern re_mon_day = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
35         Matcher m_mon_day = re_mon_day.matcher(present);
36
37         if (m_mon_day.matches()) {
38             month = Integer.parseInt(m_mon_day.group(1))-1;
39             day = Integer.parseInt(m_mon_day.group(2));
40         }
41         else {
42             Pattern re_day = Pattern.compile("^\\s*(\\d{1,2})\\s*$");
43             Matcher m_day = re_day.matcher(present);
44             if (m_day.matches()) {
45                 day = Integer.parseInt(m_day.group(1));
46             }
47         }
48
49         DatePickerDialog dpd =  new DatePickerDialog(Objects.requireNonNull(getActivity()), this, year, month, day);
50         // quicker date selection available in API 26
51         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
52             DatePicker dp = dpd.getDatePicker();
53             dp.setOnDateChangedListener(this);
54         }
55
56         return dpd;
57     }
58
59     @TargetApi(Build.VERSION_CODES.O)
60     public void onDateSet(DatePicker view, int year, int month, int day) {
61         TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
62
63         final Calendar c = GregorianCalendar.getInstance();
64         if ( c.get(GregorianCalendar.YEAR) == year && c.get(GregorianCalendar.MONTH) == month) {
65             date.setText(String.format(Locale.US, "%d", day));
66         }
67         else {
68             date.setText(String.format(Locale.US, "%d/%d", month+1, day));
69         }
70
71         TextView description = Objects.requireNonNull(getActivity())
72                 .findViewById(R.id.new_transaction_description);
73         description.requestFocus();
74     }
75
76     @Override
77     public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
78         TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
79
80         final Calendar c = GregorianCalendar.getInstance();
81         if ( c.get(GregorianCalendar.YEAR) == year && c.get(GregorianCalendar.MONTH) == monthOfYear) {
82             date.setText(String.format(Locale.US, "%d", dayOfMonth));
83         }
84         else {
85             date.setText(String.format(Locale.US, "%d/%d", monthOfYear+1, dayOfMonth));
86         }
87
88         TextView description = Objects.requireNonNull(getActivity())
89                 .findViewById(R.id.new_transaction_description);
90         description.requestFocus();
91
92         this.dismiss();
93     }
94 }