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!); } } }