unread posts tab

This commit is contained in:
Lea 2023-01-01 02:01:50 +01:00
parent 72f61ea804
commit 3b44a0f535
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
2 changed files with 124 additions and 37 deletions

View file

@ -1,5 +1,6 @@
import 'dart:async';
import 'package:feet/widgets/centered_page_hint.dart';
import 'package:feet/widgets/homepage_post.dart';
import 'package:feet/widgets/login_prompt.dart';
import 'package:flutter/material.dart';
@ -20,8 +21,7 @@ class MyApp extends StatelessWidget {
Future<bool> _loadPrefs() async {
var prefs = await SharedPreferences.getInstance();
this.prefs = prefs;
var apiKey = prefs.getString('apiKey'),
apiUrl = prefs.getString('apiUrl');
var apiKey = prefs.getString('apiKey'), apiUrl = prefs.getString('apiUrl');
if (apiUrl != null && apiKey != null) {
api.apiUrl = apiUrl;
@ -50,7 +50,9 @@ class MyApp extends StatelessWidget {
themeMode: ThemeMode.system,
home: FutureBuilder<bool>(
builder: (context, snapshot) {
if (snapshot.hasData && prefs != null) return MyHomePage(title: 'Feet', api: api, prefs: prefs!);
if (snapshot.hasData && prefs != null) {
return MyHomePage(title: 'Feet', api: api, prefs: prefs!);
}
return const SizedBox();
},
future: _loadPrefs(),
@ -64,16 +66,62 @@ class MyHomePage extends StatefulWidget {
final String title;
final FeverAPI api;
final SharedPreferences prefs;
const MyHomePage({ super.key, required this.title, required this.api, required this.prefs });
const MyHomePage(
{super.key, required this.title, required this.api, required this.prefs});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var _index = 0;
@override
Widget build(BuildContext context) {
var loggedIn = widget.api.loggedIn();
var posts = widget.api.cache.items.getAll().values;
var savedPosts = posts.where((element) => element.isSaved);
var unreadPosts = posts.where((element) => !element.isRead);
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."
),
/* 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!"
),
/* 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!"
),
];
// I'm aware that this is stupid, but I'm sick and tired and really don't care as long as it works
if (!loggedIn) {
@ -92,25 +140,41 @@ class _MyHomePageState extends State<MyHomePage> {
actions: [
IconButton(
onPressed: () {
widget.api.request()
widget.api
.request()
.withFeeds()
.withGroups()
.withItems()
.execute()
.then((_) { setState(() {}); });
.then((_) {
setState(() {});
});
},
icon: const Icon(Icons.replay_outlined),
),
],
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _index,
onTap: (value) => setState(() => _index = value),
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.list),
label: 'Feed',
),
BottomNavigationBarItem(
icon: Icon(Icons.lens_blur),
label: 'Unread',
),
BottomNavigationBarItem(
icon: Icon(Icons.bookmark_outline),
activeIcon: Icon(Icons.bookmark),
label: 'Saved',
),
],
),
body: loggedIn
? ListView(
children: widget.api.cache.items.getAll()
.values
.map((value) => HomepagePost(post: value))
.toList()
..sort((a, b) => b.post.createdOnTime.millisecondsSinceEpoch.compareTo(a.post.createdOnTime.millisecondsSinceEpoch)),
)
? pages[_index]
: Center(child: LoginPrompt(api: widget.api)),
);
}

View file

@ -0,0 +1,23 @@
import 'package:flutter/material.dart';
class CenteredPageHint extends StatelessWidget {
final IconData icon;
final String text;
const CenteredPageHint({super.key, required this.icon, required this.text});
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: const EdgeInsets.all(12.0),
child: Icon(icon, size: 48),
),
Text(text),
],
),
);
}
}