Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting of field values #62

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/lib/pass_backside/pass_backside_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class _PassBacksidePageState extends State<PassBacksidePage> {
ListTile(
title: Text(entry.label ?? ''),
subtitle: Linkify(
text: entry.value.toString(),
text: entry.formatted() ?? '',
onOpen: (link) => launchUrl(Uri.parse(link.url)),
),
),
Expand Down
72 changes: 70 additions & 2 deletions passkit/lib/src/passkit/field_dict.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:intl/intl.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:passkit/src/passkit/semantics.dart';

Expand All @@ -11,7 +12,7 @@ class FieldDict {
this.dataDetectorTypes,
required this.key,
this.label,
this.textAlignment,
this.textAlignment = PkTextAlignment.natural,
required this.value,
this.currencyCode,
this.dateStyle,
Expand Down Expand Up @@ -81,7 +82,7 @@ class FieldDict {
/// appropriately based on its script direction.
///
/// This key is not allowed for primary fields or back fields.
final PkTextAlignment? textAlignment;
final PkTextAlignment textAlignment;

/// Required. Value of the field, for example, 42.
/// This can contain a localizable string, ISO 8601 date as a string,
Expand Down Expand Up @@ -132,6 +133,56 @@ class FieldDict {
/// Possible Values: 0, 1
/// This field is only valid for event pass types.
final int? row;

String? formatted({String? locale}) {
if (value == null) {
return null;
}
if (value is num) {
if (numberStyle != null) {
return numberStyle!.asFormat(currencyCode, locale).format(value as num);
}
if (currencyCode != null) {
return NumberFormat.simpleCurrency(locale: locale, name: currencyCode)
.format(value as num);
}
return value.toString();
}

var dateTime = DateTime.tryParse(value as String);
if (dateTime != null) {
DateFormat format = DateFormat(null, locale);

if (dateStyle != null) {
format = switch (dateStyle!) {
DateStyle.none => DateFormat(null, locale),
DateStyle.full => DateFormat.yMMMMEEEEd(locale),
DateStyle.long => DateFormat.yMMMMd(locale),
// We can't easily do spellOut, so fall back to decimal
DateStyle.medium => DateFormat.yMMMd(locale),
DateStyle.short => DateFormat.yMd(locale),
};
}
if (timeStyle != null) {
switch (timeStyle!) {
case DateStyle.none:
break;
case DateStyle.short:
format.add_jm();
case DateStyle.medium:
case DateStyle.long:
case DateStyle.full:
format.add_jms();
}
}
if (!(ignoresTimeZone ?? false)) {
dateTime = dateTime.toLocal();
}
return format.format(dateTime);
}
// TODO(any): Could be localizable
return value?.toString();
}
}

enum PkTextAlignment {
Expand Down Expand Up @@ -202,3 +253,20 @@ enum NumberStyle {
@JsonValue('PKNumberStyleSpellOut')
spellOut,
}

extension on NumberStyle {
NumberFormat asFormat(String? currency, String? locale) {
return switch (this) {
NumberStyle.decimal => () {
if (currency != null) {
return NumberFormat.simpleCurrency(locale: locale, name: currency);
}
return NumberFormat.decimalPattern(locale);
}(),
NumberStyle.percent => NumberFormat.percentPattern(locale),
NumberStyle.scientific => NumberFormat.scientificPattern(locale),
// We can't easily do spellOut, so fall back to decimal
NumberStyle.spellOut => NumberFormat.decimalPattern(locale),
};
}
}
7 changes: 4 additions & 3 deletions passkit/lib/src/passkit/field_dict.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions passkit/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies:
csslib: ^1.0.0
http: ^1.2.0
http_parser: ^4.0.0
intl: ^0.19.0
json_annotation: ^4.9.0

dev_dependencies:
Expand Down
Loading