2024-05-03 08:35:28 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-04-24 15:19:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-03 08:35:28 +00:00
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
2024-04-24 15:19:26 +00:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/providers/notify.dart';
|
|
|
|
import 'package:solian/utils/service_url.dart';
|
2024-05-03 05:39:52 +00:00
|
|
|
import 'package:solian/widgets/scaffold.dart';
|
2024-04-24 15:19:26 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
import 'package:url_launcher/url_launcher_string.dart';
|
|
|
|
import 'package:solian/models/notification.dart' as model;
|
|
|
|
|
|
|
|
class NotificationScreen extends StatefulWidget {
|
|
|
|
const NotificationScreen({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<NotificationScreen> createState() => _NotificationScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _NotificationScreenState extends State<NotificationScreen> {
|
2024-05-03 08:35:28 +00:00
|
|
|
bool _isSubmitting = false;
|
|
|
|
|
|
|
|
Future<void> markAllRead() async {
|
|
|
|
setState(() => _isSubmitting = true);
|
|
|
|
|
2024-04-24 15:19:26 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
2024-05-03 08:35:28 +00:00
|
|
|
if (!await auth.isAuthorized()) {
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final nty = context.read<NotifyProvider>();
|
|
|
|
List<int> markList = List.empty(growable: true);
|
|
|
|
for (final element in nty.notifications) {
|
|
|
|
if (element.isRealtime) continue;
|
|
|
|
markList.add(element.id);
|
|
|
|
}
|
|
|
|
|
2024-05-05 15:01:08 +00:00
|
|
|
nty.clearRealtime();
|
|
|
|
|
|
|
|
if(markList.isNotEmpty) {
|
|
|
|
var uri = getRequestUri('passport', '/api/notifications/batch/read');
|
|
|
|
await auth.client!.put(
|
|
|
|
uri,
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
body: jsonEncode({'messages': markList}),
|
|
|
|
);
|
|
|
|
}
|
2024-05-03 08:35:28 +00:00
|
|
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.notifyMarkAllReadDone),
|
|
|
|
));
|
|
|
|
|
|
|
|
await nty.fetch(auth);
|
|
|
|
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
}
|
2024-04-24 15:19:26 +00:00
|
|
|
|
2024-05-03 08:35:28 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
Future.delayed(Duration.zero, () {
|
|
|
|
final nty = context.read<NotifyProvider>();
|
2024-04-29 12:22:06 +00:00
|
|
|
nty.allRead();
|
|
|
|
});
|
2024-05-03 08:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
final nty = context.watch<NotifyProvider>();
|
2024-04-29 12:22:06 +00:00
|
|
|
|
2024-05-03 05:39:52 +00:00
|
|
|
return IndentScaffold(
|
2024-04-24 15:19:26 +00:00
|
|
|
noSafeArea: true,
|
2024-04-25 13:33:53 +00:00
|
|
|
hideDrawer: true,
|
2024-04-24 15:19:26 +00:00
|
|
|
title: AppLocalizations.of(context)!.notification,
|
|
|
|
child: RefreshIndicator(
|
|
|
|
onRefresh: () => nty.fetch(auth),
|
|
|
|
child: CustomScrollView(
|
|
|
|
slivers: [
|
2024-05-03 08:35:28 +00:00
|
|
|
SliverToBoxAdapter(
|
|
|
|
child: _isSubmitting ? const LinearProgressIndicator().animate().scaleX() : Container(),
|
|
|
|
),
|
|
|
|
if (nty.notifications.isNotEmpty)
|
|
|
|
SliverToBoxAdapter(
|
|
|
|
child: ListTile(
|
|
|
|
tileColor: Theme.of(context).colorScheme.secondaryContainer,
|
|
|
|
leading: const Icon(Icons.checklist),
|
|
|
|
title: Text(AppLocalizations.of(context)!.notifyMarkAllRead),
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 28),
|
|
|
|
onTap: _isSubmitting ? null : markAllRead,
|
|
|
|
),
|
|
|
|
),
|
2024-04-24 15:19:26 +00:00
|
|
|
nty.notifications.isEmpty
|
|
|
|
? SliverToBoxAdapter(
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
|
|
color: Theme.of(context).colorScheme.surfaceVariant,
|
|
|
|
child: ListTile(
|
|
|
|
leading: const Icon(Icons.check),
|
|
|
|
title: Text(AppLocalizations.of(context)!.notifyDone),
|
2024-05-03 08:35:28 +00:00
|
|
|
subtitle: Text(AppLocalizations.of(context)!.notifyDoneCaption),
|
2024-04-24 15:19:26 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: SliverList.builder(
|
|
|
|
itemCount: nty.notifications.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
var element = nty.notifications[index];
|
|
|
|
return NotificationItem(
|
|
|
|
index: index,
|
|
|
|
item: element,
|
2024-04-29 12:22:06 +00:00
|
|
|
onDismiss: () => nty.clearAt(index),
|
2024-04-24 15:19:26 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
SliverToBoxAdapter(
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.only(top: 12),
|
|
|
|
child: Text(
|
|
|
|
AppLocalizations.of(context)!.notifyListHint,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NotificationItem extends StatelessWidget {
|
|
|
|
final int index;
|
|
|
|
final model.Notification item;
|
|
|
|
final void Function()? onDismiss;
|
|
|
|
|
2024-05-03 08:35:28 +00:00
|
|
|
const NotificationItem({super.key, required this.index, required this.item, this.onDismiss});
|
2024-04-24 15:19:26 +00:00
|
|
|
|
2024-05-03 05:39:52 +00:00
|
|
|
bool get hasLinks => item.links != null && item.links!.isNotEmpty;
|
2024-04-24 15:19:26 +00:00
|
|
|
|
|
|
|
void showLinks(BuildContext context) {
|
2024-05-03 05:39:52 +00:00
|
|
|
if (!hasLinks) return;
|
2024-04-24 15:19:26 +00:00
|
|
|
|
|
|
|
showModalBottomSheet<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Padding(
|
2024-05-03 08:35:28 +00:00
|
|
|
padding: const EdgeInsets.only(left: 16, right: 16, top: 34, bottom: 12),
|
2024-04-24 15:19:26 +00:00
|
|
|
child: Text(
|
2024-05-01 16:49:38 +00:00
|
|
|
'Links',
|
2024-04-24 15:19:26 +00:00
|
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: ListView.builder(
|
|
|
|
itemCount: item.links!.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
var element = item.links![index];
|
|
|
|
return ListTile(
|
|
|
|
title: Text(element.label),
|
|
|
|
onTap: () async {
|
|
|
|
await launchUrlString(element.url);
|
|
|
|
if (Navigator.canPop(context)) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-03 08:35:28 +00:00
|
|
|
Future<void> markAsRead(model.Notification element, BuildContext context) async {
|
2024-04-24 15:19:26 +00:00
|
|
|
if (element.isRealtime) return;
|
|
|
|
|
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) return;
|
|
|
|
|
|
|
|
var id = element.id;
|
|
|
|
var uri = getRequestUri('passport', '/api/notifications/$id/read');
|
|
|
|
await auth.client!.put(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Dismissible(
|
2024-05-03 08:35:28 +00:00
|
|
|
key: Key('n${item.id}'),
|
2024-04-24 15:19:26 +00:00
|
|
|
onDismissed: (direction) {
|
|
|
|
markAsRead(item, context).then((value) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2024-05-05 15:01:08 +00:00
|
|
|
SnackBar(content: Text('${item.subject} is marked as read')),
|
2024-04-24 15:19:26 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
if (onDismiss != null) {
|
|
|
|
onDismiss!();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
background: Container(
|
|
|
|
color: Colors.lightBlue,
|
|
|
|
),
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.only(left: 10),
|
|
|
|
child: ListTile(
|
|
|
|
title: Text(item.subject),
|
|
|
|
subtitle: Text(item.content),
|
2024-05-03 05:39:52 +00:00
|
|
|
trailing: hasLinks
|
2024-04-24 15:19:26 +00:00
|
|
|
? TextButton(
|
|
|
|
onPressed: () => showLinks(context),
|
|
|
|
style: TextButton.styleFrom(shape: const CircleBorder()),
|
|
|
|
child: const Icon(Icons.more_vert),
|
|
|
|
)
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|