Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add edit wallet option #501

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions lib/screens/preferences/edit_wallet.dart
Original file line number Diff line number Diff line change
@@ -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<StatefulWidget> createState() => EditWalletDetailsState();
}

class EditWalletDetailsState extends State<EditWalletDetails> {
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<ApiDatabase>()
.walletStorage
.currentWallet
.name);
_walletName = _nameController.value.text;
}

void _updateWalletName(String data) {
Wallet _wallet =
Locator.instance.get<ApiDatabase>().walletStorage.currentWallet;
setState(() {
FocusManager.instance.primaryFocus?.unfocus();
_walletName = data;
Locator.instance.get<ApiDatabase>().deleteWallet(_wallet);
_wallet.name = _walletName;
Locator.instance.get<ApiDatabase>().addWallet(_wallet);
Locator.instance
.get<ApiDatabase>()
.walletStorage
.setCurrentWallet(_wallet.id);
editName = false;
});
BlocProvider.of<DashboardBloc>(context).add(DashboardUpdateWalletEvent(
currentWallet: _wallet,
currentAddress: Locator.instance
.get<ApiDatabase>()
.walletStorage
.currentAccount
.address));
}

String get currentWalletName =>
Locator.instance.get<ApiDatabase>().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),
],
);
}
}
2 changes: 2 additions & 0 deletions lib/screens/preferences/wallet_config.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -59,6 +60,7 @@ class WalletConfigState extends State<WalletConfig> {
padding: EdgeInsets.only(left: 8, right: 8),
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
SizedBox(height: 24),
EditWalletDetails(),
Text(
localization.walletConfigHeader,
style: theme.textTheme.titleMedium,
Expand Down
2 changes: 1 addition & 1 deletion lib/util/storage/database/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Wallet {

final WalletType walletType;
late String id;
final String name;
String name;
late List<String?> txHashes;
late String? xprv;
late String? externalXpub;
Expand Down
Loading