From 3b44a0f53580f849d2bbd03a7884c5fdfd89b0b0 Mon Sep 17 00:00:00 2001 From: Lea Date: Sun, 1 Jan 2023 02:01:50 +0100 Subject: [PATCH] unread posts tab --- lib/main.dart | 138 ++++++++++++++++++++-------- lib/widgets/centered_page_hint.dart | 23 +++++ 2 files changed, 124 insertions(+), 37 deletions(-) create mode 100644 lib/widgets/centered_page_hint.dart diff --git a/lib/main.dart b/lib/main.dart index c9c55c2..d61e958 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 _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; @@ -36,26 +36,28 @@ class MyApp extends StatelessWidget { Widget build(BuildContext context) { return DynamicColorBuilder( builder: ((lightDynamic, darkDynamic) => MaterialApp( - title: 'Feet', - theme: ThemeData( - brightness: Brightness.light, - useMaterial3: true, - colorScheme: lightDynamic, - ), - darkTheme: ThemeData( - brightness: Brightness.dark, - useMaterial3: true, - colorScheme: darkDynamic, - ), - themeMode: ThemeMode.system, - home: FutureBuilder( - builder: (context, snapshot) { - if (snapshot.hasData && prefs != null) return MyHomePage(title: 'Feet', api: api, prefs: prefs!); - return const SizedBox(); - }, - future: _loadPrefs(), - ), - )), + title: 'Feet', + theme: ThemeData( + brightness: Brightness.light, + useMaterial3: true, + colorScheme: lightDynamic, + ), + darkTheme: ThemeData( + brightness: Brightness.dark, + useMaterial3: true, + colorScheme: darkDynamic, + ), + themeMode: ThemeMode.system, + home: FutureBuilder( + builder: (context, snapshot) { + 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 createState() => _MyHomePageState(); } class _MyHomePageState extends State { + 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,26 +140,42 @@ class _MyHomePageState extends State { actions: [ IconButton( onPressed: () { - widget.api.request() - .withFeeds() - .withGroups() - .withItems() - .execute() - .then((_) { setState(() {}); }); + widget.api + .request() + .withFeeds() + .withGroups() + .withItems() + .execute() + .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)), - ) - : Center(child: LoginPrompt(api: widget.api)), + ? pages[_index] + : Center(child: LoginPrompt(api: widget.api)), ); } } diff --git a/lib/widgets/centered_page_hint.dart b/lib/widgets/centered_page_hint.dart new file mode 100644 index 0000000..a475911 --- /dev/null +++ b/lib/widgets/centered_page_hint.dart @@ -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), + ], + ), + ); + } +}