From e40f0491fc464ba8eb785428941a447bccfec4e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Mon, 8 Jan 2024 23:22:35 +0100 Subject: [PATCH] fix: actually make recurring entries work correctly --- lib/api/wallet.dart | 59 +++++++++++++++++++++++++++++++++------------ lib/views/home.dart | 1 + 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/lib/api/wallet.dart b/lib/api/wallet.dart index 959add1..16ac990 100644 --- a/lib/api/wallet.dart +++ b/lib/api/wallet.dart @@ -1,6 +1,7 @@ import 'package:currency_picker/currency_picker.dart'; import 'package:intl/intl.dart'; import 'package:json_annotation/json_annotation.dart'; +import 'package:logger/logger.dart'; import 'package:prasule/api/category.dart'; import 'package:prasule/api/recurring_entry.dart'; import 'package:prasule/api/wallet_entry.dart'; @@ -56,9 +57,11 @@ class Wallet { /// Getter for the next unused unique number ID in the wallet's **entry** list int get nextId { var id = 1; + logger.d("Making ID after ${entries.length} entries"); while (entries.where((element) => element.id == id).isNotEmpty) { id++; // create unique ID } + logger.d("New ID: $id\nLast entry ID: ${entries.lastOrNull?.id}"); return id; } @@ -91,33 +94,57 @@ class Wallet { while (n.isAfter( m, )) { - logger.i("Adding recurring entry ${ent.data.name}"); + logger + ..i("Adding recurring entry ${ent.data.name}") + ..d("Current entry count: ${entries.length}"); recurringEntries[recurringEntries.indexOf(ent)].lastRunDate = m; // update date on recurring entry logger.i(recurringEntries[recurringEntries.indexOf(ent)].lastRunDate); - final addedEntry = (recurringEntries[recurringEntries.indexOf(ent)] - as WalletSingleEntry) - ..date = DateTime.now() - ..id = nextId; // copy entry with today's date and unique ID - entries.add( - addedEntry, - ); // add it to entries + + var id = 1; + logger.d("Making ID after ${entries.length} entries"); + while (entries.where((element) => element.id == id).isNotEmpty) { + id++; // create unique ID + } + logger.d("New ID: $id\nLast entry ID: ${entries.lastOrNull?.id}"); + + final addedEntry = WalletSingleEntry( + data: recurringEntries[recurringEntries.indexOf(ent)].data, + type: recurringEntries[recurringEntries.indexOf(ent)].type, + date: m, + category: recurringEntries[recurringEntries.indexOf(ent)].category, + id: id, + ); + + entries.add(addedEntry); m = DateTime( (ent.recurType == RecurType.year) - ? ent.lastRunDate.year + ent.repeatAfter - : ent.lastRunDate.year, + ? recurringEntries[recurringEntries.indexOf(ent)] + .lastRunDate + .year + + ent.repeatAfter + : recurringEntries[recurringEntries.indexOf(ent)] + .lastRunDate + .year, (ent.recurType == RecurType.month) - ? ent.lastRunDate.month + ent.repeatAfter - : ent.lastRunDate.month, + ? recurringEntries[recurringEntries.indexOf(ent)] + .lastRunDate + .month + + ent.repeatAfter + : recurringEntries[recurringEntries.indexOf(ent)] + .lastRunDate + .month, (ent.recurType == RecurType.day) - ? ent.lastRunDate.day + ent.repeatAfter - : ent.lastRunDate.day, - ); // add tne variable again to check if we aren't missing any entries + ? recurringEntries[recurringEntries.indexOf(ent)] + .lastRunDate + .day + + ent.repeatAfter + : recurringEntries[recurringEntries.indexOf(ent)].lastRunDate.day, + ); // add the variable again to check if we aren't missing any entries logger.i( "Last recurred date is now on ${DateFormat.yMMMMd().format(m)} (${n.isAfter(m)})"); } - WalletManager.saveWallet(this); // save wallet } } diff --git a/lib/views/home.dart b/lib/views/home.dart index 563821a..f0c24b3 100644 --- a/lib/views/home.dart +++ b/lib/views/home.dart @@ -374,6 +374,7 @@ class _HomeViewState extends State { selectedWallet?.entries.removeWhere( (e) => e.id == element.id, ); + WalletManager.saveWallet( selectedWallet!, );