From d93eac4f0443dc3e32b4555f7f7cfc3bd6668a9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Wed, 17 Jul 2024 16:44:02 +0200 Subject: [PATCH] fix: create basic debt class structure --- lib/api/category.dart | 4 ++-- lib/api/category.g.dart | 8 ++----- lib/api/debt_entry.dart | 38 +++++++++++++++++++++++++++++++++ lib/api/debt_entry.g.dart | 23 ++++++++++++++++++++ lib/api/debt_person.dart | 23 ++++++++++++++++++++ lib/api/debt_person.g.dart | 18 ++++++++++++++++ lib/api/debt_scenario.dart | 39 ++++++++++++++++++++++++++++++++++ lib/api/debt_scenario.g.dart | 30 ++++++++++++++++++++++++++ lib/api/entry_data.dart | 4 ++-- lib/api/entry_data.g.dart | 4 ---- lib/api/recurring_entry.dart | 4 ++-- lib/api/recurring_entry.g.dart | 8 ++----- lib/api/wallet.dart | 4 ++-- lib/api/wallet.g.dart | 4 ---- lib/api/wallet_entry.dart | 4 ++-- lib/api/wallet_entry.g.dart | 6 +----- 16 files changed, 186 insertions(+), 35 deletions(-) create mode 100644 lib/api/debt_entry.dart create mode 100644 lib/api/debt_entry.g.dart create mode 100644 lib/api/debt_person.dart create mode 100644 lib/api/debt_person.g.dart create mode 100644 lib/api/debt_scenario.dart create mode 100644 lib/api/debt_scenario.g.dart diff --git a/lib/api/category.dart b/lib/api/category.dart index bc011fa..ee7f78d 100644 --- a/lib/api/category.dart +++ b/lib/api/category.dart @@ -18,7 +18,7 @@ class WalletCategory { required this.color, }); - /// Connects the generated fromJson method + /// Generates a class instance from a Map factory WalletCategory.fromJson(Map json) => _$WalletCategoryFromJson(json); @@ -36,7 +36,7 @@ class WalletCategory { @JsonKey(fromJson: _colorFromJson, toJson: _colorToJson) Color color; - /// Connects the generated toJson method + /// Converts the data in this instance into a Map Map toJson() => _$WalletCategoryToJson(this); @override diff --git a/lib/api/category.g.dart b/lib/api/category.g.dart index e08fa70..deaf100 100644 --- a/lib/api/category.g.dart +++ b/lib/api/category.g.dart @@ -1,7 +1,3 @@ -// SPDX-FileCopyrightText: (C) 2024 Matyáš Caras -// -// SPDX-License-Identifier: AGPL-3.0-only - // GENERATED CODE - DO NOT MODIFY BY HAND part of 'category.dart'; @@ -13,9 +9,9 @@ part of 'category.dart'; WalletCategory _$WalletCategoryFromJson(Map json) => WalletCategory( name: json['name'] as String, - id: json['id'] as int, + id: (json['id'] as num).toInt(), icon: _iconDataFromJson(json['icon'] as Map), - color: _colorFromJson(json['color'] as int), + color: _colorFromJson((json['color'] as num).toInt()), ); Map _$WalletCategoryToJson(WalletCategory instance) => diff --git a/lib/api/debt_entry.dart b/lib/api/debt_entry.dart new file mode 100644 index 0000000..9f32489 --- /dev/null +++ b/lib/api/debt_entry.dart @@ -0,0 +1,38 @@ +import 'package:json_annotation/json_annotation.dart'; +import 'package:prasule/api/debt_person.dart'; +part 'debt_entry.g.dart'; + +/// Single transaction +/// +/// The debt will be split between people who are not in [whoPayed] +@JsonSerializable() +class DebtEntry { + /// Single transaction + /// + /// The debt will be split between people who are not in [whoPayed] + DebtEntry({ + required this.id, + required this.amount, + required this.name, + required this.whoPayed, + }) : assert(whoPayed.isNotEmpty, "There has to be at least one payer"); + + /// Generates a class instance from a Map + factory DebtEntry.fromJson(Map json) => + _$DebtEntryFromJson(json); + + /// Converts the data in this instance into a Map + Map toJson() => _$DebtEntryToJson(this); + + /// Unique identifier + final int id; + + /// The payed amount + int amount; + + /// User-friendly identifier for the transaction + String name; + + /// List of people who payed + List whoPayed; +} diff --git a/lib/api/debt_entry.g.dart b/lib/api/debt_entry.g.dart new file mode 100644 index 0000000..7ec825e --- /dev/null +++ b/lib/api/debt_entry.g.dart @@ -0,0 +1,23 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'debt_entry.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +DebtEntry _$DebtEntryFromJson(Map json) => DebtEntry( + id: (json['id'] as num).toInt(), + amount: (json['amount'] as num).toInt(), + name: json['name'] as String, + whoPayed: (json['whoPayed'] as List) + .map((e) => DebtPerson.fromJson(e as Map)) + .toList(), + ); + +Map _$DebtEntryToJson(DebtEntry instance) => { + 'id': instance.id, + 'amount': instance.amount, + 'name': instance.name, + 'whoPayed': instance.whoPayed, + }; diff --git a/lib/api/debt_person.dart b/lib/api/debt_person.dart new file mode 100644 index 0000000..47531fc --- /dev/null +++ b/lib/api/debt_person.dart @@ -0,0 +1,23 @@ +import 'package:json_annotation/json_annotation.dart'; +part 'debt_person.g.dart'; + +@JsonSerializable() + +/// Represents a single person in a debt scenario +class DebtPerson { + /// Represents a single person in a debt scenario + DebtPerson({required this.id, required this.name}); + + /// Generates a class instance from a Map + factory DebtPerson.fromJson(Map json) => + _$DebtPersonFromJson(json); + + /// Converts the data in this instance into a Map + Map toJson() => _$DebtPersonToJson(this); + + /// Unique identifier + final int id; + + /// Identifier that the user will see + String name; +} diff --git a/lib/api/debt_person.g.dart b/lib/api/debt_person.g.dart new file mode 100644 index 0000000..d947448 --- /dev/null +++ b/lib/api/debt_person.g.dart @@ -0,0 +1,18 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'debt_person.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +DebtPerson _$DebtPersonFromJson(Map json) => DebtPerson( + id: (json['id'] as num).toInt(), + name: json['name'] as String, + ); + +Map _$DebtPersonToJson(DebtPerson instance) => + { + 'id': instance.id, + 'name': instance.name, + }; diff --git a/lib/api/debt_scenario.dart b/lib/api/debt_scenario.dart new file mode 100644 index 0000000..0ede810 --- /dev/null +++ b/lib/api/debt_scenario.dart @@ -0,0 +1,39 @@ +import 'package:json_annotation/json_annotation.dart'; +import 'package:prasule/api/debt_entry.dart'; +import 'package:prasule/api/debt_person.dart'; +part 'debt_scenario.g.dart'; + +/// A folder for different entries and people +@JsonSerializable() +class DebtScenario { + /// A folder for different entries and people + DebtScenario({ + required this.id, + required this.name, + required this.isArchived, + this.entries = const [], + this.people = const [], + }); + + /// Generates a class instance from a Map + factory DebtScenario.fromJson(Map json) => + _$DebtScenarioFromJson(json); + + /// Converts the data in this instance into a Map + Map toJson() => _$DebtScenarioToJson(this); + + /// Unique identified + final int id; + + /// User-friendly identifier + String name; + + /// Whether this scenario should be shown under archived ones + bool isArchived; + + /// All entries + List entries; + + /// All people + List people; +} diff --git a/lib/api/debt_scenario.g.dart b/lib/api/debt_scenario.g.dart new file mode 100644 index 0000000..38818cd --- /dev/null +++ b/lib/api/debt_scenario.g.dart @@ -0,0 +1,30 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'debt_scenario.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +DebtScenario _$DebtScenarioFromJson(Map json) => DebtScenario( + id: (json['id'] as num).toInt(), + name: json['name'] as String, + isArchived: json['isArchived'] as bool, + entries: (json['entries'] as List?) + ?.map((e) => DebtEntry.fromJson(e as Map)) + .toList() ?? + const [], + people: (json['people'] as List?) + ?.map((e) => DebtPerson.fromJson(e as Map)) + .toList() ?? + const [], + ); + +Map _$DebtScenarioToJson(DebtScenario instance) => + { + 'id': instance.id, + 'name': instance.name, + 'isArchived': instance.isArchived, + 'entries': instance.entries, + 'people': instance.people, + }; diff --git a/lib/api/entry_data.dart b/lib/api/entry_data.dart index 0b1fe2d..498e07e 100644 --- a/lib/api/entry_data.dart +++ b/lib/api/entry_data.dart @@ -11,7 +11,7 @@ class EntryData { /// Contains raw data EntryData({required this.name, required this.amount, this.description = ""}); - /// Connects the generated fromJson method + /// Generates a class instance from a Map factory EntryData.fromJson(Map json) => _$EntryDataFromJson(json); @@ -24,6 +24,6 @@ class EntryData { /// Amount for entry double amount; - /// Connects the generated toJson method + /// Converts the data in this instance into a Map Map toJson() => _$EntryDataToJson(this); } diff --git a/lib/api/entry_data.g.dart b/lib/api/entry_data.g.dart index 794a01f..48a4faa 100644 --- a/lib/api/entry_data.g.dart +++ b/lib/api/entry_data.g.dart @@ -1,7 +1,3 @@ -// SPDX-FileCopyrightText: (C) 2024 Matyáš Caras -// -// SPDX-License-Identifier: AGPL-3.0-only - // GENERATED CODE - DO NOT MODIFY BY HAND part of 'entry_data.dart'; diff --git a/lib/api/recurring_entry.dart b/lib/api/recurring_entry.dart index 7457f68..fc57bc0 100644 --- a/lib/api/recurring_entry.dart +++ b/lib/api/recurring_entry.dart @@ -24,11 +24,11 @@ class RecurringWalletEntry extends WalletSingleEntry { this.repeatAfter = 1, }); - /// Connects the generated fromJson method + /// Generates a class instance from a Map factory RecurringWalletEntry.fromJson(Map json) => _$RecurringWalletEntryFromJson(json); - /// Connects the generated toJson method + /// Converts the data in this instance into a Map @override Map toJson() => _$RecurringWalletEntryToJson(this); diff --git a/lib/api/recurring_entry.g.dart b/lib/api/recurring_entry.g.dart index d67364a..a8abb86 100644 --- a/lib/api/recurring_entry.g.dart +++ b/lib/api/recurring_entry.g.dart @@ -1,7 +1,3 @@ -// SPDX-FileCopyrightText: (C) 2024 Matyáš Caras -// -// SPDX-License-Identifier: AGPL-3.0-only - // GENERATED CODE - DO NOT MODIFY BY HAND part of 'recurring_entry.dart'; @@ -18,10 +14,10 @@ RecurringWalletEntry _$RecurringWalletEntryFromJson( date: DateTime.parse(json['date'] as String), category: WalletCategory.fromJson(json['category'] as Map), - id: json['id'] as int, + id: (json['id'] as num).toInt(), lastRunDate: DateTime.parse(json['lastRunDate'] as String), - repeatAfter: json['repeatAfter'] as int, recurType: $enumDecode(_$RecurTypeEnumMap, json['recurType']), + repeatAfter: (json['repeatAfter'] as num?)?.toInt() ?? 1, ); Map _$RecurringWalletEntryToJson( diff --git a/lib/api/wallet.dart b/lib/api/wallet.dart index f984b67..df9f81e 100644 --- a/lib/api/wallet.dart +++ b/lib/api/wallet.dart @@ -34,7 +34,7 @@ class Wallet { this.starterBalance = 0, }); - /// Connects the generated fromJson method + /// Generates a class instance from a Map factory Wallet.fromJson(Map json) => _$WalletFromJson(json); /// A list of all [RecurringWalletEntry]s @@ -58,7 +58,7 @@ class Wallet { @JsonKey(fromJson: _currencyFromJson) final Currency currency; - /// Connects the generated toJson method + /// Converts the data in this instance into a Map Map toJson() => _$WalletToJson(this); /// Getter for the next unused unique number ID in the wallet's **entry** list diff --git a/lib/api/wallet.g.dart b/lib/api/wallet.g.dart index 54219bd..561b831 100644 --- a/lib/api/wallet.g.dart +++ b/lib/api/wallet.g.dart @@ -1,7 +1,3 @@ -// SPDX-FileCopyrightText: (C) 2024 Matyáš Caras -// -// SPDX-License-Identifier: AGPL-3.0-only - // GENERATED CODE - DO NOT MODIFY BY HAND part of 'wallet.dart'; diff --git a/lib/api/wallet_entry.dart b/lib/api/wallet_entry.dart index 34dcdb7..77aed05 100644 --- a/lib/api/wallet_entry.dart +++ b/lib/api/wallet_entry.dart @@ -21,7 +21,7 @@ class WalletSingleEntry { required this.id, }); - /// Connects the generated fromJson method + /// Generates a class instance from a Map factory WalletSingleEntry.fromJson(Map json) => _$WalletSingleEntryFromJson(json); @@ -40,6 +40,6 @@ class WalletSingleEntry { /// Unique entry ID int id; - /// Connects the generated toJson method + /// Converts the data in this instance into a Map Map toJson() => _$WalletSingleEntryToJson(this); } diff --git a/lib/api/wallet_entry.g.dart b/lib/api/wallet_entry.g.dart index 9d8b511..4ce4cff 100644 --- a/lib/api/wallet_entry.g.dart +++ b/lib/api/wallet_entry.g.dart @@ -1,7 +1,3 @@ -// SPDX-FileCopyrightText: (C) 2024 Matyáš Caras -// -// SPDX-License-Identifier: AGPL-3.0-only - // GENERATED CODE - DO NOT MODIFY BY HAND part of 'wallet_entry.dart'; @@ -17,7 +13,7 @@ WalletSingleEntry _$WalletSingleEntryFromJson(Map json) => date: DateTime.parse(json['date'] as String), category: WalletCategory.fromJson(json['category'] as Map), - id: json['id'] as int, + id: (json['id'] as num).toInt(), ); Map _$WalletSingleEntryToJson(WalletSingleEntry instance) =>