2024-02-18 17:36:58 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:simplecloudnotifier/api/api_client.dart';
|
|
|
|
import 'package:simplecloudnotifier/models/channel.dart';
|
2024-05-26 00:20:25 +02:00
|
|
|
import 'package:simplecloudnotifier/state/application_log.dart';
|
2024-05-25 22:06:43 +02:00
|
|
|
import 'package:simplecloudnotifier/state/user_account.dart';
|
|
|
|
import 'package:simplecloudnotifier/pages/channel_list/channel_list_item.dart';
|
2024-02-18 17:36:58 +01:00
|
|
|
|
|
|
|
class ChannelRootPage extends StatefulWidget {
|
|
|
|
const ChannelRootPage({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ChannelRootPage> createState() => _ChannelRootPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChannelRootPageState extends State<ChannelRootPage> {
|
|
|
|
final PagingController<int, Channel> _pagingController = PagingController(firstPageKey: 0);
|
|
|
|
|
|
|
|
late UserAccount userAcc;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
_pagingController.addPageRequestListener((pageKey) {
|
|
|
|
_fetchPage(pageKey);
|
|
|
|
});
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_pagingController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _fetchPage(int pageKey) async {
|
|
|
|
final acc = Provider.of<UserAccount>(context, listen: false);
|
|
|
|
|
|
|
|
if (acc.auth == null) {
|
|
|
|
_pagingController.error = 'Not logged in';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-06-01 03:06:02 +02:00
|
|
|
final items = (await APIClient.getChannelList(acc.auth!, ChannelSelector.all)).map((p) => p.channel).toList();
|
2024-02-18 17:36:58 +01:00
|
|
|
|
2024-05-26 19:24:19 +02:00
|
|
|
items.sort((a, b) => -1 * (a.timestampLastSent ?? '').compareTo(b.timestampLastSent ?? ''));
|
|
|
|
|
2024-02-18 17:36:58 +01:00
|
|
|
_pagingController.appendLastPage(items);
|
2024-05-26 00:20:25 +02:00
|
|
|
} catch (exc, trace) {
|
|
|
|
_pagingController.error = exc.toString();
|
|
|
|
ApplicationLog.error('Failed to list channels: ' + exc.toString(), trace: trace);
|
2024-02-18 17:36:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-26 19:24:19 +02:00
|
|
|
return RefreshIndicator(
|
|
|
|
onRefresh: () => Future.sync(
|
|
|
|
() => _pagingController.refresh(),
|
|
|
|
),
|
|
|
|
child: PagedListView<int, Channel>(
|
|
|
|
pagingController: _pagingController,
|
|
|
|
builderDelegate: PagedChildBuilderDelegate<Channel>(
|
|
|
|
itemBuilder: (context, item, index) => ChannelListItem(
|
|
|
|
channel: item,
|
|
|
|
onPressed: () {/*TODO*/},
|
|
|
|
),
|
2024-02-18 17:36:58 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|