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);
|
||||
}
|
||||
}
|
||||
21
lib/repositories/account/models/account.dart
Normal file
21
lib/repositories/account/models/account.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'transaction.dart';
|
||||
|
||||
class Account {
|
||||
List<Transaction> transactions;
|
||||
|
||||
Account({
|
||||
this.transactions = const [],
|
||||
});
|
||||
|
||||
factory Account.fromJson(Map<String, dynamic> json) {
|
||||
return Account(
|
||||
transactions: (jsonDecode(json['transactions']) as List<dynamic>).map((transaction) => Transaction.fromJson(transaction)).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, String> toJson() => {
|
||||
'transactions': jsonEncode(transactions.map((transaction) => transaction.toJson()).toList()),
|
||||
};
|
||||
}
|
||||
33
lib/repositories/account/models/transaction.dart
Normal file
33
lib/repositories/account/models/transaction.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
class Transaction {
|
||||
DateTime date;
|
||||
String category;
|
||||
String description;
|
||||
String account;
|
||||
double value;
|
||||
|
||||
Transaction({
|
||||
required this.date,
|
||||
required this.category,
|
||||
required this.description,
|
||||
required this.account,
|
||||
required this.value
|
||||
});
|
||||
|
||||
factory Transaction.fromJson(Map<String, dynamic> json) {
|
||||
return Transaction(
|
||||
date: DateTime.parse(json['date']),
|
||||
category: json['category'],
|
||||
description: json['description'],
|
||||
account: json['account'],
|
||||
value: double.parse(json['value']),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, String> toJson() => {
|
||||
'date': date.toIso8601String(),
|
||||
'category': category,
|
||||
'description': description,
|
||||
'account': account,
|
||||
'value': value.toString(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user