This commit is contained in:
efrilm
2025-10-26 16:09:56 +07:00
parent 0e83d213fc
commit 655902a659
23 changed files with 3519 additions and 6 deletions
@@ -0,0 +1,78 @@
import 'package:bloc/bloc.dart';
import 'package:dartz/dartz.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';
import '../../../domain/table/table.dart';
part 'table_loader_event.dart';
part 'table_loader_state.dart';
part 'table_loader_bloc.freezed.dart';
@injectable
class TableLoaderBloc extends Bloc<TableLoaderEvent, TableLoaderState> {
final ITableRepository _repository;
TableLoaderBloc(this._repository) : super(TableLoaderState.initial()) {
on<TableLoaderEvent>(_onTableLoadedEvent);
}
Future<void> _onTableLoadedEvent(
TableLoaderEvent event,
Emitter<TableLoaderState> 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<TableLoaderState> _mapFetchedToState(
TableLoaderState state, {
bool isRefresh = false,
}) async {
state = state.copyWith(isFetching: false);
if (state.hasReachedMax && state.tables.isNotEmpty && !isRefresh) {
return state;
}
if (isRefresh) {
state = state.copyWith(
page: 1,
failureOption: none(),
hasReachedMax: false,
tables: [],
);
}
final failureOrTable = await _repository.fetchTables(page: state.page);
state = failureOrTable.fold(
(f) {
if (state.tables.isNotEmpty) {
return state.copyWith(hasReachedMax: true);
}
return state.copyWith(failureOption: optionOf(f));
},
(tables) {
return state.copyWith(
tables: List.from(state.tables)..addAll(tables.tables),
failureOption: none(),
page: state.page + 1,
hasReachedMax: tables.tables.length < 10,
);
},
);
return state;
}
}
@@ -0,0 +1,478 @@
// 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 'table_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 _$TableLoaderEvent {
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 TableLoaderEvent
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$TableLoaderEventCopyWith<TableLoaderEvent> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $TableLoaderEventCopyWith<$Res> {
factory $TableLoaderEventCopyWith(
TableLoaderEvent value,
$Res Function(TableLoaderEvent) then,
) = _$TableLoaderEventCopyWithImpl<$Res, TableLoaderEvent>;
@useResult
$Res call({bool isRefresh});
}
/// @nodoc
class _$TableLoaderEventCopyWithImpl<$Res, $Val extends TableLoaderEvent>
implements $TableLoaderEventCopyWith<$Res> {
_$TableLoaderEventCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of TableLoaderEvent
/// 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 $TableLoaderEventCopyWith<$Res> {
factory _$$FetchedImplCopyWith(
_$FetchedImpl value,
$Res Function(_$FetchedImpl) then,
) = __$$FetchedImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({bool isRefresh});
}
/// @nodoc
class __$$FetchedImplCopyWithImpl<$Res>
extends _$TableLoaderEventCopyWithImpl<$Res, _$FetchedImpl>
implements _$$FetchedImplCopyWith<$Res> {
__$$FetchedImplCopyWithImpl(
_$FetchedImpl _value,
$Res Function(_$FetchedImpl) _then,
) : super(_value, _then);
/// Create a copy of TableLoaderEvent
/// 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 'TableLoaderEvent.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 TableLoaderEvent
/// 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 TableLoaderEvent {
const factory _Fetched({final bool isRefresh}) = _$FetchedImpl;
@override
bool get isRefresh;
/// Create a copy of TableLoaderEvent
/// 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 _$TableLoaderState {
List<Table> get tables => throw _privateConstructorUsedError;
Option<TableFailure> get failureOption => throw _privateConstructorUsedError;
bool get isFetching => throw _privateConstructorUsedError;
bool get hasReachedMax => throw _privateConstructorUsedError;
int get page => throw _privateConstructorUsedError;
/// Create a copy of TableLoaderState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$TableLoaderStateCopyWith<TableLoaderState> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $TableLoaderStateCopyWith<$Res> {
factory $TableLoaderStateCopyWith(
TableLoaderState value,
$Res Function(TableLoaderState) then,
) = _$TableLoaderStateCopyWithImpl<$Res, TableLoaderState>;
@useResult
$Res call({
List<Table> tables,
Option<TableFailure> failureOption,
bool isFetching,
bool hasReachedMax,
int page,
});
}
/// @nodoc
class _$TableLoaderStateCopyWithImpl<$Res, $Val extends TableLoaderState>
implements $TableLoaderStateCopyWith<$Res> {
_$TableLoaderStateCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of TableLoaderState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? tables = null,
Object? failureOption = null,
Object? isFetching = null,
Object? hasReachedMax = null,
Object? page = null,
}) {
return _then(
_value.copyWith(
tables: null == tables
? _value.tables
: tables // ignore: cast_nullable_to_non_nullable
as List<Table>,
failureOption: null == failureOption
? _value.failureOption
: failureOption // ignore: cast_nullable_to_non_nullable
as Option<TableFailure>,
isFetching: null == isFetching
? _value.isFetching
: isFetching // ignore: cast_nullable_to_non_nullable
as bool,
hasReachedMax: null == hasReachedMax
? _value.hasReachedMax
: hasReachedMax // 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 _$$TableLoaderStateImplCopyWith<$Res>
implements $TableLoaderStateCopyWith<$Res> {
factory _$$TableLoaderStateImplCopyWith(
_$TableLoaderStateImpl value,
$Res Function(_$TableLoaderStateImpl) then,
) = __$$TableLoaderStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({
List<Table> tables,
Option<TableFailure> failureOption,
bool isFetching,
bool hasReachedMax,
int page,
});
}
/// @nodoc
class __$$TableLoaderStateImplCopyWithImpl<$Res>
extends _$TableLoaderStateCopyWithImpl<$Res, _$TableLoaderStateImpl>
implements _$$TableLoaderStateImplCopyWith<$Res> {
__$$TableLoaderStateImplCopyWithImpl(
_$TableLoaderStateImpl _value,
$Res Function(_$TableLoaderStateImpl) _then,
) : super(_value, _then);
/// Create a copy of TableLoaderState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? tables = null,
Object? failureOption = null,
Object? isFetching = null,
Object? hasReachedMax = null,
Object? page = null,
}) {
return _then(
_$TableLoaderStateImpl(
tables: null == tables
? _value._tables
: tables // ignore: cast_nullable_to_non_nullable
as List<Table>,
failureOption: null == failureOption
? _value.failureOption
: failureOption // ignore: cast_nullable_to_non_nullable
as Option<TableFailure>,
isFetching: null == isFetching
? _value.isFetching
: isFetching // ignore: cast_nullable_to_non_nullable
as bool,
hasReachedMax: null == hasReachedMax
? _value.hasReachedMax
: hasReachedMax // ignore: cast_nullable_to_non_nullable
as bool,
page: null == page
? _value.page
: page // ignore: cast_nullable_to_non_nullable
as int,
),
);
}
}
/// @nodoc
class _$TableLoaderStateImpl implements _TableLoaderState {
_$TableLoaderStateImpl({
required final List<Table> tables,
required this.failureOption,
this.isFetching = false,
this.hasReachedMax = false,
this.page = 1,
}) : _tables = tables;
final List<Table> _tables;
@override
List<Table> get tables {
if (_tables is EqualUnmodifiableListView) return _tables;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_tables);
}
@override
final Option<TableFailure> failureOption;
@override
@JsonKey()
final bool isFetching;
@override
@JsonKey()
final bool hasReachedMax;
@override
@JsonKey()
final int page;
@override
String toString() {
return 'TableLoaderState(tables: $tables, failureOption: $failureOption, isFetching: $isFetching, hasReachedMax: $hasReachedMax, page: $page)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$TableLoaderStateImpl &&
const DeepCollectionEquality().equals(other._tables, _tables) &&
(identical(other.failureOption, failureOption) ||
other.failureOption == failureOption) &&
(identical(other.isFetching, isFetching) ||
other.isFetching == isFetching) &&
(identical(other.hasReachedMax, hasReachedMax) ||
other.hasReachedMax == hasReachedMax) &&
(identical(other.page, page) || other.page == page));
}
@override
int get hashCode => Object.hash(
runtimeType,
const DeepCollectionEquality().hash(_tables),
failureOption,
isFetching,
hasReachedMax,
page,
);
/// Create a copy of TableLoaderState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$TableLoaderStateImplCopyWith<_$TableLoaderStateImpl> get copyWith =>
__$$TableLoaderStateImplCopyWithImpl<_$TableLoaderStateImpl>(
this,
_$identity,
);
}
abstract class _TableLoaderState implements TableLoaderState {
factory _TableLoaderState({
required final List<Table> tables,
required final Option<TableFailure> failureOption,
final bool isFetching,
final bool hasReachedMax,
final int page,
}) = _$TableLoaderStateImpl;
@override
List<Table> get tables;
@override
Option<TableFailure> get failureOption;
@override
bool get isFetching;
@override
bool get hasReachedMax;
@override
int get page;
/// Create a copy of TableLoaderState
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$TableLoaderStateImplCopyWith<_$TableLoaderStateImpl> get copyWith =>
throw _privateConstructorUsedError;
}
@@ -0,0 +1,7 @@
part of 'table_loader_bloc.dart';
@freezed
class TableLoaderEvent with _$TableLoaderEvent {
const factory TableLoaderEvent.fetched({@Default(false) bool isRefresh}) =
_Fetched;
}
@@ -0,0 +1,15 @@
part of 'table_loader_bloc.dart';
@freezed
class TableLoaderState with _$TableLoaderState {
factory TableLoaderState({
required List<Table> tables,
required Option<TableFailure> failureOption,
@Default(false) bool isFetching,
@Default(false) bool hasReachedMax,
@Default(1) int page,
}) = _TableLoaderState;
factory TableLoaderState.initial() =>
TableLoaderState(tables: [], failureOption: none());
}