Skip to content

Commit

Permalink
Fix account and category add/edit
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaantonelli committed Dec 29, 2023
1 parent 7469593 commit 1a4af39
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 66 deletions.
2 changes: 1 addition & 1 deletion lib/model/bank_account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class BankAccountMethods extends SossoldiDatabase {
final db = await database;

final orderByASC = '${BankAccountFields.createdAt} ASC';
final where = '${BankAccountFields.active} = 1';
final where = '${BankAccountFields.active} = 1';

final result = await db.rawQuery('''
SELECT b.*, (b.${BankAccountFields.startingValue} +
Expand Down
9 changes: 4 additions & 5 deletions lib/pages/account_page/account_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import '../../custom_widgets/line_chart.dart';
import '../../providers/accounts_provider.dart';

class AccountPage extends ConsumerStatefulWidget {
const AccountPage({Key? key}) : super(key: key);
const AccountPage({super.key});

@override
ConsumerState<ConsumerStatefulWidget> createState() => _AccountPage();
Expand All @@ -17,12 +17,11 @@ class AccountPage extends ConsumerStatefulWidget {
class _AccountPage extends ConsumerState<AccountPage> with Functions {
@override
Widget build(BuildContext context) {
final accountName = ref.read(accountNameProvider);
final accountAmount = ref.read(accountStartingValueProvider);
final account = ref.read(selectedAccountProvider);

return Scaffold(
appBar: AppBar(
title: Text(accountName ?? "", style: const TextStyle(color: white)),
title: Text(account?.name ?? "", style: const TextStyle(color: white)),
backgroundColor: blue5,
elevation: 0,
),
Expand All @@ -35,7 +34,7 @@ class _AccountPage extends ConsumerState<AccountPage> with Functions {
child: Column(
children: [
Text(
numToCurrency(accountAmount),
numToCurrency(account?.total),
style: const TextStyle(
color: white,
fontSize: 32.0,
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/accounts/account_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class _AccountListState extends ConsumerState<AccountList> with Functions {
),
actions: [
IconButton(
onPressed: () => Navigator.of(context).pushNamed('/add-account'),
onPressed: () {
ref.read(accountsProvider.notifier).reset();
Navigator.of(context).pushNamed('/add-account');
},
icon: const Icon(Icons.add_circle),
splashRadius: 28,
),
Expand Down
34 changes: 11 additions & 23 deletions lib/pages/accounts/add_account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,28 @@ class _AddAccountState extends ConsumerState<AddAccount> with Functions {
final TextEditingController nameController = TextEditingController();
final TextEditingController startingValueController = TextEditingController();

@override
void initState() {
nameController.text = ref.read(selectedAccountProvider)?.name ?? '';
startingValueController.text =
ref.read(selectedAccountProvider)?.startingValue.toString() ?? '';
super.initState();
}

@override
void dispose() {
ref.invalidate(selectedAccountProvider);
ref.invalidate(accountNameProvider);
ref.invalidate(accountIconProvider);
ref.invalidate(accountColorProvider);
ref.invalidate(accountStartingValueProvider);
ref.invalidate(accountMainSwitchProvider);
ref.invalidate(countNetWorthSwitchProvider);
nameController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
final accountName =
ref.read(accountNameProvider); // Used only to retrieve the value when updating an account
final selectedAccount = ref.watch(selectedAccountProvider);
final accountIcon = ref.watch(accountIconProvider);
final accountColor = ref.watch(accountColorProvider);
final showAccountIcons = ref.watch(showAccountIconsProvider);
final accountMainSwitch = ref.watch(accountMainSwitchProvider);
final countNetWorth = ref.watch(countNetWorthSwitchProvider);
ref.listen(accountNameProvider, (_, __) {});

setState(() {
nameController.text = accountName ?? '';
});

return Scaffold(
backgroundColor: Theme.of(context).colorScheme.background,
Expand Down Expand Up @@ -83,7 +78,6 @@ class _AddAccountState extends ConsumerState<AddAccount> with Functions {
contentPadding: const EdgeInsets.all(0),
),
style: Theme.of(context).textTheme.titleLarge!.copyWith(color: grey1),
onChanged: (value) => ref.read(accountNameProvider.notifier).state = value,
)
],
),
Expand Down Expand Up @@ -237,7 +231,6 @@ class _AddAccountState extends ConsumerState<AddAccount> with Functions {
],
),
),

if (selectedAccount == null)
Container(
width: double.infinity,
Expand Down Expand Up @@ -279,14 +272,10 @@ class _AddAccountState extends ConsumerState<AddAccount> with Functions {
.textTheme
.titleLarge!
.copyWith(color: grey1),
onChanged: (value) => ref
.read(accountStartingValueProvider.notifier)
.state = currencyToNum(value),
)
],
),
),

Container(
width: double.infinity,
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
Expand Down Expand Up @@ -331,7 +320,6 @@ class _AddAccountState extends ConsumerState<AddAccount> with Functions {
.bodyLarge!
.copyWith(color: Theme.of(context).colorScheme.primary),
),
// TODO: Need to add this feature in the db
CupertinoSwitch(
value: countNetWorth,
onChanged: (value) =>
Expand Down Expand Up @@ -393,12 +381,12 @@ class _AddAccountState extends ConsumerState<AddAccount> with Functions {
if (selectedAccount != null) {
ref
.read(accountsProvider.notifier)
.updateAccount(selectedAccount)
.updateAccount(nameController.text)
.whenComplete(() => Navigator.of(context).pop());
} else {
ref
.read(accountsProvider.notifier)
.addAccount()
.addAccount(nameController.text, startingValueController.text.isEmpty ? null : currencyToNum(startingValueController.text))
.whenComplete(() => Navigator.of(context).pop());
}
},
Expand Down
13 changes: 4 additions & 9 deletions lib/pages/categories/add_category.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ class _AddCategoryState extends ConsumerState<AddCategory> with Functions {

@override
void initState() {
nameController.text = ref.read(categoryNameProvider) ?? '';
nameController.text = ref.read(selectedCategoryProvider)?.name ?? '';
super.initState();
}

@override
void dispose() {
ref.invalidate(selectedCategoryProvider);
ref.invalidate(categoryNameProvider);
ref.invalidate(categoryIconProvider);
ref.invalidate(categoryColorProvider);
nameController.dispose();
super.dispose();
}

Expand All @@ -38,7 +35,6 @@ class _AddCategoryState extends ConsumerState<AddCategory> with Functions {
final categoryIcon = ref.watch(categoryIconProvider);
final categoryColor = ref.watch(categoryColorProvider);
final showCategoryIcons = ref.watch(showCategoryIconsProvider);
ref.listen(categoryNameProvider, (_, __) {});
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.background,
appBar: AppBar(title: Text("${selectedCategory == null ? "New" : "Edit"} Category")),
Expand Down Expand Up @@ -74,7 +70,6 @@ class _AddCategoryState extends ConsumerState<AddCategory> with Functions {
contentPadding: const EdgeInsets.all(0),
),
style: Theme.of(context).textTheme.titleLarge!.copyWith(color: grey1),
onChanged: (value) => ref.read(categoryNameProvider.notifier).state = value,
)
],
),
Expand Down Expand Up @@ -305,12 +300,12 @@ class _AddCategoryState extends ConsumerState<AddCategory> with Functions {
if (selectedCategory != null) {
ref
.read(categoriesProvider.notifier)
.updateCategory(selectedCategory)
.updateCategory(nameController.text)
.whenComplete(() => Navigator.of(context).pop());
} else {
ref
.read(categoriesProvider.notifier)
.addCategory()
.addCategory(nameController.text)
.whenComplete(() => Navigator.of(context).pop());
}
},
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/categories/category_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class _CategoryListState extends ConsumerState<CategoryList> with Functions {
),
actions: [
IconButton(
onPressed: () => Navigator.of(context).pushNamed('/add-category'),
onPressed: () {
ref.read(categoriesProvider.notifier).reset();
Navigator.of(context).pushNamed('/add-category');
},
icon: const Icon(Icons.add_circle),
splashRadius: 28,
),
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ class _HomePageState extends ConsumerState<HomePage> with Functions {
),
maxLines: 2,
),
onPressed: () => Navigator.of(context).pushNamed('/add-account'),
onPressed: () {
ref.read(accountsProvider.notifier).reset();
Navigator.of(context).pushNamed('/add-account');
},
),
),
);
Expand Down
40 changes: 22 additions & 18 deletions lib/providers/accounts_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import '../model/bank_account.dart';
final mainAccountProvider = StateProvider<BankAccount?>((ref) => null);

final selectedAccountProvider = StateProvider<BankAccount?>((ref) => null);
final accountNameProvider = StateProvider<String?>((ref) => null);
final accountIconProvider = StateProvider<String>((ref) => accountIconList.keys.first);
final accountColorProvider = StateProvider<int>((ref) => 0);
final accountStartingValueProvider = StateProvider<num?>((ref) => null);
final accountMainSwitchProvider = StateProvider<bool>((ref) => false);
final countNetWorthSwitchProvider = StateProvider<bool>((ref) => true);

Expand All @@ -20,57 +18,55 @@ class AsyncAccountsNotifier extends AsyncNotifier<List<BankAccount>> {
}

Future<List<BankAccount>> _getAccounts() async {
final account = await BankAccountMethods().selectAll();
return account;
final accounts = await BankAccountMethods().selectAll();
return accounts;
}

Future<BankAccount?> _getMainAccount() async {
final account = await BankAccountMethods().selectMain();
return account;
}

Future<void> addAccount() async {
state = const AsyncValue.loading();

Future<void> addAccount(String name, num? startingValue) async {
BankAccount account = BankAccount(
name: ref.read(accountNameProvider)!,
name: name,
symbol: ref.read(accountIconProvider),
color: ref.read(accountColorProvider),
startingValue: ref.read(accountStartingValueProvider) ?? 0,
active: true,
startingValue: startingValue ?? 0,
active: ref.read(countNetWorthSwitchProvider),
mainAccount: ref.read(accountMainSwitchProvider),
);

state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
await BankAccountMethods().insert(account);
return _getAccounts();
});
}

Future<void> updateAccount(BankAccount account) async {
BankAccount editAccount = account.copy(
name: ref.read(accountNameProvider)!,
Future<void> updateAccount(String name) async {
BankAccount account = ref.read(selectedAccountProvider)!.copy(
name: name,
symbol: ref.read(accountIconProvider),
color: ref.read(accountColorProvider),
active: ref.read(countNetWorthSwitchProvider),
mainAccount: ref.read(accountMainSwitchProvider),
);

state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
await BankAccountMethods().updateItem(editAccount);
if(editAccount.mainAccount) {
ref.read(mainAccountProvider.notifier).state = editAccount;
await BankAccountMethods().updateItem(account);
if(account.mainAccount) {
ref.read(mainAccountProvider.notifier).state = account;
}
return _getAccounts();
});
}

Future<void> selectedAccount(BankAccount account) async {
ref.read(selectedAccountProvider.notifier).state = account;
ref.read(accountNameProvider.notifier).state = account.name;
ref.read(accountIconProvider.notifier).state = account.symbol;
ref.read(accountColorProvider.notifier).state = account.color;
ref.read(accountStartingValueProvider.notifier).state = account.startingValue;
ref.read(accountMainSwitchProvider.notifier).state = account.mainAccount;
}

Expand All @@ -81,6 +77,14 @@ class AsyncAccountsNotifier extends AsyncNotifier<List<BankAccount>> {
return _getAccounts();
});
}

void reset() {
ref.invalidate(selectedAccountProvider);
ref.invalidate(accountIconProvider);
ref.invalidate(accountColorProvider);
ref.invalidate(accountMainSwitchProvider);
ref.invalidate(countNetWorthSwitchProvider);
}
}

final accountsProvider = AsyncNotifierProvider<AsyncAccountsNotifier, List<BankAccount>>(() {
Expand Down
23 changes: 16 additions & 7 deletions lib/providers/categories_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import '../constants/constants.dart';
import '../model/category_transaction.dart';

final selectedCategoryProvider = StateProvider<CategoryTransaction?>((ref) => null);
final categoryNameProvider = StateProvider<String?>((ref) => null);
final categoryIconProvider = StateProvider<String>((ref) => iconList.keys.first);
final categoryColorProvider = StateProvider<int>((ref) => 0);

Expand All @@ -19,22 +18,27 @@ class AsyncCategoriesNotifier extends AsyncNotifier<List<CategoryTransaction>> {
return categories;
}

Future<void> addCategory() async {
state = const AsyncValue.loading();

Future<void> addCategory(String name) async {
CategoryTransaction category = CategoryTransaction(
name: ref.read(categoryNameProvider)!,
name: name,
symbol: ref.read(categoryIconProvider),
color: ref.read(categoryColorProvider),
);

state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
await CategoryTransactionMethods().insert(category);
return _getCategories();
});
}

Future<void> updateCategory(CategoryTransaction category) async {
Future<void> updateCategory(String name) async {
CategoryTransaction category = ref.read(selectedCategoryProvider)!.copy(
name: name,
symbol: ref.read(categoryIconProvider),
color: ref.read(categoryColorProvider),
);

state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
await CategoryTransactionMethods().updateItem(category);
Expand All @@ -44,7 +48,6 @@ class AsyncCategoriesNotifier extends AsyncNotifier<List<CategoryTransaction>> {

void selectedCategory(CategoryTransaction category) {
ref.read(selectedCategoryProvider.notifier).state = category;
ref.read(categoryNameProvider.notifier).state = category.name;
ref.read(categoryIconProvider.notifier).state = category.symbol;
ref.read(categoryColorProvider.notifier).state = category.color;
}
Expand All @@ -56,6 +59,12 @@ class AsyncCategoriesNotifier extends AsyncNotifier<List<CategoryTransaction>> {
return _getCategories();
});
}

void reset() {
ref.invalidate(selectedCategoryProvider);
ref.invalidate(categoryIconProvider);
ref.invalidate(categoryColorProvider);
}
}

final categoriesProvider = AsyncNotifierProvider<AsyncCategoriesNotifier, List<CategoryTransaction>>(() {
Expand Down

0 comments on commit 1a4af39

Please sign in to comment.