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();
}
class _MyHomePageState extends State<MyHomePage> {
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
var _index = 0;
var _fetched = false;
var _fetchedCount = 0;
var _knownPostsSize = -1;
var _isDemoUser = false;
AppLifecycleState? _appState;
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();
setState(() {
_fetchedCount = 0;
_knownPostsSize = -1;
_feedFilter = [];
});
print("Starting refresh");
}
var data = await widget.api
.request()
@ -116,47 +163,27 @@ class _MyHomePageState extends State<MyHomePage> {
.withGroups()
.withFavicons()
.withItems()
.sinceId(0)
.sinceId(fullRefresh ? 0 : listIds().first)
.execute();
while ((data['items'] as List<dynamic>).isNotEmpty &&
_fetchedCount < fetchMaxPosts) {
print("Fetching more items");
var ids = widget.api.cache.items
.getAll()
.values
.map((value) => value.id)
.toList()
..sort((a, b) => b - a);
var ids = listIds();
setState(() {
_fetchedCount = ids.length;
_knownPostsSize = data['total_items'] ?? -1;
});
data =
await widget.api.request().withItems().sinceId(ids.first).execute();
data = await widget.api.request().withItems().sinceId(ids.first).execute();
print(
"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
Widget build(BuildContext context) {
var isDemo = widget.prefs.getBool("isDemo");
@ -218,7 +245,7 @@ class _MyHomePageState extends State<MyHomePage> {
if (loggedIn) {
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,
),
);
refresh().then((_) {
refresh(fullRefresh: false).then((_) {
setState(() {});
});
},