2023-09-08 11:50:21 +02:00
|
|
|
import 'package:currency_picker/currency_picker.dart';
|
|
|
|
import 'package:json_annotation/json_annotation.dart';
|
|
|
|
import 'package:prasule/api/category.dart';
|
2023-10-09 17:28:05 +02:00
|
|
|
import 'package:prasule/api/walletentry.dart';
|
2023-12-31 12:41:10 +01:00
|
|
|
import 'package:prasule/api/walletmanager.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
part 'wallet.g.dart';
|
|
|
|
|
|
|
|
Currency _currencyFromJson(Map<String, dynamic> data) =>
|
|
|
|
Currency.from(json: data);
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Represents a single wallet
|
|
|
|
///
|
|
|
|
/// A wallet stores [WalletSingleEntry]s categorized under [WalletCategory]s
|
2023-09-08 11:50:21 +02:00
|
|
|
@JsonSerializable()
|
|
|
|
class Wallet {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// A wallet stores [WalletSingleEntry]s categorized under [WalletCategory]s
|
2023-12-31 12:41:10 +01:00
|
|
|
Wallet({
|
|
|
|
required this.name,
|
|
|
|
required this.currency,
|
|
|
|
this.categories = const [],
|
|
|
|
this.entries = const [],
|
|
|
|
this.starterBalance = 0,
|
|
|
|
});
|
2023-09-08 11:50:21 +02:00
|
|
|
|
2023-12-31 12:41:10 +01:00
|
|
|
/// Connects generated fromJson method
|
2023-09-08 11:50:21 +02:00
|
|
|
factory Wallet.fromJson(Map<String, dynamic> json) => _$WalletFromJson(json);
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// 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;
|
|
|
|
|
2023-12-31 12:41:10 +01:00
|
|
|
/// Connects generated toJson method
|
2023-09-08 11:50:21 +02:00
|
|
|
Map<String, dynamic> toJson() => _$WalletToJson(this);
|
2023-11-01 17:58:05 +01:00
|
|
|
|
2023-12-31 12:41:10 +01:00
|
|
|
/// Getter for the next unused unique number ID in the wallet's **entry** list
|
2023-11-01 17:58:05 +01:00
|
|
|
int get nextId {
|
|
|
|
var id = 1;
|
|
|
|
while (entries.where((element) => element.id == id).isNotEmpty) {
|
|
|
|
id++; // create unique ID
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
2023-11-21 20:23:14 +01:00
|
|
|
|
2023-12-31 12:41:10 +01:00
|
|
|
/// Getter for the next unused unique number ID in the wallet's **category**
|
|
|
|
/// list
|
|
|
|
int get nextCategoryId {
|
|
|
|
var id = 0;
|
|
|
|
while (categories.where((element) => element.id == id).isNotEmpty) {
|
|
|
|
id++; // create unique ID
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the specified category.
|
|
|
|
///
|
|
|
|
/// All [WalletSingleEntry]s will have their category reassigned
|
|
|
|
/// to the default *No category*
|
|
|
|
Future<void> removeCategory(WalletCategory category) async {
|
|
|
|
// First remove the category from existing entries
|
|
|
|
for (final entryToChange
|
|
|
|
in entries.where((element) => element.category.id == category.id)) {
|
|
|
|
entryToChange.category =
|
|
|
|
categories.where((element) => element.id == 0).first;
|
|
|
|
}
|
|
|
|
// Remove the category
|
|
|
|
categories.removeWhere((element) => element.id == category.id);
|
|
|
|
// Save
|
|
|
|
await WalletManager.saveWallet(this);
|
|
|
|
}
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Empty wallet used for placeholders
|
2023-11-21 20:23:14 +01:00
|
|
|
static final Wallet empty = Wallet(
|
|
|
|
name: "Empty",
|
|
|
|
currency: Currency.from(
|
|
|
|
json: {
|
|
|
|
"code": "USD",
|
|
|
|
"name": "United States Dollar",
|
2023-12-29 21:39:54 +01:00
|
|
|
"symbol": r"$",
|
2023-11-21 20:23:14 +01:00
|
|
|
"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,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2023-09-08 11:50:21 +02:00
|
|
|
}
|