2024-10-04 14:11:06 +04:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-10-16 14:37:36 +04:00
|
|
|
import 'package:flutter_app/components/utils/debounce.dart';
|
2024-10-16 12:08:15 +04:00
|
|
|
import 'package:flutter_app/data/repositories/manga_repository.dart';
|
|
|
|
import 'package:flutter_app/presentation/details_page/details_page.dart';
|
2024-10-16 14:37:36 +04:00
|
|
|
import 'package:flutter_app/presentation/home_page/bloc/bloc.dart';
|
|
|
|
import 'package:flutter_app/presentation/home_page/bloc/events.dart';
|
|
|
|
import 'package:flutter_app/presentation/home_page/bloc/state.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-10-04 14:11:06 +04:00
|
|
|
|
|
|
|
import '../../domain/models/carddata.dart';
|
2024-10-17 11:02:31 +04:00
|
|
|
import '../common/svg_objects.dart';
|
|
|
|
import '../like_bloc/like_bloc.dart';
|
|
|
|
import '../like_bloc/like_event.dart';
|
|
|
|
import '../like_bloc/like_state.dart';
|
|
|
|
import '../locale_bloc/locale_bloc.dart';
|
|
|
|
import '../locale_bloc/locale_events.dart';
|
|
|
|
import '../locale_bloc/locale_state.dart';
|
2024-10-04 14:11:06 +04:00
|
|
|
|
|
|
|
part 'card.dart';
|
|
|
|
|
2024-10-16 14:37:36 +04:00
|
|
|
class HomePage extends StatefulWidget {
|
|
|
|
const HomePage({super.key});
|
2024-10-04 14:11:06 +04:00
|
|
|
|
|
|
|
@override
|
2024-10-16 14:37:36 +04:00
|
|
|
State<HomePage> createState() => _HomePageState();
|
2024-10-04 14:11:06 +04:00
|
|
|
}
|
|
|
|
|
2024-10-16 14:37:36 +04:00
|
|
|
class _HomePageState extends State<HomePage> {
|
2024-10-04 14:11:06 +04:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-10-16 14:37:36 +04:00
|
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
2024-10-04 14:11:06 +04:00
|
|
|
),
|
|
|
|
body: const Body(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 12:08:15 +04:00
|
|
|
class Body extends StatefulWidget {
|
2024-10-04 14:11:06 +04:00
|
|
|
const Body({super.key});
|
|
|
|
|
2024-10-16 12:08:15 +04:00
|
|
|
@override
|
|
|
|
State<Body> createState() => _BodyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BodyState extends State<Body> {
|
2024-10-16 14:37:36 +04:00
|
|
|
final scrollController = ScrollController();
|
|
|
|
final searchController = TextEditingController();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2024-10-17 11:02:31 +04:00
|
|
|
SvgObjects.init();
|
|
|
|
|
2024-10-16 14:37:36 +04:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
2024-10-17 11:02:31 +04:00
|
|
|
context.read<LikeBloc>().add(const LoadLikesEvent());
|
2024-10-16 14:37:36 +04:00
|
|
|
});
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-10-16 12:08:15 +04:00
|
|
|
final MangaRepository repo = MangaRepository();
|
|
|
|
var data = MangaRepository().loadData();
|
|
|
|
|
2024-10-17 11:02:31 +04:00
|
|
|
void _onLike(String? id, String title, bool isLiked) {
|
|
|
|
if (id != null) {
|
|
|
|
context.read<LikeBloc>().add(ChangeLikeEvent(id));
|
|
|
|
_showSnackBar(context, title, !isLiked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
2024-10-04 14:11:06 +04:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(
|
2024-10-16 12:08:15 +04:00
|
|
|
'Лайк на $title ${isLiked ? 'поставлен' : 'убран'}',
|
2024-10-16 14:37:36 +04:00
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
2024-10-04 14:11:06 +04:00
|
|
|
),
|
|
|
|
backgroundColor: Colors.orangeAccent,
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-16 12:08:15 +04:00
|
|
|
void _navToDetails(BuildContext context, CardData data) {
|
|
|
|
Navigator.push(
|
|
|
|
context, CupertinoPageRoute(builder: (context) => DetailsPage(data)));
|
2024-10-04 14:11:06 +04:00
|
|
|
}
|
2024-10-16 12:08:15 +04:00
|
|
|
|
2024-10-16 14:37:36 +04:00
|
|
|
Future<void> _onRefresh() {
|
|
|
|
context
|
|
|
|
.read<HomeBloc>()
|
|
|
|
.add(HomeLoadDataEvent(search: searchController.text));
|
|
|
|
return Future.value(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Padding(
|
|
|
|
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
child: CupertinoSearchTextField(
|
|
|
|
controller: searchController,
|
|
|
|
onChanged: (search) {
|
|
|
|
Debounce.run(() => context
|
|
|
|
.read<HomeBloc>()
|
|
|
|
.add(HomeLoadDataEvent(search: search)));
|
|
|
|
})),
|
2024-10-17 11:02:31 +04:00
|
|
|
GestureDetector(
|
|
|
|
onTap: () =>
|
|
|
|
context.read<LocaleBloc>().add(const ChangeLocaleEvent()),
|
|
|
|
child: SizedBox.square(
|
|
|
|
dimension: 50,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.only(right: 12),
|
|
|
|
child: BlocBuilder<LocaleBloc, LocaleState>(
|
|
|
|
builder: (context, state) {
|
|
|
|
return state.currentLocale.languageCode == 'ru'
|
|
|
|
? const SvgRu()
|
|
|
|
: const SvgUk();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-10-16 14:37:36 +04:00
|
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) => state.isLoading
|
|
|
|
? CircularProgressIndicator()
|
2024-10-17 11:02:31 +04:00
|
|
|
: BlocBuilder<LikeBloc, LikeState>(
|
|
|
|
builder: (context, likeState) => Expanded(
|
|
|
|
child: RefreshIndicator(
|
|
|
|
onRefresh: _onRefresh,
|
|
|
|
child: ListView.builder(
|
|
|
|
controller: scrollController,
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
itemCount: state.data?.data?.length ?? 0,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final data = state.data?.data?[index];
|
|
|
|
return data != null
|
|
|
|
? _Card.fromData(
|
|
|
|
data,
|
|
|
|
onLike: _onLike,
|
|
|
|
onTap: () =>
|
|
|
|
_navToDetails(context, data),
|
|
|
|
)
|
|
|
|
: const SizedBox.shrink();
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
))),
|
2024-10-16 14:37:36 +04:00
|
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) => state.isPaginationLoading
|
|
|
|
? const CircularProgressIndicator()
|
|
|
|
: const SizedBox.shrink())
|
|
|
|
],
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2024-10-16 12:08:15 +04:00
|
|
|
|
2024-10-16 14:37:36 +04:00
|
|
|
class HomeData {
|
|
|
|
final List<CardData>? data;
|
|
|
|
final int? nextPage;
|
2024-10-16 12:08:15 +04:00
|
|
|
|
2024-10-16 14:37:36 +04:00
|
|
|
HomeData({this.data, this.nextPage});
|
|
|
|
}
|