Skip to content

Commit

Permalink
Fix my issues and changes (#1417)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
EchoEllet authored Oct 4, 2023
1 parent ff381e6 commit 9fcd0a7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
27 changes: 18 additions & 9 deletions flutter_quill_extensions/lib/embeds/builders.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down
17 changes: 9 additions & 8 deletions flutter_quill_extensions/lib/embeds/utils.dart
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -54,10 +58,8 @@ Future<bool> _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;
}
Expand All @@ -75,9 +77,8 @@ Future<Uint8List> _convertFileToUint8List(File file) async {
Future<bool> _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;
}
Expand Down
17 changes: 2 additions & 15 deletions flutter_quill_extensions/lib/flutter_quill_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion flutter_quill_extensions/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9fcd0a7

Please sign in to comment.