Skip to content

Commit

Permalink
add edit/delete wallet option
Browse files Browse the repository at this point in the history
add editWalletDetails widget
add delete method
  • Loading branch information
parodyBit committed Feb 13, 2024
1 parent 8a39562 commit ef93b99
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 2 deletions.
125 changes: 125 additions & 0 deletions lib/screens/preferences/edit_wallet.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
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;
String? defaultWalletName;
bool editName = false;

void initState() {
super.initState();
_nameController = TextEditingController();
_nameController.value = TextEditingValue(
text: Locator.instance
.get<ApiDatabase>()
.walletStorage
.currentWallet
.name);
_walletName = _nameController.value.text;
defaultWalletName =
"wallet-${Locator.instance.get<ApiDatabase>().walletStorage.wallets.length + 1}";
}

Widget buildNameInputOrText(theme) {
IconButton editNameBtn = IconButton(
onPressed: () => {setState(() => editName = !editName)},
icon: Icon(FontAwesomeIcons.pen));

if (!editName) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(Locator.instance
.get<ApiDatabase>()
.walletStorage
.currentWallet
.name),
editNameBtn,
],
);
}

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);
print(_wallet.jsonMap());
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));
}
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;
});
},
),
),
editNameBtn,
]);
}

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text("Edit Wallet Name",
style: theme.textTheme.titleMedium,
),
SizedBox(height: 8),
buildNameInputOrText(theme),
SizedBox(height: 16),
],
);
}
}
39 changes: 38 additions & 1 deletion lib/screens/preferences/wallet_config.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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/shared/api_database.dart';
import 'package:my_wit_wallet/util/get_localization.dart';
import 'package:my_wit_wallet/widgets/PaddedButton.dart';
import 'package:my_wit_wallet/widgets/custom_divider.dart';

import 'package:my_wit_wallet/bloc/crypto/crypto_bloc.dart';
import 'package:my_wit_wallet/bloc/explorer/explorer_bloc.dart';
import 'package:my_wit_wallet/shared/locator.dart';
import 'package:my_wit_wallet/screens/dashboard/bloc/dashboard_bloc.dart';
import 'package:my_wit_wallet/screens/login/bloc/login_bloc.dart';

enum WalletConfigActions { exportXprv, signMsg }

class WalletConfig extends StatefulWidget {
Expand All @@ -20,7 +29,7 @@ class WalletConfig extends StatefulWidget {

class WalletConfigState extends State<WalletConfig> {
WalletConfigActions? currentSetting;

ApiDatabase db = Locator.instance.get<ApiDatabase>();
@override
void initState() {
super.initState();
Expand Down Expand Up @@ -59,6 +68,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 All @@ -85,9 +95,36 @@ class WalletConfigState extends State<WalletConfig> {
enabled: true,
onPressed: () => _toggleSetting(WalletConfigActions.signMsg)),
SizedBox(height: 16),
PaddedButton(
padding: EdgeInsets.only(bottom: 16, top: 16),
text: "Delete Wallet",
type: ButtonType.primary,
enabled: true,
onPressed: () {
int walletCount = db.walletStorage.wallets.length;
db.deleteWallet(db.walletStorage.currentWallet);
if(walletCount == 1){
_logOut();
} else {
db.loadWalletsDatabase();
db.walletStorage.setCurrentWallet(db.walletStorage.wallets.values.first.id);
}
BlocProvider.of<DashboardBloc>(context).add(
DashboardUpdateWalletEvent(
currentWallet: db.walletStorage.currentWallet,
currentAddress: Locator.instance.get<ApiDatabase>().walletStorage.currentAccount.address));
}),
]));
}

void _logOut() {
BlocProvider.of<ExplorerBloc>(context)
.add(CancelSyncWalletEvent(ExplorerStatus.unknown));
BlocProvider.of<DashboardBloc>(context).add(DashboardResetEvent());
BlocProvider.of<CryptoBloc>(context).add(CryptoReadyEvent());
Navigator.of(context).popUntil((route) => route.isFirst);
BlocProvider.of<LoginBloc>(context).add(LoginLogoutEvent());
}
@override
Widget build(BuildContext context) {
return currentSetting == null
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

0 comments on commit ef93b99

Please sign in to comment.