Feat: Macht die Kontoliste und Versionsanzeige funktional
This commit is contained in:
@@ -49,48 +49,152 @@ class AccountController {
|
||||
}
|
||||
|
||||
static final AccountController _instance = AccountController._internal();
|
||||
BuildContext? _buildContext;
|
||||
final AccountRepository _accountRepository = AccountRepository();
|
||||
|
||||
DynamicDialog? _newAccountDialog;
|
||||
DynamicDialog? _errorNameEmptyDialog;
|
||||
DynamicDialog? _accountCreatedDialog;
|
||||
|
||||
Account? _selected;
|
||||
final ValueNotifier<Account?> _selected = ValueNotifier<Account?>(null);
|
||||
|
||||
/// Stellt das ausgewählte Konto dar, das angezeigt wird
|
||||
Future<Account?> get selected async => _selected ??= (await getAccounts())[0];
|
||||
ValueNotifier<Account?> get selected {
|
||||
if (_selected.value == null) {
|
||||
unawaited(
|
||||
updateAccounts().then((_) {
|
||||
_selected.value = accounts.value.firstOrNull;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return _selected;
|
||||
}
|
||||
|
||||
set selected(final Account selected) {
|
||||
_selected = selected;
|
||||
_selected.value = selected;
|
||||
}
|
||||
|
||||
final ValueNotifier<List<Account>> _accounts = ValueNotifier<List<Account>>(
|
||||
[],
|
||||
);
|
||||
|
||||
/// Stellt die Liste der Konten dar
|
||||
ValueNotifier<List<Account>> get accounts {
|
||||
if (_accounts.value == []) {
|
||||
unawaited(updateAccounts());
|
||||
}
|
||||
|
||||
return _accounts;
|
||||
}
|
||||
|
||||
set accounts(final List<Account> accounts) {
|
||||
_accounts.value = accounts;
|
||||
}
|
||||
|
||||
/// Gibt die gespeicherten Konten als Liste zurück
|
||||
Future<List<Account>> getAccounts() async {
|
||||
Future<void> updateAccounts() async {
|
||||
final List<Account> accounts = await _accountRepository.findBy(
|
||||
orderBy: 'nameAsc',
|
||||
);
|
||||
|
||||
return accounts;
|
||||
_accounts.value = accounts;
|
||||
}
|
||||
|
||||
/// Startet den Prozess um ein neues Konto anzulegen
|
||||
void newAccountHandler(final BuildContext buildContext) {
|
||||
_buildContext = buildContext;
|
||||
|
||||
unawaited(_newAccountDialog?.show(buildContext));
|
||||
/// Startet den Prozess, um ein neues Konto anzulegen
|
||||
void newAccountHandler() {
|
||||
unawaited(_newAccountDialog?.show());
|
||||
}
|
||||
|
||||
Future<void> _saveNewAccount(final Map<String, String> values) async {
|
||||
if (values['name'] == null || values['name']!.isEmpty) {
|
||||
if (_buildContext != null) {
|
||||
await _errorNameEmptyDialog?.show(_buildContext!);
|
||||
}
|
||||
/// Startet den Prozess, um ein Konto umzubenennen
|
||||
Future<void> renameAccountHandler(final int accountId) async {
|
||||
final Account? account = await _accountRepository.find(accountId);
|
||||
|
||||
if (account != null) {
|
||||
final renameAccountDialog = DynamicDialog(
|
||||
title: '${account.name} umbenennen',
|
||||
icon: Icons.edit,
|
||||
inputFields: [
|
||||
DialogInputField(
|
||||
id: 'name',
|
||||
label: 'Name',
|
||||
initialValue: account.name,
|
||||
autoFocus: true,
|
||||
),
|
||||
],
|
||||
actions: [
|
||||
DialogAction(label: 'Abbruch'),
|
||||
DialogAction(
|
||||
label: 'Speichern',
|
||||
isPrimary: true,
|
||||
onPressed: _renameAccount,
|
||||
),
|
||||
],
|
||||
hiddenValues: {'account': account},
|
||||
);
|
||||
unawaited(renameAccountDialog.show());
|
||||
}
|
||||
}
|
||||
|
||||
/// Startet den Prozess, um ein Konto zu löschen
|
||||
Future<void> deleteAccountHandler(final int accountId) async {
|
||||
final Account? account = await _accountRepository.find(accountId);
|
||||
|
||||
if (account != null) {
|
||||
final deleteAccountDialog = DynamicDialog(
|
||||
dialogType: DialogTypeEnum.error,
|
||||
title: '${account.name} löschen',
|
||||
content: Text('Willst du ${account.name} wirklich löschen?'),
|
||||
icon: Icons.delete_forever,
|
||||
actions: [
|
||||
DialogAction(label: 'Abbruch', isPrimary: true),
|
||||
DialogAction(label: 'Account löschen', onPressed: _deleteAccount),
|
||||
],
|
||||
hiddenValues: {'account': account},
|
||||
);
|
||||
unawaited(deleteAccountDialog.show());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _saveNewAccount(final Map<String, dynamic> values) async {
|
||||
if (values['name'] == null || values['name'] == '') {
|
||||
await _errorNameEmptyDialog?.show();
|
||||
} else {
|
||||
final account = AccountsCompanion(name: Value(values['name']!));
|
||||
|
||||
await _accountRepository.add(account);
|
||||
await _accountCreatedDialog?.show(_buildContext!);
|
||||
await _accountCreatedDialog?.show();
|
||||
|
||||
await updateAccounts();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _renameAccount(final Map<String, dynamic> values) async {
|
||||
if (values['account'] != null && values['name'] != null) {
|
||||
final Account account = values['account'];
|
||||
final acc = AccountsCompanion(
|
||||
id: Value(account.id),
|
||||
name: Value(values['name']),
|
||||
);
|
||||
|
||||
await _accountRepository.update(acc);
|
||||
await updateAccounts();
|
||||
|
||||
if (account == selected.value) {
|
||||
selected.value = await _accountRepository.find(account.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _deleteAccount(final Map<String, dynamic> values) async {
|
||||
if (values['account'] != null) {
|
||||
final Account account = values['account'];
|
||||
await _accountRepository.remove(account);
|
||||
|
||||
await updateAccounts();
|
||||
|
||||
if (account == _selected.value) {
|
||||
_selected.value = _accounts.value.firstOrNull;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user