2024-02-08 07:19:37 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-03-23 12:56:32 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2024-02-08 07:19:37 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-03-23 12:56:32 +00:00
|
|
|
import 'package:flutter/widgets.dart';
|
2024-03-17 12:22:46 +00:00
|
|
|
import 'package:solaragent/auth.dart';
|
2024-02-08 07:19:37 +00:00
|
|
|
|
|
|
|
class NotificationScreen extends StatefulWidget {
|
|
|
|
const NotificationScreen({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<NotificationScreen> createState() => _NotificationScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _NotificationScreenState extends State<NotificationScreen> {
|
2024-03-23 12:56:32 +00:00
|
|
|
final notificationEndpoint =
|
|
|
|
Uri.parse('https://id.solsynth.dev/api/notifications?skip=0&take=25');
|
2024-02-08 07:19:37 +00:00
|
|
|
|
|
|
|
List<dynamic> notifications = List.empty();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-03-23 12:56:32 +00:00
|
|
|
pullNotifications();
|
2024-02-08 07:19:37 +00:00
|
|
|
}
|
|
|
|
|
2024-03-23 12:56:32 +00:00
|
|
|
Future<void> pullNotifications() async {
|
2024-03-17 12:22:46 +00:00
|
|
|
if (await authClient.isAuthorized()) {
|
2024-03-23 12:56:32 +00:00
|
|
|
var res = await authClient.client!.get(notificationEndpoint);
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
setState(() {
|
|
|
|
notifications = jsonDecode(utf8.decode(res.bodyBytes))["data"];
|
|
|
|
});
|
|
|
|
}
|
2024-02-08 07:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 12:56:32 +00:00
|
|
|
Future<void> markAsRead(element) async {
|
2024-03-17 12:22:46 +00:00
|
|
|
if (authClient.client != null) {
|
2024-02-08 07:19:37 +00:00
|
|
|
var id = element['id'];
|
2024-03-23 12:56:32 +00:00
|
|
|
var uri = Uri.parse('https://id.solsynth.dev/api/notifications/$id/read');
|
2024-03-17 12:22:46 +00:00
|
|
|
await authClient.client!.put(uri);
|
2024-02-08 07:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2024-03-23 15:05:04 +00:00
|
|
|
body: RefreshIndicator(
|
|
|
|
onRefresh: pullNotifications,
|
|
|
|
child: CustomScrollView(
|
|
|
|
slivers: [
|
|
|
|
// Title
|
|
|
|
SliverToBoxAdapter(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 10, right: 10, top: 20),
|
|
|
|
child: ListTile(
|
|
|
|
title: Text(
|
|
|
|
'Notifications',
|
|
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
2024-03-23 12:56:32 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-03-23 15:05:04 +00:00
|
|
|
),
|
|
|
|
// Content
|
|
|
|
notifications.isEmpty
|
|
|
|
? SliverToBoxAdapter(
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
|
|
color: Colors.grey[300],
|
|
|
|
child: const ListTile(
|
|
|
|
leading: Icon(Icons.check),
|
|
|
|
title: Text('You\'re done!'),
|
|
|
|
subtitle: Text(
|
|
|
|
'There are no notifications unread for you.',
|
2024-03-23 12:56:32 +00:00
|
|
|
),
|
|
|
|
),
|
2024-03-23 15:05:04 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
: 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: RichText(
|
|
|
|
text: TextSpan(children: [
|
|
|
|
TextSpan(
|
|
|
|
text: subject,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
const TextSpan(
|
|
|
|
text: " is marked as read",
|
|
|
|
)
|
|
|
|
]),
|
2024-03-23 12:56:32 +00:00
|
|
|
),
|
2024-03-23 15:05:04 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
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"]),
|
2024-02-08 07:19:37 +00:00
|
|
|
),
|
2024-03-23 15:05:04 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
2024-03-23 12:56:32 +00:00
|
|
|
),
|
2024-03-23 15:05:04 +00:00
|
|
|
// Tips
|
|
|
|
SliverToBoxAdapter(
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.only(top: 12),
|
|
|
|
child: Text(
|
|
|
|
"Pull to refresh, swipe to dismiss",
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: Theme.of(context).textTheme.bodySmall,
|
2024-03-23 12:56:32 +00:00
|
|
|
),
|
|
|
|
),
|
2024-03-23 15:05:04 +00:00
|
|
|
),
|
|
|
|
],
|
2024-02-08 07:19:37 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|