payment method

This commit is contained in:
efrilm
2025-10-27 14:24:29 +07:00
parent 3135dde317
commit 9e5a6cc7ae
22 changed files with 3058 additions and 2 deletions
@@ -0,0 +1,83 @@
import 'package:bloc/bloc.dart';
import 'package:dartz/dartz.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';
import '../../../domain/payment_method/payment_method.dart';
part 'payment_method_loader_event.dart';
part 'payment_method_loader_state.dart';
part 'payment_method_loader_bloc.freezed.dart';
@injectable
class PaymentMethodLoaderBloc
extends Bloc<PaymentMethodLoaderEvent, PaymentMethodLoaderState> {
final IPaymentMethodRepository _repository;
PaymentMethodLoaderBloc(this._repository)
: super(PaymentMethodLoaderState.initial()) {
on<PaymentMethodLoaderEvent>(_onPaymentMethodLoaderEvent);
}
Future<void> _onPaymentMethodLoaderEvent(
PaymentMethodLoaderEvent event,
Emitter<PaymentMethodLoaderState> emit,
) {
return event.map(
fetched: (e) async {
var newState = state;
if (e.isRefresh) {
newState = newState.copyWith(isFetching: true);
emit(newState);
}
newState = await _mapFetchedToState(newState, isRefresh: e.isRefresh);
emit(newState);
},
);
}
Future<PaymentMethodLoaderState> _mapFetchedToState(
PaymentMethodLoaderState state, {
bool isRefresh = false,
}) async {
state = state.copyWith(isFetching: false);
if (state.hasReachedMax && state.paymentMethods.isNotEmpty && !isRefresh) {
return state;
}
if (isRefresh) {
state = state.copyWith(
page: 1,
failureOption: none(),
hasReachedMax: false,
paymentMethods: [],
);
}
final failureOrTable = await _repository.getPaymentMethods(
page: state.page,
);
state = failureOrTable.fold(
(f) {
if (state.paymentMethods.isNotEmpty) {
return state.copyWith(hasReachedMax: true);
}
return state.copyWith(failureOption: optionOf(f));
},
(paymentMethods) {
return state.copyWith(
paymentMethods: List.from(state.paymentMethods)
..addAll(paymentMethods.paymentMethods),
failureOption: none(),
page: state.page + 1,
hasReachedMax: paymentMethods.paymentMethods.length < 10,
);
},
);
return state;
}
}
@@ -0,0 +1,492 @@
// 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 'payment_method_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 _$PaymentMethodLoaderEvent {
bool get isRefresh => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult when<TResult extends Object?>({
required TResult Function(bool isRefresh) fetched,
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function(bool isRefresh)? fetched,
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function(bool isRefresh)? 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;
/// Create a copy of PaymentMethodLoaderEvent
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$PaymentMethodLoaderEventCopyWith<PaymentMethodLoaderEvent> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $PaymentMethodLoaderEventCopyWith<$Res> {
factory $PaymentMethodLoaderEventCopyWith(
PaymentMethodLoaderEvent value,
$Res Function(PaymentMethodLoaderEvent) then,
) = _$PaymentMethodLoaderEventCopyWithImpl<$Res, PaymentMethodLoaderEvent>;
@useResult
$Res call({bool isRefresh});
}
/// @nodoc
class _$PaymentMethodLoaderEventCopyWithImpl<
$Res,
$Val extends PaymentMethodLoaderEvent
>
implements $PaymentMethodLoaderEventCopyWith<$Res> {
_$PaymentMethodLoaderEventCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of PaymentMethodLoaderEvent
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({Object? isRefresh = null}) {
return _then(
_value.copyWith(
isRefresh: null == isRefresh
? _value.isRefresh
: isRefresh // ignore: cast_nullable_to_non_nullable
as bool,
)
as $Val,
);
}
}
/// @nodoc
abstract class _$$FetchedImplCopyWith<$Res>
implements $PaymentMethodLoaderEventCopyWith<$Res> {
factory _$$FetchedImplCopyWith(
_$FetchedImpl value,
$Res Function(_$FetchedImpl) then,
) = __$$FetchedImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({bool isRefresh});
}
/// @nodoc
class __$$FetchedImplCopyWithImpl<$Res>
extends _$PaymentMethodLoaderEventCopyWithImpl<$Res, _$FetchedImpl>
implements _$$FetchedImplCopyWith<$Res> {
__$$FetchedImplCopyWithImpl(
_$FetchedImpl _value,
$Res Function(_$FetchedImpl) _then,
) : super(_value, _then);
/// Create a copy of PaymentMethodLoaderEvent
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({Object? isRefresh = null}) {
return _then(
_$FetchedImpl(
isRefresh: null == isRefresh
? _value.isRefresh
: isRefresh // ignore: cast_nullable_to_non_nullable
as bool,
),
);
}
}
/// @nodoc
class _$FetchedImpl implements _Fetched {
const _$FetchedImpl({this.isRefresh = false});
@override
@JsonKey()
final bool isRefresh;
@override
String toString() {
return 'PaymentMethodLoaderEvent.fetched(isRefresh: $isRefresh)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$FetchedImpl &&
(identical(other.isRefresh, isRefresh) ||
other.isRefresh == isRefresh));
}
@override
int get hashCode => Object.hash(runtimeType, isRefresh);
/// Create a copy of PaymentMethodLoaderEvent
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$FetchedImplCopyWith<_$FetchedImpl> get copyWith =>
__$$FetchedImplCopyWithImpl<_$FetchedImpl>(this, _$identity);
@override
@optionalTypeArgs
TResult when<TResult extends Object?>({
required TResult Function(bool isRefresh) fetched,
}) {
return fetched(isRefresh);
}
@override
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function(bool isRefresh)? fetched,
}) {
return fetched?.call(isRefresh);
}
@override
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function(bool isRefresh)? fetched,
required TResult orElse(),
}) {
if (fetched != null) {
return fetched(isRefresh);
}
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 PaymentMethodLoaderEvent {
const factory _Fetched({final bool isRefresh}) = _$FetchedImpl;
@override
bool get isRefresh;
/// Create a copy of PaymentMethodLoaderEvent
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$FetchedImplCopyWith<_$FetchedImpl> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
mixin _$PaymentMethodLoaderState {
List<PaymentMethod> get paymentMethods => throw _privateConstructorUsedError;
Option<PaymentMethodFailure> get failureOption =>
throw _privateConstructorUsedError;
bool get hasReachedMax => throw _privateConstructorUsedError;
bool get isFetching => throw _privateConstructorUsedError;
int get page => throw _privateConstructorUsedError;
/// Create a copy of PaymentMethodLoaderState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$PaymentMethodLoaderStateCopyWith<PaymentMethodLoaderState> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $PaymentMethodLoaderStateCopyWith<$Res> {
factory $PaymentMethodLoaderStateCopyWith(
PaymentMethodLoaderState value,
$Res Function(PaymentMethodLoaderState) then,
) = _$PaymentMethodLoaderStateCopyWithImpl<$Res, PaymentMethodLoaderState>;
@useResult
$Res call({
List<PaymentMethod> paymentMethods,
Option<PaymentMethodFailure> failureOption,
bool hasReachedMax,
bool isFetching,
int page,
});
}
/// @nodoc
class _$PaymentMethodLoaderStateCopyWithImpl<
$Res,
$Val extends PaymentMethodLoaderState
>
implements $PaymentMethodLoaderStateCopyWith<$Res> {
_$PaymentMethodLoaderStateCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of PaymentMethodLoaderState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? paymentMethods = null,
Object? failureOption = null,
Object? hasReachedMax = null,
Object? isFetching = null,
Object? page = null,
}) {
return _then(
_value.copyWith(
paymentMethods: null == paymentMethods
? _value.paymentMethods
: paymentMethods // ignore: cast_nullable_to_non_nullable
as List<PaymentMethod>,
failureOption: null == failureOption
? _value.failureOption
: failureOption // ignore: cast_nullable_to_non_nullable
as Option<PaymentMethodFailure>,
hasReachedMax: null == hasReachedMax
? _value.hasReachedMax
: hasReachedMax // ignore: cast_nullable_to_non_nullable
as bool,
isFetching: null == isFetching
? _value.isFetching
: isFetching // ignore: cast_nullable_to_non_nullable
as bool,
page: null == page
? _value.page
: page // ignore: cast_nullable_to_non_nullable
as int,
)
as $Val,
);
}
}
/// @nodoc
abstract class _$$PaymentMethodLoaderStateImplCopyWith<$Res>
implements $PaymentMethodLoaderStateCopyWith<$Res> {
factory _$$PaymentMethodLoaderStateImplCopyWith(
_$PaymentMethodLoaderStateImpl value,
$Res Function(_$PaymentMethodLoaderStateImpl) then,
) = __$$PaymentMethodLoaderStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({
List<PaymentMethod> paymentMethods,
Option<PaymentMethodFailure> failureOption,
bool hasReachedMax,
bool isFetching,
int page,
});
}
/// @nodoc
class __$$PaymentMethodLoaderStateImplCopyWithImpl<$Res>
extends
_$PaymentMethodLoaderStateCopyWithImpl<
$Res,
_$PaymentMethodLoaderStateImpl
>
implements _$$PaymentMethodLoaderStateImplCopyWith<$Res> {
__$$PaymentMethodLoaderStateImplCopyWithImpl(
_$PaymentMethodLoaderStateImpl _value,
$Res Function(_$PaymentMethodLoaderStateImpl) _then,
) : super(_value, _then);
/// Create a copy of PaymentMethodLoaderState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? paymentMethods = null,
Object? failureOption = null,
Object? hasReachedMax = null,
Object? isFetching = null,
Object? page = null,
}) {
return _then(
_$PaymentMethodLoaderStateImpl(
paymentMethods: null == paymentMethods
? _value._paymentMethods
: paymentMethods // ignore: cast_nullable_to_non_nullable
as List<PaymentMethod>,
failureOption: null == failureOption
? _value.failureOption
: failureOption // ignore: cast_nullable_to_non_nullable
as Option<PaymentMethodFailure>,
hasReachedMax: null == hasReachedMax
? _value.hasReachedMax
: hasReachedMax // ignore: cast_nullable_to_non_nullable
as bool,
isFetching: null == isFetching
? _value.isFetching
: isFetching // ignore: cast_nullable_to_non_nullable
as bool,
page: null == page
? _value.page
: page // ignore: cast_nullable_to_non_nullable
as int,
),
);
}
}
/// @nodoc
class _$PaymentMethodLoaderStateImpl implements _PaymentMethodLoaderState {
_$PaymentMethodLoaderStateImpl({
required final List<PaymentMethod> paymentMethods,
required this.failureOption,
this.hasReachedMax = false,
this.isFetching = false,
this.page = 1,
}) : _paymentMethods = paymentMethods;
final List<PaymentMethod> _paymentMethods;
@override
List<PaymentMethod> get paymentMethods {
if (_paymentMethods is EqualUnmodifiableListView) return _paymentMethods;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_paymentMethods);
}
@override
final Option<PaymentMethodFailure> failureOption;
@override
@JsonKey()
final bool hasReachedMax;
@override
@JsonKey()
final bool isFetching;
@override
@JsonKey()
final int page;
@override
String toString() {
return 'PaymentMethodLoaderState(paymentMethods: $paymentMethods, failureOption: $failureOption, hasReachedMax: $hasReachedMax, isFetching: $isFetching, page: $page)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$PaymentMethodLoaderStateImpl &&
const DeepCollectionEquality().equals(
other._paymentMethods,
_paymentMethods,
) &&
(identical(other.failureOption, failureOption) ||
other.failureOption == failureOption) &&
(identical(other.hasReachedMax, hasReachedMax) ||
other.hasReachedMax == hasReachedMax) &&
(identical(other.isFetching, isFetching) ||
other.isFetching == isFetching) &&
(identical(other.page, page) || other.page == page));
}
@override
int get hashCode => Object.hash(
runtimeType,
const DeepCollectionEquality().hash(_paymentMethods),
failureOption,
hasReachedMax,
isFetching,
page,
);
/// Create a copy of PaymentMethodLoaderState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$PaymentMethodLoaderStateImplCopyWith<_$PaymentMethodLoaderStateImpl>
get copyWith =>
__$$PaymentMethodLoaderStateImplCopyWithImpl<
_$PaymentMethodLoaderStateImpl
>(this, _$identity);
}
abstract class _PaymentMethodLoaderState implements PaymentMethodLoaderState {
factory _PaymentMethodLoaderState({
required final List<PaymentMethod> paymentMethods,
required final Option<PaymentMethodFailure> failureOption,
final bool hasReachedMax,
final bool isFetching,
final int page,
}) = _$PaymentMethodLoaderStateImpl;
@override
List<PaymentMethod> get paymentMethods;
@override
Option<PaymentMethodFailure> get failureOption;
@override
bool get hasReachedMax;
@override
bool get isFetching;
@override
int get page;
/// Create a copy of PaymentMethodLoaderState
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$PaymentMethodLoaderStateImplCopyWith<_$PaymentMethodLoaderStateImpl>
get copyWith => throw _privateConstructorUsedError;
}
@@ -0,0 +1,8 @@
part of 'payment_method_loader_bloc.dart';
@freezed
class PaymentMethodLoaderEvent with _$PaymentMethodLoaderEvent {
const factory PaymentMethodLoaderEvent.fetched({
@Default(false) bool isRefresh,
}) = _Fetched;
}
@@ -0,0 +1,14 @@
part of 'payment_method_loader_bloc.dart';
@freezed
class PaymentMethodLoaderState with _$PaymentMethodLoaderState {
factory PaymentMethodLoaderState({
required List<PaymentMethod> paymentMethods,
required Option<PaymentMethodFailure> failureOption,
@Default(false) bool hasReachedMax,
@Default(false) bool isFetching,
@Default(1) int page,
}) = _PaymentMethodLoaderState;
factory PaymentMethodLoaderState.initial() =>
PaymentMethodLoaderState(paymentMethods: [], failureOption: none());
}