Solian/lib/screens/feed.dart

151 lines
4.9 KiB
Dart
Raw Normal View History

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';
import 'package:solian/models/feed.dart';
2024-05-18 10:17:16 +00:00
import 'package:solian/models/pagination.dart';
2024-05-19 10:01:00 +00:00
import 'package:solian/providers/auth.dart';
2024-05-25 16:11:00 +00:00
import 'package:solian/providers/content/post.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';
import 'package:solian/widgets/posts/feed_list.dart';
2024-05-18 10:17:16 +00:00
2024-07-06 12:55:53 +00:00
class FeedScreen extends StatefulWidget {
const FeedScreen({super.key});
2024-05-18 10:17:16 +00:00
@override
2024-07-06 12:55:53 +00:00
State<FeedScreen> createState() => _FeedScreenState();
2024-05-18 10:17:16 +00:00
}
2024-07-06 12:55:53 +00:00
class _FeedScreenState extends State<FeedScreen> {
final PagingController<int, FeedRecord> _pagingController =
PagingController(firstPageKey: 0);
2024-05-18 10:17:16 +00:00
2024-05-19 10:01:00 +00:00
getPosts(int pageKey) async {
2024-05-25 05:19:16 +00:00
final PostProvider provider = Get.find();
Response resp;
try {
resp = await provider.listFeed(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);
final parsed = result.data?.map((e) => FeedRecord.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(
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-06 12:55:53 +00:00
title: AppBarTitle('feed'.tr),
2024-05-29 12:13:53 +00:00
centerTitle: false,
floating: true,
2024-06-22 15:59:11 +00:00
titleSpacing: SolianTheme.titleSpacing(context),
toolbarHeight: SolianTheme.toolbarHeight(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-29 12:13:53 +00:00
],
2024-05-25 05:00:40 +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-06 12:55:53 +00:00
class FeedCreationButton extends StatelessWidget {
2024-07-06 19:02:10 +00:00
final Function? onCreated;
const FeedCreationButton({super.key, this.onCreated});
2024-07-06 12:55:53 +00:00
@override
Widget build(BuildContext context) {
final AuthProvider auth = Get.find();
return FutureBuilder(
future: auth.isAuthorized,
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data == true) {
2024-07-08 11:56:03 +00:00
return PopupMenuButton(
2024-07-09 13:23:38 +00:00
icon: const Icon(Icons.edit_square),
2024-07-08 11:56:03 +00:00
itemBuilder: (BuildContext context) => [
PopupMenuItem(
child: ListTile(
title: Text('postCreate'.tr),
leading: const Icon(Icons.article),
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
),
onTap: () {
2024-07-09 13:23:38 +00:00
AppRouter.instance.pushNamed('postCreate').then((val) {
2024-07-08 11:56:03 +00:00
if (val != null && onCreated != null) {
onCreated!();
}
});
},
),
PopupMenuItem(
child: ListTile(
title: Text('articleCreate'.tr),
leading: const Icon(Icons.newspaper),
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
),
2024-07-09 13:23:38 +00:00
onTap: () {
AppRouter.instance.pushNamed('articleCreate').then((val) {
if (val != null && onCreated != null) {
onCreated!();
}
});
},
2024-07-08 11:56:03 +00:00
),
PopupMenuItem(
child: ListTile(
title: Text('draftBoxOpen'.tr),
leading: const Icon(Icons.drafts),
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
),
2024-07-09 13:23:38 +00:00
onTap: () {
AppRouter.instance.goNamed('draftBox');
},
2024-07-08 11:56:03 +00:00
),
],
2024-07-06 12:55:53 +00:00
);
}
return const SizedBox();
});
}
}