prasule/lib/api/walletmanager.dart
Matyáš Caras d889611e19
feat: create basic spending chart + fix sorting
Added a basic line chart for spending (monthly and yearly) with its separate view. Also fixed sorting of entry groups on homepage.

#8
2023-11-21 20:23:14 +01:00

56 lines
1.6 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:prasule/api/wallet.dart';
class WalletManager {
static Future<List<Wallet>> listWallets() async {
var path =
Directory("${(await getApplicationDocumentsDirectory()).path}/wallets");
if (!path.existsSync()) {
path.createSync();
}
var wallets = <Wallet>[];
for (var w in path.listSync().map((e) => e.path.split("/").last).toList()) {
try {
wallets.add(await loadWallet(w));
} catch (e) {
// TODO: do something with unreadable wallets
}
}
return wallets;
}
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<bool> 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<void> deleteWallet(Wallet w) async {
var path =
Directory("${(await getApplicationDocumentsDirectory()).path}/wallets");
var wallet = File("${path.path}/${w.name}");
wallet.deleteSync();
}
}