]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
f801bdaadc8961daa4083695eac3cfa0bc7e8251
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / DatePickerFragment.java
1 /*
2  * Copyright © 2018 Damyan Ivanov.
3  * This file is part of Mobile-Ledger.
4  * Mobile-Ledger 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  * Mobile-Ledger 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 Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui;
19
20 import android.annotation.TargetApi;
21 import android.app.DatePickerDialog;
22 import android.app.Dialog;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.support.annotation.NonNull;
26 import android.support.v7.app.AppCompatDialogFragment;
27 import android.widget.DatePicker;
28 import android.widget.TextView;
29
30 import net.ktnx.mobileledger.R;
31
32 import java.util.Calendar;
33 import java.util.GregorianCalendar;
34 import java.util.Locale;
35 import java.util.Objects;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39 public class DatePickerFragment extends AppCompatDialogFragment
40 implements DatePickerDialog.OnDateSetListener, DatePicker.OnDateChangedListener
41 {
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         TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
50
51         CharSequence present = date.getText();
52
53         Pattern re_mon_day = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
54         Matcher m_mon_day = re_mon_day.matcher(present);
55
56         if (m_mon_day.matches()) {
57             month = Integer.parseInt(m_mon_day.group(1))-1;
58             day = Integer.parseInt(m_mon_day.group(2));
59         }
60         else {
61             Pattern re_day = Pattern.compile("^\\s*(\\d{1,2})\\s*$");
62             Matcher m_day = re_day.matcher(present);
63             if (m_day.matches()) {
64                 day = Integer.parseInt(m_day.group(1));
65             }
66         }
67
68         DatePickerDialog dpd =  new DatePickerDialog(Objects.requireNonNull(getActivity()), this, year, month, day);
69         // quicker date selection available in API 26
70         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
71             DatePicker dp = dpd.getDatePicker();
72             dp.setOnDateChangedListener(this);
73         }
74
75         return dpd;
76     }
77
78     @TargetApi(Build.VERSION_CODES.O)
79     public void onDateSet(DatePicker view, int year, int month, int day) {
80         TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
81
82         final Calendar c = GregorianCalendar.getInstance();
83         if ( c.get(GregorianCalendar.YEAR) == year && c.get(GregorianCalendar.MONTH) == month) {
84             date.setText(String.format(Locale.US, "%d", day));
85         }
86         else {
87             date.setText(String.format(Locale.US, "%d/%d", month+1, day));
88         }
89
90         TextView description = Objects.requireNonNull(getActivity())
91                 .findViewById(R.id.new_transaction_description);
92         description.requestFocus();
93     }
94
95     @Override
96     public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
97         TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
98
99         final Calendar c = GregorianCalendar.getInstance();
100         if ( c.get(GregorianCalendar.YEAR) == year && c.get(GregorianCalendar.MONTH) == monthOfYear) {
101             date.setText(String.format(Locale.US, "%d", dayOfMonth));
102         }
103         else {
104             date.setText(String.format(Locale.US, "%d/%d", monthOfYear+1, dayOfMonth));
105         }
106
107         TextView description = Objects.requireNonNull(getActivity())
108                 .findViewById(R.id.new_transaction_description);
109         description.requestFocus();
110
111         this.dismiss();
112     }
113 }