loris/lib/pages/notifications/notifications.dart

142 lines
3.5 KiB
Dart
Raw Normal View History

import 'dart:async';
2022-08-15 21:30:53 +00:00
import 'dart:convert';
2022-07-02 22:03:10 +00:00
import 'package:flutter/material.dart';
2022-08-22 11:32:40 +00:00
import 'package:localization/localization.dart';
import 'package:loris/business_logic/notifications/notifs.dart';
2022-08-15 21:30:53 +00:00
import 'package:loris/pages/notifications/single_notif.dart';
2022-08-28 18:55:23 +00:00
import 'package:loris/partials/loadingbox.dart';
2022-08-15 18:31:44 +00:00
import '../../business_logic/websocket.dart' as websocket;
2022-07-02 22:03:10 +00:00
2022-08-22 11:32:40 +00:00
final notifStream = StreamController<int>.broadcast();
2022-08-15 18:31:44 +00:00
class Notifications extends StatefulWidget {
const Notifications({Key? key}) : super(key: key);
@override
State<Notifications> createState() => _NotificationsState();
}
class _NotificationsState extends State<Notifications> {
List<Widget> notifs = [];
2022-08-22 11:32:40 +00:00
Map<String, String> maxIdData = {};
final ScrollController _controller = ScrollController();
Future<void> loadMore() async {
final data = await loadOldNotifications(maxIdData);
final models = data.models;
maxIdData = data.latest;
List<Widget> widgets = [];
for (int i = 0; i < models.length; i++) {
widgets.add(
SingleNotif(
model: models[i],
),
);
}
if (mounted) {
setState(() {
notifs.addAll(widgets);
});
cleanChildren();
}
}
2022-08-15 21:30:53 +00:00
2022-08-28 18:55:23 +00:00
void reload() {
if (mounted) {
setState(() {
_controller.animateTo(
0,
duration: const Duration(seconds: 1),
curve: Curves.easeInOut,
);
maxIdData = {};
notifs = [const LoadingBox()];
});
loadMore();
}
}
2022-08-15 18:31:44 +00:00
@override
2022-08-15 21:30:53 +00:00
void initState() {
2022-08-22 11:32:40 +00:00
super.initState();
2022-08-15 18:31:44 +00:00
for (int i = 0; i < websocket.map.length; i++) {
final keyI = websocket.map.keys.toList()[i];
2022-08-15 21:30:53 +00:00
websocket.map[keyI]!["home"]!.stream.listen((event) async {
Map<String, dynamic> json = jsonDecode(event);
if (json["event"] == "notification") {
SingleNotif notif = SingleNotif(
model: NotificationModel.fromJson(
jsonDecode(json["payload"]),
keyI,
),
2022-08-15 21:30:53 +00:00
);
2022-08-15 21:51:38 +00:00
if (mounted) {
setState(() {
notifStream.sink.add(1);
2022-08-15 21:51:38 +00:00
notifs.insert(0, notif);
});
}
2022-08-15 21:30:53 +00:00
}
});
2022-08-15 18:31:44 +00:00
}
2022-08-22 11:32:40 +00:00
loadMore();
}
void cleanChildren() {
setState(() {
notifs.removeWhere((element) {
return element.runtimeType != SingleNotif;
});
notifs.add(
TextButton.icon(
onPressed: () {
loadMore();
},
icon: const Icon(Icons.more_horiz),
label: Text(
"load-older-notifications".i18n(),
),
),
);
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
2022-08-15 21:30:53 +00:00
}
@override
Widget build(BuildContext context) {
2022-08-22 11:32:40 +00:00
return Column(
children: [
2022-08-28 18:55:23 +00:00
Container(
color: Theme.of(context).colorScheme.surface,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
onPressed: () {
reload();
},
icon: const Icon(Icons.refresh))
],
),
),
2022-08-22 11:32:40 +00:00
Expanded(
child: ListView.separated(
controller: _controller,
addSemanticIndexes: true,
itemBuilder: ((context, index) => notifs[index]),
separatorBuilder: (context, index) => const SizedBox(
height: 8,
),
itemCount: notifs.length),
),
],
);
2022-08-15 18:31:44 +00:00
}
2022-07-02 22:03:10 +00:00
}