From 9fcd0a7e7aecdc6f116e96c1d4728253728d38a6 Mon Sep 17 00:00:00 2001 From: Ahmed Hnewa <73608287+ahmedhnewa@users.noreply.github.com> Date: Wed, 4 Oct 2023 21:33:22 +0300 Subject: [PATCH] Fix my issues and changes (#1417) * Add a event that triggers after removing the image from the editor && delete unused dependencies and upgrade all packages and plugins and remove gallery_saver which has not been updated for more than 23 months, it was a great plugin but it old now, and I also add some simple documentation and other minor improvements * I have add a documentation comments to flutter_quill_extensions, add new event to allow the user to confirm removing the image before actually remove it, translated some text in Arabic languague since it was incorrect or missing * Fix analyzer error * Switch back to gal and more changes * Remove required parameters --- .../lib/embeds/builders.dart | 27 ++++++++++++------- .../lib/embeds/utils.dart | 17 ++++++------ .../lib/flutter_quill_extensions.dart | 17 ++---------- flutter_quill_extensions/pubspec.yaml | 2 +- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/flutter_quill_extensions/lib/embeds/builders.dart b/flutter_quill_extensions/lib/embeds/builders.dart index a8fdb470a..58ad4ff88 100644 --- a/flutter_quill_extensions/lib/embeds/builders.dart +++ b/flutter_quill_extensions/lib/embeds/builders.dart @@ -11,8 +11,7 @@ import 'package:math_keyboard/math_keyboard.dart'; import 'package:universal_html/html.dart' as html; import '../shims/dart_ui_fake.dart' - if (dart.library.html) 'package:flutter_quill_extensions/shims/dart_ui_real.dart' - as ui; + if (dart.library.html) '../shims/dart_ui_real.dart' as ui; import 'embed_types.dart'; import 'utils.dart'; import 'widgets/image.dart'; @@ -22,11 +21,11 @@ import 'widgets/youtube_video_app.dart'; class ImageEmbedBuilder extends EmbedBuilder { ImageEmbedBuilder({ - required this.afterRemoveImageFromEditor, - required this.shouldRemoveImageFromEditor, + this.afterRemoveImageFromEditor, + this.shouldRemoveImageFromEditor, }); - final ImageEmbedBuilderAfterRemoveImageFromEditor afterRemoveImageFromEditor; - final ImageEmbedBuilderShouldRemoveImageFromEditor + final ImageEmbedBuilderAfterRemoveImageFromEditor? afterRemoveImageFromEditor; + final ImageEmbedBuilderShouldRemoveImageFromEditor? shouldRemoveImageFromEditor; @override @@ -136,8 +135,15 @@ class ImageEmbedBuilder extends EmbedBuilder { Navigator.of(context).pop(); final imageFile = File(imageUrl); - final shouldRemoveImage = - await shouldRemoveImageFromEditor(imageFile); + + final shouldRemoveImageEvent = shouldRemoveImageFromEditor; + + var shouldRemoveImage = true; + if (shouldRemoveImageEvent != null) { + shouldRemoveImage = await shouldRemoveImageEvent( + imageFile, + ); + } if (!shouldRemoveImage) { return; @@ -152,7 +158,10 @@ class ImageEmbedBuilder extends EmbedBuilder { '', TextSelection.collapsed(offset: offset), ); - await afterRemoveImageFromEditor(imageFile); + final afterRemoveImageEvent = afterRemoveImageFromEditor; + if (afterRemoveImageEvent != null) { + await afterRemoveImageEvent(imageFile); + } }, ); return Padding( diff --git a/flutter_quill_extensions/lib/embeds/utils.dart b/flutter_quill_extensions/lib/embeds/utils.dart index d4b937a37..a2be5b98f 100644 --- a/flutter_quill_extensions/lib/embeds/utils.dart +++ b/flutter_quill_extensions/lib/embeds/utils.dart @@ -1,8 +1,8 @@ import 'dart:io'; import 'package:flutter/foundation.dart' show Uint8List; +import 'package:gal/gal.dart'; import 'package:http/http.dart' as http; -import 'package:image_gallery_saver/image_gallery_saver.dart'; // I would like to orgnize the project structure and the code more // but here I don't want to change too much since that is a community project @@ -30,7 +30,11 @@ class _SaveImageResult { Future<_SaveImageResult> saveImage(String imageUrl) async { final imageFile = File(imageUrl); + final hasPermission = await Gal.hasAccess(); final imageExistsLocally = await imageFile.exists(); + if (!hasPermission) { + await Gal.requestAccess(); + } if (!imageExistsLocally) { final success = await _saveNetworkImageToLocal(imageUrl); return _SaveImageResult( @@ -54,10 +58,8 @@ Future _saveNetworkImageToLocal(String imageUrl) async { return false; } final imageBytes = response.bodyBytes; - final result = await ImageGallerySaver.saveImage( - Uint8List.fromList(imageBytes), - ); - return result['isSuccess']; + await Gal.putImageBytes(imageBytes); + return true; } catch (e) { return false; } @@ -75,9 +77,8 @@ Future _convertFileToUint8List(File file) async { Future _saveImageLocally(File imageFile) async { try { final imageBytes = await _convertFileToUint8List(imageFile); - final result = await ImageGallerySaver.saveImage(imageBytes); - - return result['isSuccess']; + await Gal.putImageBytes(imageBytes); + return true; } catch (e) { return false; } diff --git a/flutter_quill_extensions/lib/flutter_quill_extensions.dart b/flutter_quill_extensions/lib/flutter_quill_extensions.dart index ecc3b9497..2686581e7 100644 --- a/flutter_quill_extensions/lib/flutter_quill_extensions.dart +++ b/flutter_quill_extensions/lib/flutter_quill_extensions.dart @@ -72,21 +72,8 @@ class FlutterQuillEmbeds { }) => [ ImageEmbedBuilder( - afterRemoveImageFromEditor: afterRemoveImageFromEditor ?? - (imageFile) async { - // TODO: Change the default event if you want to - final fileExists = await imageFile.exists(); - if (fileExists) { - await imageFile.delete(); - } - }, - shouldRemoveImageFromEditor: shouldRemoveImageFromEditor ?? - (imageFile) { - // TODO: Before pubish the changes - // please consider change the name - // of the events if you want to - return Future.value(true); - }, + afterRemoveImageFromEditor: afterRemoveImageFromEditor, + shouldRemoveImageFromEditor: shouldRemoveImageFromEditor, ), VideoEmbedBuilder(onVideoInit: onVideoInit), FormulaEmbedBuilder(), diff --git a/flutter_quill_extensions/pubspec.yaml b/flutter_quill_extensions/pubspec.yaml index 9216304d8..c3efe10e2 100644 --- a/flutter_quill_extensions/pubspec.yaml +++ b/flutter_quill_extensions/pubspec.yaml @@ -26,7 +26,7 @@ dependencies: # url_launcher: ^6.1.14 # dio: ^5.3.3 - image_gallery_saver: ^2.0.3 + gal: ^2.1.1 dev_dependencies: flutter_test: