diff --git a/lib/ui/pages/loans/add_loan.dart b/lib/ui/pages/loans/add_loan.dart index 5e85f34..e54b00f 100644 --- a/lib/ui/pages/loans/add_loan.dart +++ b/lib/ui/pages/loans/add_loan.dart @@ -74,13 +74,54 @@ class AddBudgetState extends State { children: [ OutlinedButton( onPressed: () async { - if (_beneficiaryTextController.text.isEmpty) { + final beneficiaryName = _beneficiaryTextController.text; + if (_selectedBeneficiary == null && beneficiaryName.isEmpty) { return; } - final bloc = GetIt.I.get(); + Beneficiary? beneficiary = _selectedBeneficiary?.item; + if (beneficiary == null || + getBeneficiaryName(beneficiary) != beneficiaryName) { + // Add a new beneficiary, if none was selected + final result = await showDialog( + context: context, + builder: + (context) => AlertDialog( + title: Text(t.common.beneficiary.addBeneficiary.title), + content: Text( + t.common.beneficiary.addBeneficiary.body(name: beneficiaryName), + ), + actions: [ + TextButton( + style: TextButton.styleFrom( + textStyle: Theme.of(context).textTheme.labelLarge, + ), + child: Text(t.modals.add), + onPressed: () => Navigator.of(context).pop(true), + ), + TextButton( + style: TextButton.styleFrom( + textStyle: Theme.of(context).textTheme.labelLarge, + ), + child: Text(t.modals.cancel), + onPressed: () => Navigator.of(context).pop(false), + ), + ], + ), + ); + if (result == null || !result) { + return; + } + + beneficiary = + Beneficiary() + ..name = beneficiaryName + ..type = BeneficiaryType.other; + await upsertBeneficiary(beneficiary); + } + final loan = - Loan()..beneficiary.value = _selectedBeneficiary!.item; + Loan()..beneficiary.value = beneficiary; await upsertLoan(loan); widget.onDone(); },