2 * Copyright © 2020 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.
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.
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/>.
18 package net.ktnx.mobileledger.ui;
20 import android.app.Dialog;
21 import android.os.Bundle;
22 import android.widget.CalendarView;
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.appcompat.app.AppCompatDialogFragment;
28 import net.ktnx.mobileledger.R;
29 import net.ktnx.mobileledger.model.FutureDates;
30 import net.ktnx.mobileledger.utils.SimpleDate;
32 import java.util.Calendar;
33 import java.util.GregorianCalendar;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
37 public class DatePickerFragment extends AppCompatDialogFragment
38 implements CalendarView.OnDateChangeListener {
39 static final Pattern reYMD = Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
40 static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
41 static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$");
42 private final Calendar presentDate = GregorianCalendar.getInstance();
43 private DatePickedListener onDatePickedListener;
44 private long minDate = 0;
45 private long maxDate = Long.MAX_VALUE;
46 public void setDateRange(@Nullable SimpleDate minDate, @Nullable SimpleDate maxDate) {
50 this.minDate = minDate.toDate().getTime();
53 this.maxDate = Long.MAX_VALUE;
55 this.maxDate = maxDate.toDate().getTime();
57 public void setFutureDates(FutureDates futureDates) {
58 if (futureDates == FutureDates.All) {
59 maxDate = Long.MAX_VALUE;
62 final Calendar dateLimit = GregorianCalendar.getInstance();
63 switch (futureDates) {
68 dateLimit.add(Calendar.DAY_OF_MONTH, 7);
71 dateLimit.add(Calendar.DAY_OF_MONTH, 14);
74 dateLimit.add(Calendar.MONTH, 1);
77 dateLimit.add(Calendar.MONTH, 2);
80 dateLimit.add(Calendar.MONTH, 3);
83 dateLimit.add(Calendar.MONTH, 6);
86 dateLimit.add(Calendar.YEAR, 1);
89 maxDate = dateLimit.getTime()
93 public void setCurrentDateFromText(CharSequence present) {
94 final Calendar now = GregorianCalendar.getInstance();
95 int year = now.get(GregorianCalendar.YEAR);
96 int month = now.get(GregorianCalendar.MONTH);
97 int day = now.get(GregorianCalendar.DAY_OF_MONTH);
99 Matcher m = reYMD.matcher(present);
101 year = Integer.parseInt(m.group(1));
102 month = Integer.parseInt(m.group(2)) - 1; // month is 0-based
103 day = Integer.parseInt(m.group(3));
106 m = reMD.matcher(present);
108 month = Integer.parseInt(m.group(1)) - 1;
109 day = Integer.parseInt(m.group(2));
112 m = reD.matcher(present);
114 day = Integer.parseInt(m.group(1));
119 presentDate.set(year, month, day);
123 public Dialog onCreateDialog(Bundle savedInstanceState) {
124 Dialog dpd = new Dialog(requireActivity());
125 dpd.setContentView(R.layout.date_picker_view);
127 CalendarView cv = dpd.findViewById(R.id.calendarView);
128 cv.setDate(presentDate.getTime()
131 cv.setMinDate(minDate);
132 cv.setMaxDate(maxDate);
134 cv.setOnDateChangeListener(this);
139 public void onSelectedDayChange(@NonNull CalendarView view, int year, int month,
142 if (onDatePickedListener != null)
143 onDatePickedListener.onDatePicked(year, month, dayOfMonth);
145 public void setOnDatePickedListener(DatePickedListener listener) {
146 onDatePickedListener = listener;
148 public interface DatePickedListener {
149 void onDatePicked(int year, int month, int day);