From bdcd28488e8d74739b708775f733a7de3fbbbd7c Mon Sep 17 00:00:00 2001 From: NBA2K1 <78034913+NBA2K1@users.noreply.github.com> Date: Mon, 29 Dec 2025 01:58:49 +0100 Subject: [PATCH] Same with history_screen.dart - remove hideItems parameter - Make TabBar and TabBarView cleaner --- lib/modules/history/history_screen.dart | 78 +++++++++++-------------- 1 file changed, 34 insertions(+), 44 deletions(-) diff --git a/lib/modules/history/history_screen.dart b/lib/modules/history/history_screen.dart index 2a4d8151..62954aa7 100644 --- a/lib/modules/history/history_screen.dart +++ b/lib/modules/history/history_screen.dart @@ -35,6 +35,8 @@ class _HistoryScreenState extends ConsumerState with TickerProviderStateMixin { final _textEditingController = TextEditingController(); late TabController _tabBarController; + late List _visibleTabTypes; + late final List hideItems; void tabListener() { setState(() { @@ -46,13 +48,16 @@ class _HistoryScreenState extends ConsumerState @override void initState() { super.initState(); - final hideItems = ref.read(hideItemsStateProvider); - final tabCount = [ - if (!hideItems.contains("/MangaLibrary")) "/MangaLibrary", - if (!hideItems.contains("/AnimeLibrary")) "/AnimeLibrary", - if (!hideItems.contains("/NovelLibrary")) "/NovelLibrary", - ].length; - _tabBarController = TabController(length: tabCount, vsync: this); + hideItems = ref.read(hideItemsStateProvider); + _visibleTabTypes = [ + if (!hideItems.contains("/MangaLibrary")) ItemType.manga, + if (!hideItems.contains("/AnimeLibrary")) ItemType.anime, + if (!hideItems.contains("/NovelLibrary")) ItemType.novel, + ]; + _tabBarController = TabController( + length: _visibleTabTypes.length, + vsync: this, + ); _tabBarController.addListener(tabListener); } @@ -66,8 +71,18 @@ class _HistoryScreenState extends ConsumerState bool _isSearch = false; @override Widget build(BuildContext context) { - final hideItems = ref.watch(hideItemsStateProvider); final l10n = l10nLocalizations(context)!; + String localizedItemType(ItemType type) { + switch (type) { + case ItemType.manga: + return l10n.manga; + case ItemType.anime: + return l10n.anime; + case ItemType.novel: + return l10n.novel; + } + } + return Scaffold( appBar: AppBar( elevation: 0, @@ -128,7 +143,7 @@ class _HistoryScreenState extends ConsumerState TextButton( onPressed: () async { if (mounted) Navigator.pop(context); - await _clearHistory(hideItems); + await _clearHistory(); }, child: Text(l10n.ok), ), @@ -148,57 +163,32 @@ class _HistoryScreenState extends ConsumerState bottom: TabBar( indicatorSize: TabBarIndicatorSize.tab, controller: _tabBarController, - tabs: [ - if (!hideItems.contains("/MangaLibrary")) Tab(text: l10n.manga), - if (!hideItems.contains("/AnimeLibrary")) Tab(text: l10n.anime), - if (!hideItems.contains("/NovelLibrary")) Tab(text: l10n.novel), - ], + tabs: _visibleTabTypes.map((type) { + return Tab(text: localizedItemType(type)); + }).toList(), ), ), body: TabBarView( controller: _tabBarController, - children: [ - if (!hideItems.contains("/MangaLibrary")) - HistoryTab( - itemType: ItemType.manga, - query: _textEditingController.text, - ), - if (!hideItems.contains("/AnimeLibrary")) - HistoryTab( - itemType: ItemType.anime, - query: _textEditingController.text, - ), - if (!hideItems.contains("/NovelLibrary")) - HistoryTab( - itemType: ItemType.novel, - query: _textEditingController.text, - ), - ], + children: _visibleTabTypes.map((type) { + return HistoryTab(itemType: type, query: _textEditingController.text); + }).toList(), ), ); } - Future _clearHistory(List hideItems) async { + Future _clearHistory() async { List histories = await isar.historys .filter() .idIsNotNull() - .chapter( - (q) => - q.manga((q) => q.itemTypeEqualTo(getCurrentItemType(hideItems))), - ) + .chapter((q) => q.manga((q) => q.itemTypeEqualTo(getCurrentItemType()))) .findAll(); final List idsToDelete = histories.map((h) => h.id!).toList(); await isar.writeTxn(() => isar.historys.deleteAll(idsToDelete)); } - ItemType getCurrentItemType(List hideItems) { - return _tabBarController.index == 0 && !hideItems.contains("/MangaLibrary") - ? ItemType.manga - : _tabBarController.index == - 1 - (hideItems.contains("/MangaLibrary") ? 1 : 0) && - !hideItems.contains("/AnimeLibrary") - ? ItemType.anime - : ItemType.novel; + ItemType getCurrentItemType() { + return _visibleTabTypes[_tabBarController.index]; } }