2025-08-17 10:10:31 +07:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
|
import 'package:dartz/dartz.dart';
|
|
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
|
|
|
import 'package:injectable/injectable.dart';
|
|
|
|
|
|
|
|
|
|
import '../../../domain/analytic/analytic.dart';
|
|
|
|
|
import '../../../domain/analytic/repositories/i_analytic_repository.dart';
|
|
|
|
|
|
|
|
|
|
part 'sales_loader_event.dart';
|
|
|
|
|
part 'sales_loader_state.dart';
|
|
|
|
|
part 'sales_loader_bloc.freezed.dart';
|
|
|
|
|
|
|
|
|
|
@injectable
|
|
|
|
|
class SalesLoaderBloc extends Bloc<SalesLoaderEvent, SalesLoaderState> {
|
|
|
|
|
final IAnalyticRepository _analyticRepository;
|
|
|
|
|
SalesLoaderBloc(this._analyticRepository)
|
|
|
|
|
: super(SalesLoaderState.initial()) {
|
|
|
|
|
on<SalesLoaderEvent>(_onSalesLoaderEvent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _onSalesLoaderEvent(
|
|
|
|
|
SalesLoaderEvent event,
|
|
|
|
|
Emitter<SalesLoaderState> emit,
|
2025-08-18 16:43:07 +07:00
|
|
|
) {
|
|
|
|
|
return event.map(
|
|
|
|
|
rangeDateChanged: (e) async {
|
|
|
|
|
emit(state.copyWith(dateFrom: e.dateFrom, dateTo: e.dateTo));
|
|
|
|
|
},
|
|
|
|
|
fectched: (e) async {
|
|
|
|
|
emit(state.copyWith(isFetching: true, failureOptionSales: none()));
|
2025-08-17 10:10:31 +07:00
|
|
|
|
2025-08-18 16:43:07 +07:00
|
|
|
final result = await _analyticRepository.getSales(
|
|
|
|
|
dateFrom: state.dateFrom,
|
|
|
|
|
dateTo: state.dateTo,
|
|
|
|
|
);
|
2025-08-17 10:10:31 +07:00
|
|
|
|
2025-08-18 16:43:07 +07:00
|
|
|
var data = result.fold(
|
|
|
|
|
(f) => state.copyWith(failureOptionSales: optionOf(f)),
|
|
|
|
|
(sales) => state.copyWith(sales: sales),
|
|
|
|
|
);
|
2025-08-17 10:10:31 +07:00
|
|
|
|
2025-08-18 16:43:07 +07:00
|
|
|
emit(data.copyWith(isFetching: false));
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-08-17 10:10:31 +07:00
|
|
|
}
|
|
|
|
|
}
|