Skip to content

Commit 5ea2ae9

Browse files
authored
Merge pull request #188 from coluzziandrea/fix/187
Bug fix/187: update account after updating transaction
2 parents dc89ae8 + 6e9a0db commit 5ea2ae9

File tree

2 files changed

+79
-45
lines changed

2 files changed

+79
-45
lines changed

lib/pages/add_page/add_page.dart

Lines changed: 74 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
77
import '../../constants/functions.dart';
88
import '../../constants/style.dart';
99
import '../../model/transaction.dart';
10+
import '../../providers/accounts_provider.dart';
1011
import '../../providers/transactions_provider.dart';
1112
import "widgets/account_selector.dart";
1213
import 'widgets/amount_section.dart';
@@ -31,8 +32,10 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
3132

3233
@override
3334
void initState() {
34-
amountController.text = numToCurrency(ref.read(selectedTransactionUpdateProvider)?.amount);
35-
noteController.text = ref.read(selectedTransactionUpdateProvider)?.note ?? '';
35+
amountController.text =
36+
numToCurrency(ref.read(selectedTransactionUpdateProvider)?.amount);
37+
noteController.text =
38+
ref.read(selectedTransactionUpdateProvider)?.note ?? '';
3639

3740
amountController.addListener(_updateAmount);
3841

@@ -47,7 +50,9 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
4750

4851
if (recurrencyEditingPermittedFromRoute == null) {
4952
final argsMap = args as Map<String, dynamic>?;
50-
recurrencyEditingPermittedFromRoute = argsMap?['recurrencyEditingPermitted'] ?? widget.recurrencyEditingPermitted;
53+
recurrencyEditingPermittedFromRoute =
54+
argsMap?['recurrencyEditingPermitted'] ??
55+
widget.recurrencyEditingPermitted;
5156
}
5257
}
5358

@@ -60,7 +65,8 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
6065

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

6571
// Remove leading zeros only if the number does not start with "0."
6672
if (!cleanNumberString.startsWith('0.')) {
@@ -95,6 +101,13 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
95101
}
96102
}
97103

104+
void _refreshAccountAndNavigateBack() {
105+
ref
106+
.read(accountsProvider.notifier)
107+
.selectAndUpdateAccount(ref.read(bankAccountProvider)!)
108+
.whenComplete(() => Navigator.of(context).pop());
109+
}
110+
98111
void _createOrUpdateTransaction() {
99112
final selectedType = ref.read(transactionTypeProvider);
100113
final selectedTransaction = ref.read(selectedTransactionUpdateProvider);
@@ -106,54 +119,68 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
106119
if (selectedTransaction != null) {
107120
// if the original transaction is not recurrent, but user sets a recurrency, add the corrispondent record
108121
// and edit the original transaction
109-
if(ref.read(selectedRecurringPayProvider) && !selectedTransaction.recurring) {
122+
if (ref.read(selectedRecurringPayProvider) &&
123+
!selectedTransaction.recurring) {
110124
ref
111125
.read(transactionsProvider.notifier)
112-
.addRecurringTransaction(currencyToNum(cleanAmount), noteController.text)
126+
.addRecurringTransaction(
127+
currencyToNum(cleanAmount), noteController.text)
113128
.then((value) {
114-
if (value != null) {
115-
ref
116-
.read(transactionsProvider.notifier)
117-
.updateTransaction(currencyToNum(cleanAmount), noteController.text, value.id)
118-
.whenComplete(() => Navigator.of(context).pop());
119-
}
120-
});
129+
if (value != null) {
130+
ref
131+
.read(transactionsProvider.notifier)
132+
.updateTransaction(
133+
currencyToNum(cleanAmount), noteController.text, value.id)
134+
.whenComplete(() => _refreshAccountAndNavigateBack());
135+
}
136+
});
121137
} else {
122138
ref
123139
.read(transactionsProvider.notifier)
124-
.updateTransaction(currencyToNum(cleanAmount), noteController.text, selectedTransaction.idRecurringTransaction)
125-
.whenComplete(() => Navigator.of(context).pop());
140+
.updateTransaction(
141+
currencyToNum(cleanAmount),
142+
noteController.text,
143+
selectedTransaction.idRecurringTransaction)
144+
.whenComplete(() => _refreshAccountAndNavigateBack());
126145
}
127-
128-
129146
} else {
130147
if (selectedType == TransactionType.transfer) {
131148
if (ref.read(bankAccountTransferProvider) != null) {
132149
ref
133150
.read(transactionsProvider.notifier)
134151
.addTransaction(currencyToNum(cleanAmount), noteController.text)
135-
.whenComplete(() => Navigator.of(context).pop());
152+
.whenComplete(() => _refreshAccountAndNavigateBack());
136153
}
137154
} else {
138155
// It's an income or an expense
139156
if (ref.read(categoryProvider) != null) {
140-
if(ref.read(selectedRecurringPayProvider)) {
157+
if (ref.read(selectedRecurringPayProvider)) {
141158
ref
142-
.read(transactionsProvider.notifier)
143-
.addRecurringTransaction(currencyToNum(cleanAmount), noteController.text)
144-
.whenComplete(() => Navigator.of(context).pop());
159+
.read(transactionsProvider.notifier)
160+
.addRecurringTransaction(
161+
currencyToNum(cleanAmount), noteController.text)
162+
.whenComplete(() => _refreshAccountAndNavigateBack());
145163
} else {
146164
ref
147-
.read(transactionsProvider.notifier)
148-
.addTransaction(currencyToNum(cleanAmount), noteController.text)
149-
.whenComplete(() => Navigator.of(context).pop());
165+
.read(transactionsProvider.notifier)
166+
.addTransaction(
167+
currencyToNum(cleanAmount), noteController.text)
168+
.whenComplete(() => _refreshAccountAndNavigateBack());
150169
}
151170
}
152171
}
153172
}
154173
}
155174
}
156175

