mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Backend list notifications now supports pagination. Frontend
notification list is inifinite and lazy
This commit is contained in:
@@ -1,71 +1,77 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../utils/friendly_date_formatter.dart';
|
||||
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
||||
|
||||
import '../model/notification.dart' as oott_model;
|
||||
import '../utils/friendly_date_formatter.dart';
|
||||
import '../utils/oott_api.dart';
|
||||
|
||||
class NotificationList extends StatefulWidget {
|
||||
const NotificationList({super.key});
|
||||
|
||||
@override
|
||||
_NotificationListState createState() => _NotificationListState();
|
||||
}
|
||||
|
||||
class _NotificationListState extends State<NotificationList> {
|
||||
bool _isLoading = true;
|
||||
List<oott_model.Notification>? _events;
|
||||
final _pagingController = PagingController<int, oott_model.Notification>(
|
||||
getNextPageKey: (state) => state.lastPageIsEmpty
|
||||
? null
|
||||
: (state.items == null ? 0 : state.items?.length),
|
||||
fetchPage: (pageKey) => BackendAPI.instance.listNotifications(pageKey),
|
||||
);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_getData();
|
||||
}
|
||||
|
||||
void _getData() async {
|
||||
_events = await BackendAPI.instance.listNotifications();
|
||||
_isLoading = false;
|
||||
setState(() {});
|
||||
void dispose() {
|
||||
_pagingController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Notifications')),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: ListView.separated(
|
||||
itemCount: _events!.length,
|
||||
itemBuilder: (context, index) {
|
||||
return Dismissible(
|
||||
key: UniqueKey(),
|
||||
onDismissed: (direction) {
|
||||
setState(() {
|
||||
_events!.removeAt(index);
|
||||
});
|
||||
body: PagingListener(
|
||||
controller: _pagingController,
|
||||
builder: (context, state, fetchNextPage) =>
|
||||
PagedListView<int, oott_model.Notification>(
|
||||
state: state,
|
||||
fetchNextPage: fetchNextPage,
|
||||
builderDelegate: PagedChildBuilderDelegate(
|
||||
itemBuilder: (context, item, index) {
|
||||
return Dismissible(
|
||||
key: UniqueKey(),
|
||||
onDismissed: (direction) {
|
||||
setState(() {
|
||||
// _events!.removeAt(index);
|
||||
});
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Event marked as read'),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
showCloseIcon: true,
|
||||
),
|
||||
);
|
||||
},
|
||||
background: Container(
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
alignment: Alignment.center,
|
||||
child: Icon(Icons.done),
|
||||
),
|
||||
child: ListTile(
|
||||
leading: Icon(_events![index].notificationType.icon),
|
||||
title: Text(
|
||||
'${FriendlyDateFormatter().format(_events![index].createdOn)} - ${_events![index].title}',
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Event marked as read'),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
showCloseIcon: true,
|
||||
),
|
||||
);
|
||||
},
|
||||
background: Container(
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
alignment: Alignment.center,
|
||||
child: Icon(Icons.done),
|
||||
),
|
||||
subtitle: Text(_events![index].body),
|
||||
trailing: Icon(Icons.more_vert),
|
||||
onTap: () {},
|
||||
),
|
||||
);
|
||||
},
|
||||
separatorBuilder: (context, index) => Divider(),
|
||||
child: ListTile(
|
||||
leading: Icon(item.notificationType.icon),
|
||||
title: Text(
|
||||
'${FriendlyDateFormatter().format(item.createdOn)} - ${item.title}',
|
||||
),
|
||||
subtitle: Text(item.body),
|
||||
trailing: Icon(Icons.more_vert),
|
||||
onTap: () {},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:encrypter/encrypter/xor.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../utils/oott_api.dart';
|
||||
import '../utils/pref_utils.dart';
|
||||
import '../utils/ui_snackbars.dart';
|
||||
|
||||
class Settings extends StatefulWidget {
|
||||
const Settings({super.key});
|
||||
|
||||
@override
|
||||
_SettingsState createState() => _SettingsState();
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import '../model/notification.dart';
|
||||
|
||||
class BackendAPI {
|
||||
static final BackendAPI _instance = BackendAPI._internal();
|
||||
static const _pageSize = 5;
|
||||
|
||||
static BackendAPI get instance => _instance;
|
||||
|
||||
@@ -65,11 +66,14 @@ class BackendAPI {
|
||||
late String _apiKey;
|
||||
late Dio _dio;
|
||||
|
||||
Future<List<Notification>> listNotifications() async {
|
||||
Future<List<Notification>> listNotifications(int offset) async {
|
||||
print('About to call /notifications');
|
||||
|
||||
Response response;
|
||||
response = await _dio.get('/notifications');
|
||||
response = await _dio.get(
|
||||
'/notifications',
|
||||
queryParameters: {'page_offset': offset, 'page_limit': _pageSize},
|
||||
);
|
||||
|
||||
print('Received: ' + response.data.toString());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user