Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting covers from web #545

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion assets/translations/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,7 @@
"default_books_format": "Default books format",
"cover_still_downloaded": "Cover is still being downloaded",
"wait_for_downloading_to_finish": "Wait for downloading to finish",
"save_without_cover": "Save without the cover"
"save_without_cover": "Save without the cover",
"search_online_for_cover": "Search online for the cover",
"book_cover": "book cover"
}
3 changes: 3 additions & 0 deletions lib/core/constants/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ class Constants {
static const blurHashY = 2;

static const maxTagLength = 100;

static const duckDuckGoURL = 'https://duckduckgo.com/';
static const duckDuckGoImagesURL = 'https://duckduckgo.com/i.js';
}
47 changes: 47 additions & 0 deletions lib/core/helpers/helpers.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:blurhash/blurhash.dart' as blurhash;
import 'package:image_cropper/image_cropper.dart';
import 'package:openreads/core/constants/constants.dart';
import 'package:openreads/generated/locale_keys.g.dart';

import 'package:openreads/logic/cubit/edit_book_cubit.dart';
import 'package:openreads/main.dart';
import 'package:openreads/model/book.dart';

Future generateBlurHash(Uint8List bytes, BuildContext context) async {
Expand Down Expand Up @@ -43,3 +49,44 @@ DateTime? getLatestStartDate(Book book) {
.map((e) => e.startDate)
.reduce((value, element) => value!.isAfter(element!) ? value : element);
}

Future<CroppedFile?> cropImage(BuildContext context, Uint8List cover) async {
final colorScheme = Theme.of(context).colorScheme;

//write temporary file as ImageCropper requires a file
final tmpCoverTimestamp = DateTime.now().millisecondsSinceEpoch;
final tmpCoverFile = File(
'${appTempDirectory.path}/$tmpCoverTimestamp.jpg',
);
await tmpCoverFile.writeAsBytes(cover);

return await ImageCropper().cropImage(
maxWidth: 1024,
maxHeight: 1024,
sourcePath: tmpCoverFile.path,
compressQuality: 90,
uiSettings: [
AndroidUiSettings(
toolbarTitle: LocaleKeys.edit_cover.tr(),
toolbarColor: Colors.black,
statusBarColor: Colors.black,
toolbarWidgetColor: Colors.white,
backgroundColor: colorScheme.surface,
cropGridColor: Colors.black87,
activeControlsWidgetColor: colorScheme.primary,
cropFrameColor: Colors.black87,
lockAspectRatio: false,
hideBottomControls: false,
),
IOSUiSettings(
title: LocaleKeys.edit_cover.tr(),
cancelButtonTitle: LocaleKeys.cancel.tr(),
doneButtonTitle: LocaleKeys.save.tr(),
rotateButtonsHidden: false,
rotateClockwiseButtonHidden: true,
aspectRatioPickerButtonHidden: false,
aspectRatioLockDimensionSwapEnabled: false,
),
],
);
}
2 changes: 2 additions & 0 deletions lib/generated/locale_keys.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,6 @@ abstract class LocaleKeys {
static const coverStillDownloaded = 'cover_still_downloaded';
static const waitForDownloadingToFinish = 'wait_for_downloading_to_finish';
static const saveWithoutCover = 'save_without_cover';
static const searchOnlineForCover = 'search_online_for_cover';
static const bookCover = 'book_cover';
}
4 changes: 4 additions & 0 deletions lib/ui/add_book_screen/widgets/book_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class _BookTextFieldState extends State<BookTextField> {
void initState() {
super.initState();

if (widget.controller.text.isNotEmpty) {
showClearButton = true;
}

widget.controller.addListener(() {
setState(() {
if (widget.controller.text.isNotEmpty) {
Expand Down
64 changes: 39 additions & 25 deletions lib/ui/add_book_screen/widgets/cover_placeholder.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dotted_border/dotted_border.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:openreads/core/themes/app_theme.dart';
Expand All @@ -17,32 +18,45 @@ class CoverPlaceholder extends StatelessWidget {
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: InkWell(
customBorder: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(cornerRadius),
),
onTap: onPressed,
child: Ink(
height: defaultHeight + 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(cornerRadius),
color: Theme.of(context).colorScheme.primary.withOpacity(0.3),
border: Border.all(color: dividerColor),
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 10),
child: Center(
child: Row(
children: [
const SizedBox(width: 8),
Icon(
Icons.image,
size: 24,
color: Theme.of(context).colorScheme.primary,
child: DottedBorder(
borderType: BorderType.RRect,
radius: Radius.circular(cornerRadius),
dashPattern: const [10, 8],
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.3),
strokeWidth: 2,
child: Padding(
padding: const EdgeInsets.all(10),
child: InkWell(
customBorder: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(cornerRadius),
),
onTap: onPressed,
child: Ink(
height: defaultHeight + 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(cornerRadius),
color: Theme.of(context).colorScheme.primary.withOpacity(0.1),
border: Border.all(color: dividerColor),
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 0,
vertical: 10,
),
child: Center(
child: Row(
children: [
const SizedBox(width: 8),
Icon(
Icons.image,
size: 24,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 15),
Text(LocaleKeys.click_to_add_cover.tr()),
],
),
const SizedBox(width: 15),
Text(LocaleKeys.click_to_add_cover.tr()),
],
),
),
),
),
Expand Down
84 changes: 36 additions & 48 deletions lib/ui/add_book_screen/widgets/cover_view_edit.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:io';
// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:image/image.dart' as img;
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:blurhash_dart/blurhash_dart.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
Expand All @@ -19,6 +19,7 @@ import 'package:openreads/generated/locale_keys.g.dart';
import 'package:openreads/logic/cubit/edit_book_cubit.dart';
import 'package:openreads/main.dart';
import 'package:openreads/ui/add_book_screen/widgets/widgets.dart';
import 'package:openreads/ui/search_covers_screen/search_covers_screen.dart';

class CoverViewEdit extends StatefulWidget {
const CoverViewEdit({super.key});
Expand Down Expand Up @@ -54,7 +55,10 @@ class _CoverViewEditState extends State<CoverViewEdit> {
return;
}

final croppedPhoto = await _cropImage(await photoXFile.readAsBytes());
final croppedPhoto = await cropImage(
context,
await photoXFile.readAsBytes(),
);

if (croppedPhoto == null) {
_setCoverLoading(false);
Expand All @@ -80,9 +84,15 @@ class _CoverViewEditState extends State<CoverViewEdit> {
_setCoverLoading(false);
}

void _editCurrentCover(BuildContext context) async {
void _editCurrentCover({
required BuildContext context,
bool pop = true,
}) async {
_setCoverLoading(true);
Navigator.of(context).pop();

if (pop) {
Navigator.of(context).pop();
}

final cover = context.read<EditBookCoverCubit>().state;

Expand All @@ -91,7 +101,7 @@ class _CoverViewEditState extends State<CoverViewEdit> {
return;
}

final croppedPhoto = await _cropImage(cover);
final croppedPhoto = await cropImage(context, cover);

if (croppedPhoto == null) {
_setCoverLoading(false);
Expand All @@ -118,47 +128,6 @@ class _CoverViewEditState extends State<CoverViewEdit> {
_setCoverLoading(false);
}

Future<CroppedFile?> _cropImage(Uint8List cover) async {
final colorScheme = Theme.of(context).colorScheme;

//write temporary file as ImageCropper requires a file
final tmpCoverTimestamp = DateTime.now().millisecondsSinceEpoch;
final tmpCoverFile = File(
'${appTempDirectory.path}/$tmpCoverTimestamp.jpg',
);
await tmpCoverFile.writeAsBytes(cover);

return await ImageCropper().cropImage(
maxWidth: 1024,
maxHeight: 1024,
sourcePath: tmpCoverFile.path,
compressQuality: 90,
uiSettings: [
AndroidUiSettings(
toolbarTitle: LocaleKeys.edit_cover.tr(),
toolbarColor: Colors.black,
statusBarColor: Colors.black,
toolbarWidgetColor: Colors.white,
backgroundColor: colorScheme.surface,
cropGridColor: Colors.black87,
activeControlsWidgetColor: colorScheme.primary,
cropFrameColor: Colors.black87,
lockAspectRatio: false,
hideBottomControls: false,
),
IOSUiSettings(
title: LocaleKeys.edit_cover.tr(),
cancelButtonTitle: LocaleKeys.cancel.tr(),
doneButtonTitle: LocaleKeys.save.tr(),
rotateButtonsHidden: false,
rotateClockwiseButtonHidden: true,
aspectRatioPickerButtonHidden: false,
aspectRatioLockDimensionSwapEnabled: false,
),
],
);
}

_deleteCover(BuildContext context) async {
_setCoverLoading(true);

Expand Down Expand Up @@ -205,6 +174,19 @@ class _CoverViewEditState extends State<CoverViewEdit> {
_setCoverLoading(false);
}

_searchForCoverOnline(BuildContext context) async {
Navigator.of(context).pop();

Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SearchCoversScreen(
book: context.read<EditBookCubit>().state,
),
),
);
}

showCoverLoadBottomSheet(BuildContext context) {
FocusManager.instance.primaryFocus?.unfocus();

Expand Down Expand Up @@ -238,6 +220,12 @@ class _CoverViewEditState extends State<CoverViewEdit> {
onPressed: () => _loadCoverFromStorage(context),
),
const SizedBox(height: 15),
CoverOptionButton(
text: LocaleKeys.searchOnlineForCover.tr(),
icon: FontAwesomeIcons.magnifyingGlass,
onPressed: () => _searchForCoverOnline(context),
),
const SizedBox(height: 15),
CoverOptionButton(
text: LocaleKeys.get_cover_from_open_library.tr(),
icon: FontAwesomeIcons.globe,
Expand All @@ -253,7 +241,7 @@ class _CoverViewEditState extends State<CoverViewEdit> {
text: LocaleKeys.edit_current_cover.tr(),
icon: FontAwesomeIcons.image,
onPressed: () => _editCurrentCover(
context,
context: context,
),
),
],
Expand Down
Loading
Loading