✨ Notifications
This commit is contained in:
@ -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(),
|
||||
|
96
lib/screens/notifications.dart
Normal file
96
lib/screens/notifications.dart
Normal 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"]),
|
||||
));
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user