prasule/lib/api/debt_scenario.dart
Matyáš Caras ef52caa836
feat: creating debt scenarios
also changed way of loading selected wallet
2024-07-19 17:34:45 +02:00

64 lines
1.7 KiB
Dart

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,
required this.people,
this.entries = const [],
});
/// Generates a class instance from a Map
factory DebtScenario.fromJson(Map<String, dynamic> json) =>
_$DebtScenarioFromJson(json);
/// Converts the data in this instance into a Map
Map<String, dynamic> toJson() => _$DebtScenarioToJson(this);
/// Unique identifier
@JsonKey(disallowNullValue: true)
final int id;
/// User-friendly identifier
@JsonKey(defaultValue: "Unknown")
String name;
/// Whether this scenario should be shown under archived ones
@JsonKey(defaultValue: false)
bool isArchived;
/// All entries
@JsonKey(defaultValue: [])
final List<DebtEntry> entries;
/// All people
@JsonKey(defaultValue: _defaultPeopleList)
final List<DebtPerson> people;
/// Getter for the next unused unique number ID for a [DebtPerson]
int get nextPersonId {
var id = 1;
while (people.where((element) => element.id == id).isNotEmpty) {
id++; // create unique ID
}
return id;
}
/// Getter for the next unused unique number ID for a [DebtEntry]
int get nextEntryId {
var id = 1;
while (entries.where((element) => element.id == id).isNotEmpty) {
id++; // create unique ID
}
return id;
}
}
List<DebtPerson> _defaultPeopleList() => [DebtPerson.unknownPerson()];