Сделано

This commit is contained in:
Максим Яковлев 2024-11-11 18:13:49 +04:00
parent 4b6d0fc446
commit 22de980f50
8 changed files with 144 additions and 55 deletions

3
devtools_options.yaml Normal file
View File

@ -0,0 +1,3 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:

View File

@ -0,0 +1,20 @@
import 'dart:async';
import 'dart:ui';
class Debounce {
factory Debounce() => _instance;
Debounce._();
static final Debounce _instance = Debounce._();
static Timer? _timer;
static void run(
VoidCallback action, {
Duration delay = const Duration(milliseconds: 500),
}) {
_timer?.cancel();
_timer = Timer(delay, action);
}
}

View File

@ -1,5 +1,7 @@
import 'package:pmu_labs/domain/models/card.dart'; import 'package:pmu_labs/domain/models/card.dart';
typedef OnErrorCallback = void Function(String? error);
abstract class ApiInterface{ abstract class ApiInterface{
Future<List<CardData>?> loadData(); Future<List<CardData>?> loadData();
} }

View File

@ -49,7 +49,7 @@ class BossesRepository extends ApiInterface {
static const String _baseUrl = 'https://sekiro.fandom.com/ru/api.php'; static const String _baseUrl = 'https://sekiro.fandom.com/ru/api.php';
@override @override
Future<List<CardData>?> loadData({String? q}) async { Future<List<CardData>?> loadData({OnErrorCallback? onError,String? q,}) async {
try { try {
String url = ''; String url = '';
if(q != null && q != ""){ if(q != null && q != ""){
@ -94,6 +94,7 @@ class BossesRepository extends ApiInterface {
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList(); final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
return data; return data;
} on DioException catch (e) { } on DioException catch (e) {
onError?.call(e.error?.toString());
return null; return null;
} }
} }

View File

@ -3,14 +3,28 @@ import 'package:pmu_labs/presentation/home_page/bloc/events.dart';
import 'package:pmu_labs/presentation/home_page/bloc/state.dart'; import 'package:pmu_labs/presentation/home_page/bloc/state.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
class HomeBloc extends Bloc<HomeEvent, HomeState>{ class HomeBloc extends Bloc<HomeEvent, HomeState> {
final BossesRepository rep; final BossesRepository rep;
HomeBloc(this.rep) : super(const HomeState()){ HomeBloc(this.rep) : super(const HomeState()) {
on<HomeLoadDataEvent>(_onLoadData); on<HomeLoadDataEvent>(_onLoadData);
} }
void _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit){ Future<void> _onLoadData(
emit(state.copyWith(data: rep.loadData())); HomeLoadDataEvent event, Emitter<HomeState> emit) async {
emit(state.copyWith(isLoading: true));
String? error;
final data = await rep.loadData(
q: event.search,
onError: (e) => error = e,
);
emit(state.copyWith(
isLoading: false,
data: data,
error: error,
));
} }
} }

View File

@ -3,5 +3,7 @@ abstract class HomeEvent{
} }
class HomeLoadDataEvent extends HomeEvent{ class HomeLoadDataEvent extends HomeEvent{
const HomeLoadDataEvent(); final String? search;
const HomeLoadDataEvent({this.search});
} }

View File

@ -1,13 +1,32 @@
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:pmu_labs/domain/models/card.dart'; import 'package:pmu_labs/domain/models/card.dart';
class HomeState extends Equatable{ class HomeState extends Equatable {
final Future<List<CardData>?>? data; final List<CardData>? data;
final bool isLoading;
final String? error;
const HomeState({this.data}); const HomeState({
this.data,
this.isLoading = false,
this.error,
});
HomeState copyWith({Future<List<CardData>?>? data}) => HomeState(data: data ?? this.data); HomeState copyWith({
List<CardData>? data,
bool? isLoading,
String? error,
}) =>
HomeState(
data: data ?? this.data,
isLoading: isLoading ?? this.isLoading,
error: error ?? this.error,
);
@override @override
List<Object?> get props => [data]; List<Object?> get props => [
data,
isLoading,
error,
];
} }

View File

@ -1,8 +1,12 @@
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pmu_labs/data/repositories/bosses_repository.dart'; import 'package:flutter_bloc/flutter_bloc.dart%20%20';
import 'package:pmu_labs/presentation/details_page/details_page.dart'; import 'package:pmu_labs/presentation/details_page/details_page.dart';
import 'package:pmu_labs/presentation/home_page/bloc/bloc.dart';
import 'package:pmu_labs/presentation/home_page/bloc/events.dart';
import 'package:pmu_labs/presentation/home_page/bloc/state.dart';
import '../../components/utils/debounce.dart';
import '../../domain/models/card.dart'; import '../../domain/models/card.dart';
part 'card.dart'; part 'card.dart';
@ -40,54 +44,78 @@ class Body extends StatefulWidget {
class _BodyState extends State<Body> { class _BodyState extends State<Body> {
final searchController = TextEditingController(); final searchController = TextEditingController();
late Future<List<CardData>?> data;
final repo = BossesRepository();
@override @override
void initState() { void initState() {
data = repo.loadData(); WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<HomeBloc>().add(const HomeLoadDataEvent());
});
super.initState(); super.initState();
} }
@override
void dispose() {
searchController.dispose();
super.dispose();
}
Future<void> _onRefresh() {
context
.read<HomeBloc>()
.add(HomeLoadDataEvent(search: searchController.text));
return Future.value(null);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Padding( return Column(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top), children: [
child: Column(children: [
Padding( Padding(
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(12),
child: CupertinoSearchTextField( child: CupertinoSearchTextField(
controller: searchController, controller: searchController,
onSubmitted: (search) { onChanged: (search ) {
setState(() { Debounce.run(() => context
data = repo.loadData(q: search); .read<HomeBloc>()
}); .add(HomeLoadDataEvent(search: search)));
}, },
), ),
), ),
Expanded( BlocBuilder<HomeBloc, HomeState>(
child: Center( builder: (context, state) => state.error != null
child: FutureBuilder<List<CardData>?>( ? Text(
future: data, state.error ?? '',
builder: (context, snapshot) => SingleChildScrollView( style: Theme.of(context)
child: snapshot.hasData .textTheme
? Column( .headlineSmall
mainAxisAlignment: MainAxisAlignment.center, ?.copyWith(color: Colors.red),
children: snapshot.data?.map((data) { )
return _Card.fromData( : state.isLoading
? const CircularProgressIndicator()
: Expanded(
child: RefreshIndicator(
onRefresh: _onRefresh,
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: state.data?.length ?? 0,
itemBuilder: (context, index) {
final data = state.data?[index];
return data != null
? _Card.fromData(
data, data,
onLike: (String title, bool isLiked) => onLike: (String title, bool isLiked) =>
_showSnackBar(context, title, isLiked), _showSnackBar(context, title, isLiked),
onTap: () => _navToDetails(context, data), onTap: () => _navToDetails(context, data),
);
}).toList() ??
[],
) )
: const CircularProgressIndicator(), : const SizedBox.shrink();
},
), ),
), ),
), ),
), ),
])); ],
} );
}
void _navToDetails(BuildContext context, CardData data) { void _navToDetails(BuildContext context, CardData data) {
Navigator.push( Navigator.push(