gecko/lib/models/myWallets.dart

157 lines
4.1 KiB
Dart

import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:gecko/globals.dart';
import 'package:gecko/models/walletData.dart';
import 'package:provider/provider.dart';
class MyWalletsProvider with ChangeNotifier {
List<WalletData> listWallets = [];
String pinCode;
int pinLenght;
int getCurrentChest() {
File _currentChestFile = File('${walletsDirectory.path}/currentChest.conf');
bool isCurrentChestExist = _currentChestFile.existsSync();
if (!isCurrentChestExist) {
_currentChestFile.createSync();
_currentChestFile.writeAsString('0');
}
return int.parse(_currentChestFile.readAsStringSync());
}
bool checkIfWalletExist() {
if (appPath == null) {
return false;
}
final List _walletList = readAllWallets(0);
if (_walletList.isEmpty) {
log.i('No wallets detected');
return false;
} else {
return true;
}
}
List<WalletData> readAllWallets(int _chest) {
walletBox.toMap().forEach((key, value) {
if (value.chest == _chest) {
listWallets.add(value);
}
});
return listWallets;
}
WalletData getWalletData(String _id) {
if (_id == '') return WalletData();
int _chest = int.parse(_id.split(':')[0]);
int _nbr = int.parse(_id.split(':')[1]);
walletBox.toMap().forEach((key, value) {
if (value.chest == _chest && value.number == _nbr) {
return value;
}
});
return WalletData();
}
void getDefaultWallet() {
if (defaultWallet == null) {
configBox.put('defaultWallet', [0, 0]);
}
defaultWallet = configBox.get('defaultWallet');
}
Future<int> deleteAllWallet(context) async {
MyWalletsProvider _myWalletProvider =
Provider.of<MyWalletsProvider>(context, listen: false);
try {
log.w('DELETE THAT ?: $walletsDirectory');
final bool _answer = await _confirmDeletingAllWallets(context);
if (_answer) {
await walletsDirectory.delete(recursive: true);
await defaultWalletFile.delete();
await walletsDirectory.create();
// await Future.delayed(Duration(milliseconds: 500));
// scheduleMicrotask(() {
notifyListeners();
rebuildWidget();
_myWalletProvider.rebuildWidget();
// });
Navigator.pop(context);
}
return 0;
} catch (e) {
return 1;
}
}
Future<bool> _confirmDeletingAllWallets(context) 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 tous vos trousseaux ?'),
content: SingleChildScrollView(child: Text('')),
actions: <Widget>[
TextButton(
child: Text("Non"),
onPressed: () {
Navigator.pop(context, false);
},
),
TextButton(
key: Key('confirmDeletingAllWallets'),
child: Text("Oui"),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
},
);
}
Future<void> generateNewDerivation(context, String _name) async {
int _newDerivationNbr;
int _newWalletNbr;
final _walletConfig = File('${walletsDirectory.path}/0/list.conf');
if (await _walletConfig.readAsString() == '') {
_newDerivationNbr = 3;
_newWalletNbr = 0;
} else {
String _lastWallet =
await _walletConfig.readAsLines().then((value) => value.last);
int _lastDerivation = int.parse(_lastWallet.split(':')[3]);
_newDerivationNbr = _lastDerivation + 3;
int _lastWalletNbr = int.parse(_lastWallet.split(':')[1]);
_newWalletNbr = _lastWalletNbr + 1;
}
await _walletConfig.writeAsString(
'\n0:$_newWalletNbr:$_name:$_newDerivationNbr',
mode: FileMode.append);
notifyListeners();
Navigator.pop(context);
}
void rebuildWidget() {
notifyListeners();
}
}