| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | import 'package:flutter_new_badger/flutter_new_badger.dart'; | 
					
						
							|  |  |  | import 'package:qhd_prevention/http/ApiService.dart'; | 
					
						
							| 
									
										
										
										
											2025-08-08 10:52:15 +08:00
										 |  |  | import 'package:qhd_prevention/tools/tools.dart'; | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /// 单例角标管理,多接口分模块更新及全局原生角标同步
 | 
					
						
							|  |  |  | class BadgeManager extends ChangeNotifier { | 
					
						
							|  |  |  |   BadgeManager._internal(); | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |   static final BadgeManager _instance = BadgeManager._internal(); | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |   factory BadgeManager() => _instance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 各模块未读
 | 
					
						
							|  |  |  |   int _appCount = 0; | 
					
						
							|  |  |  |   int _appDysCount = 0; // 新增:待验收
 | 
					
						
							|  |  |  |   int _appDzgCount = 0; // 新增:待整改
 | 
					
						
							|  |  |  |   int _notifCount = 0; | 
					
						
							|  |  |  |   int _envInspectCount = 0; | 
					
						
							|  |  |  |   int _eightWorkCount = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// 总未读角标数
 | 
					
						
							|  |  |  |   int get count => _appCount + _notifCount + _envInspectCount + _eightWorkCount; | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |   /// MainPage 整体“应用”角标
 | 
					
						
							|  |  |  |   int get appCount => _appCount; | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |   /// MainPage “待验收”子项
 | 
					
						
							|  |  |  |   int get appDysCount => _appDysCount; | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |   /// MainPage “待整改”子项
 | 
					
						
							|  |  |  |   int get appDzgCount => _appDzgCount; | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |   int get notifCount => _notifCount; | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |   int get envInspectCount => _envInspectCount; | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |   int get eightWorkCount => _eightWorkCount; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// 登录后或有用户上下文时,初始化所有接口汇总数,并同步原生角标
 | 
					
						
							|  |  |  |   Future<void> initAllModules() async { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       final results = await Future.wait([ | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  |         ApiService.getWork(), // 应用中心
 | 
					
						
							|  |  |  |         ApiService.getNotifRedPoint(), // 通知公告
 | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |         ApiService.getSafetyEnvironmentalInspectionCount(), // 安全巡检
 | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  |         ApiService.getRedPoint(), // 八项作业
 | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |       ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // 应用中心部分
 | 
					
						
							| 
									
										
										
										
											2025-08-08 10:52:15 +08:00
										 |  |  |       final workJson = results[0]; | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |       final hid = workJson['hidCount'] as Map<String, dynamic>? ?? {}; | 
					
						
							|  |  |  |       _appDysCount = (hid['dys'] ?? 0) as int; | 
					
						
							|  |  |  |       _appDzgCount = (hid['dzg'] ?? 0) as int; | 
					
						
							|  |  |  |       _appCount = _appDysCount + _appDzgCount; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // 通知公告部分
 | 
					
						
							| 
									
										
										
										
											2025-08-08 10:52:15 +08:00
										 |  |  |       final notifJson = results[1]; | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |       _notifCount = notifJson['count'] as int? ?? 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // 安全巡检部分
 | 
					
						
							| 
									
										
										
										
											2025-08-08 10:52:15 +08:00
										 |  |  |       final checkJson = results[2]; | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |       _envInspectCount = | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  |           checkJson['repulseCount']?['repulseCount'] ?? 0; | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // 八项作业部分
 | 
					
						
							| 
									
										
										
										
											2025-08-08 10:52:15 +08:00
										 |  |  |       final redPointJson = results[3]; | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |       _eightWorkCount = 0; | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  |       (redPointJson['count'] as Map<String, dynamic>? ?? {}).values.forEach(( | 
					
						
							|  |  |  |         v, | 
					
						
							|  |  |  |       ) { | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |         _eightWorkCount += (v ?? 0) as int; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       _syncNative(); | 
					
						
							|  |  |  |       notifyListeners(); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       debugPrint('BadgeManager.initAllModules error: $e'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// 更新 MainPage 的应用中心角标
 | 
					
						
							|  |  |  |   void updateAppCount() async { | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2025-08-08 10:52:15 +08:00
										 |  |  |       final workJson = await ApiService.getWork(); | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |       final hid = workJson['hidCount'] as Map<String, dynamic>? ?? {}; | 
					
						
							|  |  |  |       _appDysCount = (hid['dys'] ?? 0) as int; | 
					
						
							|  |  |  |       _appDzgCount = (hid['dzg'] ?? 0) as int; | 
					
						
							|  |  |  |       _appCount = _appDysCount + _appDzgCount; | 
					
						
							|  |  |  |       _onModuleChanged(); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       debugPrint('updateAppCount error: $e'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// 更新 MainPage 的通知公告角标
 | 
					
						
							|  |  |  |   void updateNotifCount() async { | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2025-08-08 10:52:15 +08:00
										 |  |  |       final notifJson = await ApiService.getNotifRedPoint(); | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |       _notifCount = notifJson['count'] as int? ?? 0; | 
					
						
							|  |  |  |       _onModuleChanged(); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       debugPrint('updateNotifCount error: $e'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// 更新 HomePage 安全巡检角标
 | 
					
						
							|  |  |  |   void updateEnvInspectCount() async { | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  |       final checkJson = | 
					
						
							|  |  |  |           await ApiService.getSafetyEnvironmentalInspectionCount(); | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |       _envInspectCount = | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  |           (checkJson['confirmCount']?['confirmCount'] ?? 0)( | 
					
						
							|  |  |  |                 checkJson['repulseCount']?['repulseCount'] ?? 0, | 
					
						
							|  |  |  |               )(checkJson['repulseAndCheckCount']?['repulseAndCheckCount'] ?? 0) | 
					
						
							|  |  |  |               as int; | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |       _onModuleChanged(); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       debugPrint('updateEnvInspectCount error: $e'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// 更新 HomePage 八项作业角标
 | 
					
						
							|  |  |  |   void updateEightWorkCount() async { | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2025-08-08 10:52:15 +08:00
										 |  |  |       final redPointJson = await ApiService.getRedPoint(); | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |       int sum = 0; | 
					
						
							| 
									
										
										
										
											2025-08-19 11:06:16 +08:00
										 |  |  |       (redPointJson['count'] as Map<String, dynamic>? ?? {}).values.forEach(( | 
					
						
							|  |  |  |         v, | 
					
						
							|  |  |  |       ) { | 
					
						
							| 
									
										
										
										
											2025-08-07 17:33:16 +08:00
										 |  |  |         sum += (v ?? 0) as int; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       _eightWorkCount = sum; | 
					
						
							|  |  |  |       _onModuleChanged(); | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       debugPrint('updateEightWorkCount error: $e'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// 清空所有角标
 | 
					
						
							|  |  |  |   void clearAll() { | 
					
						
							|  |  |  |     _appCount = _notifCount = _envInspectCount = _eightWorkCount = 0; | 
					
						
							|  |  |  |     _appDysCount = _appDzgCount = 0; | 
					
						
							|  |  |  |     _syncNative(); | 
					
						
							|  |  |  |     notifyListeners(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void _onModuleChanged() { | 
					
						
							|  |  |  |     _syncNative(); | 
					
						
							|  |  |  |     notifyListeners(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void _syncNative() { | 
					
						
							|  |  |  |     final total = count; | 
					
						
							|  |  |  |     if (total > 0) { | 
					
						
							|  |  |  |       FlutterNewBadger.setBadge(total); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       FlutterNewBadger.removeBadge(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |