2024-05-18 10:17:16 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
2024-05-19 10:01:00 +00:00
|
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:solian/models/pagination.dart';
|
2024-07-23 10:09:41 +00:00
|
|
|
import 'package:solian/models/post.dart';
|
2024-05-19 10:01:00 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-07-23 10:09:41 +00:00
|
|
|
import 'package:solian/providers/content/posts.dart';
|
2024-05-19 10:01:00 +00:00
|
|
|
import 'package:solian/router.dart';
|
2024-05-25 05:00:40 +00:00
|
|
|
import 'package:solian/screens/account/notification.dart';
|
|
|
|
import 'package:solian/theme.dart';
|
2024-06-22 15:59:11 +00:00
|
|
|
import 'package:solian/widgets/app_bar_title.dart';
|
2024-06-06 12:23:50 +00:00
|
|
|
import 'package:solian/widgets/current_state_action.dart';
|
2024-07-12 14:31:45 +00:00
|
|
|
import 'package:solian/widgets/app_bar_leading.dart';
|
2024-07-09 14:39:44 +00:00
|
|
|
import 'package:solian/widgets/feed/feed_list.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
|
2024-07-12 08:19:54 +00:00
|
|
|
class HomeScreen extends StatefulWidget {
|
|
|
|
const HomeScreen({super.key});
|
2024-05-18 10:17:16 +00:00
|
|
|
|
|
|
|
@override
|
2024-07-12 08:19:54 +00:00
|
|
|
State<HomeScreen> createState() => _HomeScreenState();
|
2024-05-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 08:19:54 +00:00
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
2024-07-23 10:09:41 +00:00
|
|
|
final PagingController<int, Post> _pagingController =
|
2024-05-21 16:05:03 +00:00
|
|
|
PagingController(firstPageKey: 0);
|
2024-05-18 10:17:16 +00:00
|
|
|
|
2024-05-19 10:01:00 +00:00
|
|
|
getPosts(int pageKey) async {
|
2024-07-23 10:09:41 +00:00
|
|
|
final PostProvider provider = Get.find();
|
2024-05-25 05:19:16 +00:00
|
|
|
|
|
|
|
Response resp;
|
|
|
|
try {
|
2024-07-23 10:09:41 +00:00
|
|
|
resp = await provider.listRecommendations(pageKey);
|
2024-05-25 05:19:16 +00:00
|
|
|
} catch (e) {
|
|
|
|
_pagingController.error = e;
|
2024-05-19 10:01:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-05-18 10:17:16 +00:00
|
|
|
|
2024-05-19 10:01:00 +00:00
|
|
|
final PaginationResult result = PaginationResult.fromJson(resp.body);
|
2024-07-23 10:09:41 +00:00
|
|
|
final parsed = result.data?.map((e) => Post.fromJson(e)).toList();
|
2024-05-19 10:01:00 +00:00
|
|
|
if (parsed != null && parsed.length >= 10) {
|
|
|
|
_pagingController.appendPage(parsed, pageKey + parsed.length);
|
|
|
|
} else if (parsed != null) {
|
|
|
|
_pagingController.appendLastPage(parsed);
|
|
|
|
}
|
2024-05-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-05-19 10:01:00 +00:00
|
|
|
_pagingController.addPageRequestListener(getPosts);
|
2024-05-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-19 10:01:00 +00:00
|
|
|
return Scaffold(
|
|
|
|
body: Material(
|
2024-05-21 16:05:03 +00:00
|
|
|
color: Theme.of(context).colorScheme.surface,
|
2024-05-29 12:13:53 +00:00
|
|
|
child: RefreshIndicator(
|
|
|
|
onRefresh: () => Future.sync(() => _pagingController.refresh()),
|
|
|
|
child: CustomScrollView(
|
|
|
|
slivers: [
|
|
|
|
SliverAppBar(
|
2024-07-12 08:19:54 +00:00
|
|
|
title: AppBarTitle('home'.tr),
|
2024-05-29 12:13:53 +00:00
|
|
|
centerTitle: false,
|
|
|
|
floating: true,
|
2024-06-22 15:59:11 +00:00
|
|
|
toolbarHeight: SolianTheme.toolbarHeight(context),
|
2024-07-12 14:37:58 +00:00
|
|
|
leading: AppBarLeadingButton.adaptive(context),
|
2024-05-29 12:13:53 +00:00
|
|
|
actions: [
|
2024-06-06 12:23:50 +00:00
|
|
|
const BackgroundStateWidget(),
|
2024-05-29 12:13:53 +00:00
|
|
|
const NotificationButton(),
|
2024-07-06 19:02:10 +00:00
|
|
|
FeedCreationButton(
|
|
|
|
onCreated: () {
|
|
|
|
_pagingController.refresh();
|
|
|
|
},
|
|
|
|
),
|
2024-05-29 12:13:53 +00:00
|
|
|
SizedBox(
|
|
|
|
width: SolianTheme.isLargeScreen(context) ? 8 : 16,
|
2024-05-21 16:05:03 +00:00
|
|
|
),
|
2024-05-29 12:13:53 +00:00
|
|
|
],
|
2024-05-25 05:00:40 +00:00
|
|
|
),
|
2024-07-07 03:46:48 +00:00
|
|
|
FeedListWidget(controller: _pagingController),
|
2024-05-29 12:13:53 +00:00
|
|
|
],
|
2024-05-19 10:01:00 +00:00
|
|
|
),
|
2024-05-18 16:56:32 +00:00
|
|
|
),
|
2024-05-18 10:17:16 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-07-09 14:39:44 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_pagingController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
2024-05-18 10:17:16 +00:00
|
|
|
}
|
2024-07-06 12:55:53 +00:00
|
|
|
|
|
|
|
class FeedCreationButton extends StatelessWidget {
|
2024-07-09 14:39:44 +00:00
|
|
|
final bool hideDraftBox;
|
2024-07-06 19:02:10 +00:00
|
|
|
final Function? onCreated;
|
|
|
|
|
2024-07-09 14:39:44 +00:00
|
|
|
const FeedCreationButton({
|
|
|
|
super.key,
|
|
|
|
this.hideDraftBox = false,
|
|
|
|
this.onCreated,
|
|
|
|
});
|
2024-07-06 12:55:53 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final AuthProvider auth = Get.find();
|
|
|
|
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) {
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
return PopupMenuButton(
|
|
|
|
icon: const Icon(Icons.edit_square),
|
|
|
|
itemBuilder: (BuildContext context) => [
|
|
|
|
PopupMenuItem(
|
|
|
|
child: ListTile(
|
|
|
|
title: Text('postEditor'.tr),
|
|
|
|
leading: const Icon(Icons.article),
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
AppRouter.instance.pushNamed('postEditor').then((val) {
|
|
|
|
if (val != null && onCreated != null) {
|
|
|
|
onCreated!();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
if (!hideDraftBox)
|
|
|
|
PopupMenuItem(
|
|
|
|
child: ListTile(
|
|
|
|
title: Text('draftBoxOpen'.tr),
|
|
|
|
leading: const Icon(Icons.drafts),
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
AppRouter.instance.pushNamed('draftBox');
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
2024-07-06 12:55:53 +00:00
|
|
|
}
|
|
|
|
}
|