PIbd-32_Chubykina_P.P._mobilki/lib/data/mappers/pokemon_mapper.dart

29 lines
1.1 KiB
Dart
Raw Normal View History

2024-10-14 21:25:43 +04:00
import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart';
import 'package:mobilki_lab1/domain/models/card.dart';
import 'package:mobilki_lab1/domain/models/home.dart';
2024-11-19 23:43:02 +04:00
import 'package:flutter/material.dart';
2024-10-14 21:25:43 +04:00
const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
2024-10-29 14:54:56 +04:00
extension PokemonDtoToModel on PokemonDto {
HomeData toDomain() => HomeData(
2024-11-19 23:43:02 +04:00
data: data?.map((e) => e.toDomain()).toList(),
nextPage: meta?.pagination?.next,
);
2024-10-18 21:36:44 +04:00
}
2024-10-14 21:25:43 +04:00
2024-10-29 14:54:56 +04:00
extension PokemonDataDtoToModel on PokemonDataDto {
CardData toDomain() => CardData(
name ?? 'UNKNOWN',
2024-11-19 23:43:02 +04:00
imageUrl: images?.small ?? _imagePlaceholder,
descriptionText: _makeDescriptionText(types, evolvesTo),
id: id,
);
2024-11-19 23:43:02 +04:00
String _makeDescriptionText(List<String>? types, List<String>? evolvesTo) {
final typeText = types != null && types.isNotEmpty ? 'Types: ${types.join(", ")}' : '';
final evolvesToText = evolvesTo != null && evolvesTo.isNotEmpty ? 'Evolves To: ${evolvesTo.join(", ")}' : '';
return '$typeText\n$evolvesToText'.trim();
}
}