2023-09-08 11:50:21 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:prasule/api/wallet.dart';
|
|
|
|
|
|
|
|
class WalletManager {
|
|
|
|
static Future<List<String>> listWallets() async {
|
|
|
|
var path =
|
|
|
|
Directory("${(await getApplicationDocumentsDirectory()).path}/wallets");
|
|
|
|
if (!path.existsSync()) {
|
|
|
|
path.createSync();
|
|
|
|
}
|
|
|
|
return path.listSync().map((e) => e.path.split("/").last).toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<Wallet> 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<void> saveWallet(Wallet w) async {
|
|
|
|
var path =
|
|
|
|
Directory("${(await getApplicationDocumentsDirectory()).path}/wallets");
|
|
|
|
var wallet = File("${path.path}/${w.name}");
|
|
|
|
if (!path.existsSync()) {
|
|
|
|
path.createSync();
|
|
|
|
}
|
|
|
|
wallet.writeAsStringSync(jsonEncode(w.toJson()));
|
|
|
|
}
|
2023-09-14 17:11:40 +02:00
|
|
|
|
|
|
|
static Future<void> deleteWallet(Wallet w) async {
|
|
|
|
var path =
|
|
|
|
Directory("${(await getApplicationDocumentsDirectory()).path}/wallets");
|
|
|
|
var wallet = File("${path.path}/${w.name}");
|
|
|
|
wallet.deleteSync();
|
|
|
|
}
|
2023-09-08 11:50:21 +02:00
|
|
|
}
|