From 7fa0b900d612fc689d4346f39080f6490967f697 Mon Sep 17 00:00:00 2001
From: vjrj <vjrj@comunes.org>
Date: Sun, 2 Apr 2023 13:54:34 +0200
Subject: [PATCH] Allow edit contacts

---
 .../first_screen/pay_contact_search_page.dart |  4 +-
 lib/ui/widgets/third_screen/contact_form.dart | 86 +++++++++++++++++++
 2 files changed, 88 insertions(+), 2 deletions(-)
 create mode 100644 lib/ui/widgets/third_screen/contact_form.dart

diff --git a/lib/ui/widgets/first_screen/pay_contact_search_page.dart b/lib/ui/widgets/first_screen/pay_contact_search_page.dart
index 3e5f9d37..f285bad3 100644
--- a/lib/ui/widgets/first_screen/pay_contact_search_page.dart
+++ b/lib/ui/widgets/first_screen/pay_contact_search_page.dart
@@ -223,11 +223,11 @@ class _PayContactSearchPageState extends State<PayContactSearchPage> {
       contact,
       index,
       context,
-      () {
+      onTap: () {
         context.read<PaymentCubit>().selectUser(contact);
         Navigator.pop(context);
       },
-      BlocBuilder<ContactsCubit, ContactsState>(
+      trailing: BlocBuilder<ContactsCubit, ContactsState>(
           builder: (BuildContext context, ContactsState state) {
         final ContactsCubit contactsCubit = context.read<ContactsCubit>();
         final bool isFavorite = contactsCubit.isContact(contact.pubKey);
diff --git a/lib/ui/widgets/third_screen/contact_form.dart b/lib/ui/widgets/third_screen/contact_form.dart
new file mode 100644
index 00000000..515a4567
--- /dev/null
+++ b/lib/ui/widgets/third_screen/contact_form.dart
@@ -0,0 +1,86 @@
+import 'package:easy_localization/easy_localization.dart';
+import 'package:flutter/material.dart';
+
+import '../../../data/models/contact.dart';
+import '../../ui_helpers.dart';
+
+class ContactEditDialog extends StatefulWidget {
+  const ContactEditDialog(
+      {super.key, required this.contact, required this.onSave});
+
+  final Contact contact;
+  final Function(Contact) onSave;
+
+  @override
+  State<ContactEditDialog> createState() => _ContactEditDialogState();
+}
+
+class _ContactEditDialogState extends State<ContactEditDialog> {
+  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
+  late Contact _updatedContact;
+
+  @override
+  void initState() {
+    super.initState();
+    _updatedContact = widget.contact;
+  }
+
+  @override
+  Widget build(BuildContext context) {
+    return AlertDialog(
+      title: Text(tr('form_contact_title')),
+      content: Form(
+        key: _formKey,
+        child: Column(
+          mainAxisSize: MainAxisSize.min,
+          children: <Widget>[
+            TextFormField(
+              initialValue: humanizePubKey(_updatedContact.pubKey),
+              decoration: InputDecoration(
+                labelText: tr('form_contact_pub_key'),
+              ),
+              enabled: false,
+            ),
+            TextFormField(
+              initialValue: _updatedContact.name,
+              decoration: const InputDecoration(labelText: 'Name'),
+              validator: (String? value) {
+                if (value == null || value.isEmpty) {
+                  return tr('form_contact_name_validation');
+                }
+                return null;
+              },
+              onChanged: (String? value) {
+                _updatedContact = _updatedContact.copyWith(name: value);
+              },
+            ),
+            TextFormField(
+              initialValue: _updatedContact.notes,
+              decoration: InputDecoration(labelText: tr('form_contact_notes')),
+              onChanged: (String? value) {
+                _updatedContact = _updatedContact.copyWith(notes: value);
+              },
+            ),
+          ],
+        ),
+      ),
+      actions: <Widget>[
+        TextButton(
+          onPressed: () {
+            Navigator.of(context).pop();
+          },
+          child: Text(tr('cancel')),
+        ),
+        TextButton(
+          onPressed: () {
+            if (_formKey.currentState!.validate()) {
+              widget.onSave(_updatedContact);
+              Navigator.of(context).pop();
+            }
+          },
+          child: Text(tr('form_save')),
+        ),
+      ],
+    );
+  }
+}
-- 
GitLab