From ba78685bfdef811bbd6a7b668c868951efa30443 Mon Sep 17 00:00:00 2001
From: "Alexander \"PapaTutuWawa" <papatutuwawa@polynom.me>
Date: Mon, 12 May 2025 22:22:01 +0200
Subject: [PATCH] Fix adding a new beneficiary in the loan screen

---
 lib/ui/pages/loans/add_loan.dart | 47 ++++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 3 deletions(-)

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<AddLoanPopup> {
           children: [
             OutlinedButton(
               onPressed: () async {
-                if (_beneficiaryTextController.text.isEmpty) {
+                final beneficiaryName = _beneficiaryTextController.text;
+                if (_selectedBeneficiary == null && beneficiaryName.isEmpty) {
                   return;
                 }
 
-                final bloc = GetIt.I.get<CoreCubit>();
+                Beneficiary? beneficiary = _selectedBeneficiary?.item;
+                if (beneficiary == null ||
+                    getBeneficiaryName(beneficiary) != beneficiaryName) {
+                  // Add a new beneficiary, if none was selected
+                  final result = await showDialog<bool>(
+                    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();
               },