2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 05:26:47 +00:00

Update notifications periodically (#311)

- Call periodically from API class
This commit is contained in:
Oliver 2023-04-11 21:25:39 +10:00 committed by GitHub
parent 0156329fb6
commit 946abb60a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 44 deletions

View File

@ -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<void> _refreshNotifications() async {
if (!isConnected()) {
return;
}
if (!supportsNotifications) {
return;
}
InvenTreeNotification().count(filters: {"read": "false"}).then((int n) {
notification_counter = n;
});
}
}

View File

@ -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,
)

View File

@ -42,16 +42,6 @@ class _InvenTreeHomePageState extends State<InvenTreeHomePage> 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<InvenTreeHomePage> with BaseWidgetPr
setState(() {});
}
/*
* Refresh the number of active notifications for this user
*/
Future<void> _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();