Skip to content

Commit

Permalink
fix: refresh account after transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
coluzziandrea committed Jan 6, 2025
1 parent 0422cc4 commit 6e9a0db
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 45 deletions.
106 changes: 61 additions & 45 deletions lib/pages/add_page/add_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {

@override
void initState() {
amountController.text = numToCurrency(ref.read(selectedTransactionUpdateProvider)?.amount);
noteController.text = ref.read(selectedTransactionUpdateProvider)?.note ?? '';
amountController.text =
numToCurrency(ref.read(selectedTransactionUpdateProvider)?.amount);
noteController.text =
ref.read(selectedTransactionUpdateProvider)?.note ?? '';

amountController.addListener(_updateAmount);

Expand All @@ -48,7 +50,9 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {

if (recurrencyEditingPermittedFromRoute == null) {
final argsMap = args as Map<String, dynamic>?;
recurrencyEditingPermittedFromRoute = argsMap?['recurrencyEditingPermitted'] ?? widget.recurrencyEditingPermitted;
recurrencyEditingPermittedFromRoute =
argsMap?['recurrencyEditingPermitted'] ??
widget.recurrencyEditingPermitted;
}
}

Expand All @@ -61,7 +65,8 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {

String getCleanAmountString() {
// Remove all non-numeric characters
var cleanNumberString = amountController.text.replaceAll(RegExp(r'[^0-9\.]'), '');
var cleanNumberString =
amountController.text.replaceAll(RegExp(r'[^0-9\.]'), '');

// Remove leading zeros
return cleanNumberString.replaceAll(RegExp(r'^[0\.]+(?=.)'), '');
Expand All @@ -88,10 +93,11 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
}
}

void _updateAccount() {
void _refreshAccountAndNavigateBack() {
ref
.read(accountsProvider.notifier)
.updateAccount(ref.watch(bankAccountProvider)!.name);
.selectAndUpdateAccount(ref.read(bankAccountProvider)!)
.whenComplete(() => Navigator.of(context).pop());
}

void _createOrUpdateTransaction() {
Expand All @@ -105,53 +111,57 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
if (selectedTransaction != null) {
// if the original transaction is not recurrent, but user sets a recurrency, add the corrispondent record
// and edit the original transaction
if(ref.read(selectedRecurringPayProvider) && !selectedTransaction.recurring) {
if (ref.read(selectedRecurringPayProvider) &&
!selectedTransaction.recurring) {
ref
.read(transactionsProvider.notifier)
.addRecurringTransaction(currencyToNum(cleanAmount), noteController.text)
.addRecurringTransaction(
currencyToNum(cleanAmount), noteController.text)
.then((value) {
if (value != null) {
ref
.read(transactionsProvider.notifier)
.updateTransaction(currencyToNum(cleanAmount), noteController.text, value.id)
.whenComplete(() => Navigator.of(context).pop());
}
});
if (value != null) {
ref
.read(transactionsProvider.notifier)
.updateTransaction(
currencyToNum(cleanAmount), noteController.text, value.id)
.whenComplete(() => _refreshAccountAndNavigateBack());
}
});
} else {
ref
.read(transactionsProvider.notifier)
.updateTransaction(currencyToNum(cleanAmount), noteController.text, selectedTransaction.idRecurringTransaction)
.whenComplete(() => Navigator.of(context).pop());
.updateTransaction(
currencyToNum(cleanAmount),
noteController.text,
selectedTransaction.idRecurringTransaction)
.whenComplete(() => _refreshAccountAndNavigateBack());
}


} else {
if (selectedType == TransactionType.transfer) {
if (ref.read(bankAccountTransferProvider) != null) {
ref
.read(transactionsProvider.notifier)
.addTransaction(currencyToNum(cleanAmount), noteController.text)
.whenComplete(() => Navigator.of(context).pop());
.whenComplete(() => _refreshAccountAndNavigateBack());
}
} else {
// It's an income or an expense
if (ref.read(categoryProvider) != null) {
if(ref.read(selectedRecurringPayProvider)) {
if (ref.read(selectedRecurringPayProvider)) {
ref
.read(transactionsProvider.notifier)
.addRecurringTransaction(currencyToNum(cleanAmount), noteController.text)
.whenComplete(() => Navigator.of(context).pop());
.read(transactionsProvider.notifier)
.addRecurringTransaction(
currencyToNum(cleanAmount), noteController.text)
.whenComplete(() => _refreshAccountAndNavigateBack());
} else {
ref
.read(transactionsProvider.notifier)
.addTransaction(currencyToNum(cleanAmount), noteController.text)
.whenComplete(() => Navigator.of(context).pop());
.read(transactionsProvider.notifier)
.addTransaction(
currencyToNum(cleanAmount), noteController.text)
.whenComplete(() => _refreshAccountAndNavigateBack());
}
}
}
}

_updateAccount();
}
}

Expand All @@ -160,8 +170,7 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
ref
.read(transactionsProvider.notifier)
.deleteTransaction(selectedTransaction!.id!)
.whenComplete(() => Navigator.pop(context));
_updateAccount();
.whenComplete(() => _refreshAccountAndNavigateBack());
}

@override
Expand All @@ -174,14 +183,17 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
return Scaffold(
appBar: AppBar(
title: Text(
(selectedTransaction != null) ? "Editing transaction" : "New transaction",
(selectedTransaction != null)
? "Editing transaction"
: "New transaction",
),
leadingWidth: 100,
leading: TextButton(
onPressed: () => Navigator.pop(context),
child: Text(
'Cancel',
style: Theme.of(context).textTheme.titleMedium!.copyWith(color: blue5),
style:
Theme.of(context).textTheme.titleMedium!.copyWith(color: blue5),
),
),
actions: [
Expand Down Expand Up @@ -303,8 +315,9 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
minimumYear: 2015,
maximumYear: 2050,
mode: CupertinoDatePickerMode.date,
onDateTimeChanged: (date) =>
ref.read(dateProvider.notifier).state = date,
onDateTimeChanged: (date) => ref
.read(dateProvider.notifier)
.state = date,
),
),
);
Expand All @@ -316,16 +329,18 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
lastDate: DateTime(2050),
);
if (pickedDate != null) {
ref.read(dateProvider.notifier).state = pickedDate;
ref.read(dateProvider.notifier).state =
pickedDate;
}
}
},
),
if (selectedType == TransactionType.expense) ...[
RecurrenceListTile(
recurrencyEditingPermitted: widget.recurrencyEditingPermitted,
selectedTransaction: ref.read(selectedTransactionUpdateProvider)
)
recurrencyEditingPermitted:
widget.recurrencyEditingPermitted,
selectedTransaction:
ref.read(selectedTransactionUpdateProvider))
],
],
),
Expand Down Expand Up @@ -359,14 +374,15 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
onPressed: _createOrUpdateTransaction,
style: TextButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.secondary,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8)),
),
child: Text(
selectedTransaction != null ? "UPDATE TRANSACTION" : "ADD TRANSACTION",
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(color: Theme.of(context).colorScheme.onPrimary),
selectedTransaction != null
? "UPDATE TRANSACTION"
: "ADD TRANSACTION",
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Theme.of(context).colorScheme.onPrimary),
),
),
),
Expand Down
5 changes: 5 additions & 0 deletions lib/providers/accounts_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class AsyncAccountsNotifier extends AsyncNotifier<List<BankAccount>> {
});
}

Future<void> selectAndUpdateAccount(BankAccount account) async {
await selectedAccount(account);
await updateAccount(account.name);
}

Future<void> updateAccount(String name) async {
BankAccount account = ref.read(selectedAccountProvider)!.copy(
name: name,
Expand Down

0 comments on commit 6e9a0db

Please sign in to comment.