basic csv loader, transaction list & half done stats page
This commit is contained in:
45
lib/repositories/account/account_repository.dart
Normal file
45
lib/repositories/account/account_repository.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:rxdart/subjects.dart';
|
||||
import 'package:tunas/clients/storage/storage_client.dart';
|
||||
import 'package:tunas/repositories/account/models/account.dart';
|
||||
import 'package:tunas/repositories/account/models/transaction.dart';
|
||||
|
||||
class AccountRepository {
|
||||
String accountFile = 'main_account.json';
|
||||
|
||||
final StorageClient _storageClient;
|
||||
|
||||
final _transactionsController = BehaviorSubject<List<Transaction>>.seeded(const []);
|
||||
|
||||
AccountRepository({
|
||||
required storageClient,
|
||||
}) : _storageClient = storageClient {
|
||||
init();
|
||||
}
|
||||
|
||||
init() async {
|
||||
final account = await getAccount();
|
||||
_transactionsController.add(account.transactions);
|
||||
}
|
||||
|
||||
Stream<List<Transaction>> getTransactionsStream() {
|
||||
return _transactionsController.asBroadcastStream();
|
||||
}
|
||||
|
||||
Future<Account> getAccount() async {
|
||||
String json = await _storageClient.load(accountFile);
|
||||
Map<String, dynamic> accountJson = jsonDecode(json);
|
||||
return Account.fromJson(accountJson);
|
||||
}
|
||||
|
||||
saveAccount(Account account) async {
|
||||
await _storageClient.save(accountFile, jsonEncode(account.toJson()));
|
||||
}
|
||||
|
||||
saveTransactions(List<Transaction> transactions) async {
|
||||
final account = Account(transactions: transactions);
|
||||
await saveAccount(account);
|
||||
_transactionsController.add(account.transactions);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user