Cache messages, use cache if exists, load in background

This commit is contained in:
2024-06-15 15:56:50 +02:00
parent 9c366399df
commit 35ab9a26c0
16 changed files with 556 additions and 42 deletions

View File

@@ -8,21 +8,26 @@ import 'package:simplecloudnotifier/state/app_auth.dart';
import 'package:simplecloudnotifier/pages/channel_list/channel_list_item.dart';
class ChannelRootPage extends StatefulWidget {
const ChannelRootPage({super.key});
const ChannelRootPage({super.key, required this.isVisiblePage});
final bool isVisiblePage;
@override
State<ChannelRootPage> createState() => _ChannelRootPageState();
}
class _ChannelRootPageState extends State<ChannelRootPage> {
final PagingController<int, Channel> _pagingController = PagingController(firstPageKey: 0);
final PagingController<int, Channel> _pagingController = PagingController.fromValue(PagingState(nextPageKey: null, itemList: [], error: null), firstPageKey: 0);
bool _isInitialized = false;
@override
void initState() {
_pagingController.addPageRequestListener((pageKey) {
_fetchPage(pageKey);
});
super.initState();
_pagingController.addPageRequestListener(_fetchPage);
if (widget.isVisiblePage && !_isInitialized) realInitState();
}
@override
@@ -31,9 +36,29 @@ class _ChannelRootPageState extends State<ChannelRootPage> {
super.dispose();
}
@override
void didUpdateWidget(ChannelRootPage oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.isVisiblePage != widget.isVisiblePage && widget.isVisiblePage) {
if (!_isInitialized) {
realInitState();
} else {
//TODO background refresh
}
}
}
void realInitState() {
_pagingController.refresh();
_isInitialized = true;
}
Future<void> _fetchPage(int pageKey) async {
final acc = Provider.of<AppAuth>(context, listen: false);
ApplicationLog.debug('Start ChannelList::_pagingController::_fetchPage [ ${pageKey} ]');
if (!acc.isAuth()) {
_pagingController.error = 'Not logged in';
return;