desktop edit contact address entry

This commit is contained in:
julian 2022-11-17 16:47:50 -06:00
parent 318758f768
commit cd19d776ae
2 changed files with 268 additions and 227 deletions

View file

@ -12,7 +12,11 @@ import 'package:stackwallet/utilities/barcode_scanner_interface.dart';
import 'package:stackwallet/utilities/clipboard_interface.dart'; import 'package:stackwallet/utilities/clipboard_interface.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/conditional_parent.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
import 'package:stackwallet/widgets/desktop/secondary_button.dart';
class EditContactAddressView extends ConsumerStatefulWidget { class EditContactAddressView extends ConsumerStatefulWidget {
const EditContactAddressView({ const EditContactAddressView({
@ -44,6 +48,42 @@ class _EditContactAddressViewState
late final BarcodeScannerInterface barcodeScanner; late final BarcodeScannerInterface barcodeScanner;
late final ClipboardInterface clipboard; late final ClipboardInterface clipboard;
Future<void> save(Contact contact) async {
if (FocusScope.of(context).hasFocus) {
FocusScope.of(context).unfocus();
await Future<void>.delayed(
const Duration(milliseconds: 75),
);
}
List<ContactAddressEntry> entries = contact.addresses.toList();
final entry = entries.firstWhere(
(e) =>
e.label == addressEntry.label &&
e.address == addressEntry.address &&
e.coin == addressEntry.coin,
);
final index = entries.indexOf(entry);
entries.remove(entry);
ContactAddressEntry editedEntry =
ref.read(addressEntryDataProvider(0)).buildAddressEntry();
entries.insert(index, editedEntry);
Contact editedContact = contact.copyWith(addresses: entries);
if (await ref.read(addressBookServiceProvider).editContact(editedContact)) {
if (mounted) {
Navigator.of(context).pop();
}
// TODO show success notification
} else {
// TODO show error notification
}
}
@override @override
void initState() { void initState() {
contactId = widget.contactId; contactId = widget.contactId;
@ -59,7 +99,11 @@ class _EditContactAddressViewState
final contact = ref.watch(addressBookServiceProvider final contact = ref.watch(addressBookServiceProvider
.select((value) => value.getContactById(contactId))); .select((value) => value.getContactById(contactId)));
return Scaffold( final bool isDesktop = Util.isDesktop;
return ConditionalParent(
condition: !isDesktop,
builder: (child) => Scaffold(
backgroundColor: Theme.of(context).extension<StackColors>()!.background, backgroundColor: Theme.of(context).extension<StackColors>()!.background,
appBar: AppBar( appBar: AppBar(
leading: AppBarBackButton( leading: AppBarBackButton(
@ -94,6 +138,15 @@ class _EditContactAddressViewState
child: IntrinsicHeight( child: IntrinsicHeight(
child: Padding( child: Padding(
padding: const EdgeInsets.all(4), padding: const EdgeInsets.all(4),
child: child,
),
),
),
),
);
},
),
),
child: Column( child: Column(
children: [ children: [
Row( Row(
@ -123,6 +176,12 @@ class _EditContactAddressViewState
const SizedBox( const SizedBox(
width: 16, width: 16,
), ),
if (isDesktop)
Text(
contact.name,
style: STextStyles.pageTitleH2(context),
),
if (!isDesktop)
Expanded( Expanded(
child: FittedBox( child: FittedBox(
fit: BoxFit.scaleDown, fit: BoxFit.scaleDown,
@ -145,7 +204,13 @@ class _EditContactAddressViewState
const SizedBox( const SizedBox(
height: 24, height: 24,
), ),
GestureDetector( ConditionalParent(
condition: isDesktop,
builder: (child) => MouseRegion(
cursor: SystemMouseCursors.click,
child: child,
),
child: GestureDetector(
onTap: () async { onTap: () async {
// delete address // delete address
final _addresses = contact.addresses; final _addresses = contact.addresses;
@ -157,8 +222,7 @@ class _EditContactAddressViewState
); );
_addresses.remove(entry); _addresses.remove(entry);
Contact editedContact = Contact editedContact = contact.copyWith(addresses: _addresses);
contact.copyWith(addresses: _addresses);
if (await ref if (await ref
.read(addressBookServiceProvider) .read(addressBookServiceProvider)
.editContact(editedContact)) { .editContact(editedContact)) {
@ -173,6 +237,7 @@ class _EditContactAddressViewState
style: STextStyles.link(context), style: STextStyles.link(context),
), ),
), ),
),
const Spacer(), const Spacer(),
const SizedBox( const SizedBox(
height: 16, height: 16,
@ -180,19 +245,11 @@ class _EditContactAddressViewState
Row( Row(
children: [ children: [
Expanded( Expanded(
child: TextButton( child: SecondaryButton(
style: Theme.of(context) label: "Cancel",
.extension<StackColors>()! buttonHeight: isDesktop ? ButtonHeight.l : null,
.getSecondaryEnabledButtonColor(context),
child: Text(
"Cancel",
style: STextStyles.button(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.accentColorDark),
),
onPressed: () async { onPressed: () async {
if (FocusScope.of(context).hasFocus) { if (!isDesktop && FocusScope.of(context).hasFocus) {
FocusScope.of(context).unfocus(); FocusScope.of(context).unfocus();
await Future<void>.delayed( await Future<void>.delayed(
const Duration(milliseconds: 75)); const Duration(milliseconds: 75));
@ -207,89 +264,17 @@ class _EditContactAddressViewState
width: 16, width: 16,
), ),
Expanded( Expanded(
child: Builder( child: PrimaryButton(
builder: (context) { label: "Save",
bool shouldEnableSave = enabled: ref.watch(validContactStateProvider([0])),
ref.watch(validContactStateProvider([0])); onPressed: () => save(contact),
buttonHeight: isDesktop ? ButtonHeight.l : null,
return TextButton(
style: shouldEnableSave
? Theme.of(context)
.extension<StackColors>()!
.getPrimaryEnabledButtonColor(
context)
: Theme.of(context)
.extension<StackColors>()!
.getPrimaryDisabledButtonColor(
context),
onPressed: shouldEnableSave
? () async {
if (FocusScope.of(context)
.hasFocus) {
FocusScope.of(context).unfocus();
await Future<void>.delayed(
const Duration(
milliseconds: 75),
);
}
List<ContactAddressEntry> entries =
contact.addresses.toList();
final entry = entries.firstWhere(
(e) =>
e.label ==
addressEntry.label &&
e.address ==
addressEntry.address &&
e.coin == addressEntry.coin,
);
final index =
entries.indexOf(entry);
entries.remove(entry);
ContactAddressEntry editedEntry = ref
.read(
addressEntryDataProvider(0))
.buildAddressEntry();
entries.insert(index, editedEntry);
Contact editedContact = contact
.copyWith(addresses: entries);
if (await ref
.read(
addressBookServiceProvider)
.editContact(editedContact)) {
if (mounted) {
Navigator.of(context).pop();
}
// TODO show success notification
} else {
// TODO show error notification
}
}
: null,
child: Text(
"Save",
style: STextStyles.button(context),
),
);
},
), ),
), ),
], ],
), ),
], ],
), ),
),
),
),
),
);
},
),
); );
} }
} }

View file

@ -1,15 +1,20 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:stackwallet/models/contact_address_entry.dart'; import 'package:stackwallet/models/contact_address_entry.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/pages/address_book_views/subviews/edit_contact_address_view.dart';
import 'package:stackwallet/providers/ui/address_book_providers/address_entry_data_provider.dart';
import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/clipboard_interface.dart'; import 'package:stackwallet/utilities/clipboard_interface.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/enums/flush_bar_type.dart'; import 'package:stackwallet/utilities/enums/flush_bar_type.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart'; import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart';
import 'package:stackwallet/widgets/desktop/desktop_dialog.dart';
class DesktopAddressCard extends StatelessWidget { class DesktopAddressCard extends StatelessWidget {
const DesktopAddressCard({ const DesktopAddressCard({
@ -80,9 +85,60 @@ class DesktopAddressCard extends StatelessWidget {
width: 16, width: 16,
), ),
if (contactId != "default") if (contactId != "default")
BlueTextButton( Consumer(
builder: (context, ref, child) {
return BlueTextButton(
text: "Edit", text: "Edit",
onTap: () {}, onTap: () async {
ref.read(addressEntryDataProvider(0)).address =
entry.address;
ref.read(addressEntryDataProvider(0)).addressLabel =
entry.label;
ref.read(addressEntryDataProvider(0)).coin =
entry.coin;
await showDialog<void>(
context: context,
builder: (context) => DesktopDialog(
maxWidth: 580,
maxHeight: 566,
child: Column(
children: [
Row(
children: [
const SizedBox(
width: 8,
),
const AppBarBackButton(
isCompact: true,
),
Text(
"Edit address",
style: STextStyles.desktopH3(context),
),
],
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(
top: 20,
left: 32,
right: 32,
bottom: 32,
),
child: EditContactAddressView(
contactId: contactId,
addressEntry: entry,
),
),
),
],
),
),
);
},
);
},
), ),
], ],
) )