]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/DatePickerFragment.java
add license boilerplates for authored content
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / 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;
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 java.util.Calendar;
31 import java.util.GregorianCalendar;
32 import java.util.Locale;
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 DatePickerDialog.OnDateSetListener, DatePicker.OnDateChangedListener
39 {
40     @NonNull
41     @Override
42     public Dialog onCreateDialog(Bundle savedInstanceState) {
43         final Calendar c = GregorianCalendar.getInstance();
44         int year = c.get(GregorianCalendar.YEAR);
45         int month = c.get(GregorianCalendar.MONTH);
46         int day = c.get(GregorianCalendar.DAY_OF_MONTH);
47         TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
48
49         CharSequence present = date.getText();
50
51         Pattern re_mon_day = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
52         Matcher m_mon_day = re_mon_day.matcher(present);
53
54         if (m_mon_day.matches()) {
55             month = Integer.parseInt(m_mon_day.group(1))-1;
56             day = Integer.parseInt(m_mon_day.group(2));
57         }
58         else {
59             Pattern re_day = Pattern.compile("^\\s*(\\d{1,2})\\s*$");
60             Matcher m_day = re_day.matcher(present);
61             if (m_day.matches()) {
62                 day = Integer.parseInt(m_day.group(1));
63             }
64         }
65
66         DatePickerDialog dpd =  new DatePickerDialog(Objects.requireNonNull(getActivity()), this, year, month, day);
67         // quicker date selection available in API 26
68         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
69             DatePicker dp = dpd.getDatePicker();
70             dp.setOnDateChangedListener(this);
71         }
72
73         return dpd;
74     }
75
76     @TargetApi(Build.VERSION_CODES.O)
77     public void onDateSet(DatePicker view, int year, int month, int day) {
78         TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
79
80         final Calendar c = GregorianCalendar.getInstance();
81         if ( c.get(GregorianCalendar.YEAR) == year && c.get(GregorianCalendar.MONTH) == month) {
82             date.setText(String.format(Locale.US, "%d", day));
83         }
84         else {
85             date.setText(String.format(Locale.US, "%d/%d", month+1, day));
86         }
87
88         TextView description = Objects.requireNonNull(getActivity())
89                 .findViewById(R.id.new_transaction_description);
90         description.requestFocus();
91     }
92
93     @Override
94     public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
95         TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
96
97         final Calendar c = GregorianCalendar.getInstance();
98         if ( c.get(GregorianCalendar.YEAR) == year && c.get(GregorianCalendar.MONTH) == monthOfYear) {
99             date.setText(String.format(Locale.US, "%d", dayOfMonth));
100         }
101         else {
102             date.setText(String.format(Locale.US, "%d/%d", monthOfYear+1, dayOfMonth));
103         }
104
105         TextView description = Objects.requireNonNull(getActivity())
106                 .findViewById(R.id.new_transaction_description);
107         description.requestFocus();
108
109         this.dismiss();
110     }
111 }