import 'dart:convert'; import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:prasule/api/wallet.dart'; class WalletManager { static Future> listWallets() async { var path = Directory("${(await getApplicationDocumentsDirectory()).path}/wallets"); if (!path.existsSync()) { path.createSync(); } var wallets = []; for (var w in path.listSync().map((e) => e.path.split("/").last).toList()) { wallets.add(await loadWallet(w)); } return wallets; } static Future loadWallet(String name) async { var path = Directory("${(await getApplicationDocumentsDirectory()).path}/wallets"); var wallet = File("${path.path}/$name"); if (!path.existsSync()) { path.createSync(); } if (!wallet.existsSync()) { return Future.error("Wallet does not exist"); } return Wallet.fromJson(jsonDecode(wallet.readAsStringSync())); } static Future saveWallet(Wallet w) async { var path = Directory("${(await getApplicationDocumentsDirectory()).path}/wallets"); var wallet = File("${path.path}/${w.name}"); if (!path.existsSync()) { path.createSync(); } if (wallet.existsSync()) return false; wallet.writeAsStringSync(jsonEncode(w.toJson())); return true; } static Future deleteWallet(Wallet w) async { var path = Directory("${(await getApplicationDocumentsDirectory()).path}/wallets"); var wallet = File("${path.path}/${w.name}"); wallet.deleteSync(); } }