added more blocs
This commit is contained in:
27
lib/domains/category/category_bloc.dart
Normal file
27
lib/domains/category/category_bloc.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:tunas/repositories/account/account_repository.dart';
|
||||
import 'package:tunas/repositories/account/models/category.dart';
|
||||
|
||||
part 'category_event.dart';
|
||||
part 'category_state.dart';
|
||||
|
||||
class CategoryBloc extends Bloc<CategoryEvent, CategoryState> {
|
||||
final AccountRepository _accountRepository;
|
||||
|
||||
CategoryBloc({required AccountRepository accountRepository}) : _accountRepository = accountRepository, super(const CategoryState()) {
|
||||
on<CategoriesLoad>(_onCategoriesLoad);
|
||||
|
||||
_accountRepository
|
||||
.getCategoriesStream()
|
||||
.listen((categories) => add(CategoriesLoad(categories)));
|
||||
}
|
||||
|
||||
_onCategoriesLoad(
|
||||
CategoriesLoad event, Emitter<CategoryState> emit
|
||||
) {
|
||||
emit(state.copyWith(
|
||||
categories: event.categories,
|
||||
));
|
||||
}
|
||||
}
|
||||
16
lib/domains/category/category_event.dart
Normal file
16
lib/domains/category/category_event.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
part of 'category_bloc.dart';
|
||||
|
||||
sealed class CategoryEvent extends Equatable {
|
||||
const CategoryEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class CategoriesLoad extends CategoryEvent {
|
||||
final List<Category> categories;
|
||||
const CategoriesLoad(this.categories);
|
||||
|
||||
@override
|
||||
List<Object> get props => [categories];
|
||||
}
|
||||
20
lib/domains/category/category_state.dart
Normal file
20
lib/domains/category/category_state.dart
Normal file
@@ -0,0 +1,20 @@
|
||||
part of 'category_bloc.dart';
|
||||
|
||||
final class CategoryState extends Equatable {
|
||||
final List<Category> categories;
|
||||
|
||||
const CategoryState({
|
||||
this.categories = const [],
|
||||
});
|
||||
|
||||
CategoryState copyWith({
|
||||
List<Category>? categories,
|
||||
}) {
|
||||
return CategoryState(
|
||||
categories: categories ?? this.categories,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [categories];
|
||||
}
|
||||
Reference in New Issue
Block a user