Batch mark notify as read

This commit is contained in:
LittleSheep 2024-05-03 16:35:28 +08:00
parent 740c704fb8
commit 083975bcd9
3 changed files with 69 additions and 13 deletions

View File

@ -42,6 +42,8 @@
"notifyDone": "You're done!", "notifyDone": "You're done!",
"notifyDoneCaption": "There are no notifications unread for you.", "notifyDoneCaption": "There are no notifications unread for you.",
"notifyListHint": "Pull to refresh, swipe to dismiss", "notifyListHint": "Pull to refresh, swipe to dismiss",
"notifyMarkAllRead": "Mark all as read",
"notifyMarkAllReadDone": "Marked all notifications as read",
"friend": "Friend", "friend": "Friend",
"friendPending": "Pending", "friendPending": "Pending",
"friendActive": "Active", "friendActive": "Active",

View File

@ -42,6 +42,8 @@
"notifyDone": "所有通知已读!", "notifyDone": "所有通知已读!",
"notifyDoneCaption": "这里没有什么东西可以给你看的了~", "notifyDoneCaption": "这里没有什么东西可以给你看的了~",
"notifyListHint": "下拉以刷新,左滑来已读", "notifyListHint": "下拉以刷新,左滑来已读",
"notifyMarkAllRead": "将所有标记为已读",
"notifyMarkAllReadDone": "已将所有通知标记为已读",
"friend": "好友", "friend": "好友",
"friendPending": "请求中", "friendPending": "请求中",
"friendActive": "活跃的好友", "friendActive": "活跃的好友",

View File

@ -1,4 +1,7 @@
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:solian/providers/auth.dart'; import 'package:solian/providers/auth.dart';
import 'package:solian/providers/notify.dart'; import 'package:solian/providers/notify.dart';
@ -16,15 +19,55 @@ class NotificationScreen extends StatefulWidget {
} }
class _NotificationScreenState extends State<NotificationScreen> { class _NotificationScreenState extends State<NotificationScreen> {
bool _isSubmitting = false;
Future<void> markAllRead() async {
setState(() => _isSubmitting = true);
final auth = context.read<AuthProvider>();
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);
}
var uri = getRequestUri('passport', '/api/notifications/batch/read');
await auth.client!.put(
uri,
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'messages': markList}),
);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(AppLocalizations.of(context)!.notifyMarkAllReadDone),
));
await nty.fetch(auth);
setState(() => _isSubmitting = false);
}
@override
void initState() {
super.initState();
Future.delayed(Duration.zero, () {
final nty = context.read<NotifyProvider>();
nty.allRead();
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final auth = context.read<AuthProvider>(); final auth = context.read<AuthProvider>();
final nty = context.watch<NotifyProvider>(); final nty = context.watch<NotifyProvider>();
WidgetsBinding.instance.addPostFrameCallback((_) {
nty.allRead();
});
return IndentScaffold( return IndentScaffold(
noSafeArea: true, noSafeArea: true,
hideDrawer: true, hideDrawer: true,
@ -33,6 +76,19 @@ class _NotificationScreenState extends State<NotificationScreen> {
onRefresh: () => nty.fetch(auth), onRefresh: () => nty.fetch(auth),
child: CustomScrollView( child: CustomScrollView(
slivers: [ slivers: [
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,
),
),
nty.notifications.isEmpty nty.notifications.isEmpty
? SliverToBoxAdapter( ? SliverToBoxAdapter(
child: Container( child: Container(
@ -41,8 +97,7 @@ class _NotificationScreenState extends State<NotificationScreen> {
child: ListTile( child: ListTile(
leading: const Icon(Icons.check), leading: const Icon(Icons.check),
title: Text(AppLocalizations.of(context)!.notifyDone), title: Text(AppLocalizations.of(context)!.notifyDone),
subtitle: Text( subtitle: Text(AppLocalizations.of(context)!.notifyDoneCaption),
AppLocalizations.of(context)!.notifyDoneCaption),
), ),
), ),
) )
@ -79,8 +134,7 @@ class NotificationItem extends StatelessWidget {
final model.Notification item; final model.Notification item;
final void Function()? onDismiss; final void Function()? onDismiss;
const NotificationItem( const NotificationItem({super.key, required this.index, required this.item, this.onDismiss});
{super.key, required this.index, required this.item, this.onDismiss});
bool get hasLinks => item.links != null && item.links!.isNotEmpty; bool get hasLinks => item.links != null && item.links!.isNotEmpty;
@ -94,8 +148,7 @@ class NotificationItem extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Padding( Padding(
padding: const EdgeInsets.only( padding: const EdgeInsets.only(left: 16, right: 16, top: 34, bottom: 12),
left: 16, right: 16, top: 34, bottom: 12),
child: Text( child: Text(
'Links', 'Links',
style: Theme.of(context).textTheme.headlineSmall, style: Theme.of(context).textTheme.headlineSmall,
@ -124,8 +177,7 @@ class NotificationItem extends StatelessWidget {
); );
} }
Future<void> markAsRead( Future<void> markAsRead(model.Notification element, BuildContext context) async {
model.Notification element, BuildContext context) async {
if (element.isRealtime) return; if (element.isRealtime) return;
final auth = context.read<AuthProvider>(); final auth = context.read<AuthProvider>();
@ -139,7 +191,7 @@ class NotificationItem extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Dismissible( return Dismissible(
key: Key((DateTime.now().millisecondsSinceEpoch << 10).toString()), key: Key('n${item.id}'),
onDismissed: (direction) { onDismissed: (direction) {
markAsRead(item, context).then((value) { markAsRead(item, context).then((value) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(