fix(mobile): age calculation & formatting (#16833)

This commit is contained in:
Yaros 2025-03-17 19:51:17 +01:00 committed by GitHub
parent 3ce8608662
commit 93907a89d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 87 additions and 69 deletions

View File

@ -264,7 +264,9 @@
"exif_bottom_sheet_location_add": "Add a location", "exif_bottom_sheet_location_add": "Add a location",
"exif_bottom_sheet_people": "PEOPLE", "exif_bottom_sheet_people": "PEOPLE",
"exif_bottom_sheet_person_add_person": "Add name", "exif_bottom_sheet_person_add_person": "Add name",
"exif_bottom_sheet_person_age": "Age {}", "exif_bottom_sheet_person_age_years": "Age {}",
"exif_bottom_sheet_person_age_year_months": "Age 1 year, {} months",
"exif_bottom_sheet_person_age_months": "Age {} months",
"experimental_settings_new_asset_list_subtitle": "Work in progress", "experimental_settings_new_asset_list_subtitle": "Work in progress",
"experimental_settings_new_asset_list_title": "Enable experimental photo grid", "experimental_settings_new_asset_list_title": "Enable experimental photo grid",
"experimental_settings_subtitle": "Use at your own risk!", "experimental_settings_subtitle": "Use at your own risk!",

View File

@ -1,5 +1,3 @@
import 'dart:math' as math;
import 'package:auto_route/auto_route.dart'; import 'package:auto_route/auto_route.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -26,7 +24,6 @@ class PeopleInfo extends ConsumerWidget {
.watch(assetPeopleNotifierProvider(asset)) .watch(assetPeopleNotifierProvider(asset))
.value .value
?.where((p) => !p.isHidden); ?.where((p) => !p.isHidden);
final double imageSize = math.min(context.width / 3, 150);
showPersonNameEditModel( showPersonNameEditModel(
String personId, String personId,
@ -48,12 +45,9 @@ class PeopleInfo extends ConsumerWidget {
(p) => SearchCuratedContent( (p) => SearchCuratedContent(
id: p.id, id: p.id,
label: p.name, label: p.name,
subtitle: p.birthDate != null subtitle: p.birthDate != null &&
? "exif_bottom_sheet_person_age".tr( p.birthDate!.isBefore(asset.fileCreatedAt)
args: [ ? _formatAge(p.birthDate!, asset.fileCreatedAt)
_calculateAge(p.birthDate!).toString(),
],
)
: null, : null,
), ),
) )
@ -83,27 +77,24 @@ class PeopleInfo extends ConsumerWidget {
).tr(), ).tr(),
), ),
), ),
SizedBox( Padding(
height: imageSize, padding: const EdgeInsets.only(top: 16.0),
child: Padding( child: CuratedPeopleRow(
padding: const EdgeInsets.only(top: 16.0), padding: padding,
child: CuratedPeopleRow( content: curatedPeople,
padding: padding, onTap: (content, index) {
content: curatedPeople, context
onTap: (content, index) { .pushRoute(
context PersonResultRoute(
.pushRoute( personId: content.id,
PersonResultRoute( personName: content.label,
personId: content.id, ),
personName: content.label, )
), .then((_) => peopleProvider.refresh());
) },
.then((_) => peopleProvider.refresh()); onNameTap: (person, index) => {
}, showPersonNameEditModel(person.id, person.label),
onNameTap: (person, index) => { },
showPersonNameEditModel(person.id, person.label),
},
),
), ),
), ),
], ],
@ -112,16 +103,36 @@ class PeopleInfo extends ConsumerWidget {
); );
} }
int _calculateAge(DateTime birthDate) { String _formatAge(DateTime birthDate, DateTime referenceDate) {
DateTime today = DateTime.now(); int ageInYears = _calculateAge(birthDate, referenceDate);
int age = today.year - birthDate.year; int ageInMonths = _calculateAgeInMonths(birthDate, referenceDate);
// Check if the birthday has occurred this year if (ageInMonths <= 11) {
if (today.month < birthDate.month || return "exif_bottom_sheet_person_age_months"
(today.month == birthDate.month && today.day < birthDate.day)) { .tr(args: [ageInMonths.toString()]);
} else if (ageInMonths > 12 && ageInMonths <= 23) {
return "exif_bottom_sheet_person_age_year_months"
.tr(args: [(ageInMonths - 12).toString()]);
} else {
return "exif_bottom_sheet_person_age_years"
.tr(args: [ageInYears.toString()]);
}
}
int _calculateAge(DateTime birthDate, DateTime referenceDate) {
int age = referenceDate.year - birthDate.year;
if (referenceDate.month < birthDate.month ||
(referenceDate.month == birthDate.month &&
referenceDate.day < birthDate.day)) {
age--; age--;
} }
return age; return age;
} }
int _calculateAgeInMonths(DateTime birthDate, DateTime referenceDate) {
return (referenceDate.year - birthDate.year) * 12 +
referenceDate.month -
birthDate.month -
(referenceDate.day < birthDate.day ? 1 : 0);
}
} }

View File

@ -26,43 +26,48 @@ class CuratedPeopleRow extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SizedBox( return SizedBox(
height: imageSize + 50, width: double.infinity,
child: ListView.separated( child: SingleChildScrollView(
padding: padding, padding: padding,
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
separatorBuilder: (context, index) => const SizedBox(width: 16), child: Row(
itemBuilder: (context, index) { mainAxisAlignment: MainAxisAlignment.start,
final person = content[index]; crossAxisAlignment: CrossAxisAlignment.start,
final headers = ApiService.getRequestHeaders(); children: List.generate(content.length, (index) {
return Column( final person = content[index];
crossAxisAlignment: CrossAxisAlignment.center, final headers = ApiService.getRequestHeaders();
children: [ return Padding(
GestureDetector( padding: const EdgeInsets.only(right: 16.0),
onTap: () => onTap?.call(person, index), child: Column(
child: SizedBox( crossAxisAlignment: CrossAxisAlignment.center,
height: imageSize, children: [
child: Material( GestureDetector(
shape: const CircleBorder(side: BorderSide.none), onTap: () => onTap?.call(person, index),
elevation: 3, child: SizedBox(
child: CircleAvatar( height: imageSize,
maxRadius: imageSize / 2, child: Material(
backgroundImage: NetworkImage( shape: const CircleBorder(side: BorderSide.none),
getFaceThumbnailUrl(person.id), elevation: 3,
headers: headers, child: CircleAvatar(
maxRadius: imageSize / 2,
backgroundImage: NetworkImage(
getFaceThumbnailUrl(person.id),
headers: headers,
),
),
), ),
), ),
), ),
), const SizedBox(height: 8),
SizedBox(
width: imageSize,
child: _buildPersonLabel(context, person, index),
),
],
), ),
const SizedBox(height: 8), );
SizedBox( }),
width: imageSize, ),
child: _buildPersonLabel(context, person, index),
),
],
);
},
itemCount: content.length,
), ),
); );
} }