Can save expenses to local list
This commit is contained in:
@ -17,7 +17,7 @@ typedef ExpensesList = List<Expense>;
|
||||
abstract class BaseExpenseInfo with _$BaseExpenseInfo {
|
||||
const factory BaseExpenseInfo({
|
||||
required String label,
|
||||
required int cost,
|
||||
required double cost,
|
||||
required DateTime time,
|
||||
}) = _BaseExpenseInfo;
|
||||
}
|
||||
@ -36,7 +36,7 @@ abstract class Expense with _$Expense {
|
||||
/// The cost shall always be a positive value
|
||||
required double cost,
|
||||
|
||||
/// Time associated with the expense
|
||||
/// Time associated with the expense (seconds since epoch)
|
||||
required int time,
|
||||
|
||||
/// Associated file mime type
|
||||
@ -80,21 +80,24 @@ class ExpensesManager {
|
||||
|
||||
/// Get the current list of expenses
|
||||
Future<ExpensesList> getList() async {
|
||||
// On first save the list does not exists.
|
||||
if (!await expenseFile.exists()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final jsonDec = jsonDecode(await expenseFile.readAsString());
|
||||
return List<Expense>.from(jsonDec.map((m) => Expense.fromJson(m)));
|
||||
}
|
||||
|
||||
/// Save the list of expenses
|
||||
Future<void> saveList(ExpensesList list) async {
|
||||
final jsonDoc = jsonEncode(list.map((t) => t.toJson()));
|
||||
final jsonDoc = jsonEncode(list.map((t) => t.toJson()).toList());
|
||||
await expenseFile.writeAsString(jsonDoc);
|
||||
}
|
||||
|
||||
/// Add a new expense to the list
|
||||
Future<void> add({
|
||||
required String? label,
|
||||
required double cost,
|
||||
required int time,
|
||||
required BaseExpenseInfo info,
|
||||
required List<int> fileContent,
|
||||
required String fileMimeType,
|
||||
}) async {
|
||||
@ -102,9 +105,9 @@ class ExpensesManager {
|
||||
|
||||
final exp = Expense(
|
||||
id: (list.lastOrNull?.id ?? 0) + Random().nextInt(1000),
|
||||
label: label,
|
||||
cost: cost,
|
||||
time: time,
|
||||
label: info.label,
|
||||
cost: info.cost,
|
||||
time: (info.time.millisecondsSinceEpoch / 1000).floor(),
|
||||
mimeType: fileMimeType,
|
||||
);
|
||||
|
||||
|
Reference in New Issue
Block a user