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';
|
2023-12-31 11:42:27 +01:00
|
|
|
import 'package:prasule/main.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Used for [Wallet]-managing operations
|
2023-09-08 11:50:21 +02:00
|
|
|
class WalletManager {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Returns a list of all [Wallet]s
|
2023-11-01 18:39:21 +01:00
|
|
|
static Future<List<Wallet>> listWallets() async {
|
2023-12-29 21:39:54 +01:00
|
|
|
final path =
|
2023-09-08 11:50:21 +02:00
|
|
|
Directory("${(await getApplicationDocumentsDirectory()).path}/wallets");
|
|
|
|
if (!path.existsSync()) {
|
|
|
|
path.createSync();
|
|
|
|
}
|
2023-12-29 21:39:54 +01:00
|
|
|
final wallets = <Wallet>[];
|
|
|
|
for (final w
|
|
|
|
in path.listSync().map((e) => e.path.split("/").last).toList()) {
|
2023-11-21 20:23:14 +01:00
|
|
|
try {
|
|
|
|
wallets.add(await loadWallet(w));
|
|
|
|
} catch (e) {
|
2023-12-31 11:42:27 +01:00
|
|
|
logger.e(e);
|
2023-11-21 20:23:14 +01:00
|
|
|
// TODO: do something with unreadable wallets
|
|
|
|
}
|
2023-11-01 18:39:21 +01:00
|
|
|
}
|
2023-12-31 11:42:27 +01:00
|
|
|
logger.i(wallets.length);
|
2023-11-01 18:39:21 +01:00
|
|
|
return wallets;
|
2023-09-08 11:50:21 +02:00
|
|
|
}
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Loads and returns a single [Wallet] by name
|
2023-09-08 11:50:21 +02:00
|
|
|
static Future<Wallet> loadWallet(String name) async {
|
2023-12-29 21:39:54 +01:00
|
|
|
final path =
|
2023-09-08 11:50:21 +02:00
|
|
|
Directory("${(await getApplicationDocumentsDirectory()).path}/wallets");
|
2023-12-29 21:39:54 +01:00
|
|
|
final wallet = File("${path.path}/$name");
|
2023-09-08 11:50:21 +02:00
|
|
|
if (!path.existsSync()) {
|
|
|
|
path.createSync();
|
|
|
|
}
|
|
|
|
if (!wallet.existsSync()) {
|
|
|
|
return Future.error("Wallet does not exist");
|
|
|
|
}
|
2023-12-29 21:39:54 +01:00
|
|
|
return Wallet.fromJson(
|
2023-12-31 11:42:27 +01:00
|
|
|
jsonDecode(wallet.readAsStringSync()) as Map<String, dynamic>,
|
|
|
|
);
|
2023-09-08 11:50:21 +02:00
|
|
|
}
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Converts [Wallet] to JSON and saves it to AppData
|
2023-11-01 18:39:21 +01:00
|
|
|
static Future<bool> saveWallet(Wallet w) async {
|
2023-12-29 21:39:54 +01:00
|
|
|
final path =
|
2023-09-08 11:50:21 +02:00
|
|
|
Directory("${(await getApplicationDocumentsDirectory()).path}/wallets");
|
2023-12-29 21:39:54 +01:00
|
|
|
final wallet = File("${path.path}/${w.name}");
|
2023-09-08 11:50:21 +02:00
|
|
|
if (!path.existsSync()) {
|
|
|
|
path.createSync();
|
|
|
|
}
|
2023-11-21 20:23:14 +01:00
|
|
|
// if (!wallet.existsSync()) return false;
|
2023-09-08 11:50:21 +02:00
|
|
|
wallet.writeAsStringSync(jsonEncode(w.toJson()));
|
2023-12-31 11:42:27 +01:00
|
|
|
logger.i(wallet.existsSync());
|
2023-11-01 18:39:21 +01:00
|
|
|
return true;
|
2023-09-08 11:50:21 +02:00
|
|
|
}
|
2023-09-14 17:11:40 +02:00
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Deletes the corresponding [Wallet] file
|
2023-09-14 17:11:40 +02:00
|
|
|
static Future<void> deleteWallet(Wallet w) async {
|
2023-12-29 21:39:54 +01:00
|
|
|
final path =
|
2023-09-14 17:11:40 +02:00
|
|
|
Directory("${(await getApplicationDocumentsDirectory()).path}/wallets");
|
2023-12-29 21:39:54 +01:00
|
|
|
File("${path.path}/${w.name}").deleteSync();
|
2023-09-14 17:11:40 +02:00
|
|
|
}
|
2023-09-08 11:50:21 +02:00
|
|
|
}
|