From aa17a5d52a8bd95a49d970a867b3de17782059ce Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 16 Oct 2024 00:53:29 +0800 Subject: [PATCH] :bug: Bug fixes on notifications --- lib/providers/notifications.dart | 3 ++ lib/screens/account/notification.dart | 75 +++++++-------------------- 2 files changed, 21 insertions(+), 57 deletions(-) diff --git a/lib/providers/notifications.dart b/lib/providers/notifications.dart index 2a7b6c9..1664b62 100644 --- a/lib/providers/notifications.dart +++ b/lib/providers/notifications.dart @@ -53,6 +53,7 @@ class NotificationProvider extends GetxController { List markList = List.empty(growable: true); for (final element in nty.notifications) { if (element.id <= 0) continue; + if (element.readAt != null) continue; markList.add(element.id); } @@ -75,6 +76,8 @@ class NotificationProvider extends GetxController { if (element.id <= 0) { nty.notifications.removeAt(index); return; + } else if (element.readAt != null) { + return; } isBusy.value = true; diff --git a/lib/screens/account/notification.dart b/lib/screens/account/notification.dart index f8514a3..b765abd 100644 --- a/lib/screens/account/notification.dart +++ b/lib/screens/account/notification.dart @@ -1,8 +1,6 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:solian/providers/notifications.dart'; -import 'package:solian/providers/auth.dart'; -import 'package:solian/models/notification.dart' as notify; import 'package:solian/widgets/loading_indicator.dart'; import 'package:uuid/uuid.dart'; @@ -14,54 +12,6 @@ class NotificationScreen extends StatefulWidget { } class _NotificationScreenState extends State { - bool _isBusy = false; - - Future _markAllRead() async { - final AuthProvider auth = Get.find(); - if (auth.isAuthorized.isFalse) return; - - setState(() => _isBusy = true); - - final NotificationProvider nty = Get.find(); - - List markList = List.empty(growable: true); - for (final element in nty.notifications) { - if (element.id <= 0) continue; - markList.add(element.id); - } - - if (markList.isNotEmpty) { - final client = await auth.configureClient('auth'); - await client.put('/notifications/read', {'messages': markList}); - } - - nty.notifications.clear(); - - setState(() => _isBusy = false); - } - - Future _markOneRead(notify.Notification element, int index) async { - final AuthProvider auth = Get.find(); - if (auth.isAuthorized.isFalse) return; - - final NotificationProvider nty = Get.find(); - - if (element.id <= 0) { - nty.notifications.removeAt(index); - return; - } - - setState(() => _isBusy = true); - - final client = await auth.configureClient('auth'); - - await client.put('/notifications/read/${element.id}', {}); - - nty.notifications.removeAt(index); - - setState(() => _isBusy = false); - } - @override Widget build(BuildContext context) { final NotificationProvider nty = Get.find(); @@ -81,11 +31,9 @@ class _NotificationScreenState extends State { onRefresh: () => nty.fetchNotification(), child: CustomScrollView( slivers: [ - Obx( - () => SliverToBoxAdapter( - child: LoadingIndicator( - isActive: _isBusy || nty.isBusy.value, - ), + SliverToBoxAdapter( + child: LoadingIndicator( + isActive: nty.isBusy.value, ), ), if (nty.notifications @@ -115,7 +63,9 @@ class _NotificationScreenState extends State { child: ListTile( leading: const Icon(Icons.checklist), title: Text('notifyAllRead'.tr), - onTap: _isBusy ? null : () => _markAllRead(), + onTap: nty.isBusy.value + ? null + : () => nty.markAllRead(), ), ), ), @@ -125,6 +75,9 @@ class _NotificationScreenState extends State { var element = nty.notifications[index]; return ClipRect( child: Dismissible( + direction: element.readAt == null + ? DismissDirection.vertical + : DismissDirection.none, key: Key(const Uuid().v4()), background: Container( color: Colors.lightBlue, @@ -134,6 +87,14 @@ class _NotificationScreenState extends State { child: const Icon(Icons.check, color: Colors.white), ), + secondaryBackground: Container( + color: Colors.lightBlue, + padding: + const EdgeInsets.symmetric(horizontal: 20), + alignment: Alignment.centerRight, + child: + const Icon(Icons.check, color: Colors.white), + ), child: ListTile( contentPadding: const EdgeInsets.symmetric( horizontal: 24, @@ -149,7 +110,7 @@ class _NotificationScreenState extends State { ], ), ), - onDismissed: (_) => _markOneRead(element, index), + onDismissed: (_) => nty.markOneRead(element, index), ), ); },