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.
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.appcompat.app.AppCompatDialogFragment;
27 import net.ktnx.mobileledger.R;
28 import net.ktnx.mobileledger.model.MobileLedgerProfile;
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;
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 Calendar presentDate = GregorianCalendar.getInstance();
42 private DatePickedListener onDatePickedListener;
43 private MobileLedgerProfile.FutureDates futureDates = MobileLedgerProfile.FutureDates.None;
44 public MobileLedgerProfile.FutureDates getFutureDates() {
47 public void setFutureDates(MobileLedgerProfile.FutureDates futureDates) {
48 this.futureDates = futureDates;
50 public void setCurrentDateFromText(CharSequence present) {
51 final Calendar now = GregorianCalendar.getInstance();
52 int year = now.get(GregorianCalendar.YEAR);
53 int month = now.get(GregorianCalendar.MONTH);
54 int day = now.get(GregorianCalendar.DAY_OF_MONTH);
56 Matcher m = reYMD.matcher(present);
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));
63 m = reMD.matcher(present);
65 month = Integer.parseInt(m.group(1)) - 1;
66 day = Integer.parseInt(m.group(2));
69 m = reD.matcher(present);
71 day = Integer.parseInt(m.group(1));
76 presentDate.set(year, month, day);
80 public Dialog onCreateDialog(Bundle savedInstanceState) {
81 Dialog dpd = new Dialog(Objects.requireNonNull(getActivity()));
82 dpd.setContentView(R.layout.date_picker_view);
84 CalendarView cv = dpd.findViewById(R.id.calendarView);
85 cv.setDate(presentDate.getTime()
88 if (futureDates == MobileLedgerProfile.FutureDates.All) {
89 cv.setMaxDate(Long.MAX_VALUE);
92 final Calendar dateLimit = GregorianCalendar.getInstance();
93 switch (futureDates) {
98 dateLimit.add(Calendar.DAY_OF_MONTH, 7);
101 dateLimit.add(Calendar.DAY_OF_MONTH, 14);
104 dateLimit.add(Calendar.MONTH, 1);
107 dateLimit.add(Calendar.MONTH, 2);
110 dateLimit.add(Calendar.MONTH, 3);
113 dateLimit.add(Calendar.MONTH, 6);
116 dateLimit.add(Calendar.YEAR, 1);
119 cv.setMaxDate(dateLimit.getTime()
123 cv.setOnDateChangeListener(this);
128 public void onSelectedDayChange(@NonNull CalendarView view, int year, int month,
131 if (onDatePickedListener != null)
132 onDatePickedListener.onDatePicked(year, month, dayOfMonth);
134 public void setOnDatePickedListener(DatePickedListener listener) {
135 onDatePickedListener = listener;
137 public interface DatePickedListener {
138 void onDatePicked(int year, int month, int day);