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

73 lines
1.8 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-02-10 19:57:17 +01:00
2024-02-18 17:36:58 +01:00
import '../../models/message.dart';
import '../../state/user_account.dart';
import '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});
@override
State<MessageListPage> createState() => _MessageListPageState();
}
class _MessageListPageState extends State<MessageListPage> {
static const _pageSize = 20; //TODO
final PagingController<String, Message> _pagingController = PagingController(firstPageKey: '@start');
@override
void initState() {
_pagingController.addPageRequestListener((pageKey) {
_fetchPage(pageKey);
});
super.initState();
}
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
Future<void> _fetchPage(String thisPageToken) async {
final acc = Provider.of<UserAccount>(context, listen: false);
if (acc.auth == null) {
_pagingController.error = 'Not logged in';
return;
}
try {
final [npt, newItems] = await APIClient.getMessageList(acc.auth!, thisPageToken, _pageSize);
if (npt == '@end') {
_pagingController.appendLastPage(newItems);
} else {
_pagingController.appendPage(newItems, npt);
}
} catch (error) {
_pagingController.error = error;
}
}
2024-02-10 19:57:17 +01:00
@override
Widget build(BuildContext context) {
2024-02-18 17:36:58 +01:00
return PagedListView<String, Message>(
pagingController: _pagingController,
builderDelegate: PagedChildBuilderDelegate<Message>(
itemBuilder: (context, item, index) => MessageListItem(
message: item,
2024-02-10 19:57:17 +01:00
),
),
);
}
2024-02-18 17:36:58 +01:00
void _createChannel() {
//TODO
}
2024-02-10 19:57:17 +01:00
}