X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;ds=sidebyside;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2FDatePickerFragment.java;h=abec8d6d8fb5915234c4981f74564b8be8766ed8;hb=8d69552b756ba44e254251edf08206f8866c639b;hp=d747f3449004b7ab7329906fde04b492c81f9e4f;hpb=ea17ca9e3b03ca0090be03fdc0abbbbfd954be89;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 d747f344..abec8d6d 100644
--- a/app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
+++ b/app/src/main/java/net/ktnx/mobileledger/ui/DatePickerFragment.java
@@ -1,30 +1,27 @@
/*
* Copyright © 2019 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * 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 androidx.annotation.NonNull;
+import androidx.appcompat.app.AppCompatDialogFragment;
+import android.widget.CalendarView;
import android.widget.TextView;
import net.ktnx.mobileledger.R;
@@ -37,7 +34,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DatePickerFragment extends AppCompatDialogFragment
- implements DatePickerDialog.OnDateSetListener, DatePicker.OnDateChangedListener {
+ 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*$");
@@ -48,6 +45,7 @@ public class DatePickerFragment extends AppCompatDialogFragment
int year = c.get(GregorianCalendar.YEAR);
int month = c.get(GregorianCalendar.MONTH);
int day = c.get(GregorianCalendar.DAY_OF_MONTH);
+ long todayStamp = c.getTimeInMillis();
TextView date =
Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
@@ -73,19 +71,20 @@ public class DatePickerFragment extends AppCompatDialogFragment
}
}
- 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);
- }
+ c.set(year, month, day);
+
+ 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(c.getTime().getTime());
+ cv.setMaxDate(todayStamp);
+
+ cv.setOnDateChangeListener(this);
return dpd;
}
-
- @TargetApi(Build.VERSION_CODES.O)
- public void onDateSet(DatePicker view, int year, int month, int day) {
+ private void updateDateInput(int year, int month, int day) {
TextView date =
Objects.requireNonNull(getActivity()).findViewById(R.id.new_transaction_date);
@@ -101,27 +100,10 @@ public class DatePickerFragment extends AppCompatDialogFragment
.findViewById(R.id.new_transaction_description);
description.requestFocus();
}
-
@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) {
- if (c.get(GregorianCalendar.MONTH) == monthOfYear) {
- date.setText(String.format(Locale.US, "%d", dayOfMonth));
- }
- else {
- date.setText(String.format(Locale.US, "%d/%d", monthOfYear + 1, dayOfMonth));
- }
- }
- else date.setText(String.format(Locale.US, "%d/%d/%d", year, monthOfYear + 1, dayOfMonth));
-
- TextView description = Objects.requireNonNull(getActivity())
- .findViewById(R.id.new_transaction_description);
- description.requestFocus();
-
+ public void onSelectedDayChange(@NonNull CalendarView view, int year, int month,
+ int dayOfMonth) {
+ updateDateInput(year, month, dayOfMonth);
this.dismiss();
}
}