Skip to content
Snippets Groups Projects
myWallets.dart 2.81 KiB
Newer Older
poka's avatar
poka committed
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
poka's avatar
poka committed
import 'package:gecko/globals.dart';
import 'package:provider/provider.dart';
poka's avatar
poka committed

class MyWalletsProvider with ChangeNotifier {
poka's avatar
poka committed
  List listWallets = [];
poka's avatar
poka committed

  bool checkIfWalletExist() {
poka's avatar
poka committed
    if (appPath == null) {
poka's avatar
poka committed
      return false;
    }

poka's avatar
poka committed
    var walletsFolder = new Directory("${appPath.path}/wallets/");
poka's avatar
poka committed

    bool isWalletFolderExist = walletsFolder.existsSync();

    if (!isWalletFolderExist) {
      Directory(walletsFolder.path).createSync();
    }

    List contents = walletsFolder.listSync();
    if (contents.length == 0) {
      print('No wallets detected');
      return false;
    } else {
      print('Some wallets have been detected:');
      for (var _wallets in contents) {
        print(_wallets);
      }
      return true;
    }
  }

poka's avatar
poka committed
  Future importWallet() async {}

  List getAllWalletsNames() {
    listWallets.clear();
    print('1');
    print(walletsDirectory.path);
    print('2');

    walletsDirectory
        .listSync(recursive: false, followLinks: false)
        .forEach((wallet) {
      String _name = wallet.path.split('/').last;
      print(_name);
      listWallets.add(_name);
    });
    return listWallets;
  }

  Future<int> deleteAllWallet(context) async {
    try {
      print('DELETE THAT ?: $walletsDirectory');

      final bool _answer = await _confirmDeletingAllWallets(context);

      if (_answer) {
        walletsDirectory.deleteSync(recursive: true);
        walletsDirectory.createSync();
        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 portefeuilles ?'),
          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.getAllWalletsNames();
                  _myWalletProvider.rebuildWidget();
                });
                Navigator.pop(context, true);
              },
            ),
          ],
        );
      },
    );
  }

  void rebuildWidget() {
    notifyListeners();
  }