lab5
This commit is contained in:
parent
bfc24e3634
commit
89c779a428
24
lib/data/dtos/weapons_dto.dart
Normal file
24
lib/data/dtos/weapons_dto.dart
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import 'package:json_annotation/json_annotation.dart';
|
||||||
|
part 'weapons_dto.g.dart';
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class WeaponsDto {
|
||||||
|
final List<WeaponDto>? data;
|
||||||
|
|
||||||
|
const WeaponsDto({this.data});
|
||||||
|
|
||||||
|
factory WeaponsDto.fromJson(Map<String, dynamic> json) => _$WeaponsDtoFromJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class WeaponDto {
|
||||||
|
final String? displayName;
|
||||||
|
final String? displayIcon;
|
||||||
|
final String? category;
|
||||||
|
final int? magazineSize;
|
||||||
|
final int? cost;
|
||||||
|
|
||||||
|
const WeaponDto({this.displayName, this.displayIcon, this.category, this.magazineSize, this.cost});
|
||||||
|
|
||||||
|
factory WeaponDto.fromJson(Map<String, dynamic> json) => _$WeaponDtoFromJson(json);
|
||||||
|
}
|
26
lib/data/dtos/weapons_dto.g.dart
Normal file
26
lib/data/dtos/weapons_dto.g.dart
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'weapons_dto.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// JsonSerializableGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
WeaponsDto _$WeaponsDtoFromJson(Map<String, dynamic> json) =>
|
||||||
|
WeaponsDto(
|
||||||
|
data: (json['data'] as List<dynamic>?)
|
||||||
|
?.map((e) => WeaponDto.fromJson(e as Map<String, dynamic>))
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
|
||||||
|
WeaponDto _$WeaponDtoFromJson(Map<String, dynamic> json) {
|
||||||
|
var weaponStats = json['weaponStats'] as Map<String, dynamic>?;
|
||||||
|
var shopData = json['shopData'] as Map<String, dynamic>?;
|
||||||
|
return WeaponDto(
|
||||||
|
displayName: json['displayName'] as String?,
|
||||||
|
displayIcon: json['displayIcon'] as String?,
|
||||||
|
category: shopData?['category'] as String?,
|
||||||
|
magazineSize: weaponStats?['magazineSize'] as int?,
|
||||||
|
cost: shopData?['cost'] as int?,
|
||||||
|
);
|
||||||
|
}
|
12
lib/data/mappers/weapons_mapper.dart
Normal file
12
lib/data/mappers/weapons_mapper.dart
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import '../../domain/models/card.dart';
|
||||||
|
import '../dtos/weapons_dto.dart';
|
||||||
|
|
||||||
|
extension WeaponDtoToModel on WeaponDto {
|
||||||
|
CardData toDomain() => CardData(
|
||||||
|
displayName ?? 'UNKNOWN',
|
||||||
|
descriptionText: category ?? 'Описание отсутствует',
|
||||||
|
gameDesc: magazineSize,
|
||||||
|
gameDesc2: cost,
|
||||||
|
imageUrl: displayIcon,
|
||||||
|
);
|
||||||
|
}
|
6
lib/data/repositories/api_interface.dart
Normal file
6
lib/data/repositories/api_interface.dart
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
import '../../domain/models/card.dart';
|
||||||
|
|
||||||
|
abstract class ApiInterface{
|
||||||
|
Future<List<CardData>?> loadData();
|
||||||
|
}
|
75
lib/data/repositories/mock_repository.dart
Normal file
75
lib/data/repositories/mock_repository.dart
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
|
||||||
|
import '../../domain/models/card.dart';
|
||||||
|
import 'api_interface.dart';
|
||||||
|
|
||||||
|
class MockRepository extends ApiInterface {
|
||||||
|
@override
|
||||||
|
Future<List<CardData>?> loadData() async{
|
||||||
|
return [
|
||||||
|
CardData(
|
||||||
|
'Мама с сыном',
|
||||||
|
descriptionText: '-Чё вылупился?',
|
||||||
|
imageUrl:
|
||||||
|
'https://pic.rutubelist.ru/video/8b/31/8b31b4f162bf11007c036be6787e9bb1.jpg',
|
||||||
|
gameDesc: 2,
|
||||||
|
gameDesc2: 2
|
||||||
|
),
|
||||||
|
CardData(
|
||||||
|
'Ярускин Салих',
|
||||||
|
descriptionText: 'я мусор',
|
||||||
|
imageUrl:
|
||||||
|
'https://steamuserimages-a.akamaihd.net/ugc/2030615098399987630/EB9690C3D097504388EA8F057E48631354C05182/?imw=512&&ima=fit&impolicy=Letterbox&imcolor=%23000000&letterbox=false',
|
||||||
|
gameDesc: 2,
|
||||||
|
gameDesc2: 2
|
||||||
|
),
|
||||||
|
CardData(
|
||||||
|
'Гламурная бабизяна',
|
||||||
|
descriptionText: 'сделала губки 5мл',
|
||||||
|
imageUrl:
|
||||||
|
'https://avatars.dzeninfra.ru/get-zen_doc/3310860/pub_602156c24849a6360821ff59_60215909390eb32b9bb9e012/scale_1200',
|
||||||
|
gameDesc: 2,
|
||||||
|
gameDesc2: 2
|
||||||
|
),
|
||||||
|
CardData(
|
||||||
|
'Мелкий',
|
||||||
|
descriptionText: 'невдупленыш',
|
||||||
|
imageUrl:
|
||||||
|
'https://sun9-59.userapi.com/impg/DBnGgdqZnRtBw9jchGixY6rRN-zTWaAEVjLxXw/HrYCralVB-4.jpg?size=807x577&quality=96&sign=a84b4d87249b7d5ade53369663a3a9e0&c_uniq_tag=2XoQqW9aaCVDnvITURMqqPPY9yznsdCr4HWXaSv9Q_U&type=album',
|
||||||
|
gameDesc: 2,
|
||||||
|
gameDesc2: 2
|
||||||
|
),
|
||||||
|
CardData(
|
||||||
|
'Обезьянка Олежа',
|
||||||
|
descriptionText: 'Поняла смысл жизни...',
|
||||||
|
imageUrl:
|
||||||
|
'https://avatars.yandex.net/get-music-content/5417945/f468314f.a.20066160-1/m1000x1000?webp=false',
|
||||||
|
gameDesc: 2,
|
||||||
|
gameDesc2: 2
|
||||||
|
),
|
||||||
|
CardData(
|
||||||
|
'Афанасьев Степан',
|
||||||
|
descriptionText: 'основатель ЧВК Мартышки',
|
||||||
|
imageUrl:
|
||||||
|
'https://steamuserimages-a.akamaihd.net/ugc/1621850006938404146/EA72DC3C31DED440C024F0DD1D9859C44B1BBDFF/?imw=512&&ima=fit&impolicy=Letterbox&imcolor=%23000000&letterbox=false',
|
||||||
|
gameDesc: 2,
|
||||||
|
gameDesc2: 2
|
||||||
|
),
|
||||||
|
CardData(
|
||||||
|
'Лобашов Иван',
|
||||||
|
descriptionText: 'вычисляет противников',
|
||||||
|
imageUrl:
|
||||||
|
'https://cdn1.ozone.ru/s3/multimedia-i/6449406306.jpg',
|
||||||
|
gameDesc: 2,
|
||||||
|
gameDesc2: 2
|
||||||
|
),
|
||||||
|
CardData(
|
||||||
|
'Коренной представитель ЧВК Мартышки',
|
||||||
|
descriptionText: 'сейчас он где-то в Камеруне проветривает свои блохи',
|
||||||
|
imageUrl:
|
||||||
|
'https://otvet.imgsmail.ru/download/306401642_9fecf789a8802c5805c4e21291cba6a6_800.jpg',
|
||||||
|
gameDesc: 2,
|
||||||
|
gameDesc2: 2
|
||||||
|
),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
44
lib/data/repositories/weapons_repository.dart
Normal file
44
lib/data/repositories/weapons_repository.dart
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:flutter_test_app/data/mappers/weapons_mapper.dart';
|
||||||
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||||
|
|
||||||
|
import '../../domain/models/card.dart';
|
||||||
|
import '../dtos/weapons_dto.dart';
|
||||||
|
import 'api_interface.dart';
|
||||||
|
|
||||||
|
class WeaponsRepository extends ApiInterface {
|
||||||
|
static final Dio _dio = Dio()
|
||||||
|
..interceptors.add(PrettyDioLogger(
|
||||||
|
requestHeader: true,
|
||||||
|
requestBody: true,
|
||||||
|
));
|
||||||
|
|
||||||
|
static const String _baseUrl = 'https://valorant-api.com/v1/weapons';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<CardData>?> loadData({String? q}) async {
|
||||||
|
try {
|
||||||
|
// Формирование URL для запроса
|
||||||
|
final Response<dynamic> response = await _dio.get(_baseUrl);
|
||||||
|
|
||||||
|
// Получение данных агентов из API
|
||||||
|
final WeaponsData = response.data['data'] as List<dynamic>;
|
||||||
|
|
||||||
|
// Преобразование данных JSON в WeaponsDto, затем в CardData
|
||||||
|
final WeaponsDto dto = WeaponsDto.fromJson({'data': WeaponsData});
|
||||||
|
List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
|
||||||
|
|
||||||
|
// Фильтрация данных по displayName
|
||||||
|
if (q != null && q.isNotEmpty) {
|
||||||
|
data = data?.where((Weapon) =>
|
||||||
|
Weapon.text?.toLowerCase().contains(q.toLowerCase()) ?? false).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
} on DioException catch (e) {
|
||||||
|
// Обработка ошибок запроса
|
||||||
|
print('Ошибка при загрузке данных: $e');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user