176+
void _deleteTransaction() {
177+
final selectedTransaction = ref.read(selectedTransactionUpdateProvider);
178+
ref
179+
.read(transactionsProvider.notifier)
180+
.deleteTransaction(selectedTransaction!.id!)
181+
.whenComplete(() => _refreshAccountAndNavigateBack());
182+
}
183+
157184
@override
158185
Widget build(BuildContext context) {
159186
final selectedType = ref.watch(transactionTypeProvider);
@@ -164,14 +191,17 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
164191
return Scaffold(
165192
appBar: AppBar(
166193
title: Text(
167-
(selectedTransaction != null) ? "Editing transaction" : "New transaction",
194+
(selectedTransaction != null)
195+
? "Editing transaction"
196+
: "New transaction",
168197
),
169198
leadingWidth: 100,
170199
leading: TextButton(
171200
onPressed: () => Navigator.pop(context),
172201
child: Text(
173202
'Cancel',
174-
style: Theme.of(context).textTheme.titleMedium!.copyWith(color: blue5),
203+
style:
204+
Theme.of(context).textTheme.titleMedium!.copyWith(color: blue5),
175205
),
176206
),
177207
actions: [
@@ -183,12 +213,7 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
183213
Icons.delete_outline,
184214
color: Theme.of(context).colorScheme.error,
185215
),
186-
onPressed: () async {
187-
ref
188-
.read(transactionsProvider.notifier)
189-
.deleteTransaction(selectedTransaction.id!)
190-
.whenComplete(() => Navigator.pop(context));
191-
},
216+
onPressed: _deleteTransaction,
192217
),
193218
)
194219
: const SizedBox(),
@@ -298,8 +323,9 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
298323
minimumYear: 2015,
299324
maximumYear: 2050,
300325
mode: CupertinoDatePickerMode.date,
301-
onDateTimeChanged: (date) =>
302-
ref.read(dateProvider.notifier).state = date,
326+
onDateTimeChanged: (date) => ref
327+
.read(dateProvider.notifier)
328+
.state = date,
303329
),
304330
),
305331
);
@@ -311,16 +337,18 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
311337
lastDate: DateTime(2050),
312338
);
313339
if (pickedDate != null) {
314-
ref.read(dateProvider.notifier).state = pickedDate;
340+
ref.read(dateProvider.notifier).state =
341+
pickedDate;
315342
}
316343
}
317344
},
318345
),
319346
if (selectedType == TransactionType.expense) ...[
320347
RecurrenceListTile(
321-
recurrencyEditingPermitted: widget.recurrencyEditingPermitted,
322-
selectedTransaction: ref.read(selectedTransactionUpdateProvider)
323-
)
348+
recurrencyEditingPermitted:
349+
widget.recurrencyEditingPermitted,
350+
selectedTransaction:
351+
ref.read(selectedTransactionUpdateProvider))
324352
],
325353
],
326354
),
@@ -354,14 +382,15 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
354382
onPressed: _createOrUpdateTransaction,
355383
style: TextButton.styleFrom(
356384
backgroundColor: Theme.of(context).colorScheme.secondary,
357-
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
385+
shape: RoundedRectangleBorder(
386+
borderRadius: BorderRadius.circular(8)),
358387
),
359388
child: Text(
360-
selectedTransaction != null ? "UPDATE TRANSACTION" : "ADD TRANSACTION",
361-
style: Theme.of(context)
362-
.textTheme
363-
.bodyLarge!
364-
.copyWith(color: Theme.of(context).colorScheme.onPrimary),
389+
selectedTransaction != null
390+
? "UPDATE TRANSACTION"
391+
: "ADD TRANSACTION",
392+
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
393+
color: Theme.of(context).colorScheme.onPrimary),
365394
),
366395
),
367396
),

lib/providers/accounts_provider.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class AsyncAccountsNotifier extends AsyncNotifier<List<BankAccount>> {
5656
});
5757
}
5858

59+
Future<void> selectAndUpdateAccount(BankAccount account) async {
60+
await selectedAccount(account);
61+
await updateAccount(account.name);
62+
}
63+
5964
Future<void> updateAccount(String name) async {
6065
BankAccount account = ref.read(selectedAccountProvider)!.copy(
6166
name: name,

0 commit comments

Comments
 (0)