gecko/lib/models/myWallets.dart

252 lines
7.1 KiB
Dart
Raw Normal View History

2021-01-23 16:38:03 +01:00
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
2021-01-26 21:00:26 +01:00
import 'package:gecko/globals.dart';
import 'package:provider/provider.dart';
2021-01-23 16:38:03 +01:00
class MyWalletsProvider with ChangeNotifier {
2021-03-31 02:38:44 +02:00
List<WalletData> listWallets = [];
2021-04-03 00:07:03 +02:00
String pinCode;
int pinLenght;
Future initWalletFolder() async {
2021-04-03 02:39:09 +02:00
// getDefaultWallet();
final bool isWalletFolderExist = await walletsDirectory.exists();
if (!isWalletFolderExist) {
await Directory(walletsDirectory.path).create();
}
File _currentChestFile = File('${walletsDirectory.path}/currentChest.conf');
await _currentChestFile.create();
await _currentChestFile.writeAsString('0');
final bool isChestsExist =
await Directory('${walletsDirectory.path}/0').exists();
if (!isChestsExist) {
await Directory('${walletsDirectory.path}/0').create();
await Directory('${walletsDirectory.path}/1').create();
await File('${walletsDirectory.path}/0/list.conf').create();
await File('${walletsDirectory.path}/0/order.conf').create();
await File('${walletsDirectory.path}/1/list.conf').create();
await File('${walletsDirectory.path}/1/order.conf').create();
// getDefaultWallet();
}
await getDefaultWalletAsync();
}
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());
}
2021-01-23 16:38:03 +01:00
bool checkIfWalletExist() {
2021-01-26 21:00:26 +01:00
if (appPath == null) {
2021-01-23 16:38:03 +01:00
return false;
}
2021-03-31 02:38:44 +02:00
final List _walletList = readAllWallets(0);
2021-03-31 02:38:44 +02:00
if (_walletList.isEmpty) {
2021-04-02 12:05:37 +02:00
log.i('No wallets detected');
2021-01-23 16:38:03 +01:00
return false;
} else {
return true;
}
}
2021-03-31 02:38:44 +02:00
List readAllWallets(int _chest) {
2021-04-02 13:11:45 +02:00
// log.d(walletsDirectory.path);
2021-03-31 02:38:44 +02:00
listWallets = [];
File _walletConfig = File('${walletsDirectory.path}/$_chest/list.conf');
_walletConfig.readAsLinesSync().forEach((element) {
2021-03-31 02:38:44 +02:00
listWallets.add(WalletData(element));
2021-01-26 21:00:26 +01:00
});
2021-02-15 01:44:25 +01:00
2021-04-02 13:11:45 +02:00
log.i(listWallets.toString());
2021-01-26 21:00:26 +01:00
return listWallets;
}
2021-03-31 02:38:44 +02:00
WalletData getWalletData(String _id) {
// log.d(_id);
if (_id == '') return WalletData('');
2021-03-30 02:00:46 +02:00
int chest = int.parse(_id.split(':')[0]);
final _walletConfig = File('${walletsDirectory.path}/$chest/list.conf');
2021-04-02 11:45:59 +02:00
return WalletData(_walletConfig
.readAsLinesSync()
.firstWhere((element) => element.startsWith(_id)));
2021-03-30 02:00:46 +02:00
}
Future<WalletData> getWalletDataAsync(String _id) async {
// log.d(_id);
if (_id == '') return WalletData('');
int chest = int.parse(_id.split(':')[0]);
final _walletConfig = File('${walletsDirectory.path}/$chest/list.conf');
List configLines = await _walletConfig.readAsLines();
log.d(configLines);
return WalletData(
configLines.firstWhere((element) => element.startsWith(_id)));
}
void getDefaultWallet() {
defaultWalletFile = File('${appPath.path}/defaultWallet');
2021-03-31 02:38:44 +02:00
if (!defaultWalletFile.existsSync()) {
File(defaultWalletFile.path).createSync();
2021-04-03 02:39:09 +02:00
defaultWalletFile.writeAsStringSync("0:0");
}
2021-04-02 11:45:59 +02:00
defaultWallet = getWalletData(defaultWalletFile.readAsStringSync());
}
Future getDefaultWalletAsync() async {
defaultWalletFile = File('${appPath.path}/defaultWallet');
if (!await defaultWalletFile.exists()) {
await File(defaultWalletFile.path).create();
await defaultWalletFile.writeAsString("0:0");
} else {
defaultWallet =
await getWalletDataAsync(await defaultWalletFile.readAsString());
}
}
Future<int> deleteAllWallet(context) async {
try {
2021-04-02 12:05:37 +02:00
log.w('DELETE THAT ?: $walletsDirectory');
final bool _answer = await _confirmDeletingAllWallets(context);
if (_answer) {
2021-01-29 03:11:24 +01:00
await walletsDirectory.delete(recursive: true);
2021-04-03 02:39:09 +02:00
await defaultWalletFile.delete();
2021-01-29 03:11:24 +01:00
await walletsDirectory.create();
// await defaultWalletFile.create();
await initWalletFolder();
await Future.delayed(Duration(milliseconds: 100));
notifyListeners();
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) {
MyWalletsProvider _myWalletProvider =
Provider.of<MyWalletsProvider>(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(
child: Text("Oui"),
onPressed: () {
WidgetsBinding.instance.addPostFrameCallback((_) {
_myWalletProvider.listWallets =
2021-03-31 02:38:44 +02:00
_myWalletProvider.readAllWallets(getCurrentChest());
_myWalletProvider.rebuildWidget();
});
Navigator.pop(context, true);
},
),
],
);
},
);
}
Future<void> generateNewDerivation(context, String _name) async {
int _newDerivationNbr;
int _newWalletNbr;
final _walletConfig = File('${walletsDirectory.path}/0/list.conf');
2021-02-15 01:44:25 +01:00
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;
}
2021-02-15 01:44:25 +01:00
await _walletConfig.writeAsString(
'\n0:$_newWalletNbr:$_name:$_newDerivationNbr',
2021-02-15 01:44:25 +01:00
mode: FileMode.append);
notifyListeners();
Navigator.pop(context);
}
void rebuildWidget() {
notifyListeners();
}
2021-01-23 16:38:03 +01:00
}
2021-03-31 02:38:44 +02:00
// wallet data contains elements identifying wallet
class WalletData {
int chest;
int number;
String name;
int derivation;
// constructor from ':'-separated string
WalletData(String element) {
if (element != '') {
List parts = element.split(':');
2021-03-31 02:38:44 +02:00
this.chest = int.parse(parts[0]);
this.number = int.parse(parts[1]);
this.name = parts[2];
this.derivation = int.parse(parts[3]);
}
2021-03-31 02:38:44 +02:00
}
// representation of WalletData when debugging
@override
String toString() {
return this.name;
}
// creates the ':'-separated string from the WalletData
String inLine() {
return "${this.chest}:${this.number}:${this.name}:${this.derivation}";
}
// returns only the id part of the ':'-separated string
String id() {
return "${this.chest}:${this.number}";
}
}