From 999fe5761959aacc2be90979b69e2f7bf4a7a0b1 Mon Sep 17 00:00:00 2001 From: parodyBit Date: Mon, 12 Feb 2024 21:21:20 -0700 Subject: [PATCH] add edit wallet option add editWalletDetails widget --- lib/screens/preferences/edit_wallet.dart | 139 +++++++++++++++++++++ lib/screens/preferences/wallet_config.dart | 2 + lib/util/storage/database/wallet.dart | 2 +- 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 lib/screens/preferences/edit_wallet.dart diff --git a/lib/screens/preferences/edit_wallet.dart b/lib/screens/preferences/edit_wallet.dart new file mode 100644 index 000000000..e289eddc5 --- /dev/null +++ b/lib/screens/preferences/edit_wallet.dart @@ -0,0 +1,139 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:font_awesome_flutter/font_awesome_flutter.dart'; +import 'package:my_wit_wallet/util/get_localization.dart'; + +import 'package:my_wit_wallet/shared/api_database.dart'; +import 'package:my_wit_wallet/shared/locator.dart'; +import 'package:my_wit_wallet/util/storage/database/wallet.dart'; + +import 'package:my_wit_wallet/screens/dashboard/bloc/dashboard_bloc.dart'; + +class EditWalletDetails extends StatefulWidget { + @override + State createState() => EditWalletDetailsState(); +} + +class EditWalletDetailsState extends State { + late TextEditingController _nameController; + final _focusNode = FocusNode(); + String _walletName = ''; + String? errorText; + bool editName = false; + + void initState() { + super.initState(); + _nameController = TextEditingController(); + _nameController.value = TextEditingValue( + text: Locator.instance + .get() + .walletStorage + .currentWallet + .name); + _walletName = _nameController.value.text; + } + + void _updateWalletName(String data) { + Wallet _wallet = + Locator.instance.get().walletStorage.currentWallet; + setState(() { + FocusManager.instance.primaryFocus?.unfocus(); + _walletName = data; + Locator.instance.get().deleteWallet(_wallet); + _wallet.name = _walletName; + Locator.instance.get().addWallet(_wallet); + Locator.instance + .get() + .walletStorage + .setCurrentWallet(_wallet.id); + editName = false; + }); + BlocProvider.of(context).add(DashboardUpdateWalletEvent( + currentWallet: _wallet, + currentAddress: Locator.instance + .get() + .walletStorage + .currentAccount + .address)); + } + + String get currentWalletName => + Locator.instance.get().walletStorage.currentWallet.name; + + Widget buildNameInputOrText(theme) { + IconButton editNameBtn = IconButton( + onPressed: () => {setState(() => editName = !editName)}, + icon: Icon(FontAwesomeIcons.pen)); + IconButton confirmBtn = IconButton( + onPressed: () => { + setState(() { + editName = !editName; + this._updateWalletName(_walletName); + }) + }, + icon: Icon(FontAwesomeIcons.check)); + if (!editName) { + return Row( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + SizedBox(width: 16), + Flexible( + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + currentWalletName, + textAlign: TextAlign.center, + style: theme.textTheme.bodyText1, + ), + editNameBtn, + ], + ), + ) + ], + ); + } else { + return Row( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.spaceBetween, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Flexible( + child: TextField( + autofocus: true, + style: theme.textTheme.bodyText1, + decoration: InputDecoration( + hintText: localization.walletNameHint, + errorText: errorText, + ), + controller: _nameController, + focusNode: _focusNode, + onSubmitted: _updateWalletName, + onChanged: (String value) { + setState(() { + _walletName = value; + }); + }, + ), + ), + SizedBox(width: 8), + confirmBtn, + ]); + } + } + + @override + Widget build(BuildContext context) { + final theme = Theme.of(context); + return Column( + mainAxisSize: MainAxisSize.max, + mainAxisAlignment: MainAxisAlignment.spaceBetween, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + buildNameInputOrText(theme), + SizedBox(height: 16), + ], + ); + } +} diff --git a/lib/screens/preferences/wallet_config.dart b/lib/screens/preferences/wallet_config.dart index eed6dffe2..608130b58 100644 --- a/lib/screens/preferences/wallet_config.dart +++ b/lib/screens/preferences/wallet_config.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:my_wit_wallet/screens/preferences/edit_wallet.dart'; import 'package:my_wit_wallet/screens/preferences/export_xprv.dart'; import 'package:my_wit_wallet/screens/preferences/sign_message.dart'; import 'package:my_wit_wallet/util/get_localization.dart'; @@ -59,6 +60,7 @@ class WalletConfigState extends State { padding: EdgeInsets.only(left: 8, right: 8), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox(height: 24), + EditWalletDetails(), Text( localization.walletConfigHeader, style: theme.textTheme.titleMedium, diff --git a/lib/util/storage/database/wallet.dart b/lib/util/storage/database/wallet.dart index 045ee310a..6ad93a575 100644 --- a/lib/util/storage/database/wallet.dart +++ b/lib/util/storage/database/wallet.dart @@ -52,7 +52,7 @@ class Wallet { final WalletType walletType; late String id; - final String name; + String name; late List txHashes; late String? xprv; late String? externalXpub;