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-07-07 03:46:48 +00:00
|
|
|
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-07-09 14:39:44 +00:00
|
|
|
import 'package:solian/providers/content/feed.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-09 14:39:44 +00:00
|
|
|
import 'package:solian/widgets/feed/feed_list.dart';
|
2024-07-12 03:39:44 +00:00
|
|
|
import 'package:solian/widgets/drawer_button.dart' as drawer;
|
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> {
|
2024-07-07 03:46:48 +00:00
|
|
|
final PagingController<int, FeedRecord> _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-09 14:39:44 +00:00
|
|
|
final FeedProvider provider = Get.find();
|
2024-05-25 05:19:16 +00:00
|
|
|
|
|
|
|
Response resp;
|
|
|
|
try {
|
2024-07-07 03:46:48 +00:00
|
|
|
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);
|
2024-07-07 03:46:48 +00:00
|
|
|
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(
|
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-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
|
|
|
toolbarHeight: SolianTheme.toolbarHeight(context),
|
2024-07-12 03:39:44 +00:00
|
|
|
leading: const drawer.DrawerButton(),
|
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();
|
|
|
|
|
|
|
|
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(
|
2024-07-10 02:50:10 +00:00
|
|
|
title: Text('postEditor'.tr),
|
2024-07-08 11:56:03 +00:00
|
|
|
leading: const Icon(Icons.article),
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
|
|
|
|
),
|
|
|
|
onTap: () {
|
2024-07-10 02:50:10 +00:00
|
|
|
AppRouter.instance.pushNamed('postEditor').then((val) {
|
2024-07-08 11:56:03 +00:00
|
|
|
if (val != null && onCreated != null) {
|
|
|
|
onCreated!();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
PopupMenuItem(
|
|
|
|
child: ListTile(
|
2024-07-10 02:50:10 +00:00
|
|
|
title: Text('articleEditor'.tr),
|
2024-07-08 11:56:03 +00:00
|
|
|
leading: const Icon(Icons.newspaper),
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 8),
|
|
|
|
),
|
2024-07-09 13:23:38 +00:00
|
|
|
onTap: () {
|
2024-07-10 02:50:10 +00:00
|
|
|
AppRouter.instance.pushNamed('articleEditor').then((val) {
|
2024-07-09 13:23:38 +00:00
|
|
|
if (val != null && onCreated != null) {
|
|
|
|
onCreated!();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2024-07-08 11:56:03 +00:00
|
|
|
),
|
2024-07-09 14:39:44 +00:00
|
|
|
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-08 11:56:03 +00:00
|
|
|
),
|
|
|
|
],
|
2024-07-06 12:55:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return const SizedBox();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|