not working:(
This commit is contained in:
parent
302cbf28f4
commit
c83f11ad7c
5
Makefile
5
Makefile
@ -4,11 +4,6 @@ gen:
|
||||
icon:
|
||||
flutter pub run flutter_launcher_icons:main
|
||||
|
||||
hello:
|
||||
echo "Hi!"; \
|
||||
echo "I`m makefile"; \
|
||||
echo "^_^"
|
||||
|
||||
init_res:
|
||||
dart pub global activate flutter_asset_generator
|
||||
|
||||
|
@ -5,8 +5,7 @@ part 'titans_dto.g.dart';
|
||||
@JsonSerializable(createToJson: false)
|
||||
class TitansDto {
|
||||
final List<TitansResultsDto>? results;
|
||||
final InfoDto? info;
|
||||
const TitansDto({this.results, this.info});
|
||||
const TitansDto({this.results});
|
||||
factory TitansDto.fromJson(Map<String, dynamic> json) => _$TitansDtoFromJson(json);
|
||||
}
|
||||
|
||||
@ -17,7 +16,8 @@ class TitansResultsDto {
|
||||
final String? height;
|
||||
final List<String>? abilities;
|
||||
final String? allegiance;
|
||||
const TitansResultsDto({this.id, this.name, this.height, this.abilities, this.allegiance});
|
||||
final String? image;
|
||||
const TitansResultsDto({this.id, this.name, this.height, this.abilities, this.allegiance, this.image});
|
||||
factory TitansResultsDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$CharactersDataDtoFromJson(json);
|
||||
_$TitansResultsDtoFromJson(json);
|
||||
}
|
||||
|
25
lib/data/dtos/titans_dto.g.dart
Normal file
25
lib/data/dtos/titans_dto.g.dart
Normal file
@ -0,0 +1,25 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'titans_dto.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
TitansDto _$TitansDtoFromJson(Map<String, dynamic> json) => TitansDto(
|
||||
results: (json['results'] as List<dynamic>?)
|
||||
?.map((e) => TitansResultsDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
TitansResultsDto _$TitansResultsDtoFromJson(Map<String, dynamic> json) =>
|
||||
TitansResultsDto(
|
||||
id: json['id'] as String?,
|
||||
name: json['name'] as String?,
|
||||
height: json['height'] as String?,
|
||||
abilities: (json['abilities'] as List<dynamic>?)
|
||||
?.map((e) => e as String)
|
||||
.toList(),
|
||||
allegiance: json['allegiance'] as String?,
|
||||
image: json['image'] as String?,
|
||||
);
|
@ -1,30 +1,27 @@
|
||||
import 'package:pmd_lab/data/dtos/titans_dto.dart';
|
||||
import 'package:pmd_lab/domain/models/card.dart';
|
||||
import 'package:pmd_lab/domain/models/home.dart';
|
||||
|
||||
const _imagePlaceholder = 'https://cdn-icons-png.flaticon.com/512/4054/4054617.png';
|
||||
|
||||
extension CharacterDataDtoToModel on TitansResultsDto {
|
||||
CardData toDomain() => CardData(
|
||||
name: ?.name ?? 'UNKNOWN',
|
||||
img: attributes?.image ?? _imagePlaceholder,
|
||||
description: _makeDescription(attributes?.born, attributes?.died),
|
||||
name ?? 'UNKNOWN',
|
||||
img: image ?? _imagePlaceholder,
|
||||
description: _makeDescription(height, abilities, allegiance),
|
||||
id: id,
|
||||
);
|
||||
String _makeDescription(String? born, String? died) {
|
||||
return born != null && died != null
|
||||
? '$born - $died'
|
||||
: born != null
|
||||
? 'born: $born'
|
||||
: died != null
|
||||
? 'died: $died'
|
||||
: '';
|
||||
String _makeDescription(String? height, List<String>? abilities, String? allegiance) {
|
||||
List<String> descriptionParts = [];
|
||||
if (height != null && height.isNotEmpty) {
|
||||
descriptionParts.add('Height: $height');
|
||||
}
|
||||
if (allegiance != null && allegiance.isNotEmpty) {
|
||||
descriptionParts.add('Allegiance: $allegiance');
|
||||
}
|
||||
if (abilities != null && abilities.isNotEmpty) {
|
||||
final abilitiesStr = abilities.join(', ');
|
||||
descriptionParts.add('Abilities: $abilitiesStr.');
|
||||
}
|
||||
return descriptionParts.join('\n\n');
|
||||
}
|
||||
}
|
||||
|
||||
extension CharactersDtoToModel on TitansDto {
|
||||
HomeData toDomain() => HomeData(
|
||||
data: results?.map((e) => e.toDomain()).toList(),
|
||||
nextPage: info?.pagination?.next,
|
||||
);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import 'package:pmd_lab/presentation/home_page/home_page.dart';
|
||||
import 'package:pmd_lab/presentation/like_bloc/like_bloc.dart';
|
||||
import 'package:pmd_lab/presentation/locale_bloc/locale_bloc.dart';
|
||||
import 'package:pmd_lab/presentation/locale_bloc/locale_state.dart';
|
||||
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||
import 'package:pmd_lab/repositories/titans_repository.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
@ -35,15 +35,15 @@ class MyApp extends StatelessWidget {
|
||||
scaffoldBackgroundColor: Colors.white,
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: RepositoryProvider<PotterRepository>(
|
||||
home: RepositoryProvider<TitansRepository>(
|
||||
lazy: true,
|
||||
create: (_) => PotterRepository(),
|
||||
create: (_) => TitansRepository(),
|
||||
child: BlocProvider<LikeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => LikeBloc(),
|
||||
child: BlocProvider<HomeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => HomeBloc(context.read<PotterRepository>()),
|
||||
create: (context) => HomeBloc(context.read<TitansRepository>()),
|
||||
child: const MyHomePage(),
|
||||
),
|
||||
),
|
||||
|
@ -1,37 +1,27 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:pmd_lab/presentation/home_page/bloc/events.dart';
|
||||
import 'package:pmd_lab/presentation/home_page/bloc/state.dart';
|
||||
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||
import 'package:pmd_lab/repositories/titans_repository.dart';
|
||||
|
||||
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||
final PotterRepository repo;
|
||||
final TitansRepository repo;
|
||||
|
||||
HomeBloc(this.repo) : super(const HomeState()) {
|
||||
on<HomeLoadDataEvent>(_onLoadData);
|
||||
}
|
||||
|
||||
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||||
if (event.nextPage == null) {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
} else {
|
||||
emit(state.copyWith(isPaginationLoading: true));
|
||||
}
|
||||
|
||||
String? error;
|
||||
|
||||
final data = await repo.loadData(
|
||||
final data = await repo.loadResults(
|
||||
q: event.search,
|
||||
page: event.nextPage ?? 1,
|
||||
onError: (e) => error = e,
|
||||
);
|
||||
|
||||
if (event.nextPage != null) {
|
||||
data?.data?.insertAll(0, state.data?.data ?? []);
|
||||
}
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
isPaginationLoading: false,
|
||||
data: data,
|
||||
error: error,
|
||||
));
|
||||
|
@ -1,33 +1,29 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||
import 'package:pmd_lab/domain/models/home.dart';
|
||||
import 'package:pmd_lab/domain/models/card.dart';
|
||||
|
||||
part 'state.g.dart';
|
||||
|
||||
@CopyWith()
|
||||
class HomeState extends Equatable {
|
||||
final HomeData? data;
|
||||
final List<CardData>? data;
|
||||
final bool isLoading;
|
||||
final bool isPaginationLoading;
|
||||
final String? error;
|
||||
|
||||
const HomeState({
|
||||
this.data,
|
||||
this.isLoading = false,
|
||||
this.isPaginationLoading = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
HomeState copyWith({
|
||||
HomeData? data,
|
||||
List<CardData>? data,
|
||||
bool? isLoading,
|
||||
bool? isPaginationLoading,
|
||||
String? error,
|
||||
}) =>
|
||||
HomeState(
|
||||
data: data ?? this.data,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
isPaginationLoading: isPaginationLoading ?? this.isPaginationLoading,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
|
||||
@ -35,7 +31,6 @@ class HomeState extends Equatable {
|
||||
List<Object?> get props => [
|
||||
data,
|
||||
isLoading,
|
||||
isPaginationLoading,
|
||||
error,
|
||||
];
|
||||
}
|
||||
|
@ -7,12 +7,10 @@ part of 'state.dart';
|
||||
// **************************************************************************
|
||||
|
||||
abstract class _$HomeStateCWProxy {
|
||||
HomeState data(HomeData? data);
|
||||
HomeState data(List<CardData>? data);
|
||||
|
||||
HomeState isLoading(bool isLoading);
|
||||
|
||||
HomeState isPaginationLoading(bool isPaginationLoading);
|
||||
|
||||
HomeState error(String? error);
|
||||
|
||||
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `HomeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||
@ -22,9 +20,8 @@ abstract class _$HomeStateCWProxy {
|
||||
/// HomeState(...).copyWith(id: 12, name: "My name")
|
||||
/// ````
|
||||
HomeState call({
|
||||
HomeData? data,
|
||||
List<CardData>? data,
|
||||
bool? isLoading,
|
||||
bool? isPaginationLoading,
|
||||
String? error,
|
||||
});
|
||||
}
|
||||
@ -36,15 +33,11 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
|
||||
final HomeState _value;
|
||||
|
||||
@override
|
||||
HomeState data(HomeData? data) => this(data: data);
|
||||
HomeState data(List<CardData>? data) => this(data: data);
|
||||
|
||||
@override
|
||||
HomeState isLoading(bool isLoading) => this(isLoading: isLoading);
|
||||
|
||||
@override
|
||||
HomeState isPaginationLoading(bool isPaginationLoading) =>
|
||||
this(isPaginationLoading: isPaginationLoading);
|
||||
|
||||
@override
|
||||
HomeState error(String? error) => this(error: error);
|
||||
|
||||
@ -59,24 +52,17 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
|
||||
HomeState call({
|
||||
Object? data = const $CopyWithPlaceholder(),
|
||||
Object? isLoading = const $CopyWithPlaceholder(),
|
||||
Object? isPaginationLoading = const $CopyWithPlaceholder(),
|
||||
Object? error = const $CopyWithPlaceholder(),
|
||||
}) {
|
||||
return HomeState(
|
||||
data: data == const $CopyWithPlaceholder()
|
||||
? _value.data
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: data as HomeData?,
|
||||
: data as List<CardData>?,
|
||||
isLoading: isLoading == const $CopyWithPlaceholder() || isLoading == null
|
||||
? _value.isLoading
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: isLoading as bool,
|
||||
isPaginationLoading:
|
||||
isPaginationLoading == const $CopyWithPlaceholder() ||
|
||||
isPaginationLoading == null
|
||||
? _value.isPaginationLoading
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: isPaginationLoading as bool,
|
||||
error: error == const $CopyWithPlaceholder()
|
||||
? _value.error
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
|
@ -14,9 +14,7 @@ import 'package:pmd_lab/presentation/like_bloc/like_event.dart';
|
||||
import 'package:pmd_lab/presentation/like_bloc/like_state.dart';
|
||||
import 'package:pmd_lab/presentation/locale_bloc/locale_events.dart';
|
||||
import 'package:pmd_lab/presentation/locale_bloc/locale_state.dart';
|
||||
import 'package:pmd_lab/repositories/api_interface.dart';
|
||||
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||
|
||||
import 'package:pmd_lab/repositories/titans_repository.dart';
|
||||
import '../locale_bloc/locale_bloc.dart';
|
||||
part 'card.dart';
|
||||
|
||||
@ -41,37 +39,20 @@ class Body extends StatefulWidget {
|
||||
|
||||
class _BodyState extends State<Body> {
|
||||
final searchController = TextEditingController();
|
||||
final scrollController = ScrollController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
SvgObjects.init();
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||
context.read<LikeBloc>().add(const LoadLikesEvent());
|
||||
});
|
||||
scrollController.addListener(_onNextPageListener);
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void _onNextPageListener() {
|
||||
if (scrollController.offset > scrollController.position.maxScrollExtent) {
|
||||
final bloc = context.read<HomeBloc>();
|
||||
if (!bloc.state.isPaginationLoading) {
|
||||
bloc.add(HomeLoadDataEvent(
|
||||
search: searchController.text,
|
||||
nextPage: bloc.state.data?.nextPage,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
searchController.dispose();
|
||||
scrollController.dispose();
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@ -130,11 +111,10 @@ class _BodyState extends State<Body> {
|
||||
onRefresh: _onRefresh,
|
||||
child: ListView.builder(
|
||||
physics: const BouncingScrollPhysics(),
|
||||
controller: scrollController,
|
||||
padding: EdgeInsets.zero,
|
||||
itemCount: state.data?.data?.length ?? 0,
|
||||
itemCount: state.data?.length ?? 0,
|
||||
itemBuilder: (context, index) {
|
||||
final data = state.data?.data?[index];
|
||||
final data = state.data?[index];
|
||||
return data != null
|
||||
? _Card.fromData(
|
||||
data,
|
||||
@ -150,12 +130,7 @@ class _BodyState extends State<Body> {
|
||||
);
|
||||
},
|
||||
), // Expanded
|
||||
), // BlocBuilder
|
||||
BlocBuilder<HomeBloc, HomeState>(
|
||||
builder: (context, state) => state.isPaginationLoading
|
||||
? const CircularProgressIndicator()
|
||||
: const SizedBox.shrink(),
|
||||
)
|
||||
) // BlocBuilder
|
||||
],
|
||||
),
|
||||
);
|
||||
|
@ -1,7 +1,7 @@
|
||||
import 'package:pmd_lab/domain/models/home.dart';
|
||||
import 'package:pmd_lab/domain/models/card.dart';
|
||||
|
||||
typedef OnErrorCallback = void Function(String? error);
|
||||
|
||||
abstract class ApiInterface {
|
||||
Future<HomeData?> loadData({OnErrorCallback? onError});
|
||||
Future<List<CardData>?> loadResults({OnErrorCallback? onError});
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
import 'package:pmd_lab/domain/models/card.dart';
|
||||
import 'package:pmd_lab/repositories/api_interface.dart';
|
||||
|
||||
class MockRepository extends ApiInterface {
|
||||
@override
|
||||
Future<List<CardData>?> loadData({OnErrorCallback? onError}) async {
|
||||
return [
|
||||
CardData(
|
||||
'Freeze',
|
||||
description: 'so cold..',
|
||||
img:
|
||||
'https://www.skedaddlewildlife.com/wp-content/uploads/2018/09/depositphotos_22425309-stock-photo-a-lonely-raccoon-in-winter.jpg',
|
||||
),
|
||||
CardData(
|
||||
'Hi',
|
||||
description: 'pretty face',
|
||||
img:
|
||||
'https://www.thesprucepets.com/thmb/nKNaS4I586B_H7sEUw9QAXvWM_0=/2121x0/filters:no_upscale():strip_icc()/GettyImages-135630198-5ba7d225c9e77c0050cff91b.jpg',
|
||||
),
|
||||
CardData(
|
||||
'Orange',
|
||||
description: 'I like autumn',
|
||||
img: 'https://furmanagers.com/wp-content/uploads/2019/11/dreamstime_l_22075357.jpg',
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:pmd_lab/data/dtos/titans_dto.dart';
|
||||
import 'package:pmd_lab/data/mappes/titans_mapper.dart';
|
||||
import 'package:pmd_lab/domain/models/home.dart';
|
||||
import 'package:pmd_lab/domain/models/card.dart';
|
||||
import 'package:pmd_lab/repositories/api_interface.dart';
|
||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||
|
||||
class PotterRepository extends ApiInterface {
|
||||
class TitansRepository extends ApiInterface {
|
||||
static final Dio _dio = Dio()
|
||||
..interceptors.add(PrettyDioLogger(
|
||||
requestHeader: true,
|
||||
@ -15,28 +15,22 @@ class PotterRepository extends ApiInterface {
|
||||
static const String _baseUrl = 'https://api.attackontitanapi.com';
|
||||
|
||||
@override
|
||||
Future<HomeData?> loadData({
|
||||
Future<List<CardData>?> loadResults({
|
||||
OnErrorCallback? onError,
|
||||
String? q,
|
||||
int page = 1,
|
||||
int pageSize = 25,
|
||||
String? q
|
||||
}) async {
|
||||
try {
|
||||
const String url = '$_baseUrl/v1/characters';
|
||||
const String url = '$_baseUrl/titans';
|
||||
|
||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||
url,
|
||||
queryParameters: {
|
||||
'filter[name_cont]': q,
|
||||
'page[number]': page,
|
||||
'page[size]': pageSize,
|
||||
},
|
||||
queryParameters: q != null ? {'filter[name_cont]': q} : null,
|
||||
);
|
||||
|
||||
final TitansDto dto = TitansDto.fromJson(response.data as Map<String, dynamic>);
|
||||
final HomeData data = dto.toDomain();
|
||||
return data;
|
||||
} on DioException catch (e) {
|
||||
final List<CardData>? results = dto.results?.map((e) => e.toDomain()).toList();
|
||||
return results;
|
||||
} on DioException catch (e) {
|
||||
onError?.call(e.error?.toString());
|
||||
return null;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user