Make 'to' filter of statistics inclusive

This commit is contained in:
ByteHamster 2022-03-05 10:53:13 +01:00
parent 32770fe817
commit f0aec13ea3
2 changed files with 24 additions and 17 deletions

View File

@ -41,7 +41,8 @@ public class PlaybackStatisticsListAdapter extends StatisticsListAdapter {
} }
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM yyyy", Locale.getDefault()); SimpleDateFormat dateFormat = new SimpleDateFormat("MMM yyyy", Locale.getDefault());
String dateFrom = dateFormat.format(new Date(timeFilterFrom)); String dateFrom = dateFormat.format(new Date(timeFilterFrom));
String dateTo = dateFormat.format(new Date(timeFilterTo)); // FilterTo is first day of next month => Subtract one day
String dateTo = dateFormat.format(new Date(timeFilterTo - 24L * 3600000L));
return context.getString(R.string.statistics_counting_range, dateFrom, dateTo); return context.getString(R.string.statistics_counting_range, dateFrom, dateTo);
} }

View File

@ -20,19 +20,21 @@ import java.util.Locale;
public class StatisticsFilterDialog { public class StatisticsFilterDialog {
private final Context context; private final Context context;
private final long oldestDate;
private final SharedPreferences prefs; private final SharedPreferences prefs;
private boolean includeMarkedAsPlayed; private boolean includeMarkedAsPlayed;
private long timeFilterFrom; private long timeFilterFrom;
private long timeFilterTo; private long timeFilterTo;
private final Pair<String[], Long[]> filterDatesFrom;
private final Pair<String[], Long[]> filterDatesTo;
public StatisticsFilterDialog(Context context, long oldestDate) { public StatisticsFilterDialog(Context context, long oldestDate) {
this.context = context; this.context = context;
this.oldestDate = oldestDate;
prefs = context.getSharedPreferences(StatisticsFragment.PREF_NAME, Context.MODE_PRIVATE); prefs = context.getSharedPreferences(StatisticsFragment.PREF_NAME, Context.MODE_PRIVATE);
includeMarkedAsPlayed = prefs.getBoolean(StatisticsFragment.PREF_INCLUDE_MARKED_PLAYED, false); includeMarkedAsPlayed = prefs.getBoolean(StatisticsFragment.PREF_INCLUDE_MARKED_PLAYED, false);
timeFilterFrom = prefs.getLong(StatisticsFragment.PREF_FILTER_FROM, 0); timeFilterFrom = prefs.getLong(StatisticsFragment.PREF_FILTER_FROM, 0);
timeFilterTo = prefs.getLong(StatisticsFragment.PREF_FILTER_TO, Long.MAX_VALUE); timeFilterTo = prefs.getLong(StatisticsFragment.PREF_FILTER_TO, Long.MAX_VALUE);
filterDatesFrom = makeMonthlyList(oldestDate, false);
filterDatesTo = makeMonthlyList(oldestDate, true);
} }
public void show() { public void show() {
@ -50,25 +52,24 @@ public class StatisticsFilterDialog {
}); });
dialogBinding.includeMarkedCheckbox.setChecked(includeMarkedAsPlayed); dialogBinding.includeMarkedCheckbox.setChecked(includeMarkedAsPlayed);
Pair<String[], Long[]> filterDates = makeMonthlyList(oldestDate);
ArrayAdapter<String> adapterFrom = new ArrayAdapter<>(context, ArrayAdapter<String> adapterFrom = new ArrayAdapter<>(context,
android.R.layout.simple_spinner_item, filterDates.first); android.R.layout.simple_spinner_item, filterDatesFrom.first);
adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dialogBinding.timeFromSpinner.setAdapter(adapterFrom); dialogBinding.timeFromSpinner.setAdapter(adapterFrom);
for (int i = 0; i < filterDates.second.length; i++) { for (int i = 0; i < filterDatesFrom.second.length; i++) {
if (filterDates.second[i] >= timeFilterFrom) { if (filterDatesFrom.second[i] >= timeFilterFrom) {
dialogBinding.timeFromSpinner.setSelection(i); dialogBinding.timeFromSpinner.setSelection(i);
break; break;
} }
} }
ArrayAdapter<String> adapterTo = new ArrayAdapter<>(context, ArrayAdapter<String> adapterTo = new ArrayAdapter<>(context,
android.R.layout.simple_spinner_item, filterDates.first); android.R.layout.simple_spinner_item, filterDatesTo.first);
adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dialogBinding.timeToSpinner.setAdapter(adapterTo); dialogBinding.timeToSpinner.setAdapter(adapterTo);
for (int i = 0; i < filterDates.second.length; i++) { for (int i = 0; i < filterDatesTo.second.length; i++) {
if (filterDates.second[i] >= timeFilterTo) { if (filterDatesTo.second[i] >= timeFilterTo) {
dialogBinding.timeToSpinner.setSelection(i); dialogBinding.timeToSpinner.setSelection(i);
break; break;
} }
@ -76,11 +77,11 @@ public class StatisticsFilterDialog {
dialogBinding.allTimeButton.setOnClickListener(v -> { dialogBinding.allTimeButton.setOnClickListener(v -> {
dialogBinding.timeFromSpinner.setSelection(0); dialogBinding.timeFromSpinner.setSelection(0);
dialogBinding.timeToSpinner.setSelection(filterDates.first.length - 1); dialogBinding.timeToSpinner.setSelection(filterDatesTo.first.length - 1);
}); });
dialogBinding.lastYearButton.setOnClickListener(v -> { dialogBinding.lastYearButton.setOnClickListener(v -> {
dialogBinding.timeFromSpinner.setSelection(Math.max(0, filterDates.first.length - 14)); dialogBinding.timeFromSpinner.setSelection(Math.max(0, filterDatesFrom.first.length - 14));
dialogBinding.timeToSpinner.setSelection(filterDates.first.length - 2); dialogBinding.timeToSpinner.setSelection(filterDatesTo.first.length - 2);
}); });
builder.setPositiveButton(android.R.string.ok, (dialog, which) -> { builder.setPositiveButton(android.R.string.ok, (dialog, which) -> {
@ -90,8 +91,8 @@ public class StatisticsFilterDialog {
timeFilterFrom = 0; timeFilterFrom = 0;
timeFilterTo = Long.MAX_VALUE; timeFilterTo = Long.MAX_VALUE;
} else { } else {
timeFilterFrom = filterDates.second[dialogBinding.timeFromSpinner.getSelectedItemPosition()]; timeFilterFrom = filterDatesFrom.second[dialogBinding.timeFromSpinner.getSelectedItemPosition()];
timeFilterTo = filterDates.second[dialogBinding.timeToSpinner.getSelectedItemPosition()]; timeFilterTo = filterDatesTo.second[dialogBinding.timeToSpinner.getSelectedItemPosition()];
} }
prefs.edit() prefs.edit()
.putBoolean(StatisticsFragment.PREF_INCLUDE_MARKED_PLAYED, includeMarkedAsPlayed) .putBoolean(StatisticsFragment.PREF_INCLUDE_MARKED_PLAYED, includeMarkedAsPlayed)
@ -103,7 +104,7 @@ public class StatisticsFilterDialog {
builder.show(); builder.show();
} }
private Pair<String[], Long[]> makeMonthlyList(long oldestDate) { private Pair<String[], Long[]> makeMonthlyList(long oldestDate, boolean inclusive) {
Calendar date = Calendar.getInstance(); Calendar date = Calendar.getInstance();
date.setTimeInMillis(oldestDate); date.setTimeInMillis(oldestDate);
date.set(Calendar.DAY_OF_MONTH, 1); date.set(Calendar.DAY_OF_MONTH, 1);
@ -112,13 +113,18 @@ public class StatisticsFilterDialog {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM yyyy", Locale.getDefault()); SimpleDateFormat dateFormat = new SimpleDateFormat("MMM yyyy", Locale.getDefault());
while (date.getTimeInMillis() < System.currentTimeMillis()) { while (date.getTimeInMillis() < System.currentTimeMillis()) {
names.add(dateFormat.format(new Date(date.getTimeInMillis()))); names.add(dateFormat.format(new Date(date.getTimeInMillis())));
timestamps.add(date.getTimeInMillis()); if (!inclusive) {
timestamps.add(date.getTimeInMillis());
}
if (date.get(Calendar.MONTH) == Calendar.DECEMBER) { if (date.get(Calendar.MONTH) == Calendar.DECEMBER) {
date.set(Calendar.MONTH, Calendar.JANUARY); date.set(Calendar.MONTH, Calendar.JANUARY);
date.set(Calendar.YEAR, date.get(Calendar.YEAR) + 1); date.set(Calendar.YEAR, date.get(Calendar.YEAR) + 1);
} else { } else {
date.set(Calendar.MONTH, date.get(Calendar.MONTH) + 1); date.set(Calendar.MONTH, date.get(Calendar.MONTH) + 1);
} }
if (inclusive) {
timestamps.add(date.getTimeInMillis());
}
} }
names.add(context.getString(R.string.statistics_today)); names.add(context.getString(R.string.statistics_today));
timestamps.add(Long.MAX_VALUE); timestamps.add(Long.MAX_VALUE);