97 lines
3.2 KiB
Dart
97 lines
3.2 KiB
Dart
|
import 'dart:convert';
|
||
|
|
||
|
import 'package:flutter/material.dart';
|
||
|
import 'package:goatagent/auth.dart';
|
||
|
|
||
|
class NotificationScreen extends StatefulWidget {
|
||
|
const NotificationScreen({super.key});
|
||
|
|
||
|
@override
|
||
|
State<NotificationScreen> createState() => _NotificationScreenState();
|
||
|
}
|
||
|
|
||
|
class _NotificationScreenState extends State<NotificationScreen> {
|
||
|
final notificationEndpoint = Uri.parse(
|
||
|
'https://id.smartsheep.studio/api/notifications?skip=0&take=20');
|
||
|
|
||
|
List<dynamic> notifications = List.empty();
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
_pullNotifications();
|
||
|
}
|
||
|
|
||
|
Future<void> _pullNotifications() async {
|
||
|
if (await AuthGuard().isAuthorized()) {
|
||
|
await AuthGuard().pullProfiles();
|
||
|
var profiles = await AuthGuard().readProfiles();
|
||
|
setState(() {
|
||
|
notifications = profiles['notifications'];
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Future<void> _markAsRead(element) async {
|
||
|
if (AuthGuard().client != null) {
|
||
|
var id = element['id'];
|
||
|
var uri =
|
||
|
Uri.parse('https://id.smartsheep.studio/api/notifications/$id/read');
|
||
|
await AuthGuard().client!.put(uri);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
body: SafeArea(
|
||
|
child: Padding(
|
||
|
padding: const EdgeInsets.only(left: 20, right: 20, top: 30),
|
||
|
child: RefreshIndicator(
|
||
|
onRefresh: _pullNotifications,
|
||
|
child: CustomScrollView(
|
||
|
slivers: [
|
||
|
notifications.isEmpty
|
||
|
? const SliverToBoxAdapter(
|
||
|
child: Card(
|
||
|
child: Padding(
|
||
|
padding: EdgeInsets.all(10),
|
||
|
child: ListTile(
|
||
|
leading: Icon(Icons.check),
|
||
|
title: Text('You\'re done!'),
|
||
|
subtitle: Text(
|
||
|
'There are no notifications unread for you.'),
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
)
|
||
|
: SliverList.builder(
|
||
|
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:
|
||
|
Text('「$subject」 mark as read')));
|
||
|
});
|
||
|
},
|
||
|
child: ListTile(
|
||
|
title: Text(element["subject"]),
|
||
|
subtitle: Text(element["content"]),
|
||
|
));
|
||
|
},
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|