2021-12-23 12:36:09 +01:00
|
|
|
import 'dart:async';
|
2022-06-18 00:48:07 +02:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2021-11-17 06:20:23 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:gecko/globals.dart';
|
|
|
|
import 'package:gecko/models/chest_data.dart';
|
2022-05-24 16:51:40 +02:00
|
|
|
import 'package:gecko/models/wallet_data.dart';
|
2022-08-23 00:25:16 +02:00
|
|
|
import 'package:gecko/models/widgets_keys.dart';
|
2022-05-31 18:23:56 +02:00
|
|
|
import 'package:gecko/providers/my_wallets.dart';
|
2022-05-24 16:51:40 +02:00
|
|
|
import 'package:gecko/providers/substrate_sdk.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
2021-11-17 06:20:23 +01:00
|
|
|
|
|
|
|
class ChestProvider with ChangeNotifier {
|
|
|
|
void rebuildWidget() {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
Future deleteChest(context, ChestData chest) async {
|
|
|
|
final bool? answer = await (_confirmDeletingChest(context, chest.name));
|
|
|
|
SubstrateSdk sub = Provider.of<SubstrateSdk>(context, listen: false);
|
|
|
|
if (answer ?? false) {
|
|
|
|
await sub.deleteAccounts(getChestWallets(chest));
|
|
|
|
await chestBox.delete(chest.key);
|
|
|
|
MyWalletsProvider myWalletProvider =
|
2022-05-31 18:23:56 +02:00
|
|
|
Provider.of<MyWalletsProvider>(context, listen: false);
|
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
myWalletProvider.pinCode = '';
|
2022-05-31 18:23:56 +02:00
|
|
|
|
2021-11-18 02:37:46 +01:00
|
|
|
if (chestBox.isEmpty) {
|
|
|
|
await configBox.put('currentChest', 0);
|
|
|
|
} else {
|
2021-12-23 12:36:09 +01:00
|
|
|
int? lastChest = chestBox.toMap().keys.first;
|
2021-11-18 02:37:46 +01:00
|
|
|
await configBox.put('currentChest', lastChest);
|
|
|
|
}
|
2021-11-17 06:20:23 +01:00
|
|
|
|
|
|
|
Navigator.popUntil(
|
|
|
|
context,
|
|
|
|
ModalRoute.withName('/'),
|
|
|
|
);
|
2021-11-18 02:37:46 +01:00
|
|
|
notifyListeners();
|
2021-11-17 06:20:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
List<String> getChestWallets(ChestData chest) {
|
2022-05-24 16:51:40 +02:00
|
|
|
List<String> toDelete = [];
|
2022-08-06 02:22:41 +02:00
|
|
|
log.d(chest.key);
|
2022-05-24 16:51:40 +02:00
|
|
|
walletBox.toMap().forEach((key, WalletData value) {
|
2022-08-06 02:22:41 +02:00
|
|
|
if (value.chest == chest.key) {
|
2022-05-24 16:51:40 +02:00
|
|
|
toDelete.add(value.address!);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return toDelete;
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
Future<bool?> _confirmDeletingChest(context, String? walletName) async {
|
2021-11-17 06:20:23 +01:00
|
|
|
return showDialog<bool>(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: true, // user must tap button!
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertDialog(
|
2022-08-06 02:22:41 +02:00
|
|
|
title: Text('areYouSureToDeleteWallet'.tr(args: [walletName!])),
|
2021-11-17 06:20:23 +01:00
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
2022-08-23 00:25:16 +02:00
|
|
|
child: Text("no".tr(), key: keyCancel),
|
2021-11-17 06:20:23 +01:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context, false);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextButton(
|
2022-08-23 00:25:16 +02:00
|
|
|
child: Text("yes".tr(), key: keyConfirm),
|
2021-11-17 06:20:23 +01:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context, true);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|