]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
remove references to style members from the parser classes
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / DatePickerFragment.java
1 /*
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.
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
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.appcompat.app.AppCompatDialogFragment;
27
28 import net.ktnx.mobileledger.R;
29 import net.ktnx.mobileledger.model.MobileLedgerProfile;
30 import net.ktnx.mobileledger.utils.SimpleDate;
31
32 import java.util.Calendar;
33 import java.util.GregorianCalendar;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36
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) {
47         if (minDate == null)
48             this.minDate = 0;
49         else
50             this.minDate = minDate.toDate().getTime();
51
52         if (maxDate == null)
53             this.maxDate = Long.MAX_VALUE;
54         else
55             this.maxDate = maxDate.toDate().getTime();
56     }
57     public void setFutureDates(MobileLedgerProfile.FutureDates futureDates) {
58         if (futureDates == MobileLedgerProfile.FutureDates.All) {
59             maxDate = Long.MAX_VALUE;
60         }
61         else {
62             final Calendar dateLimit = GregorianCalendar.getInstance();
63             switch (futureDates) {
64                 case None:
65                     // already there
66                     break;
67                 case OneWeek:
68                     dateLimit.add(Calendar.DAY_OF_MONTH, 7);
69                     break;
70                 case TwoWeeks:
71                     dateLimit.add(Calendar.DAY_OF_MONTH, 14);
72                     break;
73                 case OneMonth:
74                     dateLimit.add(Calendar.MONTH, 1);
75                     break;
76                 case TwoMonths:
77                     dateLimit.add(Calendar.MONTH, 2);
78                     break;
79                 case ThreeMonths:
80                     dateLimit.add(Calendar.MONTH, 3);
81                     break;
82                 case SixMonths:
83                     dateLimit.add(Calendar.MONTH, 6);
84                     break;
85                 case OneYear:
86                     dateLimit.add(Calendar.YEAR, 1);
87                     break;
88             }
89             maxDate = dateLimit.getTime()
90                                .getTime();
91         }
92     }
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);
98
99         Matcher m = reYMD.matcher(present);
100         if (m.matches()) {
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));
104         }
105         else {
106             m = reMD.matcher(present);
107             if (m.matches()) {
108                 month = Integer.parseInt(m.group(1)) - 1;
109                 day = Integer.parseInt(m.group(2));
110             }
111             else {
112                 m = reD.matcher(present);
113                 if (m.matches()) {
114                     day = Integer.parseInt(m.group(1));
115                 }
116             }
117         }
118
119         presentDate.set(year, month, day);
120     }
121     @NonNull
122     @Override
123     public Dialog onCreateDialog(Bundle savedInstanceState) {
124         Dialog dpd = new Dialog(requireActivity());
125         dpd.setContentView(R.layout.date_picker_view);
126         dpd.setTitle(null);
127         CalendarView cv = dpd.findViewById(R.id.calendarView);
128         cv.setDate(presentDate.getTime()
129                               .getTime());
130
131         cv.setMinDate(minDate);
132         cv.setMaxDate(maxDate);
133
134         cv.setOnDateChangeListener(this);
135
136         return dpd;
137     }
138     @Override
139     public void onSelectedDayChange(@NonNull CalendarView view, int year, int month,
140                                     int dayOfMonth) {
141         this.dismiss();
142         if (onDatePickedListener != null)
143             onDatePickedListener.onDatePicked(year, month, dayOfMonth);
144     }
145     public void setOnDatePickedListener(DatePickedListener listener) {
146         onDatePickedListener = listener;
147     }
148     public interface DatePickedListener {
149         void onDatePicked(int year, int month, int day);
150     }
151 }