From 4920db4138cac6ed435b225633b11d775ca60af9 Mon Sep 17 00:00:00 2001 From: violet-dev Date: Sat, 3 Feb 2024 18:30:46 +0900 Subject: [PATCH] Import/Export crop bookmark --- lib/database/user/bookmark.dart | 7 +++ lib/pages/bookmark/crop_bookmark.dart | 69 ++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/lib/database/user/bookmark.dart b/lib/database/user/bookmark.dart index a6299387a..304799ae5 100644 --- a/lib/database/user/bookmark.dart +++ b/lib/database/user/bookmark.dart @@ -127,6 +127,13 @@ class BookmarkCropImage { var db = await CommonUserDatabase.getInstance(); await db.update('BookmarkCropImage', result, 'Id=?', [id()]); } + + Map toJson() => { + 'article': article(), + 'page': page(), + 'aspectRatio': aspectRatio(), + 'area': area(), + }; } class Bookmark { diff --git a/lib/pages/bookmark/crop_bookmark.dart b/lib/pages/bookmark/crop_bookmark.dart index 727f6516e..a1229990e 100644 --- a/lib/pages/bookmark/crop_bookmark.dart +++ b/lib/pages/bookmark/crop_bookmark.dart @@ -2,11 +2,13 @@ // Copyright (C) 2020-2024. violet-team. Licensed under the Apache-2.0 License. import 'dart:async'; +import 'dart:convert'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; +import 'package:fluttertoast/fluttertoast.dart'; import 'package:material_design_icons_flutter/material_design_icons_flutter.dart'; import 'package:provider/provider.dart'; import 'package:pull_down_button/pull_down_button.dart'; @@ -15,6 +17,7 @@ import 'package:violet/component/hentai.dart'; import 'package:violet/component/image_provider.dart'; import 'package:violet/database/user/bookmark.dart'; import 'package:violet/database/user/record.dart'; +import 'package:violet/log/log.dart'; import 'package:violet/other/dialogs.dart'; import 'package:violet/pages/common/utils.dart'; import 'package:violet/pages/viewer/viewer_page.dart'; @@ -24,6 +27,7 @@ import 'package:violet/settings/settings.dart'; import 'package:violet/util/evict_image_urls.dart'; import 'package:violet/widgets/article_item/image_provider_manager.dart'; import 'package:violet/widgets/cupertino_switch_list_tile.dart'; +import 'package:violet/widgets/toast.dart'; import 'package:violet/widgets/v_cached_network_image.dart'; class CropBookmarkPage extends StatefulWidget { @@ -253,14 +257,75 @@ class _CropBookmarkPageState extends State { PullDownMenuActionsRow.medium( items: [ PullDownMenuItem( - onTap: () {}, title: 'Export', icon: CupertinoIcons.arrowshape_turn_up_left, + onTap: () async { + final crops = + await (await Bookmark.getInstance()).getCropImages(); + await showOkDialog( + context, jsonEncode(crops), 'Export Crop Bookmarks'); + }, ), PullDownMenuItem( - onTap: () {}, title: 'Import', icon: CupertinoIcons.square_arrow_down, + onTap: () async { + final text = TextEditingController(); + if (await showOkCancelDialog( + titleText: 'Import Crop Bookmarks', + context: context, + contentPadding: const EdgeInsets.fromLTRB(12, 0, 12, 0), + contentBuilder: (_) => TextField( + controller: text, + autofocus: true, + minLines: 3, + maxLines: 999, + ), + )) { + try { + var arr = jsonDecode(text.text); + if (arr is Map) { + arr = [arr]; + } + + final bookmark = await Bookmark.getInstance(); + + for (var e in arr as List) { + final elem = e as Map; + await bookmark.insertCropImage(elem['article'], + elem['page'], elem['area'], elem['aspectRatio']); + } + + FToast ftoast = FToast(); + ftoast.init(context); + ftoast.showToast( + child: const ToastWrapper( + isCheck: true, + isWarning: false, + msg: 'Successful Importing!', + ), + ignorePointer: true, + gravity: ToastGravity.BOTTOM, + toastDuration: const Duration(seconds: 4), + ); + setState(() {}); + } catch (e) { + FToast ftoast = FToast(); + ftoast.init(context); + ftoast.showToast( + child: const ToastWrapper( + isCheck: false, + isWarning: false, + msg: 'Import Error! Check Log!', + ), + ignorePointer: true, + gravity: ToastGravity.BOTTOM, + toastDuration: const Duration(seconds: 4), + ); + Logger.error('[Import Crop] $e'); + } + } + }, ), ], ),