Merge remote-tracking branch 'origin/lab6' into lab6

# Conflicts:
#	lib/presentation/home_page/home_page.dart
This commit is contained in:
Dasha 2024-12-08 02:17:31 +04:00
commit c7d00bd65f
7 changed files with 40 additions and 19 deletions

View File

@ -5,7 +5,7 @@ import '../../domain/models/home.dart';
const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
extension CharactersDtoToModel on CatsDto {
extension CatsDtoToModel on CatsDto {
HomeData toDomain() => HomeData(
data: data?.map((e) => e.toDomain()).toList(),
);

View File

@ -21,19 +21,18 @@ class CatsRepository extends ApiInterface {
Future<HomeData?> loadData({
OnErrorCallback? onError,
String? q,
int page = 1,
int offset = 0,
int pageSize = 20,
}) async {
try {
const String url = '/v1/cats?min_weight=1';
final int offset = (page - 1) * pageSize;
final Response<dynamic> response = await _dio.get<dynamic>(
url,
queryParameters: {
'name': q,
'offset': offset,
'limit': pageSize,
},
);

View File

@ -2,7 +2,7 @@ import 'card.dart';
class HomeData {
final List<CardData>? data;
final int? nextPage;
final int? offset;
HomeData({this.data, this.nextPage});
HomeData({this.data, this.offset});
}

View File

@ -11,7 +11,7 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
}
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
if (event.nextPage == null) {
if (event.offset == 0) {
emit(state.copyWith(isLoading: true));
} else {
emit(state.copyWith(isPaginationLoading: true));
@ -21,19 +21,27 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
final data = await repo.loadData(
q: event.search,
page: event.nextPage ?? 1,
onError: (e) => error = e,
offset: event.offset,
);
if (event.nextPage != null) {
data?.data?.insertAll(0, state.data?.data ?? []);
}
if (data != null) {
final updatedData = (event.offset == 0
? data.data
: [...state.data?.data ?? [], ...data.data ?? []]);
emit(state.copyWith(
isLoading: false,
isPaginationLoading: false,
data: data,
error: error,
));
emit(state.copyWith(
isLoading: false,
isPaginationLoading: false,
data: HomeData(data: updatedData),
error: error,
));
} else {
emit(state.copyWith(
isLoading: false,
isPaginationLoading: false,
error: error,
));
}
}
}

View File

@ -4,7 +4,7 @@ abstract class HomeEvent {
class HomeLoadDataEvent extends HomeEvent {
final String? search;
final int? nextPage;
final int offset;
const HomeLoadDataEvent({this.search, this.nextPage});
const HomeLoadDataEvent({this.search, this.offset = 0});
}

View File

@ -7,12 +7,14 @@ part 'state.g.dart';
@CopyWith()
class HomeState extends Equatable {
final HomeData? data;
final int offset;
final bool isLoading;
final bool isPaginationLoading;
final String? error;
const HomeState({
this.data,
this.offset = 0,
this.isLoading = false,
this.isPaginationLoading = false,
this.error,
@ -21,6 +23,7 @@ class HomeState extends Equatable {
@override
List<Object?> get props => [
data,
offset,
isLoading,
isPaginationLoading,
error,

View File

@ -9,6 +9,8 @@ part of 'state.dart';
abstract class _$HomeStateCWProxy {
HomeState data(HomeData? data);
HomeState offset(int offset);
HomeState isLoading(bool isLoading);
HomeState isPaginationLoading(bool isPaginationLoading);
@ -23,6 +25,7 @@ abstract class _$HomeStateCWProxy {
/// ````
HomeState call({
HomeData? data,
int? offset,
bool? isLoading,
bool? isPaginationLoading,
String? error,
@ -38,6 +41,9 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
@override
HomeState data(HomeData? data) => this(data: data);
@override
HomeState offset(int offset) => this(offset: offset);
@override
HomeState isLoading(bool isLoading) => this(isLoading: isLoading);
@ -58,6 +64,7 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
/// ````
HomeState call({
Object? data = const $CopyWithPlaceholder(),
Object? offset = const $CopyWithPlaceholder(),
Object? isLoading = const $CopyWithPlaceholder(),
Object? isPaginationLoading = const $CopyWithPlaceholder(),
Object? error = const $CopyWithPlaceholder(),
@ -67,6 +74,10 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
? _value.data
// ignore: cast_nullable_to_non_nullable
: data as HomeData?,
offset: offset == const $CopyWithPlaceholder() || offset == null
? _value.offset
// ignore: cast_nullable_to_non_nullable
: offset as int,
isLoading: isLoading == const $CopyWithPlaceholder() || isLoading == null
? _value.isLoading
// ignore: cast_nullable_to_non_nullable