From 3593961f94f19803bdfe70f7fe1290993c64a14c Mon Sep 17 00:00:00 2001 From: MiaoMint <1981324730@qq.com> Date: Wed, 23 Aug 2023 23:26:41 +0800 Subject: [PATCH] Feat: Delete history function --- assets/i18n/en.json | 7 +- assets/i18n/zh.json | 7 +- lib/pages/home/controller.dart | 8 +- lib/pages/home/pages/favorites_page.dart | 3 +- lib/pages/home/widgets/home_resent_card.dart | 131 ++++++++++++++++++- lib/utils/database.dart | 17 +++ 6 files changed, 160 insertions(+), 13 deletions(-) diff --git a/assets/i18n/en.json b/assets/i18n/en.json index b20e604c..6a4b5bed 100644 --- a/assets/i18n/en.json +++ b/assets/i18n/en.json @@ -31,13 +31,16 @@ "retry": "Retry", "next": "Next", "previous": "Previous", - "show-all": "Show all" + "show-all": "Show all", + "delete": "Delete", + "delete-all": "Delete all" }, "home": { "continue-watching": "Continue", "favorite": "Favorite", - "no-record": "No favorites or viewing records" + "no-record": "No favorites or viewing records", + "watched":"Watched {ep}" }, "search": { diff --git a/assets/i18n/zh.json b/assets/i18n/zh.json index 65ee4233..d8473c65 100644 --- a/assets/i18n/zh.json +++ b/assets/i18n/zh.json @@ -22,13 +22,16 @@ "retry": "重试", "next": "下一个", "previous": "上一个", - "show-all": "显示全部" + "show-all": "显示全部", + "delete": "删除", + "delete-all": "删除全部" }, "home": { "continue-watching": "继续观看", "favorite": "收藏", - "no-record": "暂无收藏和观看记录" + "no-record": "暂无收藏和观看记录", + "watched":"看到 {ep}" }, "search": { diff --git a/lib/pages/home/controller.dart b/lib/pages/home/controller.dart index 8768704b..f93f79f2 100644 --- a/lib/pages/home/controller.dart +++ b/lib/pages/home/controller.dart @@ -15,12 +15,16 @@ class HomePageController extends GetxController { super.onInit(); } - onRefresh() async { + refreshHistory() async { resents.clear(); - favorites.clear(); resents.addAll( await DatabaseUtils.getHistorysByType(), ); + } + + onRefresh() async { + favorites.clear(); + await refreshHistory(); favorites.addAll({ ExtensionType.bangumi: await DatabaseUtils.getFavoritesByType( type: ExtensionType.bangumi, diff --git a/lib/pages/home/pages/favorites_page.dart b/lib/pages/home/pages/favorites_page.dart index ce026cf6..0ebc6b4f 100644 --- a/lib/pages/home/pages/favorites_page.dart +++ b/lib/pages/home/pages/favorites_page.dart @@ -120,7 +120,8 @@ class _FavoritesPageState extends fluent.State { return LayoutBuilder( builder: ((context, constraints) => GridView.builder( - padding: const EdgeInsets.all(8), + padding: + const EdgeInsets.only(right: 8, bottom: 8, top: 8), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: constraints.maxWidth ~/ 160, childAspectRatio: 0.6, diff --git a/lib/pages/home/widgets/home_resent_card.dart b/lib/pages/home/widgets/home_resent_card.dart index 2f21b294..34cd9ae0 100644 --- a/lib/pages/home/widgets/home_resent_card.dart +++ b/lib/pages/home/widgets/home_resent_card.dart @@ -1,14 +1,20 @@ import 'dart:io'; import 'package:cached_network_image/cached_network_image.dart'; -import 'package:fluent_ui/fluent_ui.dart'; +import 'package:fluent_ui/fluent_ui.dart' as fluent; +import 'package:flutter/material.dart'; +import 'package:flutter_i18n/flutter_i18n.dart'; import 'package:get/get.dart'; import 'package:miru_app/models/extension.dart'; import 'package:miru_app/models/history.dart'; import 'package:miru_app/pages/detail/view.dart'; +import 'package:miru_app/pages/home/controller.dart'; import 'package:miru_app/router/router.dart'; +import 'package:miru_app/utils/database.dart'; import 'package:miru_app/utils/extension.dart'; import 'package:miru_app/utils/extension_runtime.dart'; +import 'package:miru_app/utils/i18n.dart'; +import 'package:miru_app/widgets/platform_widget.dart'; import 'package:palette_generator/palette_generator.dart'; class HomeRecentCard extends StatefulWidget { @@ -25,7 +31,8 @@ class HomeRecentCard extends StatefulWidget { class _HomeRecentCardState extends State { late ExtensionRuntime? _runtime; String _update = ""; - + final contextController = fluent.FlyoutController(); + final contextAttachKey = GlobalKey(); // 主要颜色 Color? primaryColor; @@ -67,6 +74,19 @@ class _HomeRecentCardState extends State { } } + _delete() async { + await DatabaseUtils.deleteHistoryByPackageAndUrl( + widget.history.package, + widget.history.url, + ); + Get.find().refreshHistory(); + } + + _delectAll() async { + await DatabaseUtils.deleteAllHistory(); + Get.find().refreshHistory(); + } + Widget _bangumiCard() { return Container( width: 350, @@ -111,7 +131,13 @@ class _HomeRecentCardState extends State { overflow: TextOverflow.ellipsis, ), Text( - "看到 ${widget.history.episodeTitle}", + FlutterI18n.translate( + context, + "home.watched", + translationParams: { + "ep": widget.history.episodeTitle, + }, + ), style: const TextStyle( color: Colors.white, fontSize: 12, @@ -194,7 +220,13 @@ class _HomeRecentCardState extends State { overflow: TextOverflow.ellipsis, ), Text( - "看到 ${widget.history.episodeTitle}", + FlutterI18n.translate( + context, + "home.watched", + translationParams: { + "ep": widget.history.episodeTitle, + }, + ), style: const TextStyle( color: Colors.white, fontSize: 12, @@ -217,8 +249,7 @@ class _HomeRecentCardState extends State { ); } - @override - Widget build(BuildContext context) { + Widget _buildWidget() { return Padding( padding: const EdgeInsets.only(right: 8), child: MouseRegion( @@ -251,4 +282,92 @@ class _HomeRecentCardState extends State { ), ); } + + Widget _buildAndroid(BuildContext context) { + return GestureDetector( + onLongPress: () { + showModalBottomSheet( + context: context, + builder: (context) { + return Column( + mainAxisSize: MainAxisSize.min, + children: [ + ListTile( + title: Text('common.delete'.i18n), + onTap: () { + _delete(); + Get.back(); + }, + ), + ListTile( + title: Text('common.delete-all'.i18n), + onTap: () { + _delectAll(); + Get.back(); + }, + ), + ], + ); + }, + ); + }, + child: _buildWidget(), + ); + } + + Widget _buildDesktop(BuildContext context) { + return GestureDetector( + onSecondaryTapUp: (d) { + final targetContext = contextAttachKey.currentContext; + if (targetContext == null) return; + final box = targetContext.findRenderObject() as RenderBox; + final position = box.localToGlobal( + d.localPosition, + ancestor: Navigator.of(context).context.findRenderObject(), + ); + contextController.showFlyout( + barrierColor: Colors.black.withOpacity(0.1), + position: position, + builder: (context) { + return fluent.FlyoutContent( + child: SizedBox( + width: 200, + child: fluent.CommandBar( + primaryItems: [ + fluent.CommandBarButton( + label: Text('common.delete'.i18n), + onPressed: () { + _delete(); + router.pop(); + }, + ), + fluent.CommandBarButton( + label: Text('common.delete-all'.i18n), + onPressed: () { + _delectAll(); + router.pop(); + }, + ), + ], + ), + ), + ); + }, + ); + }, + child: fluent.FlyoutTarget( + key: contextAttachKey, + controller: contextController, + child: _buildWidget(), + ), + ); + } + + @override + Widget build(BuildContext context) { + return PlatformBuildWidget( + androidBuilder: _buildAndroid, + desktopBuilder: _buildDesktop, + ); + } } diff --git a/lib/utils/database.dart b/lib/utils/database.dart index 6d27c6bf..a9310803 100644 --- a/lib/utils/database.dart +++ b/lib/utils/database.dart @@ -114,6 +114,23 @@ class DatabaseUtils { return db.writeTxn(() => db.historys.putByIndex(r'package&url', history)); } + // 删除历史 + static Future deleteHistoryByPackageAndUrl( + String package, String url) async { + return db.writeTxn( + () => db.historys + .filter() + .packageEqualTo(package) + .urlEqualTo(url) + .deleteAll(), + ); + } + + // 删除全部历史 + static Future deleteAllHistory() async { + return db.writeTxn(() => db.historys.where().deleteAll()); + } + // 扩展设置 // 获取扩展设置 static Future> getExtensionSettings(String package) {