Fetch new content without refetching everything

Closes #14
This commit is contained in:
Lea 2023-02-05 21:15:45 +01:00
parent d12c5d0349
commit dce3194e5d
Signed by: Lea
GPG key ID: 1BAFFE8347019C42

View file

@ -92,23 +92,70 @@ class MyHomePage extends StatefulWidget {
State<MyHomePage> createState() => _MyHomePageState(); State<MyHomePage> createState() => _MyHomePageState();
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
var _index = 0; var _index = 0;
var _fetched = false; var _fetched = false;
var _fetchedCount = 0; var _fetchedCount = 0;
var _knownPostsSize = -1; var _knownPostsSize = -1;
var _isDemoUser = false; var _isDemoUser = false;
AppLifecycleState? _appState;
List<int> _feedFilter = []; List<int> _feedFilter = [];
Future<void> refresh() async { @override
void didChangeAppLifecycleState(AppLifecycleState state) {
print("Lifecycle state changed from $_appState to $state");
if (_appState != state) {
if (state == AppLifecycleState.resumed) {
refresh(fullRefresh: false);
}
}
setState(() {
_appState = state;
});
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
setState(() {
print("Demo: ${widget.prefs.getBool("isDemo")}");
_isDemoUser = widget.prefs.getBool("isDemo") ?? false;
});
refresh(fullRefresh: true).then((_) => setState(() {
_fetched = true;
}));
}
@override
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);
}
Future<void> refresh({ required bool fullRefresh }) async {
print("Starting refresh");
listIds() => widget.api.cache.items
.getAll()
.values
.map((value) => value.id)
.toList()
..sort((a, b) => b - a);
if (fullRefresh) {
widget.api.cache.clear(); widget.api.cache.clear();
setState(() { setState(() {
_fetchedCount = 0; _fetchedCount = 0;
_knownPostsSize = -1; _knownPostsSize = -1;
_feedFilter = []; _feedFilter = [];
}); });
}
print("Starting refresh");
var data = await widget.api var data = await widget.api
.request() .request()
@ -116,47 +163,27 @@ class _MyHomePageState extends State<MyHomePage> {
.withGroups() .withGroups()
.withFavicons() .withFavicons()
.withItems() .withItems()
.sinceId(0) .sinceId(fullRefresh ? 0 : listIds().first)
.execute(); .execute();
while ((data['items'] as List<dynamic>).isNotEmpty && while ((data['items'] as List<dynamic>).isNotEmpty &&
_fetchedCount < fetchMaxPosts) { _fetchedCount < fetchMaxPosts) {
print("Fetching more items"); print("Fetching more items");
var ids = widget.api.cache.items var ids = listIds();
.getAll()
.values
.map((value) => value.id)
.toList()
..sort((a, b) => b - a);
setState(() { setState(() {
_fetchedCount = ids.length; _fetchedCount = ids.length;
_knownPostsSize = data['total_items'] ?? -1; _knownPostsSize = data['total_items'] ?? -1;
}); });
data = data = await widget.api.request().withItems().sinceId(ids.first).execute();
await widget.api.request().withItems().sinceId(ids.first).execute();
print( print(
"Item cache size is now ${widget.api.cache.items.size} ${ids.first} ${ids.last}"); "Item cache size is now ${widget.api.cache.items.size} ${ids.first} ${ids.last}");
} }
} }
@override
void initState() {
super.initState();
setState(() {
print("Demo: ${widget.prefs.getBool("isDemo")}");
_isDemoUser = widget.prefs.getBool("isDemo") ?? false;
});
refresh().then((_) => setState(() {
_fetched = true;
}));
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var isDemo = widget.prefs.getBool("isDemo"); var isDemo = widget.prefs.getBool("isDemo");
@ -218,7 +245,7 @@ class _MyHomePageState extends State<MyHomePage> {
if (loggedIn) { if (loggedIn) {
timer.cancel(); timer.cancel();
refresh().then((_) => setState(() => _fetched = true)); refresh(fullRefresh: true).then((_) => setState(() => _fetched = true));
} }
}); });
} }
@ -285,7 +312,7 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor: theme.colorScheme.secondaryContainer, backgroundColor: theme.colorScheme.secondaryContainer,
), ),
); );
refresh().then((_) { refresh(fullRefresh: false).then((_) {
setState(() {}); setState(() {});
}); });
}, },