added more blocs
This commit is contained in:
@@ -3,14 +3,18 @@ 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/budget.dart';
|
||||
import 'package:tunas/repositories/account/models/category.dart';
|
||||
import 'package:tunas/repositories/account/models/transaction.dart';
|
||||
|
||||
class AccountRepository {
|
||||
String accountFile = 'main_account.json';
|
||||
String accountFile = 'tunas_main_account.json';
|
||||
|
||||
final StorageClient _storageClient;
|
||||
|
||||
final _transactionsController = BehaviorSubject<List<Transaction>>.seeded(const []);
|
||||
final _categoriesController = BehaviorSubject<List<Category>>.seeded(const []);
|
||||
final _budgetController = BehaviorSubject<List<Budget>>.seeded(const []);
|
||||
|
||||
AccountRepository({
|
||||
required storageClient,
|
||||
@@ -21,12 +25,22 @@ class AccountRepository {
|
||||
init() async {
|
||||
final account = await getAccount();
|
||||
_transactionsController.add(account.transactions);
|
||||
_categoriesController.add(account.categories);
|
||||
_budgetController.add(account.budgets);
|
||||
}
|
||||
|
||||
Stream<List<Transaction>> getTransactionsStream() {
|
||||
return _transactionsController.asBroadcastStream();
|
||||
}
|
||||
|
||||
Stream<List<Category>> getCategoriesStream() {
|
||||
return _categoriesController.asBroadcastStream();
|
||||
}
|
||||
|
||||
Stream<List<Budget>> getBudgetsStream() {
|
||||
return _budgetController.asBroadcastStream();
|
||||
}
|
||||
|
||||
Future<Account> getAccount() async {
|
||||
String json = await _storageClient.load(accountFile);
|
||||
Map<String, dynamic> accountJson = jsonDecode(json);
|
||||
@@ -41,5 +55,14 @@ class AccountRepository {
|
||||
final account = Account(transactions: transactions);
|
||||
await saveAccount(account);
|
||||
_transactionsController.add(account.transactions);
|
||||
_categoriesController.add(account.categories);
|
||||
_budgetController.add(account.budgets);
|
||||
}
|
||||
|
||||
deleteAccount() async {
|
||||
await _storageClient.delete(accountFile);
|
||||
_transactionsController.add(const []);
|
||||
_categoriesController.add(const []);
|
||||
_budgetController.add(const []);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,32 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:tunas/repositories/account/models/budget.dart';
|
||||
import 'package:tunas/repositories/account/models/category.dart';
|
||||
|
||||
import 'transaction.dart';
|
||||
|
||||
class Account {
|
||||
List<Transaction> transactions;
|
||||
List<Budget> budgets;
|
||||
List<Category> categories;
|
||||
|
||||
Account({
|
||||
this.transactions = const [],
|
||||
this.budgets = const [],
|
||||
this.categories = const [],
|
||||
});
|
||||
|
||||
factory Account.fromJson(Map<String, dynamic> json) {
|
||||
return Account(
|
||||
transactions: (jsonDecode(json['transactions']) as List<dynamic>).map((transaction) => Transaction.fromJson(transaction)).toList(),
|
||||
budgets: (jsonDecode(json['budgets']) as List<dynamic>).map((budget) => Budget.fromJson(budget)).toList(),
|
||||
categories: (jsonDecode(json['categories']) as List<dynamic>).map((category) => Category.fromJson(category)).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, String> toJson() => {
|
||||
'transactions': jsonEncode(transactions.map((transaction) => transaction.toJson()).toList()),
|
||||
'budgets': jsonEncode(budgets.map((budget) => budget.toJson()).toList()),
|
||||
'categories': jsonEncode(categories.map((category) => category.toJson()).toList()),
|
||||
};
|
||||
}
|
||||
|
||||
21
lib/repositories/account/models/budget.dart
Normal file
21
lib/repositories/account/models/budget.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
class Budget {
|
||||
bool monthly;
|
||||
double value;
|
||||
|
||||
Budget({
|
||||
this.monthly = false,
|
||||
this.value = 0.0,
|
||||
});
|
||||
|
||||
factory Budget.fromJson(Map<String, dynamic> json) {
|
||||
return Budget(
|
||||
monthly: json['monthly'],
|
||||
value: double.parse(json['value']),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, String> toJson() => {
|
||||
'monthly': monthly.toString(),
|
||||
'value': value.toString(),
|
||||
};
|
||||
}
|
||||
21
lib/repositories/account/models/category.dart
Normal file
21
lib/repositories/account/models/category.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
class Category {
|
||||
String label;
|
||||
String color;
|
||||
|
||||
Category({
|
||||
this.label = '',
|
||||
this.color = '',
|
||||
});
|
||||
|
||||
factory Category.fromJson(Map<String, dynamic> json) {
|
||||
return Category(
|
||||
label: json['label'],
|
||||
color: json['color']
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, String> toJson() => {
|
||||
'label': label,
|
||||
'color': color,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user