Files
BudgetApp/lib/logic/category/category_cubit.dart
T
Sanders 8b75299d41 Refactors data repositories for single-user mode
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
2025-07-12 21:54:00 +03:00

62 lines
2.2 KiB
Dart

import 'package:equatable/equatable.dart'; // equatable для сравнения объектов
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../data/repositories/interfaces/icategory_repository.dart';
import '../../models/category.dart';
part 'category_state.dart'; // Используем part для разделения файла
/// Cubit для управления категориями
/// Изменения:
/// - Добавлено хранение userId в cubit
/// - Упрощена работа с состояниями по аналогии с TagCubit
class CategoryCubit extends Cubit<CategoryState> {
final ICategoryRepository repository;
CategoryCubit(this.repository) : super(CategoryInitial());
/// Загружает категории для текущего пользователя
Future<void> loadCategories() async {
emit(CategoryLoading());
try {
final categories = await repository.getAll();
emit(CategoryLoaded(categories));
} catch (e) {
emit(CategoryError('Ошибка загрузки категорий: $e'));
}
}
/// Добавляет новую категорию
Future<void> addCategory(Category category) async {
try {
// Устанавливаем userId для категории из cubit
final newCategory = category.copyWith();
await repository.add(newCategory);
// Перезагружаем список категорий
await loadCategories();
} catch (e) {
emit(CategoryError('Ошибка добавления категории: $e'));
}
}
/// Обновляет существующую категорию
Future<void> updateCategory(Category category) async {
try {
await repository.update(category);
await loadCategories();
} catch (e) {
emit(CategoryError('Ошибка обновления категории: $e'));
}
}
/// Удаляет категорию по её id
Future<void> deleteCategory(String id) async {
try {
await repository.delete(id);
await loadCategories();
} catch (e) {
emit(CategoryError('Ошибка удаления категории: $e'));
}
}
}