This commit refactors the data repositories to remove user-specific filtering. It simplifies the data access logic by retrieving all data from the Hive boxes and removing the need to filter by `userId` in the repository methods. This change makes the app function in single-user mode and removes the need to manage multiple user contexts within the data layer. Specifically: - Removes `userId` parameter from repository methods. - Updates the setting repository to use fixed keys. - Removes user ID filtering from queries. - Removes user ID parameters from Cubits and Blocs - Updates initial data creation
67 lines
2.5 KiB
Dart
67 lines
2.5 KiB
Dart
import 'package:bloc/bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:budget_app/data/repositories/interfaces/itransaction_repository.dart';
|
|
import 'package:budget_app/models/transaction_record.dart';
|
|
|
|
part 'transaction_event.dart';
|
|
part 'transaction_state.dart';
|
|
|
|
class TransactionBloc extends Bloc<TransactionEvent, TransactionState> {
|
|
final ITransactionRepository _transactionRepository;
|
|
|
|
TransactionBloc({required ITransactionRepository transactionRepository})
|
|
: _transactionRepository = transactionRepository,
|
|
super(TransactionInitial()) {
|
|
on<LoadTransactions>(_onLoadTransactions);
|
|
on<AddTransaction>(_onAddTransaction);
|
|
on<UpdateTransaction>(_onUpdateTransaction);
|
|
on<DeleteTransaction>(_onDeleteTransaction);
|
|
}
|
|
|
|
void _onLoadTransactions(LoadTransactions event, Emitter<TransactionState> emit) async {
|
|
emit(TransactionLoading());
|
|
try {
|
|
final transactions = await _transactionRepository.getAll();
|
|
emit(TransactionLoaded(transactions: transactions));
|
|
} catch (e) {
|
|
emit(TransactionError(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
void _onAddTransaction(AddTransaction event, Emitter<TransactionState> emit) async {
|
|
try {
|
|
await _transactionRepository.add(event.transaction);
|
|
final transactions = await _transactionRepository.getAll();
|
|
emit(TransactionLoaded(transactions: transactions));
|
|
} catch (e) {
|
|
emit(TransactionError(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
void _onUpdateTransaction(UpdateTransaction event, Emitter<TransactionState> emit) async {
|
|
try {
|
|
await _transactionRepository.update(event.transaction);
|
|
final transactions = await _transactionRepository.getAll();
|
|
emit(TransactionLoaded(transactions: transactions));
|
|
} catch (e) {
|
|
emit(TransactionError(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
void _onDeleteTransaction(DeleteTransaction event, Emitter<TransactionState> emit) async {
|
|
try {
|
|
// Для удаления нам нужен userId, который мы можем получить из текущего состояния
|
|
if (state is TransactionLoaded) {
|
|
final loadedState = state as TransactionLoaded;
|
|
if (loadedState.transactions.isNotEmpty) {
|
|
await _transactionRepository.delete(event.transactionId);
|
|
final transactions = await _transactionRepository.getAll();
|
|
emit(TransactionLoaded(transactions: transactions));
|
|
}
|
|
}
|
|
} catch (e) {
|
|
emit(TransactionError(message: e.toString()));
|
|
}
|
|
}
|
|
}
|