diff --git a/lib/pages/home/controller.dart b/lib/pages/home/controller.dart index 497a6b3c3..738bb5541 100644 --- a/lib/pages/home/controller.dart +++ b/lib/pages/home/controller.dart @@ -3,7 +3,6 @@ import 'dart:math'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; -import 'package:hive/hive.dart'; import 'package:PiliPalaX/models/common/tab_type.dart'; import 'package:PiliPalaX/utils/storage.dart'; import '../../http/index.dart'; @@ -16,32 +15,36 @@ class HomeController extends GetxController with GetTickerProviderStateMixin { late TabController tabController; late List tabsCtrList; late List tabsPageList; - Box userInfoCache = GStorage.userInfo; - Box settingStorage = GStorage.setting; RxBool userLogin = false.obs; RxString userFace = ''.obs; dynamic userInfo; - Box setting = GStorage.setting; late final StreamController searchBarStream = StreamController.broadcast(); late bool hideSearchBar; late List defaultTabs; late List tabbarSort; - RxString defaultSearch = ''.obs; late bool useSideBar; + late bool enableSearchWord; + late RxString defaultSearch = ''.obs; + late int lateCheckAt = 0; + @override void onInit() { super.onInit(); - userInfo = userInfoCache.get('userInfoCache'); + userInfo = GStorage.userInfo.get('userInfoCache'); userLogin.value = userInfo != null; userFace.value = userInfo != null ? userInfo.face : ''; hideSearchBar = - setting.get(SettingBoxKey.hideSearchBar, defaultValue: true); - if (setting.get(SettingBoxKey.enableSearchWord, defaultValue: true)) { - searchDefault(); + GStorage.setting.get(SettingBoxKey.hideSearchBar, defaultValue: true); + enableSearchWord = GStorage.setting + .get(SettingBoxKey.enableSearchWord, defaultValue: true); + if (enableSearchWord) { + lateCheckAt = DateTime.now().millisecondsSinceEpoch; + querySearchDefault(); } - useSideBar = setting.get(SettingBoxKey.useSideBar, defaultValue: false); + useSideBar = + GStorage.setting.get(SettingBoxKey.useSideBar, defaultValue: false); // 进行tabs配置 setTabConfig(); } @@ -60,7 +63,7 @@ class HomeController extends GetxController with GetTickerProviderStateMixin { // 更新登录状态 Future updateLoginStatus(val) async { - userInfo = await userInfoCache.get('userInfoCache'); + userInfo = await GStorage.userInfo.get('userInfoCache'); userLogin.value = val ?? false; if (val) return; userFace.value = userInfo != null ? userInfo.face : ''; @@ -68,7 +71,7 @@ class HomeController extends GetxController with GetTickerProviderStateMixin { void setTabConfig() async { defaultTabs = [...tabsConfig]; - tabbarSort = settingStorage + tabbarSort = GStorage.setting .get(SettingBoxKey.tabbarSort, defaultValue: ['live', 'rcmd', 'hot', 'rank', 'bangumi']) .map((i) => i.toString()) @@ -93,12 +96,12 @@ class HomeController extends GetxController with GetTickerProviderStateMixin { @override void dispose() { - tabController.animation!.removeListener(() {}); + tabController.animation?.removeListener(() {}); tabController.dispose(); super.dispose(); } - void searchDefault() async { + void querySearchDefault() async { var res = await Request().get(Api.searchDefault); if (res.data['code'] == 0) { defaultSearch.value = res.data['data']['name']; @@ -108,10 +111,11 @@ class HomeController extends GetxController with GetTickerProviderStateMixin { showUserInfoDialog(context) { feedBack(); showDialog( - context: context, - useSafeArea: true, - builder: (context) => const Dialog( - child: MinePage(), - )); + context: context, + useSafeArea: true, + builder: (context) => const Dialog( + child: MinePage(), + ), + ); } } diff --git a/lib/pages/home/view.dart b/lib/pages/home/view.dart index 2ec69e912..fcaec1ed2 100644 --- a/lib/pages/home/view.dart +++ b/lib/pages/home/view.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'package:PiliPalaX/utils/extension.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'package:PiliPalaX/common/widgets/network_img_layer.dart'; import 'package:PiliPalaX/utils/feed_back.dart'; @@ -18,7 +17,6 @@ class HomePage extends StatefulWidget { class _HomePageState extends State with AutomaticKeepAliveClientMixin, TickerProviderStateMixin { final HomeController _homeController = Get.put(HomeController()); - List videoList = []; late Stream stream; @override @@ -33,13 +31,6 @@ class _HomePageState extends State @override Widget build(BuildContext context) { super.build(context); - // Brightness currentBrightness = MediaQuery.of(context).platformBrightness; - // 设置状态栏图标的亮度 - // SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle( - // statusBarIconBrightness: currentBrightness == Brightness.light - // ? Brightness.dark - // : Brightness.light, - // )); return Scaffold( appBar: AppBar(toolbarHeight: 0), body: Column( @@ -49,7 +40,7 @@ class _HomePageState extends State stream: _homeController.hideSearchBar ? stream : StreamController.broadcast().stream, - ctr: _homeController, + homeController: _homeController, ), if (_homeController.tabs.length > 1) ...[ ...[ @@ -97,13 +88,13 @@ class _HomePageState extends State class CustomAppBar extends StatelessWidget implements PreferredSizeWidget { final double height; final Stream? stream; - final HomeController ctr; + final HomeController homeController; const CustomAppBar({ super.key, this.height = kToolbarHeight, this.stream, - required this.ctr, + required this.homeController, }); @override @@ -124,7 +115,7 @@ class CustomAppBar extends StatelessWidget implements PreferredSizeWidget { height: snapshot.data ? 52 : 0, padding: const EdgeInsets.fromLTRB(14, 6, 14, 0), child: SearchBarAndUser( - ctr: ctr, + homeController: homeController, ), ), ); @@ -136,18 +127,18 @@ class CustomAppBar extends StatelessWidget implements PreferredSizeWidget { class SearchBarAndUser extends StatelessWidget { const SearchBarAndUser({ super.key, - required this.ctr, + required this.homeController, }); - final HomeController ctr; + final HomeController homeController; @override Widget build(BuildContext context) { return Row( children: [ - SearchBar(ctr: ctr), + SearchBar(homeController: homeController), const SizedBox(width: 4), - Obx(() => ctr.userLogin.value + Obx(() => homeController.userLogin.value ? ClipRect( child: IconButton( tooltip: '消息', @@ -160,37 +151,40 @@ class SearchBarAndUser extends StatelessWidget { : const SizedBox.shrink()), const SizedBox(width: 8), Semantics( - label: "我的", - child: Obx( - () => ctr.userLogin.value - ? Stack( - children: [ - NetworkImgLayer( - type: 'avatar', - width: 34, - height: 34, - src: ctr.userFace.value, - ), - Positioned.fill( - child: Material( - color: Colors.transparent, - child: InkWell( - onTap: () => ctr.showUserInfoDialog(context), - splashColor: Theme.of(context) - .colorScheme - .primaryContainer - .withOpacity(0.3), - borderRadius: const BorderRadius.all( - Radius.circular(50), - ), + label: "我的", + child: Obx( + () => homeController.userLogin.value + ? Stack( + children: [ + NetworkImgLayer( + type: 'avatar', + width: 34, + height: 34, + src: homeController.userFace.value, + ), + Positioned.fill( + child: Material( + color: Colors.transparent, + child: InkWell( + onTap: () => + homeController.showUserInfoDialog(context), + splashColor: Theme.of(context) + .colorScheme + .primaryContainer + .withOpacity(0.3), + borderRadius: const BorderRadius.all( + Radius.circular(50), ), ), - ) - ], - ) - : DefaultUser( - callback: () => ctr.showUserInfoDialog(context)), - )), + ), + ) + ], + ) + : DefaultUser( + onPressed: () => homeController.showUserInfoDialog(context), + ), + ), + ), ], ); } @@ -209,45 +203,43 @@ class UserAndSearchVertical extends StatelessWidget { return Column( children: [ Semantics( - label: "我的", - child: Obx( - () => ctr.userLogin.value - ? Stack( - children: [ - NetworkImgLayer( - type: 'avatar', - width: 34, - height: 34, - src: ctr.userFace.value, - ), - Positioned.fill( - child: Material( - color: Colors.transparent, - child: InkWell( - onTap: () => ctr.showUserInfoDialog(context), - splashColor: Theme.of(context) - .colorScheme - .primaryContainer - .withOpacity(0.3), - borderRadius: const BorderRadius.all( - Radius.circular(50), - ), + label: "我的", + child: Obx( + () => ctr.userLogin.value + ? Stack( + children: [ + NetworkImgLayer( + type: 'avatar', + width: 34, + height: 34, + src: ctr.userFace.value, + ), + Positioned.fill( + child: Material( + color: Colors.transparent, + child: InkWell( + onTap: () => ctr.showUserInfoDialog(context), + splashColor: Theme.of(context) + .colorScheme + .primaryContainer + .withOpacity(0.3), + borderRadius: const BorderRadius.all( + Radius.circular(50), ), ), - ) - ], - ) - : DefaultUser( - callback: () => ctr.showUserInfoDialog(context)), - )), + ), + ) + ], + ) + : DefaultUser(onPressed: () => ctr.showUserInfoDialog(context)), + ), + ), const SizedBox(height: 8), Obx(() => ctr.userLogin.value ? IconButton( tooltip: '消息', onPressed: () => Get.toNamed('/whisper'), - icon: const Icon( - Icons.notifications_none, - ), + icon: const Icon(Icons.notifications_none), ) : const SizedBox.shrink()), IconButton( @@ -263,8 +255,8 @@ class UserAndSearchVertical extends StatelessWidget { } class DefaultUser extends StatelessWidget { - const DefaultUser({super.key, this.callback}); - final Function? callback; + const DefaultUser({super.key, required this.onPressed}); + final VoidCallback onPressed; @override Widget build(BuildContext context) { @@ -279,7 +271,7 @@ class DefaultUser extends StatelessWidget { return Theme.of(context).colorScheme.onInverseSurface; }), ), - onPressed: () => callback?.call(), + onPressed: onPressed, icon: Icon( Icons.person_rounded, size: 22, @@ -339,7 +331,7 @@ class DefaultUser extends StatelessWidget { // } class CustomChip extends StatelessWidget { - final Function onTap; + final VoidCallback onTap; final String label; final bool selected; const CustomChip({ @@ -372,7 +364,7 @@ class CustomChip extends StatelessWidget { }), padding: const EdgeInsets.fromLTRB(6, 1, 6, 1), label: Text(label, style: chipTextStyle), - onPressed: () => onTap(), + onPressed: onTap, selected: selected, showCheckmark: false, visualDensity: visualDensity, @@ -383,10 +375,10 @@ class CustomChip extends StatelessWidget { class SearchBar extends StatelessWidget { const SearchBar({ super.key, - required this.ctr, + required this.homeController, }); - final HomeController? ctr; + final HomeController homeController; @override Widget build(BuildContext context) { @@ -405,7 +397,10 @@ class SearchBar extends StatelessWidget { splashColor: colorScheme.primaryContainer.withOpacity(0.3), onTap: () => Get.toNamed( '/search', - parameters: {'hintText': ctr!.defaultSearch.value}, + parameters: { + if (homeController.enableSearchWord) + 'hintText': homeController.defaultSearch.value, + }, ), child: Row( children: [ @@ -416,16 +411,19 @@ class SearchBar extends StatelessWidget { semanticLabel: '搜索', ), const SizedBox(width: 10), - Expanded( - child: Obx( - () => Text( - ctr!.defaultSearch.value, - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: TextStyle(color: colorScheme.outline), + if (homeController.enableSearchWord) ...[ + Expanded( + child: Obx( + () => Text( + homeController.defaultSearch.value, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle(color: colorScheme.outline), + ), ), ), - ), + const SizedBox(width: 2), + ], ], ), ), diff --git a/lib/pages/main/view.dart b/lib/pages/main/view.dart index b60bd7f0a..813d2fa0a 100644 --- a/lib/pages/main/view.dart +++ b/lib/pages/main/view.dart @@ -55,6 +55,7 @@ class _MainAppState extends State @override void didPopNext() { _mainController.checkUnreadDynamic(); + _checkDefaultSearch(true); super.didPopNext(); } @@ -62,6 +63,22 @@ class _MainAppState extends State void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.resumed) { _mainController.checkUnreadDynamic(); + _checkDefaultSearch(true); + } + } + + void _checkDefaultSearch([bool shouldCheck = false]) { + if (shouldCheck && + _mainController.pages[_mainController.pageController.page?.round() ?? 0] + is! HomePage) { + return; + } + if (_homeController.enableSearchWord) { + int now = DateTime.now().millisecondsSinceEpoch; + if (now - _homeController.lateCheckAt >= 5 * 60 * 1000) { + _homeController.lateCheckAt = now; + _homeController.querySearchDefault(); + } } } @@ -80,6 +97,7 @@ class _MainAppState extends State _lastSelectTime = DateTime.now().millisecondsSinceEpoch; } _homeController.flag = true; + _checkDefaultSearch(); } else { _homeController.flag = false; }