From 946abb60a0dd09e7051bbdc79049c4c0face3f72 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 11 Apr 2023 21:25:39 +1000 Subject: [PATCH] Update notifications periodically (#311) - Call periodically from API class --- lib/api.dart | 53 ++++++++++++++++++++++++++++++++---------- lib/widget/drawer.dart | 3 +++ lib/widget/home.dart | 32 ------------------------- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/lib/api.dart b/lib/api.dart index 68a4a4e7..fb883fa1 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -5,26 +5,25 @@ import "dart:io"; import "package:flutter/foundation.dart"; import "package:http/http.dart" as http; import "package:intl/intl.dart"; -import "package:inventree/app_colors.dart"; -import "package:inventree/inventree/status_codes.dart"; -import "package:inventree/preferences.dart"; - import "package:open_filex/open_filex.dart"; import "package:cached_network_image/cached_network_image.dart"; import "package:flutter/material.dart"; import "package:font_awesome_flutter/font_awesome_flutter.dart"; import "package:flutter_cache_manager/flutter_cache_manager.dart"; - -import "package:inventree/widget/dialogs.dart"; -import "package:inventree/l10.dart"; -import "package:inventree/helpers.dart"; -import "package:inventree/inventree/sentry.dart"; -import "package:inventree/inventree/model.dart"; -import "package:inventree/user_profile.dart"; -import "package:inventree/widget/snacks.dart"; import "package:path_provider/path_provider.dart"; import "package:inventree/api_form.dart"; +import "package:inventree/app_colors.dart"; +import "package:inventree/preferences.dart"; +import "package:inventree/l10.dart"; +import "package:inventree/helpers.dart"; +import "package:inventree/inventree/model.dart"; +import "package:inventree/inventree/notification.dart"; +import "package:inventree/inventree/status_codes.dart"; +import "package:inventree/inventree/sentry.dart"; +import "package:inventree/user_profile.dart"; +import "package:inventree/widget/dialogs.dart"; +import "package:inventree/widget/snacks.dart"; /* @@ -520,6 +519,16 @@ class InvenTreeAPI { } // Ok, probably pretty good... + + if (_notification_timer == null) { + debug("starting notification timer"); + _notification_timer = Timer.periodic( + Duration(seconds: 5), + (timer) { + _refreshNotifications(); + }); + } + return true; } @@ -1461,4 +1470,24 @@ class InvenTreeAPI { InvenTreeStatusCode get StockStatus => _get_status_class("stock/status/"); InvenTreeStatusCode get PurchaseOrderStatus => _get_status_class("order/po/status/"); + int notification_counter = 0; + + Timer? _notification_timer; + + /* + * Update notification counter (called periodically) + */ + Future _refreshNotifications() async { + if (!isConnected()) { + return; + } + + if (!supportsNotifications) { + return; + } + + InvenTreeNotification().count(filters: {"read": "false"}).then((int n) { + notification_counter = n; + }); + } } diff --git a/lib/widget/drawer.dart b/lib/widget/drawer.dart index 4da5c504..6f438eca 100644 --- a/lib/widget/drawer.dart +++ b/lib/widget/drawer.dart @@ -129,9 +129,12 @@ class InvenTreeDrawer extends StatelessWidget { } if (InvenTreeAPI().supportsNotifications) { + int notification_count = InvenTreeAPI().notification_counter; + tiles.add( ListTile( leading: FaIcon(FontAwesomeIcons.bell), + trailing: notification_count > 0 ? Text(notification_count.toString()) : null, title: Text(L10().notifications), onTap: _notifications, ) diff --git a/lib/widget/home.dart b/lib/widget/home.dart index 39c74a86..c525c0df 100644 --- a/lib/widget/home.dart +++ b/lib/widget/home.dart @@ -42,16 +42,6 @@ class _InvenTreeHomePageState extends State with BaseWidgetPr // Initially load the profile and attempt server connection _loadProfile(); - _refreshNotifications(); - - // Refresh notifications every ~30 seconds - Timer.periodic( - Duration( - milliseconds: 30000, - ), (timer) { - _refreshNotifications(); - }); - InvenTreeAPI().registerCallback(() { if (mounted) { @@ -180,28 +170,6 @@ class _InvenTreeHomePageState extends State with BaseWidgetPr setState(() {}); } - /* - * Refresh the number of active notifications for this user - */ - Future _refreshNotifications() async { - - if (!InvenTreeAPI().isConnected()) { - return; - } - - // Ignore if the widget is no longer active - if (!mounted) { - return; - } - - // final notifications = await InvenTreeNotification().list(); - - setState(() { - // _notificationCounter = notifications.length; - }); - } - - Widget _listTile(BuildContext context, String label, IconData icon, {Function()? callback, String role = "", String permission = "", Widget? trailing}) { bool connected = InvenTreeAPI().isConnected();