Skip to content

Commit

Permalink
Feat: Delete history function
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoMint committed Aug 23, 2023
1 parent 79f1f12 commit 3593961
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 13 deletions.
7 changes: 5 additions & 2 deletions assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
7 changes: 5 additions & 2 deletions assets/i18n/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
8 changes: 6 additions & 2 deletions lib/pages/home/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion lib/pages/home/pages/favorites_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class _FavoritesPageState extends fluent.State<FavoritesPage> {

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,
Expand Down
131 changes: 125 additions & 6 deletions lib/pages/home/widgets/home_resent_card.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -25,7 +31,8 @@ class HomeRecentCard extends StatefulWidget {
class _HomeRecentCardState extends State<HomeRecentCard> {
late ExtensionRuntime? _runtime;
String _update = "";

final contextController = fluent.FlyoutController();
final contextAttachKey = GlobalKey();
// 主要颜色
Color? primaryColor;

Expand Down Expand Up @@ -67,6 +74,19 @@ class _HomeRecentCardState extends State<HomeRecentCard> {
}
}

_delete() async {
await DatabaseUtils.deleteHistoryByPackageAndUrl(
widget.history.package,
widget.history.url,
);
Get.find<HomePageController>().refreshHistory();
}

_delectAll() async {
await DatabaseUtils.deleteAllHistory();
Get.find<HomePageController>().refreshHistory();
}

Widget _bangumiCard() {
return Container(
width: 350,
Expand Down Expand Up @@ -111,7 +131,13 @@ class _HomeRecentCardState extends State<HomeRecentCard> {
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,
Expand Down Expand Up @@ -194,7 +220,13 @@ class _HomeRecentCardState extends State<HomeRecentCard> {
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,
Expand All @@ -217,8 +249,7 @@ class _HomeRecentCardState extends State<HomeRecentCard> {
);
}

@override
Widget build(BuildContext context) {
Widget _buildWidget() {
return Padding(
padding: const EdgeInsets.only(right: 8),
child: MouseRegion(
Expand Down Expand Up @@ -251,4 +282,92 @@ class _HomeRecentCardState extends State<HomeRecentCard> {
),
);
}

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,
);
}
}
17 changes: 17 additions & 0 deletions lib/utils/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ class DatabaseUtils {
return db.writeTxn(() => db.historys.putByIndex(r'package&url', history));
}

// 删除历史
static Future<void> deleteHistoryByPackageAndUrl(
String package, String url) async {
return db.writeTxn(
() => db.historys
.filter()
.packageEqualTo(package)
.urlEqualTo(url)
.deleteAll(),
);
}

// 删除全部历史
static Future<void> deleteAllHistory() async {
return db.writeTxn(() => db.historys.where().deleteAll());
}

// 扩展设置
// 获取扩展设置
static Future<List<ExtensionSetting>> getExtensionSettings(String package) {
Expand Down

0 comments on commit 3593961

Please sign in to comment.