fix: set default values through json key

This commit is contained in:
Matyáš Caras 2024-07-17 17:37:41 +02:00
parent d93eac4f04
commit 8e46b936a8
Signed by: hernik
GPG key ID: 2A3175F98820C5C6
16 changed files with 201 additions and 66 deletions

View file

@ -22,18 +22,36 @@ class WalletCategory {
factory WalletCategory.fromJson(Map<String, dynamic> json) => factory WalletCategory.fromJson(Map<String, dynamic> json) =>
_$WalletCategoryFromJson(json); _$WalletCategoryFromJson(json);
/// Default [WalletCategory] instance for json_serializable
factory WalletCategory.unknown() => WalletCategory(
name: "Unknown",
id: -1,
icon: Icons.question_mark,
color: Colors.green,
);
/// User-defined name /// User-defined name
@JsonKey(defaultValue: "Unknown")
String name; String name;
/// Unique identificator of the category /// Unique identificator of the category
@JsonKey(required: true, disallowNullValue: true)
final int id; final int id;
/// Selected Icon for the category /// Selected Icon for the category
@JsonKey(fromJson: _iconDataFromJson, toJson: _iconDataToJson) @JsonKey(
fromJson: _iconDataFromJson,
toJson: _iconDataToJson,
defaultValue: _defaultIcon,
)
IconData icon; IconData icon;
/// The color that will be displayed with entry /// The color that will be displayed with entry
@JsonKey(fromJson: _colorFromJson, toJson: _colorToJson) @JsonKey(
fromJson: _colorFromJson,
toJson: _colorToJson,
defaultValue: _defaultColor,
)
Color color; Color color;
/// Converts the data in this instance into a Map /// Converts the data in this instance into a Map
@ -62,3 +80,6 @@ enum EntryType {
/// Income /// Income
income income
} }
IconData _defaultIcon() => Icons.question_mark;
Color _defaultColor() => Colors.green;

View file

