Skip to content

Commit

Permalink
Merge pull request #188 from coluzziandrea/fix/187
Browse files Browse the repository at this point in the history
Bug fix/187: update account after updating transaction
  • Loading branch information
mikev-cw authored Jan 16, 2025
2 parents dc89ae8 + 6e9a0db commit 5ea2ae9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 45 deletions.
119 changes: 74 additions & 45 deletions lib/pages/add_page/add_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../constants/functions.dart';
import '../../constants/style.dart';
import '../../model/transaction.dart';
import '../../providers/accounts_provider.dart';
import '../../providers/transactions_provider.dart';
import "widgets/account_selector.dart";
import 'widgets/amount_section.dart';
Expand All @@ -31,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 @@ -47,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 @@ -60,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 only if the number does not start with "0."
if (!cleanNumberString.startsWith('0.')) {
Expand Down Expand Up @@ -95,6 +101,13 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
}
}

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

void _createOrUpdateTransaction() {
final selectedType = ref.read(transactionTypeProvider);
final selectedTransaction = ref.read(selectedTransactionUpdateProvider);
Expand All @@ -106,54 +119,68 @@ 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());
}
}
}
}
}
}

void _deleteTransaction() {
final selectedTransaction = ref.read(selectedTransactionUpdateProvider);
ref
.read(transactionsProvider.notifier)
.deleteTransaction(selectedTransaction!.id!)
.whenComplete(() => _refreshAccountAndNavigateBack());
}

@override
Widget build(BuildContext context) {
final selectedType = ref.watch(transactionTypeProvider);
Expand All @@ -164,14 +191,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 All @@ -183,12 +213,7 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
Icons.delete_outline,
color: Theme.of(context).colorScheme.error,
),
onPressed: () async {
ref
.read(transactionsProvider.notifier)
.deleteTransaction(selectedTransaction.id!)
.whenComplete(() => Navigator.pop(context));
},
onPressed: _deleteTransaction,
),
)
: const SizedBox(),
Expand Down Expand Up @@ -298,8 +323,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 @@ -311,16 +337,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 @@ -354,14 +382,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 5ea2ae9

Please sign in to comment.