prasule/lib/api/wallet.dart
2023-12-29 21:39:54 +01:00

76 lines
2 KiB
Dart

import 'package:currency_picker/currency_picker.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:prasule/api/category.dart';
import 'package:prasule/api/walletentry.dart';
part 'wallet.g.dart';
Currency _currencyFromJson(Map<String, dynamic> data) =>
Currency.from(json: data);
/// Represents a single wallet
///
/// A wallet stores [WalletSingleEntry]s categorized under [WalletCategory]s
@JsonSerializable()
class Wallet {
/// A wallet stores [WalletSingleEntry]s categorized under [WalletCategory]s
Wallet(
{required this.name,
required this.currency,
this.categories = const [],
this.entries = const [],
this.starterBalance = 0,});
/// Connects generated fromJson function
factory Wallet.fromJson(Map<String, dynamic> json) => _$WalletFromJson(json);
/// Name of the wallet
final String name;
/// A list of available categories
final List<WalletCategory> categories;
/// List of saved entries
final List<WalletSingleEntry> entries;
/// The starting balance of the wallet
///
/// Used to calculate current balance
double starterBalance;
/// Selected currency
@JsonKey(fromJson: _currencyFromJson)
final Currency currency;
/// Connects generated toJson function
Map<String, dynamic> toJson() => _$WalletToJson(this);
/// Getter for the next unused unique number ID in the wallet's entry list
int get nextId {
var id = 1;
while (entries.where((element) => element.id == id).isNotEmpty) {
id++; // create unique ID
}
return id;
}
/// Empty wallet used for placeholders
static final Wallet empty = Wallet(
name: "Empty",
currency: 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,
},
),
);
}