From 0e8b1cad447ba0a3be246e2ea217b99ebf8bd4d6 Mon Sep 17 00:00:00 2001 From: poka Date: Sun, 10 Jan 2021 11:49:28 +0100 Subject: [PATCH] Add list of wallets; Delete the wallet on click; --- lib/ui/generateWallets.dart | 73 +++++++++++++++++------------- lib/ui/myWallets.dart | 89 +++++++++++++++++++++++++++++++++++-- 2 files changed, 129 insertions(+), 33 deletions(-) diff --git a/lib/ui/generateWallets.dart b/lib/ui/generateWallets.dart index bdb7f02..dcae250 100644 --- a/lib/ui/generateWallets.dart +++ b/lib/ui/generateWallets.dart @@ -9,11 +9,13 @@ import 'dart:async'; import 'package:path_provider/path_provider.dart'; class GenerateWalletScreen extends StatefulWidget { + const GenerateWalletScreen({Key keyGenWallet}) : super(key: keyGenWallet); @override - _GenerateWalletState createState() => _GenerateWalletState(); + GenerateWalletState createState() => GenerateWalletState(); } -class _GenerateWalletState extends State { +class GenerateWalletState extends State { + GlobalKey _keyWallets = GlobalKey(); void initState() { super.initState(); DubpRust.setup(); @@ -60,7 +62,7 @@ class _GenerateWalletState extends State { body: SafeArea( child: Column(children: [ FutureBuilder( - future: checkIfWalletExist(), + future: checkIfWalletExist('tata'), builder: (context, fSnapshot) { if (fSnapshot.hasData) return Visibility( @@ -95,11 +97,12 @@ class _GenerateWalletState extends State { return Center(child: CircularProgressIndicator()); }), FutureBuilder( - future: checkIfWalletExist(), + future: checkIfWalletExist('tata'), builder: (context, fSnapshot) { if (fSnapshot.hasData) return Visibility( - visible: fSnapshot.data, child: MyWalletsScreen()); + visible: fSnapshot.data, + child: MyWalletsScreen(keyWallets: _keyWallets)); return Center(child: CircularProgressIndicator()); }), SizedBox(height: 8), @@ -250,10 +253,11 @@ class _GenerateWalletState extends State { return actualWallet; } - Future checkIfWalletExist() async { + Future checkIfWalletExist(_name) async { + final appPath = await _localPath; + final _walletFile = File('$appPath/wallets/$_name/wallet.dewif'); + // deleteWallet(); - final _walletFile = await _localWallet; - // final isExist = File(_walletFile.toString()).existsSync(); print(_walletFile.path); final isExist = await File(_walletFile.path).exists(); print('Wallet existe ? : ' + isExist.toString()); @@ -268,27 +272,10 @@ class _GenerateWalletState extends State { Future importWallet() async {} - Future deleteWallet() async { - try { - final _walletFile = await _localWallet; - - await _walletFile.delete(); - } catch (e) { - return 1; - } - } - Future get _localPath async { final directory = await getApplicationDocumentsDirectory(); - return directory.path; } - - Future get _localWallet async { - // /data/user/0/com.example.gecko/app_flutter/wallet.dewif - final path = await _localPath; - return File('$path/wallet.dewif'); - } } /* *** ValidStoreWalletScreen Class *** */ @@ -318,6 +305,8 @@ class _ValidStoreWalletScreen extends State { TextEditingController _mnemonicController = new TextEditingController(); TextEditingController _pubkey = new TextEditingController(); TextEditingController _pin = new TextEditingController(); + String walletName = 'tata'; + List _listWallets = []; @override Widget build(BuildContext context) { @@ -355,21 +344,45 @@ class _ValidStoreWalletScreen extends State { } Future storeWallet() async { - final walletFile = await _localWallet; + final appPath = await _localPath; + final walletFile = File('$appPath/wallets/${this.walletName}/wallet.dewif'); + + final isExist = await Directory('$appPath/wallets').exists(); + if (isExist == false) { + new Directory('$appPath/wallets').createSync(); + } + + new Directory('$appPath/wallets/${this.walletName}').createSync(); walletFile.writeAsString('${widget.generatedWallet.dewif}'); _pin.clear(); + + await getAllWalletsNames(); Navigator.pop(context, true); FocusScope.of(context).unfocus(); + + return this.walletName; } Future get _localPath async { final directory = await getApplicationDocumentsDirectory(); - return directory.path; } - Future get _localWallet async { - final path = await _localPath; - return File('$path/wallet.dewif'); + Future getAllWalletsNames() async { + final _appPath = await getApplicationDocumentsDirectory(); + // List _listWallets = []; + // _listWallets.add('tortuuue'); + this._listWallets.clear(); + print(_appPath); + + _appPath + .list(recursive: false, followLinks: false) + .listen((FileSystemEntity entity) { + print(entity.path.split('/').last); + this._listWallets.add(entity.path.split('/').last); + }); + + return _listWallets; + // final _local = await _appPath.path.list().toList(); } } diff --git a/lib/ui/myWallets.dart b/lib/ui/myWallets.dart index e84bf08..9928cdd 100644 --- a/lib/ui/myWallets.dart +++ b/lib/ui/myWallets.dart @@ -1,3 +1,4 @@ +import 'package:gecko/ui/generateWallets.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:dubp/dubp.dart'; @@ -8,22 +9,35 @@ import 'package:path_provider/path_provider.dart'; import 'package:pin_code_fields/pin_code_fields.dart'; class MyWalletsScreen extends StatefulWidget { + const MyWalletsScreen({Key keyWallets}) : super(key: keyWallets); @override - _MyWalletState createState() => _MyWalletState(); + MyWalletState createState() => MyWalletState(); } -class _MyWalletState extends State { +class MyWalletState extends State { + GlobalKey _keyGenWallet = GlobalKey(); StreamController errorController; + Directory appPath; + List _listWallets = []; void initState() { super.initState(); errorController = StreamController(); + initAppDirectory(); DubpRust.setup(); + // initAppDirectory(); + // _walletsList = await getAllWalletsNames(); // HistoryScreen( // keyHistory: _keyHistory, // ); } + void initAppDirectory() async { + appPath = await getApplicationDocumentsDirectory(); + appPath = Directory('${appPath.path}/wallets'); + _listWallets = getAllWalletsNames(); + } + TextEditingController _pubkey = new TextEditingController(); TextEditingController _enterPin = new TextEditingController(); final formKey = GlobalKey(); @@ -34,9 +48,20 @@ class _MyWalletState extends State { @override Widget build(BuildContext context) { + // final _walletsList = getAllWalletsNames(); return SafeArea( child: Column(children: [ SizedBox(height: 8), + for (var repository in this._listWallets) + ListTile( + contentPadding: const EdgeInsets.all(5.0), + leading: Text(repository, style: TextStyle(fontSize: 14.0)), + title: Text(repository, style: TextStyle(fontSize: 14.0)), + subtitle: Text(repository, style: TextStyle(fontSize: 14.0)), + dense: true, + onTap: () { + openWalletOptions(repository); + }), InkWell( child: TextField( enabled: false, @@ -160,6 +185,53 @@ class _MyWalletState extends State { } } + // Future checkIfWalletExist(_name) async { + // final appPath = await _localPath; + // final _walletFile = File('$appPath/$_name/wallet.dewif'); + + // // deleteWallet(); + // print(_walletFile.path); + // final isExist = await File(_walletFile.path).exists(); + // print('Wallet existe ? : ' + isExist.toString()); + // print('Is wallet generated ? : ' + walletIsGenerated.toString()); + // if (isExist == true) { + // print('Un wallet existe !'); + // return true; + // } else { + // return false; + // } + // } + + List getAllWalletsNames() { + // final _appPath = await getApplicationDocumentsDirectory(); + // List _listWallets = []; + // _listWallets.add('tortuuue'); + this._listWallets.clear(); + print(this.appPath); + + this + .appPath + .list(recursive: false, followLinks: false) + .listen((FileSystemEntity entity) { + print(entity.path.split('/').last); + this._listWallets.add(entity.path.split('/').last); + }); + + return _listWallets; + + // final _local = await _appPath.path.list().toList(); + } + + Future openWalletOptions(_name) async { + deleteWallet(_name); + // getAllWalletsNames(); + // setState(() {}); + // GenerateWalletScreen(keyGenWallet: _keyGenWallet); + // _keyGenWallet.currentState.setState(() { + // getAllWalletsNames(); + // }); + } + Future readLocalWallet(String _pin) async { // print(pin); try { @@ -186,9 +258,20 @@ class _MyWalletState extends State { } } + Future deleteWallet(_name) async { + try { + final appPath = await _localPath; + final _walletFile = File('$appPath/wallets/$_name/wallet.dewif'); + + await _walletFile.delete(); + return 0; + } catch (e) { + return 1; + } + } + Future get _localPath async { final directory = await getApplicationDocumentsDirectory(); - return directory.path; }