@ -6,13 +6,23 @@ part of 'category.dart';
// JsonSerializableGenerator // JsonSerializableGenerator
// ************************************************************************** // **************************************************************************
WalletCategory _$WalletCategoryFromJson(Map<String, dynamic> json) => WalletCategory _$WalletCategoryFromJson(Map<String, dynamic> json) {
WalletCategory( $checkKeys(
name: json['name'] as String, json,
id: (json['id'] as num).toInt(), requiredKeys: const ['id'],
icon: _iconDataFromJson(json['icon'] as Map<String, dynamic>), disallowNullValues: const ['id'],
color: _colorFromJson((json['color'] as num).toInt()),
); );
return WalletCategory(
name: json['name'] as String? ?? 'Unknown',
id: (json['id'] as num).toInt(),
icon: json['icon'] == null
? _defaultIcon()
: _iconDataFromJson(json['icon'] as Map<String, dynamic>),
color: json['color'] == null
? _defaultColor()
: _colorFromJson((json['color'] as num).toInt()),
);
}
Map<String, dynamic> _$WalletCategoryToJson(WalletCategory instance) => Map<String, dynamic> _$WalletCategoryToJson(WalletCategory instance) =>
<String, dynamic>{ <String, dynamic>{

View file

@ -25,14 +25,18 @@ class DebtEntry {
Map<String, dynamic> toJson() => _$DebtEntryToJson(this); Map<String, dynamic> toJson() => _$DebtEntryToJson(this);
/// Unique identifier /// Unique identifier
@JsonKey(required: true, disallowNullValue: true)
final int id; final int id;
/// The payed amount /// The payed amount
@JsonKey(defaultValue: 0)
int amount; int amount;
/// User-friendly identifier for the transaction /// User-friendly identifier for the transaction
@JsonKey(defaultValue: "Unknown")
String name; String name;
/// List of people who payed /// List of people who payed
@JsonKey(defaultValue: DebtPerson.unknownPerson)
List<DebtPerson> whoPayed; List<DebtPerson> whoPayed;
} }

View file

@ -6,14 +6,22 @@ part of 'debt_entry.dart';
// JsonSerializableGenerator // JsonSerializableGenerator
// ************************************************************************** // **************************************************************************
DebtEntry _$DebtEntryFromJson(Map<String, dynamic> json) => DebtEntry( DebtEntry _$DebtEntryFromJson(Map<String, dynamic> json) {
id: (json['id'] as num).toInt(), $checkKeys(
amount: (json['amount'] as num).toInt(), json,
name: json['name'] as String, requiredKeys: const ['id'],
whoPayed: (json['whoPayed'] as List<dynamic>) disallowNullValues: const ['id'],
.map((e) => DebtPerson.fromJson(e as Map<String, dynamic>))
.toList(),
); );
return DebtEntry(
id: (json['id'] as num).toInt(),
amount: (json['amount'] as num?)?.toInt() ?? 0,
name: json['name'] as String? ?? 'Unknown',
whoPayed: (json['whoPayed'] as List<dynamic>?)
?.map((e) => DebtPerson.fromJson(e as Map<String, dynamic>))
.toList() ??
DebtPerson.unknownPerson(),
);
}
Map<String, dynamic> _$DebtEntryToJson(DebtEntry instance) => <String, dynamic>{ Map<String, dynamic> _$DebtEntryToJson(DebtEntry instance) => <String, dynamic>{
'id': instance.id, 'id': instance.id,

View file

@ -8,6 +8,9 @@ class DebtPerson {
/// Represents a single person in a debt scenario /// Represents a single person in a debt scenario
DebtPerson({required this.id, required this.name}); DebtPerson({required this.id, required this.name});
/// Default [DebtPerson] instance for json_serializable
factory DebtPerson.unknownPerson() => DebtPerson(id: -1, name: "Unknown");
/// Generates a class instance from a Map /// Generates a class instance from a Map
factory DebtPerson.fromJson(Map<String, dynamic> json) => factory DebtPerson.fromJson(Map<String, dynamic> json) =>
_$DebtPersonFromJson(json); _$DebtPersonFromJson(json);
@ -16,8 +19,10 @@ class DebtPerson {
Map<String, dynamic> toJson() => _$DebtPersonToJson(this); Map<String, dynamic> toJson() => _$DebtPersonToJson(this);
/// Unique identifier /// Unique identifier
@JsonKey(required: true, disallowNullValue: true)
final int id; final int id;
/// Identifier that the user will see /// Identifier that the user will see
@JsonKey(defaultValue: "Unknown")
String name; String name;
} }

View file

@ -6,10 +6,17 @@ part of 'debt_person.dart';
// JsonSerializableGenerator // JsonSerializableGenerator
// ************************************************************************** // **************************************************************************
DebtPerson _$DebtPersonFromJson(Map<String, dynamic> json) => DebtPerson( DebtPerson _$DebtPersonFromJson(Map<String, dynamic> json) {
id: (json['id'] as num).toInt(), $checkKeys(
name: json['name'] as String, json,
requiredKeys: const ['id'],
disallowNullValues: const ['id'],
); );
return DebtPerson(
id: (json['id'] as num).toInt(),
name: json['name'] as String? ?? 'Unknown',
);
}
Map<String, dynamic> _$DebtPersonToJson(DebtPerson instance) => Map<String, dynamic> _$DebtPersonToJson(DebtPerson instance) =>
<String, dynamic>{ <String, dynamic>{

View file

@ -23,17 +23,24 @@ class DebtScenario {
Map<String, dynamic> toJson() => _$DebtScenarioToJson(this); Map<String, dynamic> toJson() => _$DebtScenarioToJson(this);
/// Unique identified /// Unique identified
@JsonKey(disallowNullValue: true)
final int id; final int id;
/// User-friendly identifier /// User-friendly identifier
@JsonKey(defaultValue: "Unknown")
String name; String name;
/// Whether this scenario should be shown under archived ones /// Whether this scenario should be shown under archived ones
@JsonKey(defaultValue: false)
bool isArchived; bool isArchived;
/// All entries /// All entries
@JsonKey(defaultValue: [])
List<DebtEntry> entries; List<DebtEntry> entries;
/// All people /// All people
@JsonKey(defaultValue: _defaultPeopleList)
List<DebtPerson> people; List<DebtPerson> people;
} }
List<DebtPerson> _defaultPeopleList() => [DebtPerson.unknownPerson()];

View file

@ -6,19 +6,25 @@ part of 'debt_scenario.dart';
// JsonSerializableGenerator // JsonSerializableGenerator
// ************************************************************************** // **************************************************************************
DebtScenario _$DebtScenarioFromJson(Map<String, dynamic> json) => DebtScenario( DebtScenario _$DebtScenarioFromJson(Map<String, dynamic> json) {
$checkKeys(
json,
disallowNullValues: const ['id'],
);
return DebtScenario(
id: (json['id'] as num).toInt(), id: (json['id'] as num).toInt(),
name: json['name'] as String, name: json['name'] as String? ?? 'Unknown',
isArchived: json['isArchived'] as bool, isArchived: json['isArchived'] as bool? ?? false,
entries: (json['entries'] as List<dynamic>?) entries: (json['entries'] as List<dynamic>?)
?.map((e) => DebtEntry.fromJson(e as Map<String, dynamic>)) ?.map((e) => DebtEntry.fromJson(e as Map<String, dynamic>))
.toList() ?? .toList() ??
const [], [],
people: (json['people'] as List<dynamic>?) people: (json['people'] as List<dynamic>?)
?.map((e) => DebtPerson.fromJson(e as Map<String, dynamic>)) ?.map((e) => DebtPerson.fromJson(e as Map<String, dynamic>))
.toList() ?? .toList() ??
const [], _defaultPeopleList(),
); );
}
Map<String, dynamic> _$DebtScenarioToJson(DebtScenario instance) => Map<String, dynamic> _$DebtScenarioToJson(DebtScenario instance) =>
<String, dynamic>{ <String, dynamic>{

View file

@ -15,13 +15,19 @@ class EntryData {
factory EntryData.fromJson(Map<String, dynamic> json) => factory EntryData.fromJson(Map<String, dynamic> json) =>
_$EntryDataFromJson(json); _$EntryDataFromJson(json);
/// [EntryData] instance used as a default value for json_serializable
factory EntryData.unknown() => EntryData(name: "Unknown", amount: 0);
/// Name of entry /// Name of entry
@JsonKey(defaultValue: "Unknown")
String name; String name;
/// Optional description, default is empty /// Optional description, default is empty
@JsonKey(defaultValue: "")
String description; String description;
/// Amount for entry /// Amount for entry
@JsonKey(defaultValue: 0)
double amount; double amount;
/// Converts the data in this instance into a Map /// Converts the data in this instance into a Map

View file

@ -7,9 +7,9 @@ part of 'entry_data.dart';
// ************************************************************************** // **************************************************************************
EntryData _$EntryDataFromJson(Map<String, dynamic> json) => EntryData( EntryData _$EntryDataFromJson(Map<String, dynamic> json) => EntryData(
name: json['name'] as String, name: json['name'] as String? ?? 'Unknown',
amount: (json['amount'] as num).toDouble(), amount: (json['amount'] as num?)?.toDouble() ?? 0,
description: json['description'] as String? ?? "", description: json['description'] as String? ?? '',
); );
Map<String, dynamic> _$EntryDataToJson(EntryData instance) => <String, dynamic>{ Map<String, dynamic> _$EntryDataToJson(EntryData instance) => <String, dynamic>{

View file

@ -33,12 +33,15 @@ class RecurringWalletEntry extends WalletSingleEntry {
Map<String, dynamic> toJson() => _$RecurringWalletEntryToJson(this); Map<String, dynamic> toJson() => _$RecurringWalletEntryToJson(this);
/// Last date the recurring entry was added into the single entry list /// Last date the recurring entry was added into the single entry list
@JsonKey(defaultValue: DateTime.now)
DateTime lastRunDate; DateTime lastRunDate;
/// After how many {recurType} should the entry recur /// After how many {recurType} should the entry recur
@JsonKey(defaultValue: 1)
int repeatAfter; int repeatAfter;
/// What type of recurrence should happen /// What type of recurrence should happen
@JsonKey(defaultValue: RecurType.month)
RecurType recurType; RecurType recurType;
} }

View file

@ -6,19 +6,33 @@ part of 'recurring_entry.dart';
// JsonSerializableGenerator // JsonSerializableGenerator
// ************************************************************************** // **************************************************************************
RecurringWalletEntry _$RecurringWalletEntryFromJson( RecurringWalletEntry _$RecurringWalletEntryFromJson(Map<String, dynamic> json) {
Map<String, dynamic> json) => $checkKeys(
RecurringWalletEntry( json,
data: EntryData.fromJson(json['data'] as Map<String, dynamic>), requiredKeys: const ['id'],
type: $enumDecode(_$EntryTypeEnumMap, json['type']), disallowNullValues: const ['id'],
date: DateTime.parse(json['date'] as String), );
category: return RecurringWalletEntry(
WalletCategory.fromJson(json['category'] as Map<String, dynamic>), data: json['data'] == null
? EntryData.unknown()
: EntryData.fromJson(json['data'] as Map<String, dynamic>),
type: $enumDecodeNullable(_$EntryTypeEnumMap, json['type']) ??
EntryType.expense,
date: json['date'] == null
? DateTime.now()
: DateTime.parse(json['date'] as String),
category: json['category'] == null
? WalletCategory.unknown()
: WalletCategory.fromJson(json['category'] as Map<String, dynamic>),
id: (json['id'] as num).toInt(), id: (json['id'] as num).toInt(),
lastRunDate: DateTime.parse(json['lastRunDate'] as String), lastRunDate: json['lastRunDate'] == null
recurType: $enumDecode(_$RecurTypeEnumMap, json['recurType']), ? DateTime.now()
: DateTime.parse(json['lastRunDate'] as String),
recurType: $enumDecodeNullable(_$RecurTypeEnumMap, json['recurType']) ??
RecurType.month,
repeatAfter: (json['repeatAfter'] as num?)?.toInt() ?? 1, repeatAfter: (json['repeatAfter'] as num?)?.toInt() ?? 1,
); );
}
Map<String, dynamic> _$RecurringWalletEntryToJson( Map<String, dynamic> _$RecurringWalletEntryToJson(
RecurringWalletEntry instance) => RecurringWalletEntry instance) =>

View file

@ -38,24 +38,32 @@ class Wallet {
factory Wallet.fromJson(Map<String, dynamic> json) => _$WalletFromJson(json); factory Wallet.fromJson(Map<String, dynamic> json) => _$WalletFromJson(json);
/// A list of all [RecurringWalletEntry]s /// A list of all [RecurringWalletEntry]s
@JsonKey(defaultValue: [])
final List<RecurringWalletEntry> recurringEntries; final List<RecurringWalletEntry> recurringEntries;
/// Name of the wallet /// Name of the wallet
@JsonKey(defaultValue: "Unknown")
final String name; final String name;
/// A list of available categories /// A list of available categories
@JsonKey(defaultValue: _defaultWalletCategory)
final List<WalletCategory> categories; final List<WalletCategory> categories;
/// List of saved entries /// List of saved entries
@JsonKey(defaultValue: [])
final List<WalletSingleEntry> entries; final List<WalletSingleEntry> entries;
/// The starting balance of the wallet /// The starting balance of the wallet
/// ///
/// Used to calculate current balance /// Used to calculate current balance
@JsonKey(defaultValue: 0)
double starterBalance; double starterBalance;
/// Selected currency /// Selected currency
@JsonKey(fromJson: _currencyFromJson) @JsonKey(
fromJson: _currencyFromJson,
defaultValue: _defaultCurrency,
)
final Currency currency; final Currency currency;
/// Converts the data in this instance into a Map /// Converts the data in this instance into a Map
@ -281,3 +289,20 @@ class Wallet {
WalletManager.saveWallet(this); WalletManager.saveWallet(this);
} }
} }
List<WalletCategory> _defaultWalletCategory() => [WalletCategory.unknown()];
Currency _defaultCurrency() => Currency.from(
json: {
"code": "USD",
"name": "United States Dollar",
"symbol": r"$",
"flag": "USD",
"decimal_digits": 2,
"number": 840,
"name_plural": "US dollars",
"thousands_separator": ",",
"decimal_separator": ".",
"space_between_amount_and_symbol": false,
"symbol_on_left": true,
},
);

View file

@ -7,22 +7,24 @@ part of 'wallet.dart';
// ************************************************************************** // **************************************************************************
Wallet _$WalletFromJson(Map<String, dynamic> json) => Wallet( Wallet _$WalletFromJson(Map<String, dynamic> json) => Wallet(
name: json['name'] as String, name: json['name'] as String? ?? 'Unknown',
currency: _currencyFromJson(json['currency'] as Map<String, dynamic>), currency: json['currency'] == null
? _defaultCurrency()
: _currencyFromJson(json['currency'] as Map<String, dynamic>),
categories: (json['categories'] as List<dynamic>?) categories: (json['categories'] as List<dynamic>?)
?.map((e) => WalletCategory.fromJson(e as Map<String, dynamic>)) ?.map((e) => WalletCategory.fromJson(e as Map<String, dynamic>))
.toList() ?? .toList() ??
const [], _defaultWalletCategory(),
entries: (json['entries'] as List<dynamic>?) entries: (json['entries'] as List<dynamic>?)
?.map( ?.map(
(e) => WalletSingleEntry.fromJson(e as Map<String, dynamic>)) (e) => WalletSingleEntry.fromJson(e as Map<String, dynamic>))
.toList() ?? .toList() ??
const [], [],
recurringEntries: (json['recurringEntries'] as List<dynamic>?) recurringEntries: (json['recurringEntries'] as List<dynamic>?)
?.map((e) => ?.map((e) =>
RecurringWalletEntry.fromJson(e as Map<String, dynamic>)) RecurringWalletEntry.fromJson(e as Map<String, dynamic>))
.toList() ?? .toList() ??
const [], [],
starterBalance: (json['starterBalance'] as num?)?.toDouble() ?? 0, starterBalance: (json['starterBalance'] as num?)?.toDouble() ?? 0,
); );

View file

@ -26,19 +26,24 @@ class WalletSingleEntry {
_$WalletSingleEntryFromJson(json); _$WalletSingleEntryFromJson(json);
/// Expense or income /// Expense or income
@JsonKey(defaultValue: EntryType.expense)
EntryType type; EntryType type;
/// Actual entry data /// Actual entry data
@JsonKey(defaultValue: EntryData.unknown)
EntryData data; EntryData data;
/// Date of entry creation /// Date of entry creation
@JsonKey(defaultValue: DateTime.now)
DateTime date; DateTime date;
/// Selected category /// Selected category
@JsonKey(defaultValue: WalletCategory.unknown)
WalletCategory category; WalletCategory category;
/// Unique entry ID /// Unique entry ID
int id; @JsonKey(required: true, disallowNullValue: true)
final int id;
/// Converts the data in this instance into a Map /// Converts the data in this instance into a Map
Map<String, dynamic> toJson() => _$WalletSingleEntryToJson(this); Map<String, dynamic> toJson() => _$WalletSingleEntryToJson(this);

View file

@ -6,15 +6,27 @@ part of 'wallet_entry.dart';
// JsonSerializableGenerator // JsonSerializableGenerator
// ************************************************************************** // **************************************************************************
WalletSingleEntry _$WalletSingleEntryFromJson(Map<String, dynamic> json) => WalletSingleEntry _$WalletSingleEntryFromJson(Map<String, dynamic> json) {
WalletSingleEntry( $checkKeys(
data: EntryData.fromJson(json['data'] as Map<String, dynamic>), json,
type: $enumDecode(_$EntryTypeEnumMap, json['type']), requiredKeys: const ['id'],
date: DateTime.parse(json['date'] as String), disallowNullValues: const ['id'],
category: );
WalletCategory.fromJson(json['category'] as Map<String, dynamic>), return WalletSingleEntry(
data: json['data'] == null
? EntryData.unknown()
: EntryData.fromJson(json['data'] as Map<String, dynamic>),
type: $enumDecodeNullable(_$EntryTypeEnumMap, json['type']) ??
EntryType.expense,
date: json['date'] == null
? DateTime.now()
: DateTime.parse(json['date'] as String),
category: json['category'] == null
? WalletCategory.unknown()
: WalletCategory.fromJson(json['category'] as Map<String, dynamic>),
id: (json['id'] as num).toInt(), id: (json['id'] as num).toInt(),
); );
}
Map<String, dynamic> _$WalletSingleEntryToJson(WalletSingleEntry instance) => Map<String, dynamic> _$WalletSingleEntryToJson(WalletSingleEntry instance) =>
<String, dynamic>{ <String, dynamic>{