2021-01-28 15:10:09 +01:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:gecko/globals.dart';
|
2021-12-23 15:13:58 +01:00
|
|
|
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';
|
2022-06-06 17:52:42 +02:00
|
|
|
import 'package:gecko/screens/animated_text.dart';
|
2022-05-28 19:13:30 +02:00
|
|
|
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';
|
2022-05-21 06:47:26 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2021-01-28 15:10:09 +01:00
|
|
|
|
|
|
|
class WalletOptionsProvider with ChangeNotifier {
|
2022-05-19 07:00:25 +02:00
|
|
|
TextEditingController address = TextEditingController();
|
2021-11-14 19:21:20 +01:00
|
|
|
final TextEditingController _newWalletName = TextEditingController();
|
2021-01-28 15:10:09 +01:00
|
|
|
bool isWalletUnlock = false;
|
|
|
|
bool ischangedPin = false;
|
2021-11-14 19:21:20 +01:00
|
|
|
TextEditingController newPin = TextEditingController();
|
2021-03-21 00:35:32 +01:00
|
|
|
bool isEditing = false;
|
2022-05-19 09:07:26 +02:00
|
|
|
bool isBalanceBlur = false;
|
2021-03-21 00:35:32 +01:00
|
|
|
FocusNode walletNameFocus = FocusNode();
|
|
|
|
TextEditingController nameController = TextEditingController();
|
2021-12-23 12:36:09 +01:00
|
|
|
late bool isDefaultWallet;
|
2021-01-28 15:10:09 +01:00
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
Future<NewWallet>? get badWallet => null;
|
2021-01-28 15:10:09 +01:00
|
|
|
|
2021-02-14 23:17:03 +01:00
|
|
|
int getPinLenght(_walletNbr) {
|
2021-12-23 15:13:58 +01:00
|
|
|
return pinLength;
|
2021-02-09 22:11:57 +01:00
|
|
|
}
|
|
|
|
|
2022-05-21 06:47:26 +02:00
|
|
|
void _renameWallet(List<int?> _walletID, String _newName,
|
2021-12-23 15:13:58 +01:00
|
|
|
{required bool isCesium}) async {
|
2022-05-21 06:47:26 +02:00
|
|
|
MyWalletsProvider myWalletClass = MyWalletsProvider();
|
2021-11-07 04:23:05 +01:00
|
|
|
|
2022-05-29 04:30:03 +02:00
|
|
|
WalletData _walletTarget = myWalletClass.getWalletDataById(_walletID)!;
|
2022-05-21 06:47:26 +02:00
|
|
|
_walletTarget.name = _newName;
|
|
|
|
await walletBox.put(_walletTarget.key, _walletTarget);
|
2021-02-15 01:44:25 +01:00
|
|
|
|
|
|
|
_newWalletName.text = '';
|
2021-01-28 15:10:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 12:36:09 +01:00
|
|
|
bool editWalletName(List<int?> _wID, {bool? isCesium}) {
|
2021-03-21 00:35:32 +01:00
|
|
|
bool nameState;
|
|
|
|
if (isEditing) {
|
|
|
|
if (!nameController.text.contains(':') &&
|
2021-03-21 01:42:29 +01:00
|
|
|
nameController.text.length <= 39) {
|
2021-12-23 12:36:09 +01:00
|
|
|
_renameWallet(_wID, nameController.text, isCesium: isCesium!);
|
2021-03-21 00:35:32 +01:00
|
|
|
nameState = true;
|
|
|
|
} else {
|
|
|
|
nameState = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nameState = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
isEditing ? isEditing = false : isEditing = true;
|
|
|
|
notifyListeners();
|
|
|
|
return nameState;
|
|
|
|
}
|
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
Future<int> deleteWallet(context, WalletData wallet) async {
|
2022-05-29 01:38:27 +02:00
|
|
|
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
|
|
|
|
final bool? _answer = await (confirmPopup(context,
|
2022-05-28 19:13:30 +02:00
|
|
|
'Êtes-vous sûr de vouloir oublier le portefeuille "${wallet.name}" ?'));
|
2021-01-28 15:10:09 +01:00
|
|
|
|
2022-05-30 14:48:12 +02:00
|
|
|
if (_answer ?? false) {
|
2022-06-08 02:30:19 +02:00
|
|
|
//Check if balance is null
|
|
|
|
final _balance = await _sub.getBalance(wallet.address!);
|
|
|
|
if (_balance != 0) {
|
|
|
|
MyWalletsProvider _myWalletProvider =
|
|
|
|
Provider.of<MyWalletsProvider>(context, listen: false);
|
|
|
|
final _defaultWallet = _myWalletProvider.getDefaultWallet();
|
|
|
|
log.d(_defaultWallet.address);
|
2022-06-08 03:14:08 +02:00
|
|
|
_sub.pay(
|
2022-06-08 02:30:19 +02:00
|
|
|
fromAddress: wallet.address!,
|
|
|
|
destAddress: _defaultWallet.address!,
|
|
|
|
amount: -1,
|
|
|
|
password: _myWalletProvider.pinCode);
|
|
|
|
}
|
|
|
|
|
2022-05-20 15:15:29 +02:00
|
|
|
await walletBox.delete(wallet.key);
|
2022-05-29 01:38:27 +02:00
|
|
|
await _sub.deleteAccounts([wallet.address!]);
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2022-05-20 15:15:29 +02:00
|
|
|
Navigator.pop(context);
|
2021-01-28 15:10:09 +01:00
|
|
|
}
|
2021-02-15 01:44:25 +01:00
|
|
|
return 0;
|
2021-01-28 15:10:09 +01:00
|
|
|
}
|
|
|
|
|
2021-03-21 00:35:32 +01:00
|
|
|
void bluringBalance() {
|
|
|
|
isBalanceBlur = !isBalanceBlur;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-05-28 19:13:30 +02:00
|
|
|
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) {
|
2022-05-28 19:13:30 +02:00
|
|
|
File imageFile = File(pickedFile.path);
|
|
|
|
if (!await imageDirectory.exists()) {
|
|
|
|
log.e("Image folder doesn't exist");
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
final newPath = "${imageDirectory.path}/${pickedFile.name}";
|
|
|
|
|
|
|
|
await imageFile.copy(newPath);
|
|
|
|
// final File newImage = File(newPath);
|
|
|
|
|
|
|
|
// await newImage.writeAsBytes(await pickedFile.readAsBytes());
|
|
|
|
// await pickedFile.saveTo(newPath);
|
|
|
|
// await Future.delayed(const Duration(milliseconds: 100));
|
|
|
|
|
|
|
|
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.');
|
2022-05-28 19:13:30 +02:00
|
|
|
return '';
|
2021-03-21 23:04:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 16:51:40 +02:00
|
|
|
Widget idtyStatus(BuildContext context, String address,
|
2022-06-06 17:52:42 +02:00
|
|
|
{bool isOwner = false, Color color = Colors.black}) {
|
|
|
|
_showText(String text,
|
|
|
|
[double size = 18, bool _bold = false, bool smooth = true]) {
|
|
|
|
log.d(text);
|
|
|
|
return AnimatedFadeOutIn<String>(
|
|
|
|
data: text,
|
|
|
|
duration: Duration(milliseconds: smooth ? 200 : 0),
|
|
|
|
builder: (value) => Text(
|
|
|
|
value,
|
2022-06-03 18:15:45 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: size,
|
2022-06-06 17:52:42 +02:00
|
|
|
color: _bold ? color : Colors.black,
|
2022-06-03 18:15:45 +02:00
|
|
|
fontWeight: _bold ? FontWeight.w500 : FontWeight.w400),
|
2022-06-06 17:52:42 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-06-03 18:15:45 +02:00
|
|
|
|
2022-05-24 16:51:40 +02:00
|
|
|
return Consumer<SubstrateSdk>(builder: (context, _sub, _) {
|
|
|
|
return FutureBuilder(
|
|
|
|
future: _sub.idtyStatus(address),
|
2022-06-06 17:52:42 +02:00
|
|
|
initialData: '',
|
2022-05-24 16:51:40 +02:00
|
|
|
builder: (context, snapshot) {
|
|
|
|
switch (snapshot.data.toString()) {
|
|
|
|
case 'noid':
|
|
|
|
{
|
2022-06-03 18:15:45 +02:00
|
|
|
return _showText('Aucune identité');
|
2022-05-24 16:51:40 +02:00
|
|
|
}
|
|
|
|
case 'Created':
|
|
|
|
{
|
2022-06-03 18:15:45 +02:00
|
|
|
return isOwner
|
|
|
|
? InkWell(
|
|
|
|
child: _showText(
|
|
|
|
'Cliquez ici pour confirmer\nvotre nouvelle identité',
|
|
|
|
18,
|
|
|
|
true),
|
|
|
|
onTap: () async {
|
|
|
|
await validateIdentity(context);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
: _showText('Identité créé');
|
2022-05-24 16:51:40 +02:00
|
|
|
}
|
|
|
|
case 'ConfirmedByOwner':
|
|
|
|
{
|
2022-06-03 18:15:45 +02:00
|
|
|
return _showText('Identité confirmé');
|
2022-05-24 16:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'Validated':
|
|
|
|
{
|
2022-06-06 17:52:42 +02:00
|
|
|
return _showText('Membre validé !', 18, true);
|
2022-05-24 16:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'expired':
|
|
|
|
{
|
2022-06-03 18:15:45 +02:00
|
|
|
return _showText('Identité expiré');
|
2022-05-24 16:51:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return SizedBox(
|
2022-06-06 17:52:42 +02:00
|
|
|
child: _showText('', 18, false, false),
|
2022-05-24 16:51:40 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-06 17:52:42 +02:00
|
|
|
Future<bool> isMember(BuildContext context, String address) async {
|
2022-06-05 02:22:22 +02:00
|
|
|
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
|
2022-06-06 17:52:42 +02:00
|
|
|
return await _sub.idtyStatus(address) == 'Validated';
|
2022-06-05 02:22:22 +02:00
|
|
|
}
|
|
|
|
|
2022-05-24 16:51:40 +02:00
|
|
|
Future<String?> validateIdentity(BuildContext context) async {
|
|
|
|
TextEditingController idtyName = TextEditingController();
|
|
|
|
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
|
|
|
|
WalletOptionsProvider _walletOptions =
|
|
|
|
Provider.of<WalletOptionsProvider>(context, listen: false);
|
|
|
|
MyWalletsProvider _myWalletProvider =
|
|
|
|
Provider.of<MyWalletsProvider>(context, listen: false);
|
|
|
|
|
|
|
|
return showDialog<String>(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: true, // user must tap button!
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertDialog(
|
2022-06-03 18:15:45 +02:00
|
|
|
title: const Text(
|
|
|
|
'Confirmez votre identité',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w500),
|
|
|
|
),
|
2022-05-24 16:51:40 +02:00
|
|
|
content: SizedBox(
|
|
|
|
height: 100,
|
|
|
|
child: Column(children: [
|
2022-06-03 18:15:45 +02:00
|
|
|
const SizedBox(height: 20),
|
|
|
|
const Text(
|
|
|
|
'Nom:',
|
|
|
|
style: TextStyle(fontSize: 19),
|
|
|
|
),
|
2022-05-24 16:51:40 +02:00
|
|
|
TextField(
|
2022-06-03 18:15:45 +02:00
|
|
|
onChanged: (_) => notifyListeners(),
|
|
|
|
textAlign: TextAlign.center,
|
2022-05-27 08:54:29 +02:00
|
|
|
autofocus: true,
|
2022-05-24 16:51:40 +02:00
|
|
|
controller: idtyName,
|
2022-06-03 18:15:45 +02:00
|
|
|
style: const TextStyle(fontSize: 19),
|
2022-05-24 16:51:40 +02:00
|
|
|
)
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
actions: <Widget>[
|
2022-06-03 18:15:45 +02:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Consumer<WalletOptionsProvider>(
|
|
|
|
builder: (context, _wOptions, _) {
|
|
|
|
return TextButton(
|
|
|
|
key: const Key('infoPopup'),
|
|
|
|
child: Text(
|
|
|
|
"Valider",
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 21,
|
|
|
|
color: idtyName.text.length >= 2
|
|
|
|
? const Color(0xffD80000)
|
|
|
|
: Colors.grey,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () async {
|
|
|
|
if (idtyName.text.length >= 2) {
|
2022-06-04 23:32:44 +02:00
|
|
|
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 const TransactionInProgress(
|
|
|
|
transType: 'comfirmIdty');
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2022-06-03 18:15:45 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
})
|
|
|
|
],
|
2022-05-24 16:51:40 +02:00
|
|
|
),
|
2022-06-03 18:15:45 +02:00
|
|
|
const SizedBox(height: 20)
|
2022-05-24 16:51:40 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-02 08:19:20 +01:00
|
|
|
void reloadBuild() {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2022-05-19 07:00:25 +02:00
|
|
|
|
2022-05-31 18:23:56 +02:00
|
|
|
Future changePinCacheChoice() async {
|
2022-06-01 07:33:56 +02:00
|
|
|
bool isCacheChecked = configBox.get('isCacheChecked') ?? false;
|
2022-05-31 18:23:56 +02:00
|
|
|
await configBox.put('isCacheChecked', !isCacheChecked);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:00:25 +02:00
|
|
|
String? getAddress(int chest, int derivation) {
|
|
|
|
String? _address;
|
|
|
|
walletBox.toMap().forEach((key, value) {
|
2022-05-19 09:07:26 +02:00
|
|
|
if (value.chest == chest && value.derivation == derivation) {
|
2022-05-19 07:00:25 +02:00
|
|
|
_address = value.address!;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
address.text = _address ?? '';
|
|
|
|
|
|
|
|
return _address;
|
|
|
|
}
|
2021-01-28 15:10:09 +01:00
|
|
|
}
|
2022-05-21 06:47:26 +02:00
|
|
|
|
2022-05-24 18:31:41 +02:00
|
|
|
Map<String, String> balanceCache = {};
|
2022-05-21 06:47:26 +02:00
|
|
|
|
2022-05-28 19:13:30 +02:00
|
|
|
Widget balance(BuildContext context, String address, double size,
|
|
|
|
[Color _color = Colors.black]) {
|
2022-05-21 06:47:26 +02:00
|
|
|
return Column(children: <Widget>[
|
|
|
|
Consumer<SubstrateSdk>(builder: (context, _sdk, _) {
|
|
|
|
return FutureBuilder(
|
|
|
|
future: _sdk.getBalance(address),
|
|
|
|
builder: (BuildContext context, AsyncSnapshot<num?> _balance) {
|
|
|
|
if (_balance.connectionState != ConnectionState.done ||
|
|
|
|
_balance.hasError) {
|
2022-05-24 18:31:41 +02:00
|
|
|
if (balanceCache[address] != null) {
|
|
|
|
return Text(balanceCache[address]!,
|
|
|
|
style: TextStyle(
|
2022-05-28 19:13:30 +02:00
|
|
|
fontSize: isTall ? size : size * 0.9, color: _color));
|
2022-05-24 18:31:41 +02:00
|
|
|
} else {
|
|
|
|
return SizedBox(
|
|
|
|
height: 15,
|
|
|
|
width: 15,
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
color: orangeC,
|
|
|
|
strokeWidth: 2,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-05-21 06:47:26 +02:00
|
|
|
}
|
2022-05-24 18:31:41 +02:00
|
|
|
balanceCache[address] = "${_balance.data.toString()} $currencyName";
|
2022-05-21 06:47:26 +02:00
|
|
|
return Text(
|
2022-05-24 18:31:41 +02:00
|
|
|
balanceCache[address]!,
|
2022-05-21 06:47:26 +02:00
|
|
|
style: TextStyle(
|
2022-05-26 02:35:29 +02:00
|
|
|
fontSize: isTall ? size : size * 0.9,
|
2022-05-28 19:13:30 +02:00
|
|
|
color: _color,
|
2022-05-21 06:47:26 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|
2022-06-02 19:58:23 +02:00
|
|
|
|
|
|
|
Widget getCerts(BuildContext context, String address, double size,
|
|
|
|
[Color _color = Colors.black]) {
|
|
|
|
return Column(children: <Widget>[
|
|
|
|
Consumer<SubstrateSdk>(builder: (context, _sdk, _) {
|
|
|
|
return FutureBuilder(
|
|
|
|
future: _sdk.getCerts(address),
|
2022-06-03 18:15:45 +02:00
|
|
|
builder: (BuildContext context, AsyncSnapshot<List<int>> _certs) {
|
|
|
|
// log.d(_certs.data);
|
|
|
|
|
|
|
|
return _certs.data?[0] != 0
|
|
|
|
? Row(
|
|
|
|
children: [
|
2022-06-04 15:08:22 +02:00
|
|
|
Image.asset('assets/medal.png', height: 20),
|
|
|
|
const SizedBox(width: 1),
|
|
|
|
Text(_certs.data?[0].toString() ?? '0',
|
|
|
|
style: const TextStyle(fontSize: 20)),
|
|
|
|
const SizedBox(width: 5),
|
|
|
|
Text(
|
|
|
|
"(${_certs.data?[1].toString() ?? '0'})",
|
|
|
|
style: const TextStyle(fontSize: 14),
|
|
|
|
)
|
2022-06-03 18:15:45 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
: const Text('');
|
2022-06-02 19:58:23 +02:00
|
|
|
});
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|