From e9aa6fcf30624aed52c463f99975f7cda11bd46e Mon Sep 17 00:00:00 2001 From: nyne Date: Thu, 12 Dec 2024 18:00:58 +0800 Subject: [PATCH] download comics in local favorites page --- assets/translation.json | 8 +- lib/foundation/local.dart | 2 +- lib/pages/favorites/favorites_page.dart | 2 + lib/pages/favorites/local_favorites_page.dart | 112 +++++++++++++----- 4 files changed, 93 insertions(+), 31 deletions(-) diff --git a/assets/translation.json b/assets/translation.json index 4cd82c7..974be32 100644 --- a/assets/translation.json +++ b/assets/translation.json @@ -249,7 +249,9 @@ "Export as pdf": "导出为pdf", "Export as epub": "导出为epub", "Aggregated Search": "聚合搜索", - "No search results found": "未找到搜索结果" + "No search results found": "未找到搜索结果", + "Added @c comics to download queue." : "已添加 @c 本漫画到下载队列", + "Download started": "下载已开始" }, "zh_TW": { "Home": "首頁", @@ -501,6 +503,8 @@ "Export as pdf": "匯出為pdf", "Export as epub": "匯出為epub", "Aggregated Search": "聚合搜索", - "No search results found": "未找到搜索結果" + "No search results found": "未找到搜索結果", + "Added @c comics to download queue." : "已添加 @c 本漫畫到下載隊列", + "Download started": "下載已開始" } } \ No newline at end of file diff --git a/lib/foundation/local.dart b/lib/foundation/local.dart index 1759102..865849e 100644 --- a/lib/foundation/local.dart +++ b/lib/foundation/local.dart @@ -389,7 +389,7 @@ class LocalManager with ChangeNotifier { return files.map((e) => "file://${e.path}").toList(); } - Future isDownloaded(String id, ComicType type, [int? ep]) async { + bool isDownloaded(String id, ComicType type, [int? ep]) { var comic = find(id, type); if (comic == null) return false; if (comic.chapters == null || ep == null) return true; diff --git a/lib/pages/favorites/favorites_page.dart b/lib/pages/favorites/favorites_page.dart index d31174d..16f23dc 100644 --- a/lib/pages/favorites/favorites_page.dart +++ b/lib/pages/favorites/favorites_page.dart @@ -10,7 +10,9 @@ import 'package:venera/foundation/comic_source/comic_source.dart'; import 'package:venera/foundation/comic_type.dart'; import 'package:venera/foundation/consts.dart'; import 'package:venera/foundation/favorites.dart'; +import 'package:venera/foundation/local.dart'; import 'package:venera/foundation/res.dart'; +import 'package:venera/network/download.dart'; import 'package:venera/pages/comic_page.dart'; import 'package:venera/utils/io.dart'; import 'package:venera/utils/translations.dart'; diff --git a/lib/pages/favorites/local_favorites_page.dart b/lib/pages/favorites/local_favorites_page.dart index 2c7bb7e..44049d3 100644 --- a/lib/pages/favorites/local_favorites_page.dart +++ b/lib/pages/favorites/local_favorites_page.dart @@ -53,23 +53,57 @@ class _LocalFavoritesPageState extends State<_LocalFavoritesPage> { super.initState(); } + void selectAll() { + setState(() { + selectedComics = comics.asMap().map((k, v) => MapEntry(v, true)); + }); + } + + void invertSelection() { + setState(() { + comics.asMap().forEach((k, v) { + selectedComics[v] = !selectedComics.putIfAbsent(v, () => false); + }); + selectedComics.removeWhere((k, v) => !v); + }); + } + + bool downloadComic(FavoriteItem c) { + var source = c.type.comicSource; + if (source != null) { + bool isDownloaded = LocalManager().isDownloaded( + c.id, + (c).type, + ); + if (isDownloaded) { + return false; + } + LocalManager().addTask(ImagesDownloadTask( + source: source, + comicId: c.id, + comicTitle: c.title, + )); + return true; + } + return false; + } + + void downloadSelected() { + int count = 0; + for (var c in selectedComics.keys) { + if (downloadComic(c as FavoriteItem)) { + count++; + } + } + if (count > 0) { + context.showMessage( + message: "Added @c comics to download queue.".tlParams({"c": count}), + ); + } + } + @override Widget build(BuildContext context) { - void selectAll() { - setState(() { - selectedComics = comics.asMap().map((k, v) => MapEntry(v, true)); - }); - } - - void invertSelection() { - setState(() { - comics.asMap().forEach((k, v) { - selectedComics[v] = !selectedComics.putIfAbsent(v, () => false); - }); - selectedComics.removeWhere((k, v) => !v); - }); - } - var body = Scaffold( body: SmoothCustomScrollView(slivers: [ if (!searchMode && !multiSelectMode) @@ -300,6 +334,11 @@ class _LocalFavoritesPageState extends State<_LocalFavoritesPage> { }, ); }), + MenuEntry( + icon: Icons.download, + text: "Download".tl, + onClick: downloadSelected, + ), ]), ], ) @@ -336,6 +375,20 @@ class _LocalFavoritesPageState extends State<_LocalFavoritesPage> { SliverGridComics( comics: comics, selections: selectedComics, + menuBuilder: (c) { + return [ + MenuEntry( + icon: Icons.download, + text: "Download".tl, + onClick: () { + downloadComic(c as FavoriteItem); + context.showMessage( + message: "Download started".tl, + ); + }, + ), + ]; + }, onTap: multiSelectMode ? (c) { setState(() { @@ -425,7 +478,7 @@ class _LocalFavoritesPageState extends State<_LocalFavoritesPage> { padding: EdgeInsets.only(bottom: context.padding.bottom + 16), child: Container( constraints: - const BoxConstraints(maxHeight: 700, maxWidth: 500), + const BoxConstraints(maxHeight: 700, maxWidth: 500), child: Column( children: [ Expanded( @@ -443,7 +496,7 @@ class _LocalFavoritesPageState extends State<_LocalFavoritesPage> { targetFolders = LocalFavoritesManager() .folderNames .where((folder) => - folder != favPage.folder) + folder != favPage.folder) .toList(); }); }); @@ -482,14 +535,14 @@ class _LocalFavoritesPageState extends State<_LocalFavoritesPage> { onChanged: disabled ? null : (v) { - setState(() { - if (v!) { - selectedLocalFolders.add(folder); - } else { - selectedLocalFolders.remove(folder); - } - }); - }, + setState(() { + if (v!) { + selectedLocalFolders.add(folder); + } else { + selectedLocalFolders.remove(folder); + } + }); + }, ); }, ), @@ -597,9 +650,12 @@ class _ReorderComicsPageState extends State<_ReorderComicsPage> { } Color lightenColor(Color color, double lightenValue) { - int red = (_floatToInt8(color.r) + ((255 - color.r) * lightenValue)).round(); - int green = (_floatToInt8(color.g) * 255 + ((255 - color.g) * lightenValue)).round(); - int blue = (_floatToInt8(color.b) * 255 + ((255 - color.b) * lightenValue)).round(); + int red = + (_floatToInt8(color.r) + ((255 - color.r) * lightenValue)).round(); + int green = (_floatToInt8(color.g) * 255 + ((255 - color.g) * lightenValue)) + .round(); + int blue = (_floatToInt8(color.b) * 255 + ((255 - color.b) * lightenValue)) + .round(); return Color.fromARGB(_floatToInt8(color.a), red, green, blue); }