Quick date picker: localization, fix datetime overflows

This commit is contained in:
exelix11 2025-10-04 23:08:16 +00:00 committed by Brandon Wees
parent 2c29697b55
commit f538b5563a
3 changed files with 44 additions and 15 deletions

View file

@ -1174,6 +1174,7 @@
"import_path": "Import path", "import_path": "Import path",
"in_albums": "In {count, plural, one {# album} other {# albums}}", "in_albums": "In {count, plural, one {# album} other {# albums}}",
"in_archive": "In archive", "in_archive": "In archive",
"in_year": "In {year}",
"include_archived": "Include archived", "include_archived": "Include archived",
"include_shared_albums": "Include shared albums", "include_shared_albums": "Include shared albums",
"include_shared_partner_assets": "Include shared partner assets", "include_shared_partner_assets": "Include shared partner assets",
@ -1210,6 +1211,8 @@
"language_setting_description": "Select your preferred language", "language_setting_description": "Select your preferred language",
"large_files": "Large Files", "large_files": "Large Files",
"last": "Last", "last": "Last",
"last_month": "Last month",
"last_nth_month": "Last {months} months",
"last_seen": "Last seen", "last_seen": "Last seen",
"latest_version": "Latest Version", "latest_version": "Latest Version",
"latitude": "Latitude", "latitude": "Latitude",
@ -1516,6 +1519,8 @@
"photos_count": "{count, plural, one {{count, number} Photo} other {{count, number} Photos}}", "photos_count": "{count, plural, one {{count, number} Photo} other {{count, number} Photos}}",
"photos_from_previous_years": "Photos from previous years", "photos_from_previous_years": "Photos from previous years",
"pick_a_location": "Pick a location", "pick_a_location": "Pick a location",
"pick_date_range": "Select a date range",
"pick_exact_date": "Enter a custom interval",
"pin_code_changed_successfully": "Successfully changed PIN code", "pin_code_changed_successfully": "Successfully changed PIN code",
"pin_code_reset_successfully": "Successfully reset PIN code", "pin_code_reset_successfully": "Successfully reset PIN code",
"pin_code_setup_successfully": "Successfully setup a PIN code", "pin_code_setup_successfully": "Successfully setup a PIN code",

View file

@ -285,15 +285,23 @@ class DriftSearchPage extends HookConsumerWidget {
final firstDate = DateTime(1900); final firstDate = DateTime(1900);
final lastDate = DateTime.now(); final lastDate = DateTime.now();
var dateRange = DateTimeRange(
start: filter.value.date.takenAfter ?? lastDate,
end: filter.value.date.takenBefore ?? lastDate,
);
// datePicked() may increase the date, this will make the date picker fail an assertion
// Fixup the end date to be at most now.
if (dateRange.end.isAfter(lastDate)) {
dateRange = DateTimeRange(start: dateRange.start, end: lastDate);
}
final date = await showDateRangePicker( final date = await showDateRangePicker(
context: context, context: context,
firstDate: firstDate, firstDate: firstDate,
lastDate: lastDate, lastDate: lastDate,
currentDate: DateTime.now(), currentDate: DateTime.now(),
initialDateRange: DateTimeRange( initialDateRange: dateRange,
start: filter.value.date.takenAfter ?? lastDate,
end: filter.value.date.takenBefore ?? lastDate,
),
helpText: 'search_filter_date_title'.t(context: context), helpText: 'search_filter_date_title'.t(context: context),
cancelText: 'cancel'.t(context: context), cancelText: 'cancel'.t(context: context),
confirmText: 'select'.t(context: context), confirmText: 'select'.t(context: context),
@ -313,7 +321,7 @@ class DriftSearchPage extends HookConsumerWidget {
showFilterBottomSheet( showFilterBottomSheet(
context: context, context: context,
child: FilterBottomSheetScaffold( child: FilterBottomSheetScaffold(
title: "Pick date range", title: "pick_date_range".tr(),
expanded: true, expanded: true,
onClear: () => datePicked(null), onClear: () => datePicked(null),
child: QuickDatePicker( child: QuickDatePicker(

View file

@ -1,3 +1,4 @@
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:flutter_hooks/flutter_hooks.dart';
@ -7,13 +8,28 @@ class QuickDatePicker extends HookWidget {
final Function() onRequestPicker; final Function() onRequestPicker;
final Function(DateTimeRange<DateTime> range) onSelect; final Function(DateTimeRange<DateTime> range) onSelect;
ListTile _monthListTile(String text, int monthsFromNow) { void _selectRange(DateTimeRange range) {
// Ensure we don't go beyond today, eg when picking "in $current_year"
final now = DateTime.now();
if (range.end.isAfter(now)) {
range = DateTimeRange(start: range.start, end: now);
}
onSelect(range);
}
ListTile _monthListTile(int monthsFromNow) {
String label = monthsFromNow == 1
? 'last_month'.tr()
: 'last_nth_month'.tr(namedArgs: {"months": monthsFromNow.toString()});
return ListTile( return ListTile(
title: Text(text), title: Text(label),
onTap: () { onTap: () {
final now = DateTime.now(); final now = DateTime.now();
final from = DateTime(now.year, now.month - monthsFromNow, now.day); // We use the first of the target month here to avoid issues with different month lengths
onSelect(DateTimeRange(start: from, end: now)); // the negative overflow of months is handled by DateTime correctly
final from = DateTime(now.year, now.month - monthsFromNow, 1);
_selectRange(DateTimeRange(start: from, end: now));
}, },
); );
} }
@ -24,27 +40,27 @@ class QuickDatePicker extends HookWidget {
itemBuilder: (context, index) { itemBuilder: (context, index) {
if (index == 0) { if (index == 0) {
return ListTile( return ListTile(
title: const Text("Exact date"), title: Text('pick_exact_date'.tr()),
onTap: () { onTap: () {
onRequestPicker(); onRequestPicker();
}, },
); );
} else if (index == 1) { } else if (index == 1) {
return _monthListTile("Last month", 1); return _monthListTile(1);
} else if (index == 2) { } else if (index == 2) {
return _monthListTile("Last 3 months", 3); return _monthListTile(3);
} else if (index == 3) { } else if (index == 3) {
return _monthListTile("Last 9 months", 9); return _monthListTile(9);
} else { } else {
final now = DateTime.now(); final now = DateTime.now();
final years = index - 4; final years = index - 4;
final year = now.year - years; final year = now.year - years;
return ListTile( return ListTile(
title: Text("In $year"), title: Text("in_year".tr(namedArgs: {"year": year.toString()})),
onTap: () { onTap: () {
final from = DateTime(year, 1, 1); final from = DateTime(year, 1, 1);
final to = DateTime(year, 12, 31, 23, 59, 59); final to = DateTime(year, 12, 31, 23, 59, 59);
onSelect(DateTimeRange(start: from, end: to)); _selectRange(DateTimeRange(start: from, end: to));
}, },
); );
} }