kopia lustrzana https://gitlab.com/mysocialportal/relatica
Add first cut at working notification panel
rodzic
c0598ad58e
commit
2f8aa26dbc
|
@ -1,8 +1,11 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../models/user_notification.dart';
|
||||
import '../services/notifications_manager.dart';
|
||||
import '../utils/dateutils.dart';
|
||||
import '../utils/snackbar_builder.dart';
|
||||
|
||||
class NotificationControl extends StatelessWidget {
|
||||
final UserNotification notification;
|
||||
|
@ -14,6 +17,7 @@ class NotificationControl extends StatelessWidget {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final manager = context.watch<NotificationsManager>();
|
||||
return ListTile(
|
||||
tileColor: notification.seen ? null : Colors.black12,
|
||||
leading: Text(notification.fromName),
|
||||
|
@ -22,7 +26,15 @@ class NotificationControl extends StatelessWidget {
|
|||
Text(ElapsedDateUtils.epochSecondsToString(notification.timestamp)),
|
||||
trailing: notification.seen
|
||||
? null
|
||||
: IconButton(onPressed: () {}, icon: Icon(Icons.close_rounded)),
|
||||
: IconButton(
|
||||
onPressed: () async {
|
||||
final result = await manager.markSeen(notification);
|
||||
if (result.isFailure) {
|
||||
buildSnackbar(
|
||||
context, 'Error marking notification: ${result.error}');
|
||||
}
|
||||
},
|
||||
icon: Icon(Icons.close_rounded)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,10 +46,22 @@ class FriendicaClient {
|
|||
});
|
||||
}
|
||||
|
||||
FutureResult<bool, ExecError> clearNotifications() async {
|
||||
final url = 'https://$serverName/api/v1/notifications/clear';
|
||||
FutureResult<bool, ExecError> clearNotifications(
|
||||
List<UserNotification> notifications) async {
|
||||
var result = true;
|
||||
for (final n in notifications) {
|
||||
result &= (await clearNotification(n)).isSuccess;
|
||||
}
|
||||
|
||||
return Result.ok(result);
|
||||
}
|
||||
|
||||
FutureResult<bool, ExecError> clearNotification(
|
||||
UserNotification notification) async {
|
||||
final url =
|
||||
'https://$serverName/api/friendica/notification/seen?id=${notification.id}';
|
||||
final request = Uri.parse(url);
|
||||
_logger.finest(() => 'Clearing unread notifications');
|
||||
_logger.finest(() => 'Clearing unread notification for $notification');
|
||||
final response = await _postUrl(request, {});
|
||||
return response.mapValue((value) => true);
|
||||
}
|
||||
|
|
|
@ -29,4 +29,9 @@ class UserNotification {
|
|||
required this.content,
|
||||
required this.link,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'UserNotification{id: $id, seen: $seen, fromName: $fromName, content: $content}';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../controls/app_bottom_nav_bar.dart';
|
||||
|
@ -8,10 +9,13 @@ import '../services/notifications_manager.dart';
|
|||
import '../utils/snackbar_builder.dart';
|
||||
|
||||
class NotificationsScreen extends StatelessWidget {
|
||||
static final _logger = Logger('$NotificationsScreen');
|
||||
|
||||
const NotificationsScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_logger.finest('Building');
|
||||
final manager = context.watch<NotificationsManager>();
|
||||
final notifications = manager.notifications;
|
||||
late final String title;
|
||||
|
|
|
@ -42,7 +42,7 @@ class NotificationsManager extends ChangeNotifier {
|
|||
return result.errorCast();
|
||||
}
|
||||
|
||||
_notifications.clear();
|
||||
//_notifications.clear();
|
||||
for (final n in result.value) {
|
||||
_notifications[n.id] = n;
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ class NotificationsManager extends ChangeNotifier {
|
|||
return Result.ok(notifications);
|
||||
}
|
||||
|
||||
FutureResult<UserNotification, ExecError> markSeen(String id) async {
|
||||
FutureResult<bool, ExecError> markSeen(UserNotification notification) async {
|
||||
final auth = getIt<AuthService>();
|
||||
final clientResult = auth.currentClient;
|
||||
if (clientResult.isFailure) {
|
||||
|
@ -60,17 +60,13 @@ class NotificationsManager extends ChangeNotifier {
|
|||
}
|
||||
|
||||
final client = clientResult.value;
|
||||
final result = await client.getNotifications();
|
||||
if (result.isFailure) {
|
||||
return result.errorCast();
|
||||
final result = await client.clearNotification(notification);
|
||||
if (result.isSuccess) {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
for (final n in result.value) {
|
||||
_notifications[n.id] = n;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
return Result.ok(notifications.first);
|
||||
updateNotifications();
|
||||
return result;
|
||||
}
|
||||
|
||||
FutureResult<List<UserNotification>, ExecError> markAllAsRead() async {
|
||||
|
@ -82,11 +78,13 @@ class NotificationsManager extends ChangeNotifier {
|
|||
}
|
||||
|
||||
final client = clientResult.value;
|
||||
final result = await client.clearNotifications();
|
||||
final unreadNotifications =
|
||||
_notifications.values.where((n) => !n.seen).toList();
|
||||
final result = await client.clearNotifications(unreadNotifications);
|
||||
if (result.isFailure) {
|
||||
return result.errorCast();
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
return updateNotifications();
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue