Adds the ability to manage categories in the settings page. This includes: - Registers CategoryCubit in the dependency injection container. - Introduces a new CategoryListPage for editing categories. - Adds translations for category management related text. - Adds unselected icon color to the theme. - Updates the date format in the transaction dialog.
50 lines
1.9 KiB
Dart
50 lines
1.9 KiB
Dart
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:budget_app/models/category.dart';
|
|
import 'package:hive_ce/hive.dart';
|
|
import 'package:budget_app/services/user_service.dart'; // Импортируем UserService
|
|
|
|
class CategoryCubit extends Cubit<List<Category>> {
|
|
final UserService _userService; // Добавляем зависимость от UserService
|
|
final Box<Category> _categoryBox; // Используем Box<Category> для типизации
|
|
|
|
CategoryCubit(this._userService) // Принимаем UserService через конструктор
|
|
: _categoryBox = Hive.box<Category>('categories'),
|
|
super([]);
|
|
|
|
// Загружает категории, фильтруя их по userId текущего пользователя
|
|
void loadCategories() {
|
|
final currentUserId = _userService.currentUser?.id;
|
|
if (currentUserId != null) {
|
|
final userCategories = _categoryBox.values
|
|
.where((category) => category.userId == currentUserId)
|
|
.toList();
|
|
emit(List.from(userCategories));
|
|
} else {
|
|
emit([]); // Если пользователя нет, список категорий пуст
|
|
}
|
|
}
|
|
|
|
// Добавляет новую категорию, присваивая ей userId текущего пользователя
|
|
void addCategory(Category category) {
|
|
final currentUserId = _userService.currentUser?.id;
|
|
if (currentUserId != null) {
|
|
final newCategory = category.copyWith(userId: currentUserId); // Присваиваем userId
|
|
_categoryBox.put(newCategory.id, newCategory);
|
|
loadCategories();
|
|
}
|
|
}
|
|
|
|
// Обновляет существующую категорию
|
|
void updateCategory(Category category) {
|
|
_categoryBox.put(category.id, category);
|
|
loadCategories();
|
|
}
|
|
|
|
// Удаляет категорию по ее id
|
|
void deleteCategory(String id) {
|
|
_categoryBox.delete(id);
|
|
loadCategories();
|
|
}
|
|
}
|