2025-04-13 18:02:20 +02: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/components/layout/scaffold.dart';
|
|
|
|
import 'package:simplecloudnotifier/models/client.dart';
|
|
|
|
import 'package:simplecloudnotifier/state/application_log.dart';
|
|
|
|
import 'package:simplecloudnotifier/state/app_auth.dart';
|
|
|
|
import 'package:simplecloudnotifier/pages/client_list/client_list_item.dart';
|
|
|
|
|
|
|
|
class ClientListPage extends StatefulWidget {
|
|
|
|
const ClientListPage({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ClientListPage> createState() => _ClientListPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ClientListPageState extends State<ClientListPage> {
|
|
|
|
final PagingController<int, Client> _pagingController = PagingController.fromValue(PagingState(nextPageKey: null, itemList: [], error: null), firstPageKey: 0);
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
_pagingController.addPageRequestListener(_fetchPage);
|
|
|
|
|
|
|
|
_pagingController.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
ApplicationLog.debug('ClientListPage::dispose');
|
|
|
|
_pagingController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _fetchPage(int pageKey) async {
|
|
|
|
final acc = Provider.of<AppAuth>(context, listen: false);
|
|
|
|
|
|
|
|
ApplicationLog.debug('Start ClientListPage::_pagingController::_fetchPage [ ${pageKey} ]');
|
|
|
|
|
|
|
|
if (!acc.isAuth()) {
|
|
|
|
_pagingController.error = 'Not logged in';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
final items = (await APIClient.getClientList(acc)).toList();
|
|
|
|
|
|
|
|
items.sort((a, b) => -1 * a.timestampCreated.compareTo(b.timestampCreated));
|
|
|
|
|
|
|
|
_pagingController.value = PagingState(nextPageKey: null, itemList: items, error: null);
|
|
|
|
} catch (exc, trace) {
|
|
|
|
_pagingController.error = exc.toString();
|
|
|
|
ApplicationLog.error('Failed to list clients: ' + exc.toString(), trace: trace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return SCNScaffold(
|
2025-04-13 19:47:18 +02:00
|
|
|
title: "Clients",
|
2025-04-13 18:02:20 +02:00
|
|
|
showSearch: false,
|
|
|
|
showShare: false,
|
|
|
|
child: Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
|
|
|
|
child: RefreshIndicator(
|
|
|
|
onRefresh: () => Future.sync(
|
|
|
|
() => _pagingController.refresh(),
|
|
|
|
),
|
|
|
|
child: PagedListView<int, Client>(
|
|
|
|
pagingController: _pagingController,
|
|
|
|
builderDelegate: PagedChildBuilderDelegate<Client>(
|
|
|
|
itemBuilder: (context, item, index) => ClientListItem(item: item),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|