diff --git a/lib/pages/main/controller.dart b/lib/pages/main/controller.dart index 10b3c293..74dfd76d 100644 --- a/lib/pages/main/controller.dart +++ b/lib/pages/main/controller.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:math' show max; import 'package:PiliPlus/grpc/dyn.dart'; import 'package:PiliPlus/grpc/im.dart'; @@ -11,8 +12,8 @@ import 'package:PiliPlus/utils/utils.dart'; import 'package:get/get.dart'; class MainController extends GetxController { - RxList navigationBars = [].obs; - int dynCount = 0; + List navigationBars = []; + RxInt dynCount = 0.obs; StreamController? bottomBarStream; late bool hideTabBar; @@ -137,9 +138,8 @@ class MainController extends GetxController { } Future setCount([int count = 0]) async { - if (dynIndex == -1 || dynCount == count) return; - dynCount = count; - navigationBars.refresh(); + if (dynIndex == -1 || dynCount.value == count) return; + dynCount.value = count; } void checkUnreadDynamic() { @@ -166,10 +166,10 @@ class MainController extends GetxController { } else { navigationBars = navBarSort.map((i) => NavigationBarType.values[i]).toList(); - if (!navBarSort.contains(defaultHomePage)) defaultHomePage = 0; } - this.navigationBars.value = navigationBars; - selectedIndex.value = defaultHomePage; + this.navigationBars = navigationBars; + selectedIndex.value = + max(0, navigationBars.indexWhere((e) => e.index == defaultHomePage)); } @override diff --git a/lib/pages/main/view.dart b/lib/pages/main/view.dart index 304d59da..e8dcc856 100644 --- a/lib/pages/main/view.dart +++ b/lib/pages/main/view.dart @@ -207,24 +207,24 @@ class _MainAppState extends State mainAxisAlignment: MainAxisAlignment.center, children: [ if (useSideBar || !isPortrait) ...[ - Obx( - () => _mainController.navigationBars.length > 1 - ? context.isTablet && GStorage.optTabletNav - ? Column( - children: [ - SizedBox( - height: - MediaQuery.paddingOf(context).top + 50), - userAndSearchVertical(theme), - const Spacer(flex: 2), - Expanded( - flex: 5, - child: SizedBox( - width: 130, - child: MediaQuery.removePadding( - context: context, - removeRight: true, - child: NavigationDrawer( + _mainController.navigationBars.length > 1 + ? context.isTablet && GStorage.optTabletNav + ? Column( + children: [ + SizedBox( + height: + MediaQuery.paddingOf(context).top + 50), + userAndSearchVertical(theme), + const Spacer(flex: 2), + Expanded( + flex: 5, + child: SizedBox( + width: 130, + child: MediaQuery.removePadding( + context: context, + removeRight: true, + child: Obx( + () => NavigationDrawer( backgroundColor: Colors.transparent, tilePadding: const EdgeInsets.symmetric( vertical: 5, horizontal: 12), @@ -237,38 +237,27 @@ class _MainAppState extends State onDestinationSelected: setIndex, selectedIndex: _mainController.selectedIndex.value, - children: [ - ..._mainController.navigationBars - .map((e) { - return NavigationDrawerDestination( - label: Text(e.label), - icon: _buildIcon( - type: e, - count: e == - NavigationBarType - .dynamics - ? _mainController.dynCount - : 0, - ), - selectedIcon: _buildIcon( - type: e, - count: e == - NavigationBarType - .dynamics - ? _mainController.dynCount - : 0, - selected: true, - ), - ); - }), - ], + children: _mainController.navigationBars + .map( + (e) => + NavigationDrawerDestination( + label: Text(e.label), + icon: _buildIcon(type: e), + selectedIcon: _buildIcon( + type: e, + selected: true, + )), + ) + .toList(), ), ), ), ), - ], - ) - : NavigationRail( + ), + ], + ) + : Obx( + () => NavigationRail( groupAlignment: 0.5, selectedIndex: _mainController.selectedIndex.value, @@ -276,37 +265,25 @@ class _MainAppState extends State labelType: NavigationRailLabelType.selected, leading: userAndSearchVertical(theme), destinations: _mainController.navigationBars - .map( - (e) => NavigationRailDestination( - label: Text(e.label), - icon: _buildIcon( - type: e, - count: e == NavigationBarType.dynamics - ? _mainController.dynCount - : 0, - ), - selectedIcon: _buildIcon( - type: e, - count: e == NavigationBarType.dynamics - ? _mainController.dynCount - : 0, - selected: true, - ), - ), - ) + .map((e) => NavigationRailDestination( + label: Text(e.label), + icon: _buildIcon(type: e), + selectedIcon: _buildIcon( + type: e, + selected: true, + ), + )) .toList(), - ) - : SafeArea( - right: false, - child: Container( - padding: const EdgeInsets.only( - top: 10, ), - width: 80, - child: userAndSearchVertical(theme), - ), + ) + : SafeArea( + right: false, + child: Container( + padding: const EdgeInsets.only(top: 10), + width: 80, + child: userAndSearchVertical(theme), ), - ), + ), VerticalDivider( width: 1, indent: MediaQuery.of(context).padding.top, @@ -357,75 +334,46 @@ class _MainAppState extends State duration: const Duration(milliseconds: 500), offset: Offset(0, snapshot.data ? 0 : 1), child: enableMYBar - ? Obx( - () => _mainController.navigationBars.length > 1 - ? NavigationBar( - onDestinationSelected: setIndex, - selectedIndex: - _mainController.selectedIndex.value, - destinations: - _mainController.navigationBars.map( - (e) { - return NavigationDestination( + ? _mainController.navigationBars.length > 1 + ? Obx( + () => NavigationBar( + onDestinationSelected: setIndex, + selectedIndex: + _mainController.selectedIndex.value, + destinations: _mainController.navigationBars + .map((e) => NavigationDestination( label: e.label, - icon: _buildIcon( - type: e, - count: e == - NavigationBarType.dynamics - ? _mainController.dynCount - : 0, - ), + icon: _buildIcon(type: e), selectedIcon: _buildIcon( type: e, - count: e == - NavigationBarType.dynamics - ? _mainController.dynCount - : 0, selected: true, - ), - ); - }, - ).toList(), - ) - : const SizedBox.shrink(), - ) - : Obx( - () => _mainController.navigationBars.length > 1 - ? BottomNavigationBar( - currentIndex: - _mainController.selectedIndex.value, - onTap: setIndex, - iconSize: 16, - selectedFontSize: 12, - unselectedFontSize: 12, - type: BottomNavigationBarType.fixed, - items: _mainController.navigationBars - .map( - (e) => BottomNavigationBarItem( - label: e.label, - icon: _buildIcon( - type: e, - count: e == - NavigationBarType - .dynamics - ? _mainController.dynCount - : 0, - ), - activeIcon: _buildIcon( - type: e, - count: e == - NavigationBarType - .dynamics - ? _mainController.dynCount - : 0, - selected: true, - ), - ), - ) - .toList(), - ) - : const SizedBox.shrink(), - ), + ))) + .toList(), + ), + ) + : const SizedBox.shrink() + : _mainController.navigationBars.length > 1 + ? Obx( + () => BottomNavigationBar( + currentIndex: + _mainController.selectedIndex.value, + onTap: setIndex, + iconSize: 16, + selectedFontSize: 12, + unselectedFontSize: 12, + type: BottomNavigationBarType.fixed, + items: _mainController.navigationBars + .map((e) => BottomNavigationBarItem( + label: e.label, + icon: _buildIcon(type: e), + activeIcon: _buildIcon( + type: e, + selected: true, + ))) + .toList(), + ), + ) + : const SizedBox.shrink(), ); }, ), @@ -436,17 +384,19 @@ class _MainAppState extends State Widget _buildIcon({ required NavigationBarType type, - required int count, bool selected = false, }) { final icon = selected ? type.selectIcon : type.icon; - return count > 0 - ? Badge( - label: _mainController.dynamicBadgeMode == DynamicBadgeMode.number - ? Text(count.toString()) - : null, - padding: const EdgeInsets.fromLTRB(6, 0, 6, 0), - child: icon, + return type == NavigationBarType.dynamics + ? Obx( + () => Badge( + isLabelVisible: _mainController.dynCount.value > 0, + label: _mainController.dynamicBadgeMode == DynamicBadgeMode.number + ? Text(_mainController.dynCount.value.toString()) + : null, + padding: const EdgeInsets.symmetric(horizontal: 6), + child: icon, + ), ) : icon; }