2024-09-23 20:37:45 +04:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-10-07 19:36:49 +04:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-10-15 22:14:24 +04:00
|
|
|
import 'package:pmu/components/extensions/context_x.dart';
|
2024-10-07 19:36:49 +04:00
|
|
|
import 'package:pmu/components/utils/debounce.dart';
|
2024-09-25 15:53:40 +04:00
|
|
|
import 'package:pmu/data/repositories/bbc_repository.dart';
|
|
|
|
import 'package:pmu/data/repositories/mock_repository.dart';
|
2024-09-23 20:37:45 +04:00
|
|
|
import 'package:pmu/domain/models/card.dart';
|
2024-10-15 22:14:24 +04:00
|
|
|
import 'package:pmu/presentation/common/svg_objects.dart';
|
2024-09-23 20:37:45 +04:00
|
|
|
import 'package:pmu/presentation/details_page/details_page.dart';
|
2024-09-25 15:53:40 +04:00
|
|
|
import 'package:pmu/presentation/dialogs/show_dialog.dart';
|
2024-10-07 19:36:49 +04:00
|
|
|
import 'package:pmu/presentation/home_page/bloc/bloc.dart';
|
|
|
|
import 'package:pmu/presentation/home_page/bloc/events.dart';
|
|
|
|
import 'package:pmu/presentation/home_page/bloc/state.dart';
|
2024-10-15 22:14:24 +04:00
|
|
|
import 'package:pmu/presentation/likes_bloc/likes_bloc.dart';
|
|
|
|
import 'package:pmu/presentation/likes_bloc/likes_events.dart';
|
|
|
|
import 'package:pmu/presentation/likes_bloc/likes_state.dart';
|
|
|
|
import 'package:pmu/presentation/locale_bloc/locale_bloc.dart';
|
|
|
|
import 'package:pmu/presentation/locale_bloc/locale_events.dart';
|
|
|
|
import 'package:pmu/presentation/locale_bloc/locale_state.dart';
|
2024-09-23 20:37:45 +04:00
|
|
|
|
|
|
|
part 'card.dart';
|
|
|
|
|
|
|
|
class MyHomePage extends StatefulWidget {
|
2024-10-07 19:36:49 +04:00
|
|
|
//const MyHomePage({super.key, required this.title});
|
2024-09-23 20:37:45 +04:00
|
|
|
|
2024-10-07 19:36:49 +04:00
|
|
|
//final String title;
|
|
|
|
const MyHomePage({super.key});
|
2024-09-23 20:37:45 +04:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-09-25 15:53:40 +04:00
|
|
|
return const Scaffold(
|
2024-10-07 19:36:49 +04:00
|
|
|
body: _Body(),
|
2024-09-23 20:37:45 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 19:36:49 +04:00
|
|
|
class _Body extends StatefulWidget {
|
|
|
|
const _Body({super.key});
|
2024-09-23 20:37:45 +04:00
|
|
|
|
2024-09-25 15:53:40 +04:00
|
|
|
@override
|
2024-10-07 19:36:49 +04:00
|
|
|
State<_Body> createState() => _BodyState();
|
2024-09-25 15:53:40 +04:00
|
|
|
}
|
|
|
|
|
2024-10-07 19:36:49 +04:00
|
|
|
class _BodyState extends State<_Body> {
|
2024-09-25 15:53:40 +04:00
|
|
|
final searchController = TextEditingController();
|
2024-10-07 19:36:49 +04:00
|
|
|
final scrollController = ScrollController();
|
|
|
|
|
|
|
|
// late Future<List<CardData>?> data;
|
|
|
|
// final repo = BbcRepository();
|
2024-09-25 15:53:40 +04:00
|
|
|
@override
|
2024-10-07 19:36:49 +04:00
|
|
|
void initState() {
|
2024-10-15 22:14:24 +04:00
|
|
|
SvgObjects.init();
|
2024-10-07 19:36:49 +04:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
2024-10-15 22:14:24 +04:00
|
|
|
context.read<LikeBloc>().add(const LoadLikesEvent());
|
2024-10-07 19:36:49 +04:00
|
|
|
});
|
|
|
|
//data = repo.loadData(onError: (e) => showErrorDialog(context, error: e));
|
|
|
|
scrollController.addListener(_onNextPageListener);
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onNextPageListener() {
|
|
|
|
if (scrollController.offset > scrollController.position.maxScrollExtent) {
|
|
|
|
// preventing multiple pagination request on multiple swipes
|
|
|
|
final bloc = context.read<HomeBloc>();
|
|
|
|
if (!bloc.state.isPaginationLoading) {
|
|
|
|
bloc.add(HomeLoadDataEvent(
|
|
|
|
search: searchController.text,
|
2024-10-15 16:57:07 +04:00
|
|
|
nextPage: 2,
|
2024-10-07 19:36:49 +04:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
searchController.dispose();
|
|
|
|
scrollController.dispose();
|
2024-09-25 15:53:40 +04:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-09-23 20:37:45 +04:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-09-25 15:53:40 +04:00
|
|
|
//final data = BbcRepository().loadData();
|
2024-10-07 19:36:49 +04:00
|
|
|
return Padding(
|
|
|
|
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
|
|
|
|
child: Column(children: [
|
2024-10-15 22:14:24 +04:00
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
flex: 4,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
child: CupertinoSearchTextField(
|
|
|
|
controller: searchController,
|
|
|
|
placeholder: context.locale.search,
|
|
|
|
onChanged: (search) {
|
|
|
|
Debounce.run(() => context
|
|
|
|
.read<HomeBloc>()
|
|
|
|
.add(HomeLoadDataEvent(search: search)));
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
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-07 19:36:49 +04:00
|
|
|
),
|
|
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) => state.error != null
|
|
|
|
? Text(
|
|
|
|
state.error ?? '',
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.headlineSmall
|
|
|
|
?.copyWith(color: Colors.red),
|
|
|
|
)
|
|
|
|
: state.isLoading
|
|
|
|
? const CircularProgressIndicator()
|
2024-10-15 22:14:24 +04:00
|
|
|
: BlocBuilder<LikeBloc, LikeState>(
|
|
|
|
builder: (context, likeState) {
|
|
|
|
return 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,
|
|
|
|
isLiked: likeState.likedIds
|
|
|
|
?.contains(data.id) ==
|
|
|
|
true,
|
|
|
|
onTap: () =>
|
|
|
|
_navToDetails(context, data),
|
|
|
|
)
|
|
|
|
: const SizedBox.shrink();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
2024-09-25 15:53:40 +04:00
|
|
|
),
|
2024-10-07 19:36:49 +04:00
|
|
|
),
|
|
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) => state.isPaginationLoading
|
|
|
|
? const CircularProgressIndicator()
|
|
|
|
: const SizedBox.shrink(),
|
|
|
|
),
|
|
|
|
]),
|
2024-09-23 20:37:45 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(
|
2024-10-15 22:14:24 +04:00
|
|
|
'The New ${isLiked ? context.locale.liked : context.locale.disliked}',
|
2024-10-07 19:36:49 +04:00
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
2024-09-23 20:37:45 +04:00
|
|
|
),
|
|
|
|
backgroundColor: Colors.lightBlue,
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _navToDetails(BuildContext context, CardData data) {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
CupertinoPageRoute(builder: (context) => DetailsPage(data)),
|
|
|
|
);
|
|
|
|
}
|
2024-10-07 19:36:49 +04:00
|
|
|
|
2024-10-15 22:14:24 +04:00
|
|
|
void _onLike(String? id, String title, bool isLiked) {
|
|
|
|
if (id != null) {
|
|
|
|
context.read<LikeBloc>().add(ChangeLikeEvent(id));
|
|
|
|
_showSnackBar(context, title, !isLiked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-07 19:36:49 +04:00
|
|
|
Future<void> _onRefresh() {
|
|
|
|
context
|
|
|
|
.read<HomeBloc>()
|
|
|
|
.add(HomeLoadDataEvent(search: searchController.text));
|
|
|
|
return Future.value(null);
|
|
|
|
}
|
2024-09-23 20:37:45 +04:00
|
|
|
}
|