mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-04-22 03:31:09 +08:00
committed by
GitHub
parent
acb3784071
commit
218e829fd4
@@ -5,18 +5,14 @@ import 'package:PiliPlus/grpc/im.dart';
|
||||
import 'package:PiliPlus/models/common/dynamic/dynamic_badge_mode.dart';
|
||||
import 'package:PiliPlus/models/common/msg/msg_unread_type.dart';
|
||||
import 'package:PiliPlus/models/common/nav_bar_config.dart';
|
||||
import 'package:PiliPlus/pages/dynamics/view.dart';
|
||||
import 'package:PiliPlus/pages/home/view.dart';
|
||||
import 'package:PiliPlus/pages/media/view.dart';
|
||||
import 'package:PiliPlus/utils/extension.dart';
|
||||
import 'package:PiliPlus/utils/storage.dart';
|
||||
import 'package:PiliPlus/utils/utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class MainController extends GetxController {
|
||||
List<Widget> pages = <Widget>[];
|
||||
RxList navigationBars = [].obs;
|
||||
RxList<NavigationBarType> navigationBars = <NavigationBarType>[].obs;
|
||||
int dynCount = 0;
|
||||
|
||||
StreamController<bool>? bottomBarStream;
|
||||
late bool hideTabBar;
|
||||
@@ -58,7 +54,7 @@ class MainController extends GetxController {
|
||||
SettingBoxKey.dynamicBadgeMode,
|
||||
defaultValue: DynamicBadgeMode.number.index)];
|
||||
|
||||
dynIndex = navigationBars.indexWhere((e) => e['id'] == 1);
|
||||
dynIndex = navigationBars.indexOf(NavigationBarType.dynamics);
|
||||
if (dynamicBadgeMode != DynamicBadgeMode.hidden) {
|
||||
if (dynIndex != -1) {
|
||||
if (checkDynamic) {
|
||||
@@ -68,7 +64,7 @@ class MainController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
homeIndex = navigationBars.indexWhere((e) => e['id'] == 0);
|
||||
homeIndex = navigationBars.indexOf(NavigationBarType.home);
|
||||
if (msgBadgeMode != DynamicBadgeMode.hidden) {
|
||||
if (homeIndex != -1) {
|
||||
lastCheckUnreadAt = DateTime.now().millisecondsSinceEpoch;
|
||||
@@ -141,8 +137,8 @@ class MainController extends GetxController {
|
||||
}
|
||||
|
||||
Future<void> setCount([int count = 0]) async {
|
||||
if (dynIndex == -1 || navigationBars[dynIndex]['count'] == count) return;
|
||||
navigationBars[dynIndex]['count'] = count;
|
||||
if (dynIndex == -1 || dynCount == count) return;
|
||||
dynCount = count;
|
||||
navigationBars.refresh();
|
||||
}
|
||||
|
||||
@@ -161,28 +157,19 @@ class MainController extends GetxController {
|
||||
}
|
||||
|
||||
void setNavBarConfig() {
|
||||
List defaultNavTabs = [...defaultNavigationBars];
|
||||
List navBarSort =
|
||||
GStorage.setting.get(SettingBoxKey.navBarSort, defaultValue: [0, 1, 2]);
|
||||
defaultNavTabs
|
||||
..retainWhere((item) => navBarSort.contains(item['id']))
|
||||
..sort((a, b) =>
|
||||
navBarSort.indexOf(a['id']).compareTo(navBarSort.indexOf(b['id'])));
|
||||
navigationBars.value = defaultNavTabs;
|
||||
int defaultHomePage = GStorage.setting
|
||||
.get(SettingBoxKey.defaultHomePage, defaultValue: 0) as int;
|
||||
int defaultIndex =
|
||||
navigationBars.indexWhere((item) => item['id'] == defaultHomePage);
|
||||
// 如果找不到匹配项,默认索引设置为0或其他合适的值
|
||||
selectedIndex.value = defaultIndex != -1 ? defaultIndex : 0;
|
||||
pages = navigationBars
|
||||
.map<Widget>((e) => switch (e['id']) {
|
||||
0 => const HomePage(),
|
||||
1 => const DynamicsPage(),
|
||||
2 => const MediaPage(),
|
||||
_ => throw UnimplementedError(),
|
||||
})
|
||||
.toList();
|
||||
List<int>? navBarSort =
|
||||
(GStorage.setting.get(SettingBoxKey.navBarSort) as List?)?.cast();
|
||||
int defaultHomePage = GStorage.defaultHomePage;
|
||||
late final List<NavigationBarType> navigationBars;
|
||||
if (navBarSort == null) {
|
||||
navigationBars = NavigationBarType.values;
|
||||
} else {
|
||||
navigationBars =
|
||||
navBarSort.map((i) => NavigationBarType.values[i]).toList();
|
||||
if (!navBarSort.contains(defaultHomePage)) defaultHomePage = 0;
|
||||
}
|
||||
this.navigationBars.value = navigationBars;
|
||||
selectedIndex.value = defaultHomePage;
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:PiliPlus/common/widgets/image/network_img_layer.dart';
|
||||
import 'package:PiliPlus/common/widgets/tabs.dart';
|
||||
import 'package:PiliPlus/models/common/dynamic/dynamic_badge_mode.dart';
|
||||
import 'package:PiliPlus/models/common/image_type.dart';
|
||||
import 'package:PiliPlus/models/common/nav_bar_config.dart';
|
||||
import 'package:PiliPlus/pages/dynamics/controller.dart';
|
||||
import 'package:PiliPlus/pages/dynamics/view.dart';
|
||||
import 'package:PiliPlus/pages/home/controller.dart';
|
||||
@@ -94,8 +95,8 @@ class _MainAppState extends State<MainApp>
|
||||
void _checkDefaultSearch([bool shouldCheck = false]) {
|
||||
if (_mainController.homeIndex != -1 && _homeController.enableSearchWord) {
|
||||
if (shouldCheck &&
|
||||
_mainController.pages[_mainController.selectedIndex.value]
|
||||
is! HomePage) {
|
||||
_mainController.navigationBars[_mainController.selectedIndex.value] !=
|
||||
NavigationBarType.home) {
|
||||
return;
|
||||
}
|
||||
int now = DateTime.now().millisecondsSinceEpoch;
|
||||
@@ -112,8 +113,8 @@ class _MainAppState extends State<MainApp>
|
||||
_mainController.homeIndex != -1 &&
|
||||
_mainController.msgBadgeMode != DynamicBadgeMode.hidden) {
|
||||
if (shouldCheck &&
|
||||
_mainController.pages[_mainController.selectedIndex.value]
|
||||
is! HomePage) {
|
||||
_mainController.navigationBars[_mainController.selectedIndex.value] !=
|
||||
NavigationBarType.home) {
|
||||
return;
|
||||
}
|
||||
int now = DateTime.now().millisecondsSinceEpoch;
|
||||
@@ -128,6 +129,7 @@ class _MainAppState extends State<MainApp>
|
||||
void setIndex(int value) {
|
||||
feedBack();
|
||||
|
||||
final currentPage = _mainController.navigationBars[value].page;
|
||||
if (value != _mainController.selectedIndex.value) {
|
||||
_mainController.selectedIndex.value = value;
|
||||
if (_mainController.mainTabBarView) {
|
||||
@@ -135,7 +137,6 @@ class _MainAppState extends State<MainApp>
|
||||
} else {
|
||||
_mainController.controller.jumpToPage(value);
|
||||
}
|
||||
dynamic currentPage = _mainController.pages[value];
|
||||
if (currentPage is HomePage) {
|
||||
_checkDefaultSearch();
|
||||
_checkUnread();
|
||||
@@ -143,8 +144,6 @@ class _MainAppState extends State<MainApp>
|
||||
_mainController.setCount();
|
||||
}
|
||||
} else {
|
||||
dynamic currentPage = _mainController.pages[value];
|
||||
|
||||
int now = DateTime.now().millisecondsSinceEpoch;
|
||||
if (now - _lastSelectTime < 500) {
|
||||
EasyThrottle.throttle('topOrRefresh', const Duration(milliseconds: 500),
|
||||
@@ -241,16 +240,23 @@ class _MainAppState extends State<MainApp>
|
||||
..._mainController.navigationBars
|
||||
.map((e) {
|
||||
return NavigationDrawerDestination(
|
||||
label: Text(e['label']),
|
||||
label: Text(e.label),
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
type: e,
|
||||
count: e ==
|
||||
NavigationBarType
|
||||
.dynamics
|
||||
? _mainController.dynCount
|
||||
: 0,
|
||||
),
|
||||
selectedIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
type: e,
|
||||
count: e ==
|
||||
NavigationBarType
|
||||
.dynamics
|
||||
? _mainController.dynCount
|
||||
: 0,
|
||||
selected: true,
|
||||
),
|
||||
);
|
||||
}),
|
||||
@@ -271,17 +277,20 @@ class _MainAppState extends State<MainApp>
|
||||
destinations: _mainController.navigationBars
|
||||
.map(
|
||||
(e) => NavigationRailDestination(
|
||||
label: Text(e.label),
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
type: e,
|
||||
count: e == NavigationBarType.dynamics
|
||||
? _mainController.dynCount
|
||||
: 0,
|
||||
),
|
||||
selectedIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
type: e,
|
||||
count: e == NavigationBarType.dynamics
|
||||
? _mainController.dynCount
|
||||
: 0,
|
||||
selected: true,
|
||||
),
|
||||
label: Text(e['label']),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
@@ -315,12 +324,16 @@ class _MainAppState extends State<MainApp>
|
||||
isPortrait ? Axis.horizontal : Axis.vertical,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
controller: _mainController.controller,
|
||||
children: _mainController.pages,
|
||||
children: _mainController.navigationBars
|
||||
.map((i) => i.page)
|
||||
.toList(),
|
||||
)
|
||||
: PageView(
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
controller: _mainController.controller,
|
||||
children: _mainController.pages,
|
||||
children: _mainController.navigationBars
|
||||
.map((i) => i.page)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -353,17 +366,22 @@ class _MainAppState extends State<MainApp>
|
||||
_mainController.navigationBars.map(
|
||||
(e) {
|
||||
return NavigationDestination(
|
||||
label: e.label,
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
type: e,
|
||||
count: e ==
|
||||
NavigationBarType.dynamics
|
||||
? _mainController.dynCount
|
||||
: 0,
|
||||
),
|
||||
selectedIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
type: e,
|
||||
count: e ==
|
||||
NavigationBarType.dynamics
|
||||
? _mainController.dynCount
|
||||
: 0,
|
||||
selected: true,
|
||||
),
|
||||
label: e['label'],
|
||||
);
|
||||
},
|
||||
).toList(),
|
||||
@@ -383,17 +401,24 @@ class _MainAppState extends State<MainApp>
|
||||
items: _mainController.navigationBars
|
||||
.map(
|
||||
(e) => BottomNavigationBarItem(
|
||||
label: e.label,
|
||||
icon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['icon'],
|
||||
type: e,
|
||||
count: e ==
|
||||
NavigationBarType
|
||||
.dynamics
|
||||
? _mainController.dynCount
|
||||
: 0,
|
||||
),
|
||||
activeIcon: _buildIcon(
|
||||
id: e['id'],
|
||||
count: e['count'],
|
||||
icon: e['selectIcon'],
|
||||
type: e,
|
||||
count: e ==
|
||||
NavigationBarType
|
||||
.dynamics
|
||||
? _mainController.dynCount
|
||||
: 0,
|
||||
selected: true,
|
||||
),
|
||||
label: e['label'],
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
@@ -409,21 +434,21 @@ class _MainAppState extends State<MainApp>
|
||||
}
|
||||
|
||||
Widget _buildIcon({
|
||||
required int id,
|
||||
required NavigationBarType type,
|
||||
required int count,
|
||||
required Widget icon,
|
||||
}) =>
|
||||
id == 1 &&
|
||||
_mainController.dynamicBadgeMode != DynamicBadgeMode.hidden &&
|
||||
count > 0
|
||||
? Badge(
|
||||
label: _mainController.dynamicBadgeMode == DynamicBadgeMode.number
|
||||
? Text(count.toString())
|
||||
: null,
|
||||
padding: const EdgeInsets.fromLTRB(6, 0, 6, 0),
|
||||
child: icon,
|
||||
)
|
||||
: icon;
|
||||
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,
|
||||
)
|
||||
: icon;
|
||||
}
|
||||
|
||||
Widget userAndSearchVertical(ThemeData theme) {
|
||||
return Column(
|
||||
|
||||
Reference in New Issue
Block a user