From 4593f7a0dccbc84eafa42d7cd2427e2ea2f4ddc3 Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 5 Dec 2022 16:36:28 -0600 Subject: [PATCH] fix desktop wallet name field --- .../wallet_view/desktop_wallet_view.dart | 92 ++++-------- lib/widgets/hover_text_field.dart | 132 ++++++++++-------- 2 files changed, 107 insertions(+), 117 deletions(-) diff --git a/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart b/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart index c28b81bf0..7e57beac9 100644 --- a/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart +++ b/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart @@ -59,7 +59,6 @@ class DesktopWalletView extends ConsumerStatefulWidget { class _DesktopWalletViewState extends ConsumerState { late final TextEditingController controller; - late final String walletId; late final EventBus eventBus; late final bool _shouldDisableAutoSyncOnLogOut; @@ -74,8 +73,9 @@ class _DesktopWalletViewState extends ConsumerState { } Future _logout() async { - final managerProvider = - ref.read(walletsChangeNotifierProvider).getManagerProvider(walletId); + final managerProvider = ref + .read(walletsChangeNotifierProvider) + .getManagerProvider(widget.walletId); if (_shouldDisableAutoSyncOnLogOut) { // disable auto sync if it was enabled only when loading wallet ref.read(managerProvider).shouldAutoSync = false; @@ -95,7 +95,7 @@ class _DesktopWalletViewState extends ConsumerState { _cnLoadingService.loadAll(ref, coin: ref .read(walletsChangeNotifierProvider) - .getManager(walletId) + .getManager(widget.walletId) .coin); } else { Logging.instance.log("User does not want to use external calls", @@ -104,8 +104,9 @@ class _DesktopWalletViewState extends ConsumerState { } void _onExchangePressed(BuildContext context) async { - final managerProvider = - ref.read(walletsChangeNotifierProvider).getManagerProvider(walletId); + final managerProvider = ref + .read(walletsChangeNotifierProvider) + .getManagerProvider(widget.walletId); unawaited(_cnLoadingService.loadAll(ref)); final coin = ref.read(managerProvider).coin; @@ -127,7 +128,6 @@ class _DesktopWalletViewState extends ConsumerState { } else { ref.read(currentExchangeNameStateProvider.state).state = ChangeNowExchange.exchangeName; - final walletId = ref.read(managerProvider).walletId; ref.read(prefsChangeNotifierProvider).exchangeRateType = ExchangeRateType.estimated; @@ -160,7 +160,7 @@ class _DesktopWalletViewState extends ConsumerState { Navigator.of(context).pushNamed( WalletInitiatedExchangeView.routeName, arguments: Tuple3( - walletId, + widget.walletId, coin, _loadCNData, ), @@ -171,8 +171,9 @@ class _DesktopWalletViewState extends ConsumerState { } Future attemptAnonymize() async { - final managerProvider = - ref.read(walletsChangeNotifierProvider).getManagerProvider(walletId); + final managerProvider = ref + .read(walletsChangeNotifierProvider) + .getManagerProvider(widget.walletId); bool shouldPop = false; unawaited( @@ -283,9 +284,9 @@ class _DesktopWalletViewState extends ConsumerState { @override void initState() { controller = TextEditingController(); - walletId = widget.walletId; - final managerProvider = - ref.read(walletsChangeNotifierProvider).getManagerProvider(walletId); + final managerProvider = ref + .read(walletsChangeNotifierProvider) + .getManagerProvider(widget.walletId); controller.text = ref.read(managerProvider).walletName; @@ -306,13 +307,19 @@ class _DesktopWalletViewState extends ConsumerState { super.initState(); } + @override + void dispose() { + controller.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { final manager = ref.watch(walletsChangeNotifierProvider - .select((value) => value.getManager(walletId))); + .select((value) => value.getManager(widget.walletId))); final coin = manager.coin; final managerProvider = ref.watch(walletsChangeNotifierProvider - .select((value) => value.getManagerProvider(walletId))); + .select((value) => value.getManagerProvider(widget.walletId))); return DesktopScaffold( appBar: DesktopAppBar( @@ -355,47 +362,8 @@ class _DesktopWalletViewState extends ConsumerState { minWidth: 48, ), child: IntrinsicWidth( - child: HoverTextField( - controller: controller, - style: STextStyles.desktopH3(context), - readOnly: true, - onDone: () async { - final currentWalletName = - ref.read(managerProvider).walletName; - final newName = controller.text; - if (newName != currentWalletName) { - final success = await ref - .read(walletsServiceChangeNotifierProvider) - .renameWallet( - from: currentWalletName, - to: newName, - shouldNotifyListeners: true, - ); - if (success) { - ref - .read(walletsChangeNotifierProvider) - .getManager(walletId) - .walletName = newName; - unawaited( - showFloatingFlushBar( - type: FlushBarType.success, - message: "Wallet renamed", - context: context, - ), - ); - } else { - unawaited( - showFloatingFlushBar( - type: FlushBarType.warning, - message: - "Wallet named \"$newName\" already exists", - context: context, - ), - ); - controller.text = currentWalletName; - } - } - }, + child: DesktopWalletNameField( + walletId: widget.walletId, ), ), ), @@ -403,20 +371,20 @@ class _DesktopWalletViewState extends ConsumerState { Row( children: [ NetworkInfoButton( - walletId: walletId, + walletId: widget.walletId, eventBus: eventBus, ), const SizedBox( width: 2, ), WalletKeysButton( - walletId: walletId, + walletId: widget.walletId, ), const SizedBox( width: 2, ), DeleteWalletButton( - walletId: walletId, + walletId: widget.walletId, ), const SizedBox( width: 12, @@ -446,7 +414,7 @@ class _DesktopWalletViewState extends ConsumerState { width: 10, ), DesktopWalletSummary( - walletId: walletId, + walletId: widget.walletId, managerProvider: managerProvider, initialSyncStatus: ref.watch(managerProvider .select((value) => value.isRefreshing)) @@ -556,7 +524,7 @@ class _DesktopWalletViewState extends ConsumerState { SizedBox( width: 450, child: MyWallet( - walletId: walletId, + walletId: widget.walletId, ), ), const SizedBox( @@ -564,7 +532,7 @@ class _DesktopWalletViewState extends ConsumerState { ), Expanded( child: RecentDesktopTransactions( - walletId: walletId, + walletId: widget.walletId, ), ), ], diff --git a/lib/widgets/hover_text_field.dart b/lib/widgets/hover_text_field.dart index 475d6c2ec..51d35aaa7 100644 --- a/lib/widgets/hover_text_field.dart +++ b/lib/widgets/hover_text_field.dart @@ -1,46 +1,31 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:stackwallet/notifications/show_flush_bar.dart'; +import 'package:stackwallet/providers/global/wallets_provider.dart'; +import 'package:stackwallet/providers/global/wallets_service_provider.dart'; import 'package:stackwallet/utilities/constants.dart'; +import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/util.dart'; -class HoverTextField extends StatefulWidget { - const HoverTextField({ +class DesktopWalletNameField extends ConsumerStatefulWidget { + const DesktopWalletNameField({ Key? key, - this.controller, - this.focusNode, - this.readOnly = false, - this.enabled, - this.onTap, - this.onChanged, - this.onEditingComplete, - this.style, - this.onDone, + required this.walletId, }) : super(key: key); - final TextEditingController? controller; - final FocusNode? focusNode; - final bool readOnly; - final bool? enabled; - final GestureTapCallback? onTap; - final ValueChanged? onChanged; - final VoidCallback? onEditingComplete; - final TextStyle? style; - final VoidCallback? onDone; + final String walletId; @override - State createState() => _HoverTextFieldState(); + ConsumerState createState() => _HoverTextFieldState(); } -class _HoverTextFieldState extends State { - late final TextEditingController? controller; - late final FocusNode? focusNode; - late bool readOnly; - late bool? enabled; - late final GestureTapCallback? onTap; - late final ValueChanged? onChanged; - late final VoidCallback? onEditingComplete; - late final TextStyle? style; - late final VoidCallback? onDone; +class _HoverTextFieldState extends ConsumerState { + late final TextEditingController controller; + late final FocusNode focusNode; + bool readOnly = true; final InputBorder inputBorder = OutlineInputBorder( borderSide: const BorderSide( @@ -50,33 +35,74 @@ class _HoverTextFieldState extends State { borderRadius: BorderRadius.circular(Constants.size.circularBorderRadius), ); + Future onDone() async { + final currentWalletName = ref + .read(walletsChangeNotifierProvider) + .getManager(widget.walletId) + .walletName; + final newName = controller.text; + if (newName != currentWalletName) { + final success = + await ref.read(walletsServiceChangeNotifierProvider).renameWallet( + from: currentWalletName, + to: newName, + shouldNotifyListeners: true, + ); + if (success) { + ref + .read(walletsChangeNotifierProvider) + .getManager(widget.walletId) + .walletName = newName; + unawaited( + showFloatingFlushBar( + type: FlushBarType.success, + message: "Wallet renamed", + context: context, + ), + ); + } else { + unawaited( + showFloatingFlushBar( + type: FlushBarType.warning, + message: "Wallet named \"$newName\" already exists", + context: context, + ), + ); + controller.text = currentWalletName; + } + } + } + + void listenerFunc() { + if (!focusNode.hasPrimaryFocus && !readOnly) { + setState(() { + readOnly = true; + }); + onDone.call(); + } + } + @override void initState() { - controller = widget.controller; - focusNode = widget.focusNode ?? FocusNode(); - readOnly = widget.readOnly; - enabled = widget.enabled; - onChanged = widget.onChanged; - style = widget.style; - onTap = widget.onTap; - onEditingComplete = widget.onEditingComplete; - onDone = widget.onDone; + controller = TextEditingController(); + focusNode = FocusNode(); - focusNode!.addListener(() { - if (!focusNode!.hasPrimaryFocus && !readOnly) { - setState(() { - readOnly = true; - }); - onDone?.call(); - } + focusNode.addListener(listenerFunc); + + WidgetsBinding.instance.addPostFrameCallback((_) { + controller.text = ref + .read(walletsChangeNotifierProvider) + .getManager(widget.walletId) + .walletName; }); + super.initState(); } @override void dispose() { - controller?.dispose(); - focusNode?.dispose(); + controller.dispose(); + focusNode.removeListener(listenerFunc); super.dispose(); } @@ -88,22 +114,18 @@ class _HoverTextFieldState extends State { controller: controller, focusNode: focusNode, readOnly: readOnly, - enabled: enabled, onTap: () { setState(() { readOnly = false; }); - onTap?.call(); }, - onChanged: onChanged, onEditingComplete: () { setState(() { readOnly = true; }); - onEditingComplete?.call(); - onDone?.call(); + onDone.call(); }, - style: style, + style: STextStyles.desktopH3(context), decoration: InputDecoration( contentPadding: const EdgeInsets.symmetric( vertical: 4,