Notifications

This commit is contained in:
2024-02-08 15:19:37 +08:00
parent 7ed4045a8c
commit b5f77c2736
9 changed files with 243 additions and 28 deletions

View File

@ -30,14 +30,17 @@ class _AccountScreenState extends State<AccountScreen> {
padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 20),
child: Column(
children: [
NameCard(
onLogin: () async {
await AuthGuard().login(context);
var authorized = await AuthGuard().isAuthorized();
setState(() {
isAuthorized = authorized;
});
},
Padding(
padding: const EdgeInsets.only(top: 20),
child: NameCard(
onLogin: () async {
await AuthGuard().login(context);
var authorized = await AuthGuard().isAuthorized();
setState(() {
isAuthorized = authorized;
});
},
),
),
FutureBuilder(
future: AuthGuard().isAuthorized(),

View File

@ -0,0 +1,96 @@
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"]),
));
},
),
],
),
),
),
),
);
}
}