]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
0c4a1c1b72c506167ce5a93cf978b218abe97b6e
[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 android.widget.CalendarView;
23 import android.widget.TextView;
24
25 import androidx.annotation.NonNull;
26 import androidx.appcompat.app.AppCompatDialogFragment;
27
28 import net.ktnx.mobileledger.R;
29
30 import java.util.Calendar;
31 import java.util.GregorianCalendar;
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     private DatePickedListener onDatePickedListener;
42     @NonNull
43     @Override
44     public Dialog onCreateDialog(Bundle savedInstanceState) {
45         final Calendar c = GregorianCalendar.getInstance();
46         int year = c.get(GregorianCalendar.YEAR);
47         int month = c.get(GregorianCalendar.MONTH);
48         int day = c.get(GregorianCalendar.DAY_OF_MONTH);
49         long todayStamp = c.getTimeInMillis();
50         TextView date =
51                 Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
52
53         CharSequence present = date.getText();
54
55         Matcher m = reYMD.matcher(present);
56         if (m.matches()) {
57             year = Integer.parseInt(m.group(1));
58             month = Integer.parseInt(m.group(2)) - 1;   // month is 0-based
59             day = Integer.parseInt(m.group(3));
60         }
61         else {
62             m = reMD.matcher(present);
63             if (m.matches()) {
64                 month = Integer.parseInt(m.group(1)) - 1;
65                 day = Integer.parseInt(m.group(2));
66             }
67             else {
68                 m = reD.matcher(present);
69                 if (m.matches()) {
70                     day = Integer.parseInt(m.group(1));
71                 }
72             }
73         }
74
75         c.set(year, month, day);
76
77         Dialog dpd = new Dialog(Objects.requireNonNull(getActivity()));
78         dpd.setContentView(R.layout.date_picker_view);
79         dpd.setTitle(null);
80         CalendarView cv = dpd.findViewById(R.id.calendarView);
81         cv.setDate(c.getTime().getTime());
82         cv.setMaxDate(todayStamp);
83
84         cv.setOnDateChangeListener(this);
85
86         return dpd;
87     }
88     @Override
89     public void onSelectedDayChange(@NonNull CalendarView view, int year, int month,
90                                     int dayOfMonth) {
91         this.dismiss();
92         if (onDatePickedListener != null)
93             onDatePickedListener.onDatePicked(year, month, dayOfMonth);
94     }
95     public void setOnDatePickedListener(DatePickedListener listener) {
96         onDatePickedListener = listener;
97     }
98     public interface DatePickedListener {
99         void onDatePicked(int year, int month, int day);
100     }
101 }