gecko/lib/providers/wallet_options.dart

307 lines
9.3 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:gecko/globals.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';
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';
class WalletOptionsProvider with 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;
FocusNode walletNameFocus = FocusNode();
TextEditingController nameController = TextEditingController();
2021-12-23 12:36:09 +01:00
late bool isDefaultWallet;
2021-12-23 12:36:09 +01:00
Future<NewWallet>? get badWallet => null;
int getPinLenght(_walletNbr) {
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,
{required bool isCesium}) async {
2022-05-21 06:47:26 +02:00
MyWalletsProvider myWalletClass = MyWalletsProvider();
2022-05-21 06:47:26 +02:00
WalletData _walletTarget = myWalletClass.getWalletData(_walletID)!;
_walletTarget.name = _newName;
await walletBox.put(_walletTarget.key, _walletTarget);
2021-02-15 01:44:25 +01:00
_newWalletName.text = '';
}
2021-12-23 12:36:09 +01:00
bool editWalletName(List<int?> _wID, {bool? isCesium}) {
bool nameState;
if (isEditing) {
if (!nameController.text.contains(':') &&
nameController.text.length <= 39) {
2021-12-23 12:36:09 +01:00
_renameWallet(_wID, nameController.text, isCesium: isCesium!);
nameState = true;
} else {
nameState = false;
}
} else {
nameState = true;
}
isEditing ? isEditing = false : isEditing = true;
notifyListeners();
return nameState;
}
Future<int> deleteWallet(context, WalletData wallet) async {
2021-12-23 21:44:24 +01:00
final bool? _answer = await (_confirmDeletingWallet(context, wallet.name));
2021-12-23 21:44:24 +01:00
if (_answer!) {
await walletBox.delete(wallet.key);
2021-02-15 01:44:25 +01:00
// Navigator.popUntil(
// context,
// ModalRoute.withName('/mywallets'),
// );
Navigator.pop(context);
}
2021-02-15 01:44:25 +01:00
return 0;
}
2021-12-23 12:36:09 +01:00
Future<bool?> _confirmDeletingWallet(context, _walletName) async {
return showDialog<bool>(
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text(
'Êtes-vous sûr de vouloir supprimer le portefeuille "$_walletName" ?'),
content: SingleChildScrollView(
child: ListBody(
2021-11-14 19:21:20 +01:00
children: const <Widget>[
2021-02-15 01:44:25 +01:00
Text('Vous pourrez restaurer ce portefeuille plus tard.'),
],
),
),
actions: <Widget>[
TextButton(
2021-11-14 19:21:20 +01:00
child: const Text("Non", key: Key('cancelDeleting')),
onPressed: () {
Navigator.pop(context, false);
},
),
TextButton(
2021-11-14 19:21:20 +01:00
child: const Text("Oui", key: Key('confirmDeleting')),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
},
);
}
void bluringBalance() {
isBalanceBlur = !isBalanceBlur;
notifyListeners();
}
2021-03-21 23:04:11 +01:00
Future changeAvatar() async {
File _image;
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) {
_image = File(pickedFile.path);
2022-05-21 06:47:26 +02:00
////TODO: Store image on disk, store path in walletBox.imagePath
2021-11-17 06:20:23 +01:00
log.i(pickedFile.path);
2021-03-21 23:04:11 +01:00
return _image;
} else {
2021-04-02 12:05:37 +02:00
log.w('No image selected.');
2022-05-21 06:47:26 +02:00
return null;
2021-03-21 23:04:11 +01:00
}
}
2022-05-24 16:51:40 +02:00
Widget idtyStatus(BuildContext context, String address,
{bool isOwner = false}) {
return Consumer<SubstrateSdk>(builder: (context, _sub, _) {
return FutureBuilder(
future: _sub.idtyStatus(address),
initialData: '...',
builder: (context, snapshot) {
switch (snapshot.data.toString()) {
case 'noid':
{
return Column(children: const <Widget>[
Text(
'Aucune identité',
style: TextStyle(fontSize: 18, color: Colors.black),
),
]);
}
case 'Created':
{
return Column(children: <Widget>[
isOwner
? InkWell(
child: const Text(
'Identité créé, cliquez pour la confirmer',
style:
TextStyle(fontSize: 18, color: Colors.black),
),
onTap: () async {
await validateIdentity(context);
},
)
: const Text(
'Identité créé',
style: TextStyle(fontSize: 18, color: Colors.black),
),
]);
}
case 'ConfirmedByOwner':
{
return Column(children: const <Widget>[
Text(
'Identité confirmé',
style: TextStyle(fontSize: 18, color: Colors.black),
),
]);
}
case 'Validated':
{
return Column(children: const <Widget>[
Text(
'Membre validé !',
style: TextStyle(fontSize: 18, color: Colors.black),
),
]);
}
case 'expired':
{
return Column(children: const <Widget>[
Text(
'Identité expiré',
style: TextStyle(fontSize: 18, color: Colors.black),
),
]);
}
}
return SizedBox(
width: 230,
child: Column(children: const <Widget>[
Text(
'Statut inconnu',
style: TextStyle(fontSize: 18, color: Colors.black),
),
]),
);
});
});
}
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(
title: const Text('Confirmez votre identité'),
content: SizedBox(
height: 100,
child: Column(children: [
const Text('Nom:'),
TextField(
autofocus: true,
2022-05-24 16:51:40 +02:00
controller: idtyName,
)
]),
),
actions: <Widget>[
TextButton(
child: const Text("Valider"),
onPressed: () async {
_sub.confirmIdentity(_walletOptions.address.text, idtyName.text,
_myWalletProvider.pinCode);
Navigator.pop(context);
},
),
],
);
},
);
}
2021-03-02 08:19:20 +01:00
void reloadBuild() {
notifyListeners();
}
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) {
_address = value.address!;
return;
}
});
address.text = _address ?? '';
return _address;
}
}
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-24 16:51:40 +02:00
Widget balance(BuildContext context, String address, double size) {
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(
fontSize: isTall ? size : size * 0.9,
));
} 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-21 06:47:26 +02:00
),
);
});
}),
]);
}