automatically fetch posts
This commit is contained in:
parent
d2f61e77b1
commit
e3f762d6bb
|
@ -5,6 +5,7 @@ import 'package:feet/widgets/homepage_post.dart';
|
|||
import 'package:feet/widgets/login_prompt.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:dynamic_color/dynamic_color.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'api/api.dart';
|
||||
|
||||
|
@ -75,6 +76,18 @@ class MyHomePage extends StatefulWidget {
|
|||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
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
|
||||
Widget build(BuildContext context) {
|
||||
|
@ -86,41 +99,36 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||
final pages = [
|
||||
/* Posts list */
|
||||
posts.isNotEmpty
|
||||
? ListView(
|
||||
children: posts.map((value) => HomepagePost(post: value)).toList()
|
||||
..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch
|
||||
.compareTo(a.post.createdOnTime.millisecondsSinceEpoch)),
|
||||
)
|
||||
: const CenteredPageHint(
|
||||
icon: Icons.coffee,
|
||||
text: "Your feed is currently empty."
|
||||
),
|
||||
? ListView(
|
||||
children: posts.map((value) => HomepagePost(post: value)).toList()
|
||||
..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch
|
||||
.compareTo(a.post.createdOnTime.millisecondsSinceEpoch)),
|
||||
)
|
||||
: const CenteredPageHint(
|
||||
icon: Icons.coffee, text: "Your feed is currently empty."),
|
||||
/* Unread */
|
||||
unreadPosts.isNotEmpty
|
||||
? ListView(
|
||||
children: unreadPosts
|
||||
.map((value) => HomepagePost(post: value))
|
||||
.toList()
|
||||
..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch
|
||||
.compareTo(a.post.createdOnTime.millisecondsSinceEpoch)),
|
||||
)
|
||||
: const CenteredPageHint(
|
||||
icon: Icons.check,
|
||||
text: "Nothing new here!"
|
||||
),
|
||||
? ListView(
|
||||
children: unreadPosts
|
||||
.map((value) => HomepagePost(post: value))
|
||||
.toList()
|
||||
..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch
|
||||
.compareTo(a.post.createdOnTime.millisecondsSinceEpoch)),
|
||||
)
|
||||
: const CenteredPageHint(
|
||||
icon: Icons.check, text: "Nothing new here!"),
|
||||
/* Saved posts */
|
||||
savedPosts.isNotEmpty
|
||||
? ListView(
|
||||
children: savedPosts
|
||||
.map((value) => HomepagePost(post: value))
|
||||
.toList()
|
||||
..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch
|
||||
.compareTo(a.post.createdOnTime.millisecondsSinceEpoch)),
|
||||
)
|
||||
: const CenteredPageHint(
|
||||
icon: Icons.bookmarks_outlined,
|
||||
text: "Nothing here yet. Try saving some posts!"
|
||||
),
|
||||
? ListView(
|
||||
children: savedPosts
|
||||
.map((value) => HomepagePost(post: value))
|
||||
.toList()
|
||||
..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch
|
||||
.compareTo(a.post.createdOnTime.millisecondsSinceEpoch)),
|
||||
)
|
||||
: const CenteredPageHint(
|
||||
icon: Icons.bookmarks_outlined,
|
||||
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
|
||||
|
@ -140,13 +148,18 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||
actions: [
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
widget.api
|
||||
.request()
|
||||
.withFeeds()
|
||||
.withGroups()
|
||||
.withItems()
|
||||
.execute()
|
||||
.then((_) {
|
||||
var theme = Theme.of(context);
|
||||
|
||||
HapticFeedback.heavyImpact();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Refreshing feeds',
|
||||
style: TextStyle(color: theme.colorScheme.onBackground)),
|
||||
duration: const Duration(seconds: 2),
|
||||
backgroundColor: theme.colorScheme.secondaryContainer,
|
||||
),
|
||||
);
|
||||
refresh().then((_) {
|
||||
setState(() {});
|
||||
});
|
||||
},
|
||||
|
@ -174,7 +187,10 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||
],
|
||||
),
|
||||
body: loggedIn
|
||||
? pages[_index]
|
||||
? (_fetched
|
||||
? pages[_index]
|
||||
: const CenteredPageHint(
|
||||
icon: Icons.downloading, text: "Fetching your feeds..."))
|
||||
: Center(child: LoginPrompt(api: widget.api)),
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue