]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
migrate to AndroidX
[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 MoLe.
4  * MoLe 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  * MoLe 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 MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui;
19
20 import android.app.Dialog;
21 import android.os.Bundle;
22 import androidx.annotation.NonNull;
23 import androidx.appcompat.app.AppCompatDialogFragment;
24 import android.widget.CalendarView;
25 import android.widget.TextView;
26
27 import net.ktnx.mobileledger.R;
28
29 import java.util.Calendar;
30 import java.util.GregorianCalendar;
31 import java.util.Locale;
32 import java.util.Objects;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
36 public class DatePickerFragment extends AppCompatDialogFragment
37         implements CalendarView.OnDateChangeListener {
38     static final Pattern reYMD = Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
39     static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
40     static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$");
41     @NonNull
42     @Override
43     public Dialog onCreateDialog(Bundle savedInstanceState) {
44         final Calendar c = GregorianCalendar.getInstance();
45         int year = c.get(GregorianCalendar.YEAR);
46         int month = c.get(GregorianCalendar.MONTH);
47         int day = c.get(GregorianCalendar.DAY_OF_MONTH);
48         long todayStamp = c.getTimeInMillis();
49         TextView date =
50                 Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
51
52         CharSequence present = date.getText();
53
54         Matcher m = reYMD.matcher(present);
55         if (m.matches()) {
56             year = Integer.parseInt(m.group(1));
57             month = Integer.parseInt(m.group(2)) - 1;   // month is 0-based
58             day = Integer.parseInt(m.group(3));
59         }
60         else {
61             m = reMD.matcher(present);
62             if (m.matches()) {
63                 month = Integer.parseInt(m.group(1)) - 1;
64                 day = Integer.parseInt(m.group(2));
65             }
66             else {
67                 m = reD.matcher(present);
68                 if (m.matches()) {
69                     day = Integer.parseInt(m.group(1));
70                 }
71             }
72         }
73
74         c.set(year, month, day);
75
76         Dialog dpd = new Dialog(Objects.requireNonNull(getActivity()));
77         dpd.setContentView(R.layout.date_picker_view);
78         dpd.setTitle(null);
79         CalendarView cv = dpd.findViewById(R.id.calendarView);
80         cv.setDate(c.getTime().getTime());
81         cv.setMaxDate(todayStamp);
82
83         cv.setOnDateChangeListener(this);
84
85         return dpd;
86     }
87     private void updateDateInput(int year, int month, int day) {
88         TextView date =
89                 Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
90
91         final Calendar c = GregorianCalendar.getInstance();
92         if (c.get(GregorianCalendar.YEAR) == year) {
93             if (c.get(GregorianCalendar.MONTH) == month)
94                 date.setText(String.format(Locale.US, "%d", day));
95             else date.setText(String.format(Locale.US, "%d/%d", month + 1, day));
96         }
97         else date.setText(String.format(Locale.US, "%d/%d/%d", year, month + 1, day));
98
99         TextView description = Objects.requireNonNull(getActivity())
100                 .findViewById(R.id.new_transaction_description);
101         description.requestFocus();
102     }
103     @Override
104     public void onSelectedDayChange(@NonNull CalendarView view, int year, int month,
105                                     int dayOfMonth) {
106         updateDateInput(year, month, dayOfMonth);
107         this.dismiss();
108     }
109 }