X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2FDatePickerFragment.java;h=3115d477a2ba97450ec7254344cecdde46fed398;hb=5e1dd65e12ff7a6c7dc3fece1238459dd0fc3efe;hp=f801bdaadc8961daa4083695eac3cfa0bc7e8251;hpb=6b740c280c79b0170321f533747cdbfc3e179a29;p=mobile-ledger.git
diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
index f801bdaa..3115d477 100644
--- a/app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
+++ b/app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
@@ -1,113 +1,140 @@
/*
- * Copyright © 2018 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * Copyright © 2019 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your opinion), any later version.
*
- * Mobile-Ledger is distributed in the hope that it will be useful,
+ * MoLe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License terms for details.
*
* You should have received a copy of the GNU General Public License
- * along with Mobile-Ledger. If not, see .
+ * along with MoLe. If not, see .
*/
package net.ktnx.mobileledger.ui;
-import android.annotation.TargetApi;
-import android.app.DatePickerDialog;
import android.app.Dialog;
-import android.os.Build;
import android.os.Bundle;
-import android.support.annotation.NonNull;
-import android.support.v7.app.AppCompatDialogFragment;
-import android.widget.DatePicker;
-import android.widget.TextView;
+import android.widget.CalendarView;
+
+import androidx.annotation.NonNull;
+import androidx.appcompat.app.AppCompatDialogFragment;
import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.model.MobileLedgerProfile;
import java.util.Calendar;
import java.util.GregorianCalendar;
-import java.util.Locale;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DatePickerFragment extends AppCompatDialogFragment
-implements DatePickerDialog.OnDateSetListener, DatePicker.OnDateChangedListener
-{
- @NonNull
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- final Calendar c = GregorianCalendar.getInstance();
- int year = c.get(GregorianCalendar.YEAR);
- int month = c.get(GregorianCalendar.MONTH);
- int day = c.get(GregorianCalendar.DAY_OF_MONTH);
- TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
-
- CharSequence present = date.getText();
-
- Pattern re_mon_day = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
- Matcher m_mon_day = re_mon_day.matcher(present);
-
- if (m_mon_day.matches()) {
- month = Integer.parseInt(m_mon_day.group(1))-1;
- day = Integer.parseInt(m_mon_day.group(2));
- }
- else {
- Pattern re_day = Pattern.compile("^\\s*(\\d{1,2})\\s*$");
- Matcher m_day = re_day.matcher(present);
- if (m_day.matches()) {
- day = Integer.parseInt(m_day.group(1));
- }
- }
-
- DatePickerDialog dpd = new DatePickerDialog(Objects.requireNonNull(getActivity()), this, year, month, day);
- // quicker date selection available in API 26
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- DatePicker dp = dpd.getDatePicker();
- dp.setOnDateChangedListener(this);
- }
-
- return dpd;
+ implements CalendarView.OnDateChangeListener {
+ static final Pattern reYMD = Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
+ static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
+ static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$");
+ private Calendar presentDate = GregorianCalendar.getInstance();
+ private DatePickedListener onDatePickedListener;
+ private MobileLedgerProfile.FutureDates futureDates = MobileLedgerProfile.FutureDates.None;
+ public MobileLedgerProfile.FutureDates getFutureDates() {
+ return futureDates;
}
-
- @TargetApi(Build.VERSION_CODES.O)
- public void onDateSet(DatePicker view, int year, int month, int day) {
- TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
-
- final Calendar c = GregorianCalendar.getInstance();
- if ( c.get(GregorianCalendar.YEAR) == year && c.get(GregorianCalendar.MONTH) == month) {
- date.setText(String.format(Locale.US, "%d", day));
+ public void setFutureDates(MobileLedgerProfile.FutureDates futureDates) {
+ this.futureDates = futureDates;
+ }
+ public void setCurrentDateFromText(CharSequence present) {
+ final Calendar now = GregorianCalendar.getInstance();
+ int year = now.get(GregorianCalendar.YEAR);
+ int month = now.get(GregorianCalendar.MONTH);
+ int day = now.get(GregorianCalendar.DAY_OF_MONTH);
+
+ Matcher m = reYMD.matcher(present);
+ if (m.matches()) {
+ year = Integer.parseInt(m.group(1));
+ month = Integer.parseInt(m.group(2)) - 1; // month is 0-based
+ day = Integer.parseInt(m.group(3));
}
else {
- date.setText(String.format(Locale.US, "%d/%d", month+1, day));
+ m = reMD.matcher(present);
+ if (m.matches()) {
+ month = Integer.parseInt(m.group(1)) - 1;
+ day = Integer.parseInt(m.group(2));
+ }
+ else {
+ m = reD.matcher(present);
+ if (m.matches()) {
+ day = Integer.parseInt(m.group(1));
+ }
+ }
}
- TextView description = Objects.requireNonNull(getActivity())
- .findViewById(R.id.new_transaction_description);
- description.requestFocus();
+ presentDate.set(year, month, day);
}
-
+ @NonNull
@Override
- public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
- TextView date = Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
-
- final Calendar c = GregorianCalendar.getInstance();
- if ( c.get(GregorianCalendar.YEAR) == year && c.get(GregorianCalendar.MONTH) == monthOfYear) {
- date.setText(String.format(Locale.US, "%d", dayOfMonth));
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ Dialog dpd = new Dialog(Objects.requireNonNull(getActivity()));
+ dpd.setContentView(R.layout.date_picker_view);
+ dpd.setTitle(null);
+ CalendarView cv = dpd.findViewById(R.id.calendarView);
+ cv.setDate(presentDate.getTime()
+ .getTime());
+
+ if (futureDates == MobileLedgerProfile.FutureDates.All) {
+ cv.setMaxDate(Long.MAX_VALUE);
}
else {
- date.setText(String.format(Locale.US, "%d/%d", monthOfYear+1, dayOfMonth));
+ final Calendar dateLimit = GregorianCalendar.getInstance();
+ switch (futureDates) {
+ case None:
+ // already there
+ break;
+ case OneWeek:
+ dateLimit.add(Calendar.DAY_OF_MONTH, 7);
+ break;
+ case TwoWeeks:
+ dateLimit.add(Calendar.DAY_OF_MONTH, 14);
+ break;
+ case OneMonth:
+ dateLimit.add(Calendar.MONTH, 1);
+ break;
+ case TwoMonths:
+ dateLimit.add(Calendar.MONTH, 2);
+ break;
+ case ThreeMonths:
+ dateLimit.add(Calendar.MONTH, 3);
+ break;
+ case SixMonths:
+ dateLimit.add(Calendar.MONTH, 6);
+ break;
+ case OneYear:
+ dateLimit.add(Calendar.YEAR, 1);
+ break;
+ }
+ cv.setMaxDate(dateLimit.getTime()
+ .getTime());
}
- TextView description = Objects.requireNonNull(getActivity())
- .findViewById(R.id.new_transaction_description);
- description.requestFocus();
+ cv.setOnDateChangeListener(this);
+ return dpd;
+ }
+ @Override
+ public void onSelectedDayChange(@NonNull CalendarView view, int year, int month,
+ int dayOfMonth) {
this.dismiss();
+ if (onDatePickedListener != null)
+ onDatePickedListener.onDatePicked(year, month, dayOfMonth);
+ }
+ public void setOnDatePickedListener(DatePickedListener listener) {
+ onDatePickedListener = listener;
+ }
+ public interface DatePickedListener {
+ void onDatePicked(int year, int month, int day);
}
}