This repository has been archived on 2024-06-08. You can view files and clone it, but cannot push or open issues or pull requests.
SolarAgent/lib/screens/notifications.dart

106 lines
3.3 KiB
Dart
Raw Normal View History

2024-02-08 07:19:37 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:solaragent/auth.dart';
2024-03-23 15:41:45 +00:00
import 'package:solaragent/models/notification.dart' as model;
import 'package:solaragent/models/pagination.dart';
import 'package:solaragent/widgets/notification.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> {
List<dynamic> notifications = List.empty();
@override
void initState() {
super.initState();
pullNotifications();
2024-02-08 07:19:37 +00:00
}
Future<void> pullNotifications() async {
if (await authClient.isAuthorized()) {
2024-03-23 15:41:45 +00:00
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) {
2024-03-23 15:41:45 +00:00
final result =
PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
setState(() {
2024-03-23 15:41:45 +00:00
notifications =
result.data?.map((x) => model.Notification.fromJson(x)).toList() ??
List.empty();
});
}
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 15:05:04 +00:00
),
// Content
notifications.isEmpty
? SliverToBoxAdapter(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
2024-03-23 15:41:45 +00:00
color: Colors.grey[50],
2024-03-23 15:05:04 +00:00
child: const ListTile(
leading: Icon(Icons.check),
title: Text('You\'re done!'),
subtitle: Text(
'There are no notifications unread for you.',
),
),
2024-03-23 15:05:04 +00:00
),
)
: SliverList.builder(
itemCount: notifications.length,
itemBuilder: (BuildContext context, int index) {
var element = notifications[index];
2024-03-23 15:41:45 +00:00
return NotificationItem(
index: index,
item: element,
onDismiss: () => setState(() {
notifications.removeAt(index);
}),
2024-03-23 15:05:04 +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 15:05:04 +00:00
),
],
2024-02-08 07:19:37 +00:00
),
),
);
}
}