gecko/lib/providers/wallet_options.dart

393 lines
13 KiB
Dart
Raw Normal View History

// ignore_for_file: use_build_context_synchronously
import 'dart:io';
2022-06-17 01:13:14 +02:00
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'package:gecko/globals.dart';
import 'package:gecko/models/widgets_keys.dart';
import 'package:gecko/providers/duniter_indexer.dart';
import 'package:gecko/providers/my_wallets.dart';
2021-11-14 19:21:20 +01:00
import 'package:gecko/models/wallet_data.dart';
2022-05-21 06:47:26 +02:00
import 'package:gecko/providers/substrate_sdk.dart';
import 'package:gecko/screens/common_elements.dart';
2022-06-04 23:32:44 +02:00
import 'package:gecko/screens/myWallets/unlocking_wallet.dart';
import 'package:gecko/screens/transaction_in_progress.dart';
2021-03-21 23:04:11 +01:00
import 'package:image_picker/image_picker.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2022-06-12 22:12:00 +02:00
import 'package:image_cropper/image_cropper.dart';
2022-12-09 19:55:40 +01:00
class WalletOptionsProvider extends ChangeNotifier {
TextEditingController address = TextEditingController();
2021-11-14 19:21:20 +01:00
final TextEditingController _newWalletName = TextEditingController();
bool isWalletUnlock = false;
bool ischangedPin = false;
2021-11-14 19:21:20 +01:00
TextEditingController newPin = TextEditingController();
bool isEditing = false;
2022-05-19 09:07:26 +02:00
bool isBalanceBlur = false;
TextEditingController nameController = TextEditingController();
2021-12-23 12:36:09 +01:00
late bool isDefaultWallet;
2022-06-12 21:19:05 +02:00
bool canValidateNameBool = false;
2021-12-23 12:36:09 +01:00
Future<NewWallet>? get badWallet => null;
Map<String, double> balanceCache = {};
int getPinLenght(walletNbr) {
return pinLength;
2021-02-09 22:11:57 +01:00
}
void _renameWallet(List<int?> walletID, String newName,
{required bool isCesium}) async {
2022-05-21 06:47:26 +02:00
MyWalletsProvider myWalletClass = MyWalletsProvider();
WalletData walletTarget = myWalletClass.getWalletDataById(walletID)!;
walletTarget.name = newName;
await walletBox.put(walletTarget.key, walletTarget);
2021-02-15 01:44:25 +01:00
_newWalletName.text = '';
}
Future<int> deleteWallet(context, WalletData wallet) async {
final sub = Provider.of<SubstrateSdk>(context, listen: false);
final bool? answer = await (confirmPopup(
2022-06-18 00:48:07 +02:00
context, 'areYouSureToForgetWallet'.tr(args: [wallet.name!])));
if (answer ?? false) {
//Check if balance is null
final balance = await sub.getBalance(wallet.address);
if (balance != {}) {
final myWalletProvider =
Provider.of<MyWalletsProvider>(context, listen: false);
final defaultWallet = myWalletProvider.getDefaultWallet();
log.d(defaultWallet.address);
sub.pay(
fromAddress: wallet.address,
destAddress: defaultWallet.address,
amount: -1,
password: myWalletProvider.pinCode);
}
await walletBox.delete(wallet.key);
await sub.deleteAccounts([wallet.address]);
2021-02-15 01:44:25 +01:00
Navigator.pop(context);
}
2021-02-15 01:44:25 +01:00
return 0;
}
void bluringBalance() {
isBalanceBlur = !isBalanceBlur;
notifyListeners();
}
Future<String> changeAvatar() async {
// File _image;
2021-03-21 23:04:11 +01:00
final picker = ImagePicker();
2021-12-23 12:36:09 +01:00
XFile? pickedFile = await picker.pickImage(source: ImageSource.gallery);
2021-03-21 23:04:11 +01:00
if (pickedFile != null) {
File imageFile = File(pickedFile.path);
if (!await imageDirectory.exists()) {
log.e("Image folder doesn't exist");
return '';
}
2022-06-12 22:12:00 +02:00
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: imageFile.path,
aspectRatioPresets: [CropAspectRatioPreset.square],
cropStyle: CropStyle.circle,
uiSettings: [
AndroidUiSettings(
hideBottomControls: true,
toolbarTitle: 'Personnalisation',
toolbarColor: Colors.deepOrange,
toolbarWidgetColor: Colors.white,
initAspectRatio: CropAspectRatioPreset.original,
lockAspectRatio: true),
IOSUiSettings(
title: 'Cropper',
),
],
);
2022-06-12 22:12:00 +02:00
final newPath = "${imageDirectory.path}/${pickedFile.name}";
2022-06-12 22:12:00 +02:00
if (croppedFile != null) {
await File(croppedFile.path).rename(newPath);
} else {
log.w('No image selected.');
return '';
}
// await imageFile.copy(newPath);
log.i(newPath);
return newPath;
2021-03-21 23:04:11 +01:00
} else {
2021-04-02 12:05:37 +02:00
log.w('No image selected.');
return '';
2021-03-21 23:04:11 +01:00
}
}
Future<String?> confirmIdentityPopup(BuildContext context) async {
2022-05-24 16:51:40 +02:00
TextEditingController idtyName = TextEditingController();
final sub = Provider.of<SubstrateSdk>(context, listen: false);
final walletOptions =
2022-05-24 16:51:40 +02:00
Provider.of<WalletOptionsProvider>(context, listen: false);
final myWalletProvider =
2022-05-24 16:51:40 +02:00
Provider.of<MyWalletsProvider>(context, listen: false);
bool canValidate = false;
bool idtyExist = false;
2022-05-24 16:51:40 +02:00
return showDialog<String>(
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
2022-06-18 00:48:07 +02:00
title: Text(
'confirmYourIdentity'.tr(),
textAlign: TextAlign.center,
2022-06-18 00:48:07 +02:00
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
),
2022-05-24 16:51:40 +02:00
content: SizedBox(
height: 100,
child: Column(children: [
const SizedBox(height: 20),
2022-08-24 23:59:51 +02:00
TextField(
key: keyEnterIdentityUsername,
onChanged: (_) async {
idtyExist = await isIdtyExist(idtyName.text);
canValidate = !idtyExist &&
!await isIdtyExist(idtyName.text) &&
idtyName.text.length >= 2 &&
idtyName.text.length <= 32;
log.d('aaaaaaaaaa: $canValidate');
notifyListeners();
},
inputFormatters: <TextInputFormatter>[
// FilteringTextInputFormatter.allow(RegExp("[0-9a-zA-Z]")),
FilteringTextInputFormatter.deny(RegExp(r'^ ')),
// FilteringTextInputFormatter.deny(RegExp(r' $')),
],
textAlign: TextAlign.center,
autofocus: true,
2022-05-24 16:51:40 +02:00
controller: idtyName,
style: const TextStyle(fontSize: 19),
),
const SizedBox(height: 10),
Consumer<WalletOptionsProvider>(builder: (context, wOptions, _) {
return Text(idtyExist ? 'Cette identité existe déjà' : '',
style: TextStyle(color: Colors.red[500]));
})
2022-05-24 16:51:40 +02:00
]),
),
actions: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Consumer<WalletOptionsProvider>(
builder: (context, wOptions, _) {
return TextButton(
2022-08-24 21:53:16 +02:00
key: keyConfirm,
onPressed: canValidate
? () async {
idtyName.text =
idtyName.text.trim().replaceAll(' ', '');
if (idtyName.text.length.clamp(3, 32) ==
idtyName.text.length) {
WalletData? defaultWallet =
myWalletProvider.getDefaultWallet();
String? pin;
if (myWalletProvider.pinCode == '') {
pin = await Navigator.push(
context,
MaterialPageRoute(
builder: (homeContext) {
return UnlockingWallet(
wallet: defaultWallet);
},
),
);
}
if (pin != null ||
myWalletProvider.pinCode != '') {
final wallet = myWalletProvider
.getWalletDataByAddress(address.text);
await sub.setCurrentWallet(wallet!);
sub.confirmIdentity(walletOptions.address.text,
idtyName.text, myWalletProvider.pinCode);
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return TransactionInProgress(
transType: 'comfirmIdty',
fromAddress:
getShortPubkey(wallet.address),
toAddress: getShortPubkey(wallet.address),
);
}),
);
}
}
}
: null,
child: Text(
2022-06-17 01:13:14 +02:00
"validate".tr(),
style: TextStyle(
fontSize: 21,
color: canValidate
? const Color(0xffD80000)
: Colors.grey[500]),
),
);
})
],
2022-05-24 16:51:40 +02:00
),
const SizedBox(height: 5)
2022-05-24 16:51:40 +02:00
],
);
},
);
}
Future<String?> editWalletName(BuildContext context, List<int?> wID) async {
2022-06-12 21:19:05 +02:00
TextEditingController walletName = TextEditingController();
canValidateNameBool = false;
return showDialog<String>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
2022-06-17 01:13:14 +02:00
title: Text(
'chooseWalletName'.tr(),
2022-06-12 21:19:05 +02:00
textAlign: TextAlign.center,
2022-06-17 01:13:14 +02:00
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
2022-06-12 21:19:05 +02:00
),
content: SizedBox(
height: 100,
child: Column(children: [
const SizedBox(height: 20),
TextField(
onChanged: (_) => canValidateName(context, walletName),
textAlign: TextAlign.center,
autofocus: true,
controller: walletName,
style: const TextStyle(fontSize: 19),
)
]),
),
actions: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Consumer<WalletOptionsProvider>(
builder: (context, wOptions, _) {
2022-06-12 21:19:05 +02:00
return TextButton(
key: keyInfoPopup,
2022-06-12 21:19:05 +02:00
child: Text(
2022-06-17 01:13:14 +02:00
"validate".tr(),
2022-06-12 21:19:05 +02:00
style: TextStyle(
fontSize: 21,
color: canValidateNameBool
? const Color(0xffD80000)
: Colors.grey,
fontWeight: FontWeight.w600,
),
),
onPressed: () async {
if (canValidateNameBool) {
nameController.text = walletName.text;
_renameWallet(wID, walletName.text, isCesium: false);
notifyListeners();
2022-06-12 21:19:05 +02:00
Navigator.pop(context);
}
},
);
})
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
key: keyCancel,
2022-06-12 21:19:05 +02:00
child: Text(
2022-06-18 03:01:22 +02:00
"cancel".tr(),
2022-06-12 21:19:05 +02:00
style: TextStyle(
fontSize: 18,
color: Colors.grey[800],
fontWeight: FontWeight.w300),
),
onPressed: () async {
Navigator.pop(context);
},
)
],
),
const SizedBox(height: 20)
],
);
},
);
}
bool canValidateName(BuildContext context, TextEditingController walletName) {
final myWalletProvider =
2022-06-12 21:19:05 +02:00
Provider.of<MyWalletsProvider>(context, listen: false);
bool isNameValid = walletName.text.length >= 2 &&
!walletName.text.contains(':') &&
walletName.text.length <= 39;
if (isNameValid) {
for (var wallet in myWalletProvider.listWallets) {
2022-06-12 21:19:05 +02:00
if (walletName.text == wallet.name!) {
canValidateNameBool = false;
break;
}
canValidateNameBool = true;
}
} else {
canValidateNameBool = false;
}
notifyListeners();
return canValidateNameBool;
}
2022-09-09 01:12:17 +02:00
void reload() {
2021-03-02 08:19:20 +01:00
notifyListeners();
}
Future changePinCacheChoice() async {
bool isCacheChecked = configBox.get('isCacheChecked') ?? false;
await configBox.put('isCacheChecked', !isCacheChecked);
notifyListeners();
}
String? getAddress(int chest, int derivation) {
String? addressGet;
walletBox.toMap().forEach((key, value) {
2022-05-19 09:07:26 +02:00
if (value.chest == chest && value.derivation == derivation) {
addressGet = value.address;
return;
}
});
address.text = addressGet ?? '';
return addressGet;
}
2022-09-11 13:14:52 +02:00
}
2022-12-09 19:55:40 +01:00
final walletOptionsProvider =
ChangeNotifierProvider<WalletOptionsProvider>((ref) {
return WalletOptionsProvider();
});