222 lines
7.3 KiB
Dart
222 lines
7.3 KiB
Dart
import 'dart:async';
|
|
import 'package:bloc/bloc.dart';
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
import 'package:okane/database/collections/account.dart';
|
|
import 'package:okane/database/collections/beneficiary.dart';
|
|
import 'package:okane/database/collections/budget.dart';
|
|
import 'package:okane/database/collections/expense_category.dart';
|
|
import 'package:okane/database/collections/loan.dart';
|
|
import 'package:okane/database/collections/recurrent.dart';
|
|
import 'package:okane/database/collections/template.dart';
|
|
import 'package:okane/database/collections/transaction.dart';
|
|
import 'package:okane/database/database.dart' as db;
|
|
import 'package:okane/ui/navigation.dart';
|
|
|
|
part 'core.freezed.dart';
|
|
|
|
@freezed
|
|
abstract class CoreState with _$CoreState {
|
|
const factory CoreState({
|
|
@Default(OkanePage.accounts) OkanePage activePage,
|
|
int? activeAccountIndex,
|
|
@Default(null) Transaction? activeTransaction,
|
|
@Default([]) List<Account> accounts,
|
|
@Default([]) List<RecurringTransaction> recurringTransactions,
|
|
@Default([]) List<Transaction> transactions,
|
|
@Default([]) List<TransactionTemplate> transactionTemplates,
|
|
@Default([]) List<Beneficiary> beneficiaries,
|
|
@Default([]) List<ExpenseCategory> expenseCategories,
|
|
@Default([]) List<Budget> budgets,
|
|
@Default(null) Budget? activeBudget,
|
|
@Default([]) List<Loan> loans,
|
|
@Default(null) Loan? activeLoan,
|
|
@Default(false) bool isDeletingAccount,
|
|
}) = _CoreState;
|
|
}
|
|
|
|
class CoreCubit extends Cubit<CoreState> {
|
|
CoreCubit() : super(CoreState());
|
|
|
|
StreamSubscription<void>? _recurringTransactionStreamSubscription;
|
|
StreamSubscription<void>? _transactionTemplatesStreamSubcription;
|
|
StreamSubscription<void>? _accountsStreamSubscription;
|
|
StreamSubscription<void>? _transactionsStreamSubscription;
|
|
StreamSubscription<void>? _beneficiariesStreamSubscription;
|
|
StreamSubscription<void>? _expenseCategoryStreamSubscription;
|
|
StreamSubscription<void>? _budgetsStreamSubscription;
|
|
StreamSubscription<void>? _loanStreamSubscription;
|
|
|
|
void setupAccountStream() {
|
|
_accountsStreamSubscription?.cancel();
|
|
_accountsStreamSubscription = db.watchAccounts().listen((_) async {
|
|
final resetStreams = state.activeAccountIndex == null;
|
|
final accounts = await db.getAccounts();
|
|
emit(
|
|
state.copyWith(
|
|
accounts: accounts,
|
|
activeAccountIndex: state.activeAccountIndex ?? 0,
|
|
),
|
|
);
|
|
|
|
if (resetStreams) {
|
|
setupStreams(accounts[0]);
|
|
}
|
|
});
|
|
}
|
|
|
|
void setupStreams(Account account) {
|
|
setupAccountStream();
|
|
_recurringTransactionStreamSubscription?.cancel();
|
|
_recurringTransactionStreamSubscription = db
|
|
.watchRecurringTransactions(activeAccount!)
|
|
.listen((_) async {
|
|
print("RECURRING UPDATE");
|
|
emit(
|
|
state.copyWith(
|
|
recurringTransactions: await db.getRecurringTransactions(
|
|
activeAccount!,
|
|
),
|
|
),
|
|
);
|
|
});
|
|
_transactionTemplatesStreamSubcription?.cancel();
|
|
_transactionTemplatesStreamSubcription = db
|
|
.watchTransactionTemplates(activeAccount!)
|
|
.listen((_) async {
|
|
emit(
|
|
state.copyWith(
|
|
transactionTemplates: await db.getTransactionTemplates(
|
|
activeAccount!,
|
|
),
|
|
),
|
|
);
|
|
});
|
|
_transactionsStreamSubscription?.cancel();
|
|
_transactionsStreamSubscription = db
|
|
.watchTransactions(activeAccount!)
|
|
.listen((_) async {
|
|
emit(
|
|
state.copyWith(
|
|
transactions: await db.getTransactions(activeAccount!),
|
|
),
|
|
);
|
|
});
|
|
_beneficiariesStreamSubscription?.cancel();
|
|
_beneficiariesStreamSubscription = db.watchBeneficiaries().listen((
|
|
_,
|
|
) async {
|
|
emit(state.copyWith(beneficiaries: await db.getBeneficiaries()));
|
|
});
|
|
_expenseCategoryStreamSubscription?.cancel();
|
|
_expenseCategoryStreamSubscription = db.watchExpenseCategory().listen((
|
|
_,
|
|
) async {
|
|
emit(state.copyWith(expenseCategories: await db.getExpenseCategories()));
|
|
});
|
|
_budgetsStreamSubscription?.cancel();
|
|
_budgetsStreamSubscription = db.watchBudgets(activeAccount!).listen((
|
|
_,
|
|
) async {
|
|
emit(state.copyWith(budgets: await db.getBudgets(activeAccount!)));
|
|
});
|
|
_loanStreamSubscription?.cancel();
|
|
_loanStreamSubscription = db.watchLoans().listen((_) async {
|
|
emit(state.copyWith(loans: await db.getLoans()));
|
|
});
|
|
}
|
|
|
|
void cancelStreams() {
|
|
_recurringTransactionStreamSubscription?.cancel();
|
|
_accountsStreamSubscription?.cancel();
|
|
_beneficiariesStreamSubscription?.cancel();
|
|
_budgetsStreamSubscription?.cancel();
|
|
_expenseCategoryStreamSubscription?.cancel();
|
|
_transactionsStreamSubscription?.cancel();
|
|
_transactionTemplatesStreamSubcription?.cancel();
|
|
_loanStreamSubscription?.cancel();
|
|
}
|
|
|
|
Future<void> init() async {
|
|
final accounts = await db.getAccounts();
|
|
final account = accounts.isEmpty ? null : accounts[0];
|
|
emit(
|
|
state.copyWith(
|
|
accounts: accounts,
|
|
activeAccountIndex: accounts.isEmpty ? null : 0,
|
|
transactions: await db.getTransactions(account),
|
|
beneficiaries: await db.getBeneficiaries(),
|
|
transactionTemplates: await db.getTransactionTemplates(account),
|
|
recurringTransactions: await db.getRecurringTransactions(account),
|
|
expenseCategories: await db.getExpenseCategories(),
|
|
budgets: await db.getBudgets(account),
|
|
loans: await db.getLoans(),
|
|
),
|
|
);
|
|
|
|
if (account != null) {
|
|
setupStreams(account);
|
|
} else {
|
|
setupAccountStream();
|
|
}
|
|
print("Core init done");
|
|
}
|
|
|
|
void setPage(OkanePage page) {
|
|
emit(state.copyWith(activePage: page));
|
|
}
|
|
|
|
Future<void> setActiveAccountIndex(int index) async {
|
|
final account = state.accounts[index];
|
|
emit(
|
|
state.copyWith(
|
|
activeAccountIndex: index,
|
|
transactions: await db.getTransactions(account),
|
|
beneficiaries: await db.getBeneficiaries(),
|
|
transactionTemplates: await db.getTransactionTemplates(account),
|
|
recurringTransactions: await db.getRecurringTransactions(account),
|
|
budgets: await db.getBudgets(account),
|
|
activeBudget: null,
|
|
activeTransaction: null,
|
|
activeLoan: null,
|
|
),
|
|
);
|
|
setupStreams(account);
|
|
}
|
|
|
|
void setActiveTransaction(Transaction? item) {
|
|
emit(state.copyWith(activeTransaction: item));
|
|
}
|
|
|
|
void setAccounts(List<Account> accounts) {
|
|
emit(state.copyWith(accounts: accounts));
|
|
}
|
|
|
|
void setActiveBudget(Budget? budget) {
|
|
emit(state.copyWith(activeBudget: budget));
|
|
}
|
|
|
|
Future<void> deleteAccount(Account account) async {
|
|
final l = List.of(state.accounts);
|
|
l.removeWhere((a) => a.id == account.id);
|
|
final newIndex = l.isEmpty ? null : 0;
|
|
emit(state.copyWith(activeAccountIndex: newIndex, isDeletingAccount: true));
|
|
|
|
cancelStreams();
|
|
try {
|
|
await db.deleteAccount(account);
|
|
} finally {
|
|
emit(state.copyWith(isDeletingAccount: false));
|
|
}
|
|
await init();
|
|
}
|
|
|
|
void setActiveLoan(Loan loan) {
|
|
emit(state.copyWith(activeLoan: loan));
|
|
}
|
|
|
|
Account? get activeAccount =>
|
|
state.activeAccountIndex == null
|
|
? null
|
|
: state.accounts[state.activeAccountIndex!];
|
|
}
|