SimpleCloudNotifier/flutter/lib/pages/message_list/message_list.dart

95 lines
3.0 KiB
Dart
Raw Normal View History

2024-02-10 19:57:17 +01:00
import 'package:flutter/material.dart';
2024-02-18 17:36:58 +01:00
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
2024-05-21 23:20:34 +02:00
import 'package:simplecloudnotifier/models/channel.dart';
import 'package:simplecloudnotifier/models/message.dart';
import 'package:simplecloudnotifier/pages/message_view/message_view.dart';
2024-05-26 00:20:25 +02:00
import 'package:simplecloudnotifier/state/application_log.dart';
2024-06-02 17:09:57 +02:00
import 'package:simplecloudnotifier/state/app_auth.dart';
2024-05-25 22:06:43 +02:00
import 'package:simplecloudnotifier/pages/message_list/message_list_item.dart';
2024-02-10 19:57:17 +01:00
2024-02-18 17:36:58 +01:00
class MessageListPage extends StatefulWidget {
const MessageListPage({super.key});
2024-06-04 08:20:28 +02:00
//TODO reload on switch to tab
//TODO reload on app to foreground
2024-02-18 17:36:58 +01:00
@override
State<MessageListPage> createState() => _MessageListPageState();
}
class _MessageListPageState extends State<MessageListPage> {
2024-05-21 23:20:34 +02:00
static const _pageSize = 128;
2024-02-18 17:36:58 +01:00
final PagingController<String, Message> _pagingController = PagingController(firstPageKey: '@start');
2024-05-21 23:20:34 +02:00
Map<String, Channel>? _channels = null;
2024-02-18 17:36:58 +01:00
@override
void initState() {
2024-06-08 20:01:23 +02:00
//TODO init with state from cache - also allow tho show cache on error
2024-02-18 17:36:58 +01:00
_pagingController.addPageRequestListener((pageKey) {
_fetchPage(pageKey);
});
super.initState();
}
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
Future<void> _fetchPage(String thisPageToken) async {
2024-06-02 17:09:57 +02:00
final acc = Provider.of<AppAuth>(context, listen: false);
2024-02-18 17:36:58 +01:00
2024-06-02 17:09:57 +02:00
if (!acc.isAuth()) {
2024-02-18 17:36:58 +01:00
_pagingController.error = 'Not logged in';
return;
}
try {
2024-05-21 23:20:34 +02:00
if (_channels == null) {
2024-06-02 17:09:57 +02:00
final channels = await APIClient.getChannelList(acc, ChannelSelector.allAny);
_channels = <String, Channel>{for (var v in channels) v.channel.channelID: v.channel};
2024-05-21 23:20:34 +02:00
}
2024-06-02 17:09:57 +02:00
final (npt, newItems) = await APIClient.getMessageList(acc, thisPageToken, pageSize: _pageSize);
2024-02-18 17:36:58 +01:00
if (npt == '@end') {
_pagingController.appendLastPage(newItems);
} else {
_pagingController.appendPage(newItems, npt);
}
2024-05-26 00:20:25 +02:00
} catch (exc, trace) {
_pagingController.error = exc.toString();
ApplicationLog.error('Failed to list messages: ' + exc.toString(), trace: trace);
2024-02-18 17:36:58 +01:00
}
}
2024-02-10 19:57:17 +01:00
@override
Widget build(BuildContext context) {
2024-05-21 23:20:34 +02:00
return Padding(
padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
2024-05-26 19:24:19 +02:00
child: RefreshIndicator(
onRefresh: () => Future.sync(
() => _pagingController.refresh(),
),
child: PagedListView<String, Message>(
pagingController: _pagingController,
builderDelegate: PagedChildBuilderDelegate<Message>(
itemBuilder: (context, item, index) => MessageListItem(
message: item,
allChannels: _channels ?? {},
onPressed: () {
Navigator.push(context, MaterialPageRoute<MessageViewPage>(builder: (context) => MessageViewPage(message: item)));
},
),
2024-05-21 23:20:34 +02:00
),
2024-02-10 19:57:17 +01:00
),
),
);
}
}