diff --git a/lib/api.dart b/lib/api.dart index 9c2674b2..ee8a6132 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -431,6 +431,7 @@ class InvenTreeAPI { // Return the received token _token = (data["token"] ?? "") as String; + print("Received token - $_token"); // Request user role information (async) diff --git a/lib/widget/home.dart b/lib/widget/home.dart index d5e7ebfb..f9c3c256 100644 --- a/lib/widget/home.dart +++ b/lib/widget/home.dart @@ -1,3 +1,5 @@ +import "dart:async"; + import "package:flutter/material.dart"; import "package:font_awesome_flutter/font_awesome_flutter.dart"; @@ -11,6 +13,9 @@ import "package:inventree/settings/login.dart"; import "package:inventree/settings/settings.dart"; import "package:inventree/user_profile.dart"; +import "package:inventree/inventree/model.dart"; +import "package:inventree/inventree/notification.dart"; + import "package:inventree/widget/category_display.dart"; import "package:inventree/widget/company_list.dart"; import "package:inventree/widget/drawer.dart"; @@ -33,18 +38,29 @@ class InvenTreeHomePage extends StatefulWidget { class _InvenTreeHomePageState extends State { _InvenTreeHomePageState() : super() { - // Load display settings _loadSettings(); // Initially load the profile and attempt server connection _loadProfile(); + _refreshNotifications(); + + // Refresh notifications every ~30 seconds + Timer.periodic( + Duration( + milliseconds: 30000, + ), (timer) { + _refreshNotifications(); + }); } // Index of bottom navigation bar int _tabIndex = 0; + // Number of outstanding notifications + int _notificationCounter = 0; + bool homeShowPo = false; bool homeShowSubscribed = false; bool homeShowManufacturers = false; @@ -160,6 +176,18 @@ class _InvenTreeHomePageState extends State { setState(() {}); } + /* + * Refresh the number of active notifications for this user + */ + Future _refreshNotifications() async { + + final notifications = await InvenTreeNotification().list(); + + setState(() { + _notificationCounter = notifications.length; + }); + } + Widget _listTile(BuildContext context, String label, IconData icon, {Function()? callback, String role = "", String permission = ""}) { @@ -347,7 +375,33 @@ class _InvenTreeHomePageState extends State { if (InvenTreeAPI().supportsNotifications) { items.add( BottomNavigationBarItem( - icon: FaIcon(FontAwesomeIcons.bell), + icon: _notificationCounter == 0 ? FaIcon(FontAwesomeIcons.bell) : Stack( + children: [ + FaIcon(FontAwesomeIcons.bell), + new Positioned( + right: 0, + child: new Container( + padding: EdgeInsets.all(2), + decoration: new BoxDecoration( + color: Colors.red, + borderRadius: BorderRadius.circular(20), + ), + constraints: BoxConstraints( + minWidth: 12, + minHeight: 12, + ), + child: new Text( + "${_notificationCounter}", + style: new TextStyle( + color: Colors.white, + fontSize: 9, + ), + textAlign: TextAlign.center, + ), + ), + ) + ], + ), label: L10().notifications, ) ); @@ -381,6 +435,8 @@ class _InvenTreeHomePageState extends State { setState(() { _tabIndex = index; }); + + _refreshNotifications(); }, items: getNavBarItems(context), ),