2024-10-02 02:39:17 +04:00
|
|
|
import 'dart:io';
|
2024-09-30 02:09:26 +04:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-09-30 22:15:55 +04:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-10-02 02:39:17 +04:00
|
|
|
import 'package:pmu_labworks/components/extensions/context_x.dart';
|
2024-09-30 22:15:55 +04:00
|
|
|
import 'package:pmu_labworks/components/utils/debounce.dart';
|
2024-09-30 02:09:26 +04:00
|
|
|
import 'package:pmu_labworks/domain/models/comment.dart';
|
|
|
|
import 'package:pmu_labworks/domain/models/user.dart';
|
2024-10-02 02:39:17 +04:00
|
|
|
import 'package:pmu_labworks/view/common/svg_objects.dart';
|
2024-09-30 02:09:26 +04:00
|
|
|
import 'package:pmu_labworks/view/details_page/details_page.dart';
|
2024-09-30 22:15:55 +04:00
|
|
|
import 'package:pmu_labworks/view/home_page/bloc/bloc.dart';
|
|
|
|
import 'package:pmu_labworks/view/home_page/bloc/events.dart';
|
|
|
|
import 'package:pmu_labworks/view/home_page/bloc/state.dart';
|
2024-10-02 02:39:17 +04:00
|
|
|
import 'package:pmu_labworks/view/reaction_bloc/reaction_bloc.dart';
|
|
|
|
import 'package:pmu_labworks/view/reaction_bloc/reaction_event.dart';
|
|
|
|
import 'package:pmu_labworks/view/reaction_bloc/reaction_state.dart';
|
|
|
|
import 'package:pmu_labworks/view/locale_bloc/locale_bloc.dart';
|
|
|
|
import 'package:pmu_labworks/view/locale_bloc/locale_events.dart';
|
|
|
|
import 'package:pmu_labworks/view/locale_bloc/locale_state.dart';
|
2024-09-30 02:09:26 +04:00
|
|
|
|
|
|
|
part 'comment.dart';
|
|
|
|
|
|
|
|
class HomePage extends StatefulWidget {
|
|
|
|
const HomePage({super.key, required this.title});
|
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<HomePage> createState() => _HomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
|
|
Color _color = Colors.transparent;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_color = _generateColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: _color,
|
|
|
|
title: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(widget.title,
|
2024-09-30 22:15:55 +04:00
|
|
|
style: const TextStyle(
|
2024-09-30 02:09:26 +04:00
|
|
|
fontSize: 20,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
)),
|
2024-10-02 02:39:17 +04:00
|
|
|
Text(
|
|
|
|
context.locale.credits + ' Factorino',
|
2024-09-30 02:09:26 +04:00
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 12,
|
|
|
|
fontStyle: FontStyle.italic,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2024-09-30 22:15:55 +04:00
|
|
|
body: const _Body(),
|
2024-09-30 02:09:26 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color _generateColor() {
|
|
|
|
final Random random = Random();
|
|
|
|
final int red = (random.nextInt(106) + 150);
|
|
|
|
final int green = (random.nextInt(106) + 150);
|
|
|
|
final int blue = (random.nextInt(106) + 150);
|
|
|
|
|
|
|
|
return Color.fromARGB(255, red, green, blue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-30 22:15:55 +04:00
|
|
|
class _Body extends StatefulWidget {
|
|
|
|
const _Body();
|
2024-09-30 02:09:26 +04:00
|
|
|
|
2024-09-30 18:24:51 +04:00
|
|
|
@override
|
2024-09-30 22:15:55 +04:00
|
|
|
State<_Body> createState() => _BodyState();
|
2024-09-30 18:24:51 +04:00
|
|
|
}
|
|
|
|
|
2024-09-30 22:15:55 +04:00
|
|
|
class _BodyState extends State<_Body> {
|
2024-09-30 18:24:51 +04:00
|
|
|
final searchController = TextEditingController();
|
2024-09-30 22:15:55 +04:00
|
|
|
final scrollController = ScrollController();
|
2024-09-30 18:24:51 +04:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2024-10-02 02:39:17 +04:00
|
|
|
SvgObjects.init();
|
|
|
|
|
2024-09-30 22:15:55 +04:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
2024-10-02 02:39:17 +04:00
|
|
|
context.read<ReactionBloc>().add(const LoadReactionsEvent());
|
2024-09-30 22:15:55 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
scrollController.addListener(_onNextPageListener);
|
|
|
|
|
2024-09-30 18:24:51 +04:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-09-30 19:52:52 +04:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
searchController.dispose();
|
2024-09-30 22:15:55 +04:00
|
|
|
scrollController.dispose();
|
2024-09-30 19:52:52 +04:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2024-09-30 02:09:26 +04:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-09-30 18:24:51 +04:00
|
|
|
return Padding(
|
|
|
|
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
2024-10-02 02:39:17 +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-09-30 18:24:51 +04:00
|
|
|
),
|
2024-09-30 22:15:55 +04:00
|
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) => state.error != null
|
|
|
|
? Text(
|
|
|
|
state.error ?? '',
|
2024-10-02 02:39:17 +04:00
|
|
|
style: Theme.of(context).textTheme.headlineSmall?.copyWith(color: Colors.red),
|
2024-09-30 22:15:55 +04:00
|
|
|
)
|
|
|
|
: state.isLoading
|
|
|
|
? const CircularProgressIndicator()
|
2024-10-02 02:39:17 +04:00
|
|
|
: BlocBuilder<ReactionBloc, ReactionState>(
|
|
|
|
builder: (context, reactionState) {
|
|
|
|
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
|
|
|
|
? _Comment.fromData(
|
|
|
|
data,
|
|
|
|
onLike: _onLike,
|
|
|
|
onDislike: _onDislike,
|
|
|
|
isLiked:
|
|
|
|
reactionState.likedIds?.contains(data.id) == true,
|
|
|
|
isDisliked:
|
|
|
|
reactionState.dislikedIds?.contains(data.id) == true,
|
|
|
|
onTap: () => _navToDetails(context, data),
|
|
|
|
)
|
|
|
|
: const SizedBox.shrink();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
2024-09-30 22:15:55 +04:00
|
|
|
),
|
|
|
|
),
|
|
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) => state.isPaginationLoading
|
|
|
|
? const CircularProgressIndicator()
|
|
|
|
: const SizedBox.shrink(),
|
2024-09-30 18:24:51 +04:00
|
|
|
),
|
|
|
|
],
|
2024-09-30 02:09:26 +04:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-30 22:15:55 +04:00
|
|
|
Future<void> _onRefresh() {
|
2024-10-02 02:39:17 +04:00
|
|
|
context.read<HomeBloc>().add(HomeLoadDataEvent(search: searchController.text));
|
2024-09-30 22:15:55 +04:00
|
|
|
return Future.value(null);
|
|
|
|
}
|
|
|
|
|
2024-10-02 02:39:17 +04:00
|
|
|
void _onLike(String? id, String nickname, bool isLiked, bool isDisliked) {
|
|
|
|
if (id != null) {
|
|
|
|
if (isDisliked) {
|
|
|
|
isDisliked = false;
|
|
|
|
}
|
|
|
|
context.read<ReactionBloc>().add(ChangeLikeEvent(id));
|
|
|
|
_showSnackBar(context, nickname, !isLiked, isDisliked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onDislike(String? id, String nickname, bool isLiked, bool isDisliked) {
|
|
|
|
if (id != null) {
|
|
|
|
if (isLiked) {
|
|
|
|
isLiked = false;
|
|
|
|
}
|
|
|
|
context.read<ReactionBloc>().add(ChangeDislikeEvent(id));
|
|
|
|
_showSnackBar(context, nickname, isLiked, !isDisliked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-30 22:15:55 +04:00
|
|
|
void _navToDetails(BuildContext context, CommentData data) {
|
2024-09-30 02:09:26 +04:00
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
CupertinoPageRoute(builder: (context) => DetailsPage(data)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-02 02:39:17 +04:00
|
|
|
void _showSnackBar(BuildContext context, String nickname, bool isLiked, bool isDisliked) {
|
2024-09-30 02:09:26 +04:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
final String action;
|
|
|
|
|
2024-10-02 02:39:17 +04:00
|
|
|
if (isLiked && !isDisliked) {
|
|
|
|
action = context.locale.liked;
|
|
|
|
} else if (!isLiked && isDisliked) {
|
|
|
|
action = context.locale.disliked;
|
2024-09-30 02:09:26 +04:00
|
|
|
} else {
|
2024-10-02 02:39:17 +04:00
|
|
|
action = context.locale.neutralized; // Состояние без лайков/дизлайков
|
2024-09-30 02:09:26 +04:00
|
|
|
}
|
2024-10-02 02:39:17 +04:00
|
|
|
print('$isLiked $isDisliked = $action');
|
2024-09-30 02:09:26 +04:00
|
|
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(
|
2024-10-02 02:39:17 +04:00
|
|
|
context.locale.comment(nickname) + ' ' + action,
|
2024-09-30 02:09:26 +04:00
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
|
|
),
|
|
|
|
backgroundColor: Colors.white70,
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
2024-09-30 22:15:55 +04:00
|
|
|
|
|
|
|
void _onNextPageListener() {
|
2024-09-30 23:21:42 +04:00
|
|
|
if (scrollController.offset >= scrollController.position.maxScrollExtent) {
|
2024-09-30 22:15:55 +04:00
|
|
|
// preventing multiple pagination request on multiple swipes
|
|
|
|
final bloc = context.read<HomeBloc>();
|
|
|
|
if (!bloc.state.isPaginationLoading) {
|
|
|
|
bloc.add(HomeLoadDataEvent(
|
|
|
|
search: searchController.text,
|
|
|
|
nextPage: bloc.state.data?.nextPage,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-30 02:09:26 +04:00
|
|
|
}
|