]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
profile list: more material design
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / DatePickerFragment.java
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of Mobile-Ledger.
4  * Mobile-Ledger is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * Mobile-Ledger is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui;
19
20 import android.annotation.TargetApi;
21 import android.app.DatePickerDialog;
22 import android.app.Dialog;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.support.annotation.NonNull;
26 import android.support.v7.app.AppCompatDialogFragment;
27 import android.widget.DatePicker;
28 import android.widget.TextView;
29
30 import net.ktnx.mobileledger.R;
31
32 import java.util.Calendar;
33 import java.util.GregorianCalendar;
34 import java.util.Locale;
35 import java.util.Objects;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39 public class DatePickerFragment extends AppCompatDialogFragment
40         implements DatePickerDialog.OnDateSetListener, DatePicker.OnDateChangedListener {
41     static final Pattern reYMD = Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
42     static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
43     static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$");
44     @NonNull
45     @Override
46     public Dialog onCreateDialog(Bundle savedInstanceState) {
47         final Calendar c = GregorianCalendar.getInstance();
48         int year = c.get(GregorianCalendar.YEAR);
49         int month = c.get(GregorianCalendar.MONTH);
50         int day = c.get(GregorianCalendar.DAY_OF_MONTH);
51         TextView date =
52                 Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
53
54         CharSequence present = date.getText();
55
56         Matcher m = reYMD.matcher(present);
57         if (m.matches()) {
58             year = Integer.parseInt(m.group(1));
59             month = Integer.parseInt(m.group(2)) - 1;   // month is 0-based
60             day = Integer.parseInt(m.group(3));
61         }
62         else {
63             m = reMD.matcher(present);
64             if (m.matches()) {
65                 month = Integer.parseInt(m.group(1)) - 1;
66                 day = Integer.parseInt(m.group(2));
67             }
68             else {
69                 m = reD.matcher(present);
70                 if (m.matches()) {
71                     day = Integer.parseInt(m.group(1));
72                 }
73             }
74         }
75
76         DatePickerDialog dpd =
77                 new DatePickerDialog(Objects.requireNonNull(getActivity()), this, year, month, day);
78         // quicker date selection available in API 26
79         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
80             DatePicker dp = dpd.getDatePicker();
81             dp.setOnDateChangedListener(this);
82         }
83
84         return dpd;
85     }
86
87     @TargetApi(Build.VERSION_CODES.O)
88     public void onDateSet(DatePicker view, int year, int month, int day) {
89         TextView date =
90                 Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
91
92         final Calendar c = GregorianCalendar.getInstance();
93         if (c.get(GregorianCalendar.YEAR) == year) {
94             if (c.get(GregorianCalendar.MONTH) == month)
95                 date.setText(String.format(Locale.US, "%d", day));
96             else date.setText(String.format(Locale.US, "%d/%d", month + 1, day));
97         }
98         else date.setText(String.format(Locale.US, "%d/%d/%d", year, month + 1, day));
99
100         TextView description = Objects.requireNonNull(getActivity())
101                 .findViewById(R.id.new_transaction_description);
102         description.requestFocus();
103     }
104
105     @Override
106     public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
107         TextView date =
108                 Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
109
110         final Calendar c = GregorianCalendar.getInstance();
111         if (c.get(GregorianCalendar.YEAR) == year) {
112             if (c.get(GregorianCalendar.MONTH) == monthOfYear) {
113                 date.setText(String.format(Locale.US, "%d", dayOfMonth));
114             }
115             else {
116                 date.setText(String.format(Locale.US, "%d/%d", monthOfYear + 1, dayOfMonth));
117             }
118         }
119         else date.setText(String.format(Locale.US, "%d/%d/%d", year, monthOfYear + 1, dayOfMonth));
120
121         TextView description = Objects.requireNonNull(getActivity())
122                 .findViewById(R.id.new_transaction_description);
123         description.requestFocus();
124
125         this.dismiss();
126     }
127 }