Newer
Older
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:provider/provider.dart';
Future initWalletFolder() async {
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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();
}
}
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());
}
print('No wallets detected');
return false;
} else {
print('Some wallets have been detected.');
print(walletsDirectory.path);

poka
committed
// int i = 0;
File _walletConfig = File('${walletsDirectory.path}/$_chest/list.conf');
_walletConfig.readAsLinesSync().forEach((element) {
print(element);
listWallets.add(WalletData(element));
// listWallets += "${element.split(':')[0]}:${element.split(':')[1]}:${element.split(':')[2]}"
// listWallets.forEach((e) {
// print(e.name);
// });
// print(listWallets);
int chest = int.parse(_id.split(':')[0]);
// int nbr = int.parse(_id.split(':')[1]);
final _walletConfig = File('${walletsDirectory.path}/$chest/list.conf');
return WalletData(_walletConfig
.readAsLinesSync()
.firstWhere((element) => element.startsWith(_id)));
defaultWalletFile = File('${appPath.path}/defaultWallet');
File(defaultWalletFile.path).createSync();
defaultWallet = getWalletData(defaultWalletFile.readAsStringSync());
print("found default wallet $defaultWallet");
Future<int> deleteAllWallet(context) async {
try {
print('DELETE THAT ?: $walletsDirectory');
final bool _answer = await _confirmDeletingAllWallets(context);
if (_answer) {
await walletsDirectory.delete(recursive: true);
await walletsDirectory.create();
await initWalletFolder();

poka
committed
notifyListeners();
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 =
_myWalletProvider.rebuildWidget();
});
Navigator.pop(context, true);
},
),
],
);
},
);
}
Future<void> generateNewDerivation(context, String _name) async {

poka
committed
int _newDerivationNbr;
int _newWalletNbr;
final _walletConfig = File('${walletsDirectory.path}/0/list.conf');

poka
committed
if (await _walletConfig.readAsString() == '') {
_newDerivationNbr = 3;
_newWalletNbr = 0;

poka
committed
} else {
String _lastWallet =
await _walletConfig.readAsLines().then((value) => value.last);
int _lastDerivation = int.parse(_lastWallet.split(':')[3]);

poka
committed
_newDerivationNbr = _lastDerivation + 3;
int _lastWalletNbr = int.parse(_lastWallet.split(':')[1]);
_newWalletNbr = _lastWalletNbr + 1;

poka
committed
}
await _walletConfig.writeAsString(
'\n0:$_newWalletNbr:$_name:$_newDerivationNbr',
mode: FileMode.append);
print(await _walletConfig.readAsString());
notifyListeners();
Navigator.pop(context);
}
void rebuildWidget() {
notifyListeners();
}
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// wallet data contains elements identifying wallet
class WalletData {
int chest;
int number;
String name;
int derivation;
// constructor from ':'-separated string
WalletData(String element) {
List parts = element.split(':');
this.chest = int.parse(parts[0]);
this.number = int.parse(parts[1]);
this.name = parts[2];
this.derivation = int.parse(parts[3]);
}
// 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}";
}
}