2021-01-23 16:38:03 +01:00
|
|
|
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';
|
2021-11-07 04:23:05 +01:00
|
|
|
import 'package:gecko/models/walletData.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;
|
|
|
|
|
2021-03-19 06:33:48 +01:00
|
|
|
int getCurrentChest() {
|
2021-11-07 04:23:05 +01:00
|
|
|
if (configBox.get('currentChest') == null) {
|
|
|
|
configBox.put('currentChest', 0);
|
2021-03-19 06:33:48 +01:00
|
|
|
}
|
2021-11-07 04:23:05 +01:00
|
|
|
|
|
|
|
return configBox.get('currentChest');
|
2021-03-19 06:33:48 +01:00
|
|
|
}
|
|
|
|
|
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-11-09 04:22:04 +01:00
|
|
|
final List _walletList = readAllWallets(getCurrentChest());
|
2021-03-11 07:08:45 +01:00
|
|
|
|
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-11-07 04:23:05 +01:00
|
|
|
List<WalletData> readAllWallets(int _chest) {
|
2021-11-07 06:50:09 +01:00
|
|
|
listWallets.clear();
|
2021-11-07 04:23:05 +01:00
|
|
|
walletBox.toMap().forEach((key, value) {
|
|
|
|
if (value.chest == _chest) {
|
|
|
|
listWallets.add(value);
|
|
|
|
}
|
2021-01-26 21:00:26 +01:00
|
|
|
});
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2021-01-26 21:00:26 +01:00
|
|
|
return listWallets;
|
|
|
|
}
|
2021-01-28 15:10:09 +01:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
WalletData getWalletData(List<int> _id) {
|
|
|
|
if (_id.isEmpty) return WalletData();
|
2021-11-09 04:22:04 +01:00
|
|
|
int _chest = _id[getCurrentChest()];
|
2021-11-07 04:23:05 +01:00
|
|
|
int _nbr = _id[1];
|
2021-11-07 06:50:09 +01:00
|
|
|
var _targetedWallet;
|
2021-04-04 15:35:38 +02:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
walletBox.toMap().forEach((key, value) {
|
|
|
|
if (value.chest == _chest && value.number == _nbr) {
|
2021-11-07 06:50:09 +01:00
|
|
|
_targetedWallet = value;
|
|
|
|
return false;
|
2021-11-07 04:23:05 +01:00
|
|
|
}
|
|
|
|
});
|
2021-04-04 18:53:32 +02:00
|
|
|
|
2021-11-07 06:50:09 +01:00
|
|
|
return _targetedWallet;
|
2021-04-04 15:35:38 +02:00
|
|
|
}
|
|
|
|
|
2021-03-29 00:52:25 +02:00
|
|
|
void getDefaultWallet() {
|
2021-11-07 06:50:09 +01:00
|
|
|
MyWalletsProvider myWalletsProvider = MyWalletsProvider();
|
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
if (configBox.get('defaultWallet') == null) {
|
2021-11-09 04:22:04 +01:00
|
|
|
configBox.put('defaultWallet', [getCurrentChest(), 0]);
|
2021-04-04 15:35:38 +02:00
|
|
|
}
|
2021-11-07 06:50:09 +01:00
|
|
|
|
|
|
|
defaultWallet = myWalletsProvider
|
|
|
|
.getWalletData(configBox.get('defaultWallet').cast<int>());
|
2021-04-04 15:35:38 +02:00
|
|
|
}
|
|
|
|
|
2021-01-28 15:10:09 +01:00
|
|
|
Future<int> deleteAllWallet(context) async {
|
|
|
|
try {
|
2021-11-07 04:23:05 +01:00
|
|
|
log.w('DELETE ALL WALLETS ?');
|
2021-01-28 15:10:09 +01:00
|
|
|
|
|
|
|
final bool _answer = await _confirmDeletingAllWallets(context);
|
|
|
|
if (_answer) {
|
2021-11-07 04:23:05 +01:00
|
|
|
await walletBox.clear();
|
|
|
|
await chestBox.clear();
|
2021-11-07 06:50:09 +01:00
|
|
|
await configBox.delete('defaultWallet');
|
2021-04-05 20:48:28 +02:00
|
|
|
|
2021-01-28 15:10:09 +01:00
|
|
|
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(
|
2021-02-14 23:17:03 +01:00
|
|
|
title:
|
|
|
|
Text('Êtes-vous sûr de vouloir supprimer tous vos trousseaux ?'),
|
2021-01-28 15:10:09 +01:00
|
|
|
content: SingleChildScrollView(child: Text('')),
|
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
|
|
|
child: Text("Non"),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context, false);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextButton(
|
2021-04-28 23:44:07 +02:00
|
|
|
key: Key('confirmDeletingAllWallets'),
|
2021-01-28 15:10:09 +01:00
|
|
|
child: Text("Oui"),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.pop(context, true);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:08:45 +01:00
|
|
|
Future<void> generateNewDerivation(context, String _name) async {
|
2021-02-15 03:59:07 +01:00
|
|
|
int _newDerivationNbr;
|
2021-03-19 06:33:48 +01:00
|
|
|
int _newWalletNbr;
|
2021-11-09 04:22:04 +01:00
|
|
|
int _chest = getCurrentChest();
|
2021-11-07 04:23:05 +01:00
|
|
|
List<WalletData> _walletConfig = readAllWallets(_chest);
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
if (_walletConfig.isEmpty) {
|
2021-02-15 03:59:07 +01:00
|
|
|
_newDerivationNbr = 3;
|
2021-03-19 06:33:48 +01:00
|
|
|
_newWalletNbr = 0;
|
2021-02-15 03:59:07 +01:00
|
|
|
} else {
|
2021-11-07 04:23:05 +01:00
|
|
|
_newDerivationNbr = _walletConfig.last.derivation + 3;
|
|
|
|
_newWalletNbr = _walletConfig.last.number + 1;
|
2021-02-15 03:59:07 +01:00
|
|
|
}
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
WalletData newWallet = WalletData(
|
|
|
|
chest: _chest,
|
|
|
|
number: _newWalletNbr,
|
|
|
|
name: _name,
|
2021-11-11 14:45:12 +01:00
|
|
|
derivation: _newDerivationNbr,
|
|
|
|
imageName: '${_newWalletNbr % 3}.png');
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
await walletBox.add(newWallet);
|
2021-02-15 01:44:25 +01:00
|
|
|
|
2021-11-07 04:23:05 +01:00
|
|
|
notifyListeners();
|
2021-02-15 01:44:25 +01:00
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:10:09 +01:00
|
|
|
void rebuildWidget() {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2021-01-23 16:38:03 +01:00
|
|
|
}
|