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 = const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png'; 'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
extension CharactersDtoToModel on CatsDto { extension CatsDtoToModel on CatsDto {
HomeData toDomain() => HomeData( HomeData toDomain() => HomeData(
data: data?.map((e) => e.toDomain()).toList(), data: data?.map((e) => e.toDomain()).toList(),
); );

View File

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

View File

@ -2,7 +2,7 @@ import 'card.dart';
class HomeData { class HomeData {
final List<CardData>? data; 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 { Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
if (event.nextPage == null) { if (event.offset == 0) {
emit(state.copyWith(isLoading: true)); emit(state.copyWith(isLoading: true));
} else { } else {
emit(state.copyWith(isPaginationLoading: true)); emit(state.copyWith(isPaginationLoading: true));
@ -21,19 +21,27 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
final data = await repo.loadData( final data = await repo.loadData(
q: event.search, q: event.search,
page: event.nextPage ?? 1,
onError: (e) => error = e, onError: (e) => error = e,
offset: event.offset,
); );
if (event.nextPage != null) { if (data != null) {
data?.data?.insertAll(0, state.data?.data ?? []); final updatedData = (event.offset == 0
} ? data.data
: [...state.data?.data ?? [], ...data.data ?? []]);
emit(state.copyWith( emit(state.copyWith(
isLoading: false, isLoading: false,
isPaginationLoading: false, isPaginationLoading: false,
data: data, data: HomeData(data: updatedData),
error: error, 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 { class HomeLoadDataEvent extends HomeEvent {
final String? search; 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() @CopyWith()
class HomeState extends Equatable { class HomeState extends Equatable {
final HomeData? data; final HomeData? data;
final int offset;
final bool isLoading; final bool isLoading;
final bool isPaginationLoading; final bool isPaginationLoading;
final String? error; final String? error;
const HomeState({ const HomeState({
this.data, this.data,
this.offset = 0,
this.isLoading = false, this.isLoading = false,
this.isPaginationLoading = false, this.isPaginationLoading = false,
this.error, this.error,
@ -21,6 +23,7 @@ class HomeState extends Equatable {
@override @override
List<Object?> get props => [ List<Object?> get props => [
data, data,
offset,
isLoading, isLoading,
isPaginationLoading, isPaginationLoading,
error, error,

View File

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