From 2873848e744a3dbd181b61e2b5bb168e3f338684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Mon, 8 Jan 2024 21:11:39 +0100 Subject: [PATCH] feat: add recurring entries into the entrylist on start --- lib/api/wallet.dart | 52 +++++++++++++++++++++++++++++++ lib/views/create_recur_entry.dart | 27 +++------------- lib/views/home.dart | 39 ++++++++++++++++++++++- lib/views/recurring_view.dart | 8 ++--- 4 files changed, 97 insertions(+), 29 deletions(-) diff --git a/lib/api/wallet.dart b/lib/api/wallet.dart index 39e4258..959add1 100644 --- a/lib/api/wallet.dart +++ b/lib/api/wallet.dart @@ -1,9 +1,11 @@ import 'package:currency_picker/currency_picker.dart'; +import 'package:intl/intl.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:prasule/api/category.dart'; import 'package:prasule/api/recurring_entry.dart'; import 'package:prasule/api/wallet_entry.dart'; import 'package:prasule/api/wallet_manager.dart'; +import 'package:prasule/main.dart'; part 'wallet.g.dart'; Currency _currencyFromJson(Map data) => @@ -70,6 +72,56 @@ class Wallet { return id; } + /// Handles adding recurring entries to the entry list + void recurEntries() { + final n = DateTime.now(); + for (final ent in recurringEntries) { + var m = DateTime( + (ent.recurType == RecurType.year) + ? ent.lastRunDate.year + ent.repeatAfter + : ent.lastRunDate.year, + (ent.recurType == RecurType.month) + ? ent.lastRunDate.month + ent.repeatAfter + : ent.lastRunDate.month, + (ent.recurType == RecurType.day) + ? ent.lastRunDate.day + ent.repeatAfter + : ent.lastRunDate.day, + ); // create the date after which we should recur + + while (n.isAfter( + m, + )) { + logger.i("Adding recurring entry ${ent.data.name}"); + 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 + + m = DateTime( + (ent.recurType == RecurType.year) + ? ent.lastRunDate.year + ent.repeatAfter + : ent.lastRunDate.year, + (ent.recurType == RecurType.month) + ? ent.lastRunDate.month + ent.repeatAfter + : 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 + logger.i( + "Last recurred date is now on ${DateFormat.yMMMMd().format(m)} (${n.isAfter(m)})"); + } + + WalletManager.saveWallet(this); // save wallet + } + } + /// Removes the specified category. /// /// All [WalletSingleEntry]s will have their category reassigned diff --git a/lib/views/create_recur_entry.dart b/lib/views/create_recur_entry.dart index f3b7b31..6047980 100644 --- a/lib/views/create_recur_entry.dart +++ b/lib/views/create_recur_entry.dart @@ -1,4 +1,3 @@ -import 'package:dynamic_color/dynamic_color.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; @@ -11,7 +10,6 @@ import 'package:prasule/api/wallet_manager.dart'; import 'package:prasule/main.dart'; import 'package:prasule/pw/platformbutton.dart'; import 'package:prasule/pw/platformfield.dart'; -import 'package:prasule/util/text_color.dart'; /// Used when user wants to add new entry class CreateRecurringEntryView extends StatefulWidget { @@ -225,6 +223,10 @@ class _CreateRecurringEntryViewState extends State { RegExp(r"^0$"), replacementString: "1", ), + FilteringTextInputFormatter.deny( + r"\d+[\.,]{0,1}\d{0,}", + replacementString: "1", + ), ], onChanged: (s) { final n = int.tryParse(s); @@ -290,28 +292,7 @@ class _CreateRecurringEntryViewState extends State { PlatformButton( text: DateFormat.yMMMMd(widget.locale) .format(newEntry.lastRunDate), - style: (widget.editEntry != null) - ? ButtonStyle( - backgroundColor: MaterialStateProperty.all( - Colors.grey.harmonizeWith( - Theme.of(context).colorScheme.primary, - ), - ), - foregroundColor: MaterialStateProperty.all( - Colors.grey - .harmonizeWith( - Theme.of(context).colorScheme.primary, - ) - .calculateTextColor() - .harmonizeWith( - Theme.of(context).colorScheme.primary), - ), - ) - : null, onPressed: () async { - if (widget.editEntry != null) { - return; // disabled on edit - } final d = await showDatePicker( context: context, firstDate: DateTime.now(), diff --git a/lib/views/home.dart b/lib/views/home.dart index 0fff9db..2ed0fac 100644 --- a/lib/views/home.dart +++ b/lib/views/home.dart @@ -15,6 +15,7 @@ import 'package:intl/date_symbol_data_local.dart'; import 'package:intl/intl.dart'; import 'package:prasule/api/category.dart'; import 'package:prasule/api/entry_data.dart'; +import 'package:prasule/api/recurring_entry.dart'; import 'package:prasule/api/wallet.dart'; import 'package:prasule/api/wallet_entry.dart'; import 'package:prasule/api/wallet_manager.dart'; @@ -67,6 +68,7 @@ class _HomeViewState extends State { return; } selectedWallet = wallets.first; + selectedWallet!.recurEntries(); setState(() {}); } @@ -86,6 +88,7 @@ class _HomeViewState extends State { // debug option to quickly fill a wallet with data if (selectedWallet == null) return; selectedWallet!.entries.clear(); + selectedWallet!.recurringEntries.clear(); final random = Random(); for (var i = 0; i < 30; i++) { selectedWallet!.entries.add( @@ -109,7 +112,41 @@ class _HomeViewState extends State { ); } - logger.i(selectedWallet!.entries.length); + logger.d( + "Created ${selectedWallet!.entries.length} regular entries", + ); + + for (var i = 0; i < 3; i++) { + final type = random.nextInt(3); + selectedWallet!.recurringEntries.add( + RecurringWalletEntry( + data: EntryData( + name: "Recurring Entry #${i + 1}", + amount: random.nextInt(20000).toDouble(), + ), + type: (random.nextInt(3) > 0) + ? EntryType.expense + : EntryType.income, + date: DateTime( + 2023, + random.nextInt(12) + 1, + random.nextInt(28) + 1, + ), + category: selectedWallet!.categories[ + random.nextInt(selectedWallet!.categories.length)], + id: selectedWallet!.nextId, + lastRunDate: DateTime.now().subtract( + Duration( + days: (type > 0) ? 3 : 3 * 31, + ), + ), + recurType: (type > 0) ? RecurType.day : RecurType.month, + ), + ); + } + + logger.d( + "Created ${selectedWallet!.recurringEntries.length} recurring entries"); // save and reload WalletManager.saveWallet(selectedWallet!).then((value) { diff --git a/lib/views/recurring_view.dart b/lib/views/recurring_view.dart index b3482b1..49bc3e3 100644 --- a/lib/views/recurring_view.dart +++ b/lib/views/recurring_view.dart @@ -129,6 +129,7 @@ class _RecurringEntriesViewState extends State { ], ), floatingActionButton: FloatingActionButton( + shape: const CircleBorder(), child: const Icon(Icons.add), onPressed: () { Navigator.of(context).push( @@ -225,11 +226,8 @@ class _RecurringEntriesViewState extends State { text: AppLocalizations.of(context).yes, onPressed: () { selectedWallet!.recurringEntries - .removeWhere( - (e) => - e.id == - selectedWallet! - .recurringEntries[i].id, + .remove( + selectedWallet!.recurringEntries[i], ); WalletManager.saveWallet( selectedWallet!,