Skip to content

Commit

Permalink
bloc change / add freezed
Browse files Browse the repository at this point in the history
  • Loading branch information
Taverz committed Aug 23, 2024
1 parent 84984a3 commit 9d6d636
Show file tree
Hide file tree
Showing 7 changed files with 1,583 additions and 49 deletions.
72 changes: 67 additions & 5 deletions lib/src/features/sales_mode/bloc/products/products_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,75 @@
import 'package:api_client/models/models.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:waiter_test/src/features/sales_mode/data/repository_product.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart' as bloc_concurrency;
part 'products_bloc.freezed.dart';

part 'products_events.dart';
part 'products_state.dart';
/// dart run build_runner build --delete-conflicting-outputs
@freezed
class ProductsEvent with _$ProductsEvent {
const ProductsEvent._();

const factory ProductsEvent.initial() = ProductsEventInitial;
const factory ProductsEvent.loading() = ProductsEventLoading;
const factory ProductsEvent.success() = ProductsEventSucess;
const factory ProductsEvent.failure() = ProductsEventFailure;
}

@freezed
class ProductsState with _$ProductsState {
const ProductsState._();

const factory ProductsState.initial() = ProductsStateInitial;
const factory ProductsState.loading() = ProductsStateLoading;
const factory ProductsState.success(List<Product> products) =
ProductsStateSuccess;
const factory ProductsState.failure(String error) = ProductsStateFailure;
}

class ProductsBloc extends Bloc<ProductsEvent, ProductsState> {
final RepositoryProduct repositoryProduct;

ProductsBloc({required this.repositoryProduct})
: super(const ProductsState(
status: ProductsStatus.initial,
));
: super(const ProductsState.initial()) {
on<ProductsEvent>(
(event, emit) => event.map(
initial: (event) => _init(event, emit),
loading: (event) => _loadProducts(event, emit),
success: (event) => _handleSuccess(event, emit),
failure: (event) => _handleFailure(event, emit),
),
transformer: bloc_concurrency.sequential(),
);
}

Future<void> _init(ProductsEvent event, Emitter<ProductsState> emit) async {
emit(const ProductsState.loading());
try {
final products = await repositoryProduct.readAllProduct();
emit(ProductsState.success(products));
} catch (e) {
emit(ProductsState.failure(e.toString()));
}
}

Future<void> _loadProducts(
ProductsEvent event, Emitter<ProductsState> emit) async {
emit(const ProductsState.loading());
try {
final products = await repositoryProduct.readAllProduct();
emit(ProductsState.success(products));
} catch (e) {
emit(ProductsState.failure(e.toString()));
}
}

Future<void> _handleSuccess(
ProductsEvent event, Emitter<ProductsState> emit) async {
// TODO
}

Future<void> _handleFailure(
ProductsEvent event, Emitter<ProductsState> emit) async {}
}
Loading

0 comments on commit 9d6d636

Please sign in to comment.