]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
657946302970aefff01c4d4576b5ceff0f395004
[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.Activity;
21 import android.app.Dialog;
22 import android.os.Bundle;
23 import android.view.WindowManager;
24 import android.widget.CalendarView;
25 import android.widget.TextView;
26
27 import androidx.annotation.NonNull;
28 import androidx.appcompat.app.AppCompatDialogFragment;
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 CalendarView.OnDateChangeListener {
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         long todayStamp = c.getTimeInMillis();
52         TextView date =
53                 Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
54
55         CharSequence present = date.getText();
56
57         Matcher m = reYMD.matcher(present);
58         if (m.matches()) {
59             year = Integer.parseInt(m.group(1));
60             month = Integer.parseInt(m.group(2)) - 1;   // month is 0-based
61             day = Integer.parseInt(m.group(3));
62         }
63         else {
64             m = reMD.matcher(present);
65             if (m.matches()) {
66                 month = Integer.parseInt(m.group(1)) - 1;
67                 day = Integer.parseInt(m.group(2));
68             }
69             else {
70                 m = reD.matcher(present);
71                 if (m.matches()) {
72                     day = Integer.parseInt(m.group(1));
73                 }
74             }
75         }
76
77         c.set(year, month, day);
78
79         Dialog dpd = new Dialog(Objects.requireNonNull(getActivity()));
80         dpd.setContentView(R.layout.date_picker_view);
81         dpd.setTitle(null);
82         CalendarView cv = dpd.findViewById(R.id.calendarView);
83         cv.setDate(c.getTime().getTime());
84         cv.setMaxDate(todayStamp);
85
86         cv.setOnDateChangeListener(this);
87
88         return dpd;
89     }
90     private void updateDateInput(int year, int month, int day) {
91         TextView date =
92                 Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
93
94         final Calendar c = GregorianCalendar.getInstance();
95         if (c.get(GregorianCalendar.YEAR) == year) {
96             if (c.get(GregorianCalendar.MONTH) == month)
97                 date.setText(String.format(Locale.US, "%d", day));
98             else date.setText(String.format(Locale.US, "%d/%d", month + 1, day));
99         }
100         else date.setText(String.format(Locale.US, "%d/%d/%d", year, month + 1, day));
101
102         Activity activity = getActivity();
103         if (activity == null) return;
104
105         TextView description = activity.findViewById(R.id.new_transaction_description);
106         boolean tookFocus = description.requestFocus();
107         if (tookFocus) activity.getWindow()
108                 .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
109     }
110     @Override
111     public void onSelectedDayChange(@NonNull CalendarView view, int year, int month,
112                                     int dayOfMonth) {
113         updateDateInput(year, month, dayOfMonth);
114         this.dismiss();
115     }
116 }