feat: profit loss

This commit is contained in:
efrilm
2025-08-17 22:36:46 +07:00
parent beb9ead4da
commit 50b06da627
21 changed files with 4548 additions and 424 deletions
@@ -0,0 +1,44 @@
import 'package:dartz/dartz.dart';
import 'package:flutter_bloc/flutter_bloc.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 'profit_loss_loader_event.dart';
part 'profit_loss_loader_state.dart';
part 'profit_loss_loader_bloc.freezed.dart';
@injectable
class ProfitLossLoaderBloc
extends Bloc<ProfitLossLoaderEvent, ProfitLossLoaderState> {
final IAnalyticRepository _repository;
ProfitLossLoaderBloc(this._repository)
: super(ProfitLossLoaderState.initial()) {
on<ProfitLossLoaderEvent>(_onProfitLossLoaderEvent);
}
Future<void> _onProfitLossLoaderEvent(
ProfitLossLoaderEvent event,
Emitter<ProfitLossLoaderState> emit,
) {
return event.map(
fetched: (e) async {
emit(state.copyWith(isFetching: true, failureOptionProfitLoss: none()));
final result = await _repository.getProfitLoss(
dateFrom: DateTime.now().subtract(const Duration(days: 30)),
dateTo: DateTime.now(),
);
var data = result.fold(
(f) => state.copyWith(failureOptionProfitLoss: optionOf(f)),
(profitLoss) => state.copyWith(profitLoss: profitLoss),
);
emit(data.copyWith(isFetching: false));
},
);
}
}
@@ -0,0 +1,384 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'profit_loss_loader_bloc.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models',
);
/// @nodoc
mixin _$ProfitLossLoaderEvent {
@optionalTypeArgs
TResult when<TResult extends Object?>({
required TResult Function() fetched,
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function()? fetched,
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function()? fetched,
required TResult orElse(),
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult map<TResult extends Object?>({
required TResult Function(_Fetched value) fetched,
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
TResult? Function(_Fetched value)? fetched,
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeMap<TResult extends Object?>({
TResult Function(_Fetched value)? fetched,
required TResult orElse(),
}) => throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $ProfitLossLoaderEventCopyWith<$Res> {
factory $ProfitLossLoaderEventCopyWith(
ProfitLossLoaderEvent value,
$Res Function(ProfitLossLoaderEvent) then,
) = _$ProfitLossLoaderEventCopyWithImpl<$Res, ProfitLossLoaderEvent>;
}
/// @nodoc
class _$ProfitLossLoaderEventCopyWithImpl<
$Res,
$Val extends ProfitLossLoaderEvent
>
implements $ProfitLossLoaderEventCopyWith<$Res> {
_$ProfitLossLoaderEventCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of ProfitLossLoaderEvent
/// with the given fields replaced by the non-null parameter values.
}
/// @nodoc
abstract class _$$FetchedImplCopyWith<$Res> {
factory _$$FetchedImplCopyWith(
_$FetchedImpl value,
$Res Function(_$FetchedImpl) then,
) = __$$FetchedImplCopyWithImpl<$Res>;
}
/// @nodoc
class __$$FetchedImplCopyWithImpl<$Res>
extends _$ProfitLossLoaderEventCopyWithImpl<$Res, _$FetchedImpl>
implements _$$FetchedImplCopyWith<$Res> {
__$$FetchedImplCopyWithImpl(
_$FetchedImpl _value,
$Res Function(_$FetchedImpl) _then,
) : super(_value, _then);
/// Create a copy of ProfitLossLoaderEvent
/// with the given fields replaced by the non-null parameter values.
}
/// @nodoc
class _$FetchedImpl implements _Fetched {
const _$FetchedImpl();
@override
String toString() {
return 'ProfitLossLoaderEvent.fetched()';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType && other is _$FetchedImpl);
}
@override
int get hashCode => runtimeType.hashCode;
@override
@optionalTypeArgs
TResult when<TResult extends Object?>({required TResult Function() fetched}) {
return fetched();
}
@override
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({TResult? Function()? fetched}) {
return fetched?.call();
}
@override
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function()? fetched,
required TResult orElse(),
}) {
if (fetched != null) {
return fetched();
}
return orElse();
}
@override
@optionalTypeArgs
TResult map<TResult extends Object?>({
required TResult Function(_Fetched value) fetched,
}) {
return fetched(this);
}
@override
@optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
TResult? Function(_Fetched value)? fetched,
}) {
return fetched?.call(this);
}
@override
@optionalTypeArgs
TResult maybeMap<TResult extends Object?>({
TResult Function(_Fetched value)? fetched,
required TResult orElse(),
}) {
if (fetched != null) {
return fetched(this);
}
return orElse();
}
}
abstract class _Fetched implements ProfitLossLoaderEvent {
const factory _Fetched() = _$FetchedImpl;
}
/// @nodoc
mixin _$ProfitLossLoaderState {
ProfitLossAnalytic get profitLoss => throw _privateConstructorUsedError;
Option<AnalyticFailure> get failureOptionProfitLoss =>
throw _privateConstructorUsedError;
bool get isFetching => throw _privateConstructorUsedError;
/// Create a copy of ProfitLossLoaderState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$ProfitLossLoaderStateCopyWith<ProfitLossLoaderState> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $ProfitLossLoaderStateCopyWith<$Res> {
factory $ProfitLossLoaderStateCopyWith(
ProfitLossLoaderState value,
$Res Function(ProfitLossLoaderState) then,
) = _$ProfitLossLoaderStateCopyWithImpl<$Res, ProfitLossLoaderState>;
@useResult
$Res call({
ProfitLossAnalytic profitLoss,
Option<AnalyticFailure> failureOptionProfitLoss,
bool isFetching,
});
$ProfitLossAnalyticCopyWith<$Res> get profitLoss;
}
/// @nodoc
class _$ProfitLossLoaderStateCopyWithImpl<
$Res,
$Val extends ProfitLossLoaderState
>
implements $ProfitLossLoaderStateCopyWith<$Res> {
_$ProfitLossLoaderStateCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of ProfitLossLoaderState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? profitLoss = null,
Object? failureOptionProfitLoss = null,
Object? isFetching = null,
}) {
return _then(
_value.copyWith(
profitLoss: null == profitLoss
? _value.profitLoss
: profitLoss // ignore: cast_nullable_to_non_nullable
as ProfitLossAnalytic,
failureOptionProfitLoss: null == failureOptionProfitLoss
? _value.failureOptionProfitLoss
: failureOptionProfitLoss // ignore: cast_nullable_to_non_nullable
as Option<AnalyticFailure>,
isFetching: null == isFetching
? _value.isFetching
: isFetching // ignore: cast_nullable_to_non_nullable
as bool,
)
as $Val,
);
}
/// Create a copy of ProfitLossLoaderState
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$ProfitLossAnalyticCopyWith<$Res> get profitLoss {
return $ProfitLossAnalyticCopyWith<$Res>(_value.profitLoss, (value) {
return _then(_value.copyWith(profitLoss: value) as $Val);
});
}
}
/// @nodoc
abstract class _$$ProfitLossLoaderStateImplCopyWith<$Res>
implements $ProfitLossLoaderStateCopyWith<$Res> {
factory _$$ProfitLossLoaderStateImplCopyWith(
_$ProfitLossLoaderStateImpl value,
$Res Function(_$ProfitLossLoaderStateImpl) then,
) = __$$ProfitLossLoaderStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({
ProfitLossAnalytic profitLoss,
Option<AnalyticFailure> failureOptionProfitLoss,
bool isFetching,
});
@override
$ProfitLossAnalyticCopyWith<$Res> get profitLoss;
}
/// @nodoc
class __$$ProfitLossLoaderStateImplCopyWithImpl<$Res>
extends
_$ProfitLossLoaderStateCopyWithImpl<$Res, _$ProfitLossLoaderStateImpl>
implements _$$ProfitLossLoaderStateImplCopyWith<$Res> {
__$$ProfitLossLoaderStateImplCopyWithImpl(
_$ProfitLossLoaderStateImpl _value,
$Res Function(_$ProfitLossLoaderStateImpl) _then,
) : super(_value, _then);
/// Create a copy of ProfitLossLoaderState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? profitLoss = null,
Object? failureOptionProfitLoss = null,
Object? isFetching = null,
}) {
return _then(
_$ProfitLossLoaderStateImpl(
profitLoss: null == profitLoss
? _value.profitLoss
: profitLoss // ignore: cast_nullable_to_non_nullable
as ProfitLossAnalytic,
failureOptionProfitLoss: null == failureOptionProfitLoss
? _value.failureOptionProfitLoss
: failureOptionProfitLoss // ignore: cast_nullable_to_non_nullable
as Option<AnalyticFailure>,
isFetching: null == isFetching
? _value.isFetching
: isFetching // ignore: cast_nullable_to_non_nullable
as bool,
),
);
}
}
/// @nodoc
class _$ProfitLossLoaderStateImpl implements _ProfitLossLoaderState {
const _$ProfitLossLoaderStateImpl({
required this.profitLoss,
required this.failureOptionProfitLoss,
this.isFetching = false,
});
@override
final ProfitLossAnalytic profitLoss;
@override
final Option<AnalyticFailure> failureOptionProfitLoss;
@override
@JsonKey()
final bool isFetching;
@override
String toString() {
return 'ProfitLossLoaderState(profitLoss: $profitLoss, failureOptionProfitLoss: $failureOptionProfitLoss, isFetching: $isFetching)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$ProfitLossLoaderStateImpl &&
(identical(other.profitLoss, profitLoss) ||
other.profitLoss == profitLoss) &&
(identical(
other.failureOptionProfitLoss,
failureOptionProfitLoss,
) ||
other.failureOptionProfitLoss == failureOptionProfitLoss) &&
(identical(other.isFetching, isFetching) ||
other.isFetching == isFetching));
}
@override
int get hashCode =>
Object.hash(runtimeType, profitLoss, failureOptionProfitLoss, isFetching);
/// Create a copy of ProfitLossLoaderState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$ProfitLossLoaderStateImplCopyWith<_$ProfitLossLoaderStateImpl>
get copyWith =>
__$$ProfitLossLoaderStateImplCopyWithImpl<_$ProfitLossLoaderStateImpl>(
this,
_$identity,
);
}
abstract class _ProfitLossLoaderState implements ProfitLossLoaderState {
const factory _ProfitLossLoaderState({
required final ProfitLossAnalytic profitLoss,
required final Option<AnalyticFailure> failureOptionProfitLoss,
final bool isFetching,
}) = _$ProfitLossLoaderStateImpl;
@override
ProfitLossAnalytic get profitLoss;
@override
Option<AnalyticFailure> get failureOptionProfitLoss;
@override
bool get isFetching;
/// Create a copy of ProfitLossLoaderState
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$ProfitLossLoaderStateImplCopyWith<_$ProfitLossLoaderStateImpl>
get copyWith => throw _privateConstructorUsedError;
}
@@ -0,0 +1,6 @@
part of 'profit_loss_loader_bloc.dart';
@freezed
class ProfitLossLoaderEvent with _$ProfitLossLoaderEvent {
const factory ProfitLossLoaderEvent.fetched() = _Fetched;
}
@@ -0,0 +1,15 @@
part of 'profit_loss_loader_bloc.dart';
@freezed
class ProfitLossLoaderState with _$ProfitLossLoaderState {
const factory ProfitLossLoaderState({
required ProfitLossAnalytic profitLoss,
required Option<AnalyticFailure> failureOptionProfitLoss,
@Default(false) bool isFetching,
}) = _ProfitLossLoaderState;
factory ProfitLossLoaderState.initial() => ProfitLossLoaderState(
profitLoss: ProfitLossAnalytic.empty(),
failureOptionProfitLoss: none(),
);
}