Notification links

This commit is contained in:
2024-03-23 23:41:45 +08:00
parent a3b4706ca2
commit c19bb3730e
3 changed files with 191 additions and 52 deletions

View File

@ -1,9 +1,10 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:solaragent/auth.dart';
import 'package:solaragent/models/notification.dart' as model;
import 'package:solaragent/models/pagination.dart';
import 'package:solaragent/widgets/notification.dart';
class NotificationScreen extends StatefulWidget {
const NotificationScreen({super.key});
@ -13,9 +14,6 @@ class NotificationScreen extends StatefulWidget {
}
class _NotificationScreenState extends State<NotificationScreen> {
final notificationEndpoint =
Uri.parse('https://id.solsynth.dev/api/notifications?skip=0&take=25');
List<dynamic> notifications = List.empty();
@override
@ -26,23 +24,21 @@ class _NotificationScreenState extends State<NotificationScreen> {
Future<void> pullNotifications() async {
if (await authClient.isAuthorized()) {
var res = await authClient.client!.get(notificationEndpoint);
var uri =
Uri.parse('https://id.solsynth.dev/api/notifications?skip=0&take=25');
var res = await authClient.client!.get(uri);
if (res.statusCode == 200) {
final result =
PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
setState(() {
notifications = jsonDecode(utf8.decode(res.bodyBytes))["data"];
notifications =
result.data?.map((x) => model.Notification.fromJson(x)).toList() ??
List.empty();
});
}
}
}
Future<void> markAsRead(element) async {
if (authClient.client != null) {
var id = element['id'];
var uri = Uri.parse('https://id.solsynth.dev/api/notifications/$id/read');
await authClient.client!.put(uri);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -67,7 +63,7 @@ class _NotificationScreenState extends State<NotificationScreen> {
? SliverToBoxAdapter(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
color: Colors.grey[300],
color: Colors.grey[50],
child: const ListTile(
leading: Icon(Icons.check),
title: Text('You\'re done!'),
@ -81,42 +77,12 @@ class _NotificationScreenState extends State<NotificationScreen> {
itemCount: notifications.length,
itemBuilder: (BuildContext context, int index) {
var element = notifications[index];
return Dismissible(
key: Key('notification-$index'),
onDismissed: (direction) {
var subject = element["subject"];
markAsRead(element).then((value) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: RichText(
text: TextSpan(children: [
TextSpan(
text: subject,
style: const TextStyle(
fontWeight: FontWeight.bold),
),
const TextSpan(
text: " is marked as read",
)
]),
),
),
);
});
setState(() {
notifications.removeAt(index);
});
},
background: Container(
color: Colors.green,
),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: ListTile(
title: Text(element["subject"]),
subtitle: Text(element["content"]),
),
),
return NotificationItem(
index: index,
item: element,
onDismiss: () => setState(() {
notifications.removeAt(index);
}),
);
},
),