diff --git a/lib/http/api.dart b/lib/http/api.dart index c8edf863..1731519b 100644 --- a/lib/http/api.dart +++ b/lib/http/api.dart @@ -467,4 +467,7 @@ class Api { /// page_size static const getSeasonDetailApi = '/x/polymer/web-space/seasons_archives_list'; + + /// 获取未读动态数 + static const getUnreadDynamic = '/x/web-interface/dynamic/entrance'; } diff --git a/lib/http/common.dart b/lib/http/common.dart new file mode 100644 index 00000000..d711a7e7 --- /dev/null +++ b/lib/http/common.dart @@ -0,0 +1,17 @@ +import 'index.dart'; + +class CommonHttp { + static Future unReadDynamic() async { + var res = await Request().get(Api.getUnreadDynamic, + data: {'alltype_offset': 0, 'video_offset': '', 'article_offset': 0}); + if (res.data['code'] == 0) { + return {'status': true, 'data': res.data['data']['dyn_basic_infos']}; + } else { + return { + 'status': false, + 'data': [], + 'msg': res.data['message'], + }; + } + } +} diff --git a/lib/pages/main/controller.dart b/lib/pages/main/controller.dart index a16e841d..a55c143e 100644 --- a/lib/pages/main/controller.dart +++ b/lib/pages/main/controller.dart @@ -5,6 +5,7 @@ import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/get.dart'; import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; +import 'package:pilipala/http/common.dart'; import 'package:pilipala/pages/dynamics/index.dart'; import 'package:pilipala/pages/home/view.dart'; import 'package:pilipala/pages/media/index.dart'; @@ -28,6 +29,7 @@ class MainController extends GetxController { size: 21, ), 'label': "首页", + 'count': 0, }, { 'icon': const Icon( @@ -39,6 +41,7 @@ class MainController extends GetxController { size: 21, ), 'label': "动态", + 'count': 0, }, { 'icon': const Icon( @@ -50,6 +53,7 @@ class MainController extends GetxController { size: 21, ), 'label': "媒体库", + 'count': 0, } ].obs; final StreamController bottomBarStream = @@ -59,6 +63,8 @@ class MainController extends GetxController { late bool hideTabBar; late PageController pageController; int selectedIndex = 0; + Box userInfoCache = GStrorage.userInfo; + RxBool userLogin = false.obs; @override void onInit() { @@ -67,6 +73,9 @@ class MainController extends GetxController { Utils.checkUpdata(); } hideTabBar = setting.get(SettingBoxKey.hideTabBar, defaultValue: true); + var userInfo = userInfoCache.get('userInfoCache'); + userLogin.value = userInfo != null; + getUnreadDynamic(); } void onBackPressed(BuildContext context) { @@ -83,4 +92,28 @@ class MainController extends GetxController { } SystemNavigator.pop(); // 退出应用 } + + void getUnreadDynamic() async { + if (!userLogin.value) { + return; + } + int dynamicItemIndex = + navigationBars.indexWhere((item) => item['label'] == "动态"); + var res = await CommonHttp.unReadDynamic(); + var data = res['data']; + if (dynamicItemIndex != -1) { + navigationBars[dynamicItemIndex]['count'] = + data == null ? 0 : data.length; // 修改 count 属性为新的值 + } + navigationBars.refresh(); + } + + void clearUnread() async { + int dynamicItemIndex = + navigationBars.indexWhere((item) => item['label'] == "动态"); + if (dynamicItemIndex != -1) { + navigationBars[dynamicItemIndex]['count'] = 0; // 修改 count 属性为新的值 + } + navigationBars.refresh(); + } } diff --git a/lib/pages/main/view.dart b/lib/pages/main/view.dart index 528ab868..5a570a8f 100644 --- a/lib/pages/main/view.dart +++ b/lib/pages/main/view.dart @@ -39,7 +39,7 @@ class _MainAppState extends State with SingleTickerProviderStateMixin { void setIndex(int value) async { feedBack(); - _mainController.pageController!.jumpToPage(value); + _mainController.pageController.jumpToPage(value); var currentPage = _mainController.pages[value]; if (currentPage is HomePage) { if (_homeController.flag) { @@ -67,6 +67,7 @@ class _MainAppState extends State with SingleTickerProviderStateMixin { _lastSelectTime = DateTime.now().millisecondsSinceEpoch; } _dynamicController.flag = true; + _mainController.clearUnread(); } else { _dynamicController.flag = false; } @@ -118,36 +119,48 @@ class _MainAppState extends State with SingleTickerProviderStateMixin { curve: Curves.easeInOutCubicEmphasized, duration: const Duration(milliseconds: 500), offset: Offset(0, snapshot.data ? 0 : 1), - child: enableMYBar - ? NavigationBar( - onDestinationSelected: (value) => setIndex(value), - selectedIndex: _mainController.selectedIndex, - destinations: [ - ..._mainController.navigationBars.map((e) { - return NavigationDestination( - icon: e['icon'], - selectedIcon: e['selectIcon'], - label: e['label'], - ); - }).toList(), - ], - ) - : BottomNavigationBar( - currentIndex: _mainController.selectedIndex, - onTap: (value) => setIndex(value), - iconSize: 16, - selectedFontSize: 12, - unselectedFontSize: 12, - items: [ - ..._mainController.navigationBars.map((e) { - return BottomNavigationBarItem( - icon: e['icon'], - activeIcon: e['selectIcon'], - label: e['label'], - ); - }).toList(), - ], - ), + child: Obx( + () => enableMYBar + ? NavigationBar( + onDestinationSelected: (value) => setIndex(value), + selectedIndex: _mainController.selectedIndex, + destinations: [ + ..._mainController.navigationBars.map((e) { + return NavigationDestination( + icon: Badge( + label: Text(e['count'].toString()), + padding: const EdgeInsets.fromLTRB(6, 0, 6, 0), + isLabelVisible: e['count'] > 0, + child: e['icon'], + ), + selectedIcon: e['selectIcon'], + label: e['label'], + ); + }).toList(), + ], + ) + : BottomNavigationBar( + currentIndex: _mainController.selectedIndex, + onTap: (value) => setIndex(value), + iconSize: 16, + selectedFontSize: 12, + unselectedFontSize: 12, + items: [ + ..._mainController.navigationBars.map((e) { + return BottomNavigationBarItem( + icon: Badge( + label: Text(e['count'].toString()), + padding: const EdgeInsets.fromLTRB(6, 0, 6, 0), + isLabelVisible: e['count'] > 0, + child: e['icon'], + ), + activeIcon: e['selectIcon'], + label: e['label'], + ); + }).toList(), + ], + ), + ), ); }, ),