diff --git a/lib/Controller/account_controller.dart b/lib/Controller/account_controller.dart new file mode 100644 index 0000000..459b43a --- /dev/null +++ b/lib/Controller/account_controller.dart @@ -0,0 +1,74 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; + +import '../Entities/account.dart'; +import '../Entities/dialog_type_enum.dart'; +import '../Pages/Dialog/dialog_action.dart'; +import '../Pages/Dialog/dialog_input_field.dart'; +import '../Pages/Dialog/dynamic_dialog.dart'; +import '../Repositories/account_repository.dart'; + +/// Steuert die Interaktion mit den Accounts +class AccountController { + /// Erstellt eine neue Instanz dieser Klasse + AccountController() { + _newAccountDialog = DynamicDialog( + title: 'Neues Konto erstellen', + icon: Icons.account_balance_wallet, + inputFields: [ + const DialogInputField(id: 'name', label: 'Name', autoFocus: true), + ], + actions: [ + DialogAction(label: 'Abbruch'), + DialogAction( + label: 'Speichern', + isPrimary: true, + onPressed: _saveNewAccount, + ), + ], + ); + + _errorNameEmptyDialog = DynamicDialog( + title: 'Fehler!', + icon: Icons.error, + content: const Text('Der Name des Kontos darf nicht leer sein!'), + dialogType: DialogTypeEnum.error, + ); + + _accountCreatedDialog = DynamicDialog( + title: 'Account erstellt!', + icon: Icons.check_circle, + content: const Text('Das Konto wurde erfolgreich erstellt!'), + dialogType: DialogTypeEnum.success, + ); + } + + BuildContext? _buildContext; + + final AccountRepository _accountRepository = AccountRepository(); + + DynamicDialog? _newAccountDialog; + DynamicDialog? _errorNameEmptyDialog; + DynamicDialog? _accountCreatedDialog; + + /// Startet den Prozess um ein neues Konto anzulegen + void newAccountHandler(final BuildContext buildContext) { + _buildContext = buildContext; + + unawaited(_newAccountDialog?.show(buildContext)); + } + + Future _saveNewAccount(final Map values) async { + if (values['name'] == null || values['name']!.isEmpty) { + if (_buildContext != null) { + await _errorNameEmptyDialog?.show(_buildContext!); + } + } else { + final account = Account()..name = values['name']!; + + await _accountRepository.add(account); + await _accountCreatedDialog?.show(_buildContext!); + } + } +} diff --git a/lib/Entities/account.dart b/lib/Entities/account.dart index 1dc95d3..8e2ccba 100644 --- a/lib/Entities/account.dart +++ b/lib/Entities/account.dart @@ -10,4 +10,7 @@ class Account { /// Der Name des Kontos String name = ''; + + @override + String toString() => '${super.toString()}($id: $name)'; } diff --git a/lib/Pages/home_page.dart b/lib/Pages/home_page.dart index 62d413c..05bda12 100644 --- a/lib/Pages/home_page.dart +++ b/lib/Pages/home_page.dart @@ -3,6 +3,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_expandable_fab/flutter_expandable_fab.dart'; import 'package:routemaster/routemaster.dart'; +import '../Controller/account_controller.dart'; + /// Eine Seite, die als Container für die verschiedenen Tabs der App dient. /// /// Diese Seite enthält eine App-Bar mit einer Kontoauswahl sowie ein @@ -17,6 +19,8 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { + final AccountController _accountController = AccountController(); + String selected = 'Konto 1'; @override @@ -89,29 +93,42 @@ class _HomePageState extends State { type: ExpandableFabType.up, childrenAnimation: ExpandableFabAnimation.none, distance: 70, - children: const [ - Row( - children: [ - Text('Neue Transaktion'), - SizedBox(width: 20), - FloatingActionButton.small( - heroTag: null, - onPressed: null, - child: Icon(Icons.add), - ), - ], + children: [ + _expandableButton( + label: 'Neue Transaktion', + icon: Icons.add, + onPressed: () {}, ), - Row( - children: [ - Text('Neues Konto'), - SizedBox(width: 20), - FloatingActionButton.small( - heroTag: null, - onPressed: null, - child: Icon(Icons.account_balance_wallet), - ), - ], + _expandableButton( + label: 'Neues Konto', + icon: Icons.account_balance_wallet, + onPressed: () { + _accountController.newAccountHandler(context); + }, ), ], ); + + Widget _expandableButton({ + required final String label, + required final IconData icon, + required final VoidCallback onPressed, + }) => GestureDetector( + onTap: onPressed, + child: Row( + children: [ + Container( + padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), + decoration: BoxDecoration(borderRadius: BorderRadius.circular(8)), + child: Text(label), + ), + const SizedBox(width: 12), + FloatingActionButton.small( + heroTag: null, + onPressed: onPressed, + child: Icon(icon), + ), + ], + ), + ); }