automatically fetch posts

This commit is contained in:
Lea 2023-01-01 02:19:24 +01:00
parent d2f61e77b1
commit e3f762d6bb
Signed by: Lea
GPG key ID: 1BAFFE8347019C42

View file

@ -5,6 +5,7 @@ import 'package:feet/widgets/homepage_post.dart';
import 'package:feet/widgets/login_prompt.dart'; import 'package:feet/widgets/login_prompt.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:dynamic_color/dynamic_color.dart'; import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'api/api.dart'; import 'api/api.dart';
@ -75,6 +76,18 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
var _index = 0; var _index = 0;
var _fetched = false;
Future<void> refresh() =>
widget.api.request().withFeeds().withGroups().withItems().execute();
@override
void initState() {
super.initState();
refresh().then((_) => setState(() {
_fetched = true;
}));
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -86,41 +99,36 @@ class _MyHomePageState extends State<MyHomePage> {
final pages = [ final pages = [
/* Posts list */ /* Posts list */
posts.isNotEmpty posts.isNotEmpty
? ListView( ? ListView(
children: posts.map((value) => HomepagePost(post: value)).toList() children: posts.map((value) => HomepagePost(post: value)).toList()
..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch ..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch
.compareTo(a.post.createdOnTime.millisecondsSinceEpoch)), .compareTo(a.post.createdOnTime.millisecondsSinceEpoch)),
) )
: const CenteredPageHint( : const CenteredPageHint(
icon: Icons.coffee, icon: Icons.coffee, text: "Your feed is currently empty."),
text: "Your feed is currently empty."
),
/* Unread */ /* Unread */
unreadPosts.isNotEmpty unreadPosts.isNotEmpty
? ListView( ? ListView(
children: unreadPosts children: unreadPosts
.map((value) => HomepagePost(post: value)) .map((value) => HomepagePost(post: value))
.toList() .toList()
..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch ..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch
.compareTo(a.post.createdOnTime.millisecondsSinceEpoch)), .compareTo(a.post.createdOnTime.millisecondsSinceEpoch)),
) )
: const CenteredPageHint( : const CenteredPageHint(
icon: Icons.check, icon: Icons.check, text: "Nothing new here!"),
text: "Nothing new here!"
),
/* Saved posts */ /* Saved posts */
savedPosts.isNotEmpty savedPosts.isNotEmpty
? ListView( ? ListView(
children: savedPosts children: savedPosts
.map((value) => HomepagePost(post: value)) .map((value) => HomepagePost(post: value))
.toList() .toList()
..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch ..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch
.compareTo(a.post.createdOnTime.millisecondsSinceEpoch)), .compareTo(a.post.createdOnTime.millisecondsSinceEpoch)),
) )
: const CenteredPageHint( : const CenteredPageHint(
icon: Icons.bookmarks_outlined, icon: Icons.bookmarks_outlined,
text: "Nothing here yet. Try saving some posts!" text: "Nothing here yet. Try saving some posts!"),
),
]; ];
// I'm aware that this is stupid, but I'm sick and tired and really don't care as long as it works // I'm aware that this is stupid, but I'm sick and tired and really don't care as long as it works
@ -140,13 +148,18 @@ class _MyHomePageState extends State<MyHomePage> {
actions: [ actions: [
IconButton( IconButton(
onPressed: () { onPressed: () {
widget.api var theme = Theme.of(context);
.request()
.withFeeds() HapticFeedback.heavyImpact();
.withGroups() ScaffoldMessenger.of(context).showSnackBar(
.withItems() SnackBar(
.execute() content: Text('Refreshing feeds',
.then((_) { style: TextStyle(color: theme.colorScheme.onBackground)),
duration: const Duration(seconds: 2),
backgroundColor: theme.colorScheme.secondaryContainer,
),
);
refresh().then((_) {
setState(() {}); setState(() {});
}); });
}, },
@ -174,7 +187,10 @@ class _MyHomePageState extends State<MyHomePage> {
], ],
), ),
body: loggedIn body: loggedIn
? pages[_index] ? (_fetched
? pages[_index]
: const CenteredPageHint(
icon: Icons.downloading, text: "Fetching your feeds..."))
: Center(child: LoginPrompt(api: widget.api)), : Center(child: LoginPrompt(api: widget.api)),
); );
} }