chore: add high level classes

Signed-off-by: The one with the braid <the-one@with-the-braid.cf>
This commit is contained in:
The one with the braid 2023-08-27 17:47:11 +02:00
parent 165226bee1
commit 78f88305ec
13 changed files with 678 additions and 316 deletions

View file

@ -1,15 +1,106 @@
import 'package:pkpass/src/models/barcode.dart';
import 'package:pkpass/src/utils/mabe_decode.dart';
import 'beacon.dart';
import 'location.dart';
import 'pass_structure_dictionary.dart';
import 'pass_web_service.dart';
/// Information that is required for all passes.
class PassMetadata {
final String? description;
/// Brief description of the pass, used by accessibility technologies.
///
/// Dont try to include all of the data on the pass in its description,
/// just include enough detail to distinguish passes of the same type.
final String description;
/// Version of the file format. The value must be 1.
final int formatVersion;
final String? organizationName;
final String? passTypeIdentifier;
final String? serialNumber;
final String? teamIdentifier;
final bool? suppressStripShine;
final EventTicket? eventTicket;
final Barcode? barcode;
final String? foregroundColor;
final List<Location>? locations;
/// Display name of the organization that originated and signed the pass.
final String organizationName;
/// Pass type identifier, as issued by Apple. The value must correspond with
/// your signing certificate.
final String passTypeIdentifier;
/// Serial number that uniquely identifies the pass. No two passes with the
/// same pass type identifier may have the same serial number.
final String serialNumber;
/// Team identifier of the organization that originated and signed the pass,
/// as issued by Apple.
final String teamIdentifier;
/// A URL to be passed to the associated app when launching it.
final String? appLaunchURL;
/// Date and time when the pass expires.
final DateTime? expirationDate;
/// Indicates that the pass is voidfor example, a one time use coupon that
/// has been redeemed. The default value is false.
final bool voided;
/// Beacons marking locations where the pass is relevant.
final List<Beacon> beacons;
/// Locations where the pass is relevant. For example, the location of your store.
final List<Location> locations;
/// Maximum distance in meters from a relevant latitude and longitude that
/// the pass is relevant. This number is compared to the passs default
/// distance and the smaller value is used.
final int? maxDistance;
/// Recommended for event tickets and boarding passes; otherwise optional.
/// Date and time when the pass becomes relevant. For example, the start
/// time of a movie.
final DateTime? relevantDate;
/// Information specific to a boarding pass.
final PassStructureDictionary? boardingPass;
/// Information specific to a coupon.
final PassStructureDictionary? coupon;
/// Information specific to an event ticket.
final PassStructureDictionary? eventTicket;
/// Information specific to a generic pass.
final PassStructureDictionary? generic;
/// Information specific to a store card.
final PassStructureDictionary? storeCard;
/// Information specific to the passs barcode.
/// The system uses the first valid barcode dictionary in the array.
/// Additional dictionaries can be added as fallbacks.
final List<PassBarcode> barcodes;
/// Background color of the pass.
final int? backgroundColor;
/// Foreground color of the pass.
final int? foregroundColor;
/// Optional for event tickets and boarding passes; otherwise not allowed.
/// Identifier used to group related passes. If a grouping identifier is
/// specified, passes with the same style, pass type identifier, and grouping
/// identifier are displayed as a group. Otherwise, passes are grouped
/// automatically.
///
/// Use this to group passes that are tightly related, such as the boarding
/// passes for different connections of the same trip.
final String? groupingIdentifier;
/// Color of the label text.
final int? labelColor;
/// Text displayed next to the logo on the pass.
final String? logoText;
/// Information used to update passes using the web service.
final PassWebService? webService;
const PassMetadata({
required this.description,
@ -18,308 +109,86 @@ class PassMetadata {
required this.passTypeIdentifier,
required this.serialNumber,
required this.teamIdentifier,
required this.suppressStripShine,
required this.eventTicket,
required this.barcode,
required this.foregroundColor,
required this.locations,
this.appLaunchURL,
this.expirationDate,
this.voided = false,
this.beacons = const [],
this.locations = const [],
this.maxDistance,
this.relevantDate,
this.boardingPass,
this.coupon,
this.eventTicket,
this.generic,
this.storeCard,
this.barcodes = const [],
this.backgroundColor,
this.foregroundColor,
this.groupingIdentifier,
this.labelColor,
this.logoText,
this.webService,
});
factory PassMetadata.fromJson(Map<String, Object?> json) => PassMetadata(
description: json['description'] as String?,
description: json['description'] as String,
formatVersion: json['formatVersion'] as int,
organizationName: json['organizationName'] as String?,
passTypeIdentifier: json['passTypeIdentifier'] as String?,
serialNumber: json['serialNumber'] as String?,
teamIdentifier: json['teamIdentifier'] as String?,
suppressStripShine: json['suppressStripShine'] as bool?,
eventTicket: json['ventTicket'] == null
organizationName: json['organizationName'] as String,
passTypeIdentifier: json['passTypeIdentifier'] as String,
serialNumber: json['serialNumber'] as String,
teamIdentifier: json['teamIdentifier'] as String,
boardingPass: json['boardingPass'] == null
? null
: EventTicket.fromJson(
json['eventTicket'] as Map<String, Object?>? ?? {},
: PassStructureDictionary.fromJson(
(json['boardingPass'] as Map).cast<String, Object?>(),
),
barcode: json['barcode'] == null
coupon: json['coupon'] == null
? null
: Barcode.fromJson(json['barcode'] as Map<String, Object?>? ?? {}),
foregroundColor: json['foregroundColor'] as String?,
locations: (json['locations'] as List)
: PassStructureDictionary.fromJson(
(json['coupon'] as Map).cast<String, Object?>(),
),
eventTicket: json['eventTicket'] == null
? null
: PassStructureDictionary.fromJson(
(json['coupon'] as Map).cast<String, Object?>(),
),
generic: json['generic'] == null
? null
: PassStructureDictionary.fromJson(
(json['generic'] as Map).cast<String, Object?>(),
),
storeCard: json['storeCard'] == null
? null
: PassStructureDictionary.fromJson(
(json['storeCard'] as Map).cast<String, Object?>(),
),
barcodes: (json['barcodes'] as List? ??
[if (json['barcode'] != null) json['barcode']])
.map((i) => PassBarcode.fromJson(i))
.toList(),
locations: (json['locations'] as List? ?? [])
.map((i) => Location.fromJson(i))
.toList(),
);
Map<String, Object?> toJson() => {
'description': description,
'formatVersion': formatVersion,
'organizationName': organizationName,
'passTypeIdentifier': passTypeIdentifier,
'serialNumber': serialNumber,
'teamIdentifier': teamIdentifier,
'suppressStripShine': suppressStripShine,
'eventTicket': eventTicket?.toJson(),
'barcode': barcode?.toJson(),
'foregroundColor': foregroundColor,
'locations': locations?.map((i) => i.toJson()).toList(),
};
}
class EventTicket {
final List<HeaderField> headerFields;
final List<PrimaryField> primaryFields;
final List<SecondaryField> secondaryFields;
final List<BackField> backFields;
final List<AuxiliaryField> auxiliaryFields;
const EventTicket({
required this.headerFields,
required this.primaryFields,
required this.secondaryFields,
required this.backFields,
required this.auxiliaryFields,
});
factory EventTicket.fromJson(Map<String, Object?> json) => EventTicket(
headerFields: (json['headerFields'] as List)
.map((i) => HeaderField.fromJson(i))
.toList(),
primaryFields: (json['primaryFields'] as List)
.map((i) => PrimaryField.fromJson(i))
.toList(),
secondaryFields: (json['secondaryFields'] as List)
.map((i) => SecondaryField.fromJson(i))
.toList(),
backFields: (json['backFields'] as List)
.map((i) => BackField.fromJson(i))
.toList(),
auxiliaryFields: (json['auxiliaryFields'] as List)
.map((i) => AuxiliaryField.fromJson(i))
appLaunchURL: json['appLaunchURL'] as String?,
expirationDate:
MaybeDecode.maybeDateTime(json['expirationDate'] as String?),
voided: json['voided'] as bool? ?? false,
beacons: (json['beacons'] as List? ?? [])
.map((i) => Beacon.fromJson(i))
.toList(),
maxDistance: json['maxDistance'] as int?,
relevantDate:
MaybeDecode.maybeDateTime(json['relevantDate'] as String?),
backgroundColor:
MaybeDecode.maybeColor(json['backgroundColor'] as String?),
foregroundColor:
MaybeDecode.maybeColor(json['foregroundColor'] as String?),
groupingIdentifier: json['locoText'] as String?,
labelColor: MaybeDecode.maybeColor(json['labelColor'] as String?),
logoText: json['locoText'] as String?,
webService: PassWebService.maybe(
authenticationToken: json['authenticationToken'] as String?,
webServiceURL: json['webServiceURL'] as String?,
),
);
Map<String, Object?> toJson() => {
'headerFields': headerFields.map((i) => i.toJson()).toList(),
'primaryFields': primaryFields.map((i) => i.toJson()).toList(),
'secondaryFields': secondaryFields.map((i) => i.toJson()).toList(),
'backFields': backFields.map((i) => i.toJson()).toList(),
'auxiliaryFields': auxiliaryFields.map((i) => i.toJson()).toList(),
};
}
class HeaderField {
final String? key;
final String? value;
final String? label;
final String? changeMessage;
final String? textAlignment;
const HeaderField({
required this.key,
required this.value,
required this.label,
required this.changeMessage,
required this.textAlignment,
});
factory HeaderField.fromJson(Map<String, Object?> json) => HeaderField(
key: json['key'] as String?,
value: json['value'] as String?,
label: json['label'] as String?,
changeMessage: json['changeMessage'] as String?,
textAlignment: json['textAlignment'] as String?,
);
Map<String, Object?> toJson() => {
'key': key,
'value': value,
'label': label,
'changeMessage': changeMessage,
'textAlignment': textAlignment,
};
}
class PrimaryField {
final String? key;
final String? value;
final String? label;
final String? changeMessage;
final String? textAlignment;
const PrimaryField({
required this.key,
required this.value,
required this.label,
required this.changeMessage,
required this.textAlignment,
});
factory PrimaryField.fromJson(Map<String, Object?> json) => PrimaryField(
key: json['key'] as String?,
value: json['value'] as String?,
label: json['label'] as String?,
changeMessage: json['changeMessage'] as String?,
textAlignment: json['textAlignment'] as String?,
);
Map<String, Object?> toJson() => {
'key': key,
'value': value,
'label': label,
'changeMessage': changeMessage,
'textAlignment': textAlignment,
};
}
class SecondaryField {
final String? key;
final String? value;
final String? label;
final String? changeMessage;
final String? textAlignment;
const SecondaryField({
required this.key,
required this.value,
required this.label,
required this.changeMessage,
required this.textAlignment,
});
factory SecondaryField.fromJson(Map<String, Object?> json) => SecondaryField(
key: json['key'] as String?,
value: json['value'] as String?,
label: json['label'] as String?,
changeMessage: json['changeMessage'] as String?,
textAlignment: json['textAlignment'] as String?,
);
Map<String, Object?> toJson() => {
'key': key,
'value': value,
'label': label,
'changeMessage': changeMessage,
'textAlignment': textAlignment,
};
}
class BackField {
final String? key;
final String? value;
final String? label;
final String? changeMessage;
final String? textAlignment;
const BackField({
required this.key,
required this.value,
required this.label,
required this.changeMessage,
required this.textAlignment,
});
factory BackField.fromJson(Map<String, Object?> json) => BackField(
key: json['key'] as String?,
value: json['value'] as String?,
label: json['label'] as String?,
changeMessage: json['changeMessage'] as String?,
textAlignment: json['textAlignment'] as String?,
);
Map<String, Object?> toJson() => {
'key': key,
'value': value,
'label': label,
'changeMessage': changeMessage,
'textAlignment': textAlignment,
};
}
class AuxiliaryField {
final String? key;
final String? value;
final String? label;
final String? changeMessage;
final String? textAlignment;
const AuxiliaryField({
required this.key,
required this.value,
required this.label,
required this.changeMessage,
required this.textAlignment,
});
factory AuxiliaryField.fromJson(Map<String, Object?> json) => AuxiliaryField(
key: json['key'] as String?,
value: json['value'] as String?,
label: json['label'] as String?,
changeMessage: json['changeMessage'] as String?,
textAlignment: json['textAlignment'] as String?,
);
Map<String, Object?> toJson() => {
'key': key,
'value': value,
'label': label,
'changeMessage': changeMessage,
'textAlignment': textAlignment,
};
}
class Barcode {
final String? format;
final String? message;
final String? messageEncoding;
final String? altText;
const Barcode({
required this.format,
required this.message,
required this.messageEncoding,
required this.altText,
});
factory Barcode.fromJson(Map<String, Object?> json) => Barcode(
format: json['format'] as String?,
message: json['message'] as String?,
messageEncoding: json['messageEncoding'] as String?,
altText: json['altText'] as String?,
);
Map<String, Object?> toJson() => {
'format': format,
'message': message,
'messageEncoding': messageEncoding,
'altText': altText,
};
}
class Location {
final double? latitude;
final double? longitude;
final double? altitude;
final double? distance;
final String? relevantText;
const Location({
required this.latitude,
required this.longitude,
required this.altitude,
required this.distance,
required this.relevantText,
});
factory Location.fromJson(Map<String, Object?> json) => Location(
latitude: json['latitude'] as double?,
longitude: json['longitude'] as double?,
altitude: json['altitude'] as double?,
distance: json['distance'] as double?,
relevantText: json['relevantText'] as String?,
);
Map<String, Object?> toJson() => {
'latitude': latitude,
'longitude': longitude,
'altitude': altitude,
'distance': distance,
'relevantText': relevantText,
};
}