Skip to content

Commit

Permalink
Import/Export crop bookmark
Browse files Browse the repository at this point in the history
  • Loading branch information
violet-dev committed Feb 3, 2024
1 parent 8f750dd commit 4920db4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/database/user/bookmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ class BookmarkCropImage {
var db = await CommonUserDatabase.getInstance();
await db.update('BookmarkCropImage', result, 'Id=?', [id()]);
}

Map<String, dynamic> toJson() => {
'article': article(),
'page': page(),
'aspectRatio': aspectRatio(),
'area': area(),
};
}

class Bookmark {
Expand Down
69 changes: 67 additions & 2 deletions lib/pages/bookmark/crop_bookmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -253,14 +257,75 @@ class _CropBookmarkPageState extends State<CropBookmarkPage> {
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<String, dynamic>) {
arr = <dynamic>[arr];
}

final bookmark = await Bookmark.getInstance();

for (var e in arr as List<dynamic>) {
final elem = e as Map<String, dynamic>;
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');
}
}
},
),
],
),
Expand Down

0 comments on commit 4920db4

Please sign in to comment.