refactor: use binary barcode data by default

Signed-off-by: The one with the braid <info@braid.business>
This commit is contained in:
The one with the braid 2024-01-27 12:12:17 +01:00
parent 3170c456e2
commit 0a32804cc6
2 changed files with 30 additions and 25 deletions

View file

@ -23,11 +23,11 @@ class PassBarcode {
/// - iso-8859-1: [Latin1Codec]
/// - utf-8: [Utf8Codec]
static Map<String, Encoding> supportedCodecs = {
'iso-8859-1': Latin1Codec(),
'iso-8859': Latin1Codec(),
'iso8859': Latin1Codec(),
'utf-8': Utf8Codec(),
'utf8': Utf8Codec(),
'iso-8859-1': latin1,
'iso-8859': latin1,
'iso8859': latin1,
'utf-8': utf8,
'utf8': utf8,
};
static const _allowedFormats = {
@ -46,10 +46,8 @@ class PassBarcode {
/// representations.
final BarcodeType format;
/// Message or payload to be displayed as a barcode.
///
/// Do not use directly, use the encoded [barcodeData] instead.
final String message;
/// Correctly encoded byte list to be displayed in the [barcode].
final Uint8List barcodeData;
/// Text encoding that is used to convert the message from the string
/// representation to a data representation to render the barcode.
@ -68,22 +66,29 @@ class PassBarcode {
/// of the barcode data in case the barcode doesnt scan.
final String? altText;
const PassBarcode({
PassBarcode({
required this.format,
required this.message,
required this.messageEncoding,
@Deprecated('Use [barcodeData] instead') String? message,
this.messageEncoding = utf8,
Uint8List? barcodeData,
required this.altText,
});
}) : barcodeData =
Uint8List.fromList(barcodeData ?? messageEncoding.encode(message!));
factory PassBarcode.fromJson(Map<String, Object?> json) => PassBarcode(
format: _allowedFormats[json['format']]!,
message: json['message'] as String,
messageEncoding:
supportedCodecs[(json['messageEncoding'] as String).toLowerCase()]!,
altText: json['altText'] as String?,
);
factory PassBarcode.fromJson(Map<String, Object?> json) {
final messageEncoding =
supportedCodecs[(json['messageEncoding'] as String).toLowerCase()]!;
return PassBarcode(
format: _allowedFormats[json['format']]!,
barcodeData:
Uint8List.fromList(messageEncoding.encode(json['message'] as String)),
messageEncoding: messageEncoding,
altText: json['altText'] as String?,
);
}
/// Correctly encoded byte list to be displayed in the [barcode].
Uint8List get barcodeData =>
Uint8List.fromList(messageEncoding.encode(message));
/// Message or payload to be displayed as a barcode.
///
/// Do not use directly, use the encoded [barcodeData] instead.
String get message => messageEncoding.decode(barcodeData);
}

View file

@ -15,9 +15,9 @@ dependencies:
barcode: ^2.2.4
crypto: ^3.0.3
http: ^1.0.0
intl: ^0.18.1
intl: ^0.19.0
dev_dependencies:
import_sorter: ^4.6.0
lints: ">= 2.0.0 < 4.0.0"
lints: ">=2.0.0 <4.0.0"
test: ^1.21.0