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/material.dart';
|
||||||
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.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 '../models/user_notification.dart';
|
||||||
|
import '../services/notifications_manager.dart';
|
||||||
import '../utils/dateutils.dart';
|
import '../utils/dateutils.dart';
|
||||||
|
import '../utils/snackbar_builder.dart';
|
||||||
|
|
||||||
class NotificationControl extends StatelessWidget {
|
class NotificationControl extends StatelessWidget {
|
||||||
final UserNotification notification;
|
final UserNotification notification;
|
||||||
|
@ -14,6 +17,7 @@ class NotificationControl extends StatelessWidget {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final manager = context.watch<NotificationsManager>();
|
||||||
return ListTile(
|
return ListTile(
|
||||||
tileColor: notification.seen ? null : Colors.black12,
|
tileColor: notification.seen ? null : Colors.black12,
|
||||||
leading: Text(notification.fromName),
|
leading: Text(notification.fromName),
|
||||||
|
@ -22,7 +26,15 @@ class NotificationControl extends StatelessWidget {
|
||||||
Text(ElapsedDateUtils.epochSecondsToString(notification.timestamp)),
|
Text(ElapsedDateUtils.epochSecondsToString(notification.timestamp)),
|
||||||
trailing: notification.seen
|
trailing: notification.seen
|
||||||
? null
|
? 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 {
|
FutureResult<bool, ExecError> clearNotifications(
|
||||||
final url = 'https://$serverName/api/v1/notifications/clear';
|
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);
|
final request = Uri.parse(url);
|
||||||
_logger.finest(() => 'Clearing unread notifications');
|
_logger.finest(() => 'Clearing unread notification for $notification');
|
||||||
final response = await _postUrl(request, {});
|
final response = await _postUrl(request, {});
|
||||||
return response.mapValue((value) => true);
|
return response.mapValue((value) => true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,4 +29,9 @@ class UserNotification {
|
||||||
required this.content,
|
required this.content,
|
||||||
required this.link,
|
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:flutter/material.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import '../controls/app_bottom_nav_bar.dart';
|
import '../controls/app_bottom_nav_bar.dart';
|
||||||
|
@ -8,10 +9,13 @@ import '../services/notifications_manager.dart';
|
||||||
import '../utils/snackbar_builder.dart';
|
import '../utils/snackbar_builder.dart';
|
||||||
|
|
||||||
class NotificationsScreen extends StatelessWidget {
|
class NotificationsScreen extends StatelessWidget {
|
||||||
|
static final _logger = Logger('$NotificationsScreen');
|
||||||
|
|
||||||
const NotificationsScreen({super.key});
|
const NotificationsScreen({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
_logger.finest('Building');
|
||||||
final manager = context.watch<NotificationsManager>();
|
final manager = context.watch<NotificationsManager>();
|
||||||
final notifications = manager.notifications;
|
final notifications = manager.notifications;
|
||||||
late final String title;
|
late final String title;
|
||||||
|
|
|
@ -42,7 +42,7 @@ class NotificationsManager extends ChangeNotifier {
|
||||||
return result.errorCast();
|
return result.errorCast();
|
||||||
}
|
}
|
||||||
|
|
||||||
_notifications.clear();
|
//_notifications.clear();
|
||||||
for (final n in result.value) {
|
for (final n in result.value) {
|
||||||
_notifications[n.id] = n;
|
_notifications[n.id] = n;
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ class NotificationsManager extends ChangeNotifier {
|
||||||
return Result.ok(notifications);
|
return Result.ok(notifications);
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureResult<UserNotification, ExecError> markSeen(String id) async {
|
FutureResult<bool, ExecError> markSeen(UserNotification notification) async {
|
||||||
final auth = getIt<AuthService>();
|
final auth = getIt<AuthService>();
|
||||||
final clientResult = auth.currentClient;
|
final clientResult = auth.currentClient;
|
||||||
if (clientResult.isFailure) {
|
if (clientResult.isFailure) {
|
||||||
|
@ -60,17 +60,13 @@ class NotificationsManager extends ChangeNotifier {
|
||||||
}
|
}
|
||||||
|
|
||||||
final client = clientResult.value;
|
final client = clientResult.value;
|
||||||
final result = await client.getNotifications();
|
final result = await client.clearNotification(notification);
|
||||||
if (result.isFailure) {
|
if (result.isSuccess) {
|
||||||
return result.errorCast();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (final n in result.value) {
|
updateNotifications();
|
||||||
_notifications[n.id] = n;
|
return result;
|
||||||
}
|
|
||||||
|
|
||||||
notifyListeners();
|
|
||||||
return Result.ok(notifications.first);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FutureResult<List<UserNotification>, ExecError> markAllAsRead() async {
|
FutureResult<List<UserNotification>, ExecError> markAllAsRead() async {
|
||||||
|
@ -82,11 +78,13 @@ class NotificationsManager extends ChangeNotifier {
|
||||||
}
|
}
|
||||||
|
|
||||||
final client = clientResult.value;
|
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) {
|
if (result.isFailure) {
|
||||||
return result.errorCast();
|
return result.errorCast();
|
||||||
}
|
}
|
||||||
|
notifyListeners();
|
||||||
return updateNotifications();
|
return updateNotifications();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue