]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
4c2721d46651119789a96686669d3d4d296d35d1
[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 import net.ktnx.mobileledger.model.MobileLedgerProfile;
30
31 import java.util.Calendar;
32 import java.util.GregorianCalendar;
33 import java.util.Objects;
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 DatePickedListener onDatePickedListener;
43     private MobileLedgerProfile.FutureDates futureDates = MobileLedgerProfile.FutureDates.None;
44     public MobileLedgerProfile.FutureDates getFutureDates() {
45         return futureDates;
46     }
47     public void setFutureDates(MobileLedgerProfile.FutureDates futureDates) {
48         this.futureDates = futureDates;
49     }
50     @NonNull
51     @Override
52     public Dialog onCreateDialog(Bundle savedInstanceState) {
53         final Calendar c = GregorianCalendar.getInstance();
54         int year = c.get(GregorianCalendar.YEAR);
55         int month = c.get(GregorianCalendar.MONTH);
56         int day = c.get(GregorianCalendar.DAY_OF_MONTH);
57         TextView date = Objects.requireNonNull(getActivity())
58                                .findViewById(R.id.new_transaction_date);
59
60         CharSequence present = date.getText();
61
62         Matcher m = reYMD.matcher(present);
63         if (m.matches()) {
64             year = Integer.parseInt(m.group(1));
65             month = Integer.parseInt(m.group(2)) - 1;   // month is 0-based
66             day = Integer.parseInt(m.group(3));
67         }
68         else {
69             m = reMD.matcher(present);
70             if (m.matches()) {
71                 month = Integer.parseInt(m.group(1)) - 1;
72                 day = Integer.parseInt(m.group(2));
73             }
74             else {
75                 m = reD.matcher(present);
76                 if (m.matches()) {
77                     day = Integer.parseInt(m.group(1));
78                 }
79             }
80         }
81
82         c.set(year, month, day);
83
84         Dialog dpd = new Dialog(Objects.requireNonNull(getActivity()));
85         dpd.setContentView(R.layout.date_picker_view);
86         dpd.setTitle(null);
87         CalendarView cv = dpd.findViewById(R.id.calendarView);
88         cv.setDate(c.getTime()
89                     .getTime());
90
91         if (futureDates == MobileLedgerProfile.FutureDates.All) {
92             cv.setMaxDate(Long.MAX_VALUE);
93         }
94         else {
95             final Calendar dateLimit = GregorianCalendar.getInstance();
96             switch (futureDates) {
97                 case None:
98                     // already there
99                     break;
100                 case OneWeek:
101                     dateLimit.add(Calendar.DAY_OF_MONTH, 7);
102                     break;
103                 case TwoWeeks:
104                     dateLimit.add(Calendar.DAY_OF_MONTH, 14);
105                     break;
106                 case OneMonth:
107                     dateLimit.add(Calendar.MONTH, 1);
108                     break;
109                 case TwoMonths:
110                     dateLimit.add(Calendar.MONTH, 2);
111                     break;
112                 case ThreeMonths:
113                     dateLimit.add(Calendar.MONTH, 3);
114                     break;
115                 case SixMonths:
116                     dateLimit.add(Calendar.MONTH, 6);
117                     break;
118                 case OneYear:
119                     dateLimit.add(Calendar.YEAR, 1);
120                     break;
121             }
122             cv.setMaxDate(dateLimit.getTime()
123                                    .getTime());
124         }
125
126         cv.setOnDateChangeListener(this);
127
128         return dpd;
129     }
130     @Override
131     public void onSelectedDayChange(@NonNull CalendarView view, int year, int month,
132                                     int dayOfMonth) {
133         this.dismiss();
134         if (onDatePickedListener != null)
135             onDatePickedListener.onDatePicked(year, month, dayOfMonth);
136     }
137     public void setOnDatePickedListener(DatePickedListener listener) {
138         onDatePickedListener = listener;
139     }
140     public interface DatePickedListener {
141         void onDatePicked(int year, int month, int day);
142     }
143 }