Skip to content

Commit

Permalink
chore: improve platform check by using constants and defaultTargetPla…
Browse files Browse the repository at this point in the history
…tform (#2188)
  • Loading branch information
EchoEllet authored Sep 7, 2024
1 parent ee08643 commit 151cba5
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 343 deletions.
18 changes: 8 additions & 10 deletions example/lib/screens/quill/my_quill_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import 'dart:io' as io show Directory, File;
import 'package:cached_network_image/cached_network_image.dart'
show CachedNetworkImageProvider;
import 'package:desktop_drop/desktop_drop.dart' show DropTarget;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:flutter_quill/extensions.dart'
show isAndroid, isDesktop, isIOS, isWeb;
import 'package:flutter_quill/extensions.dart';
import 'package:flutter_quill/flutter_quill.dart';
import 'package:flutter_quill_extensions/flutter_quill_extensions.dart';
// ignore: implementation_imports
Expand Down Expand Up @@ -65,7 +65,7 @@ class MyQuillEditor extends StatelessWidget {
placeholder: 'Start writing your notes...',
padding: const EdgeInsets.all(16),
onImagePaste: (imageBytes) async {
if (isWeb()) {
if (kIsWeb) {
return null;
}
// We will save it to system temporary files
Expand All @@ -81,7 +81,7 @@ class MyQuillEditor extends StatelessWidget {
return file.path;
},
onGifPaste: (gifBytes) async {
if (isWeb()) {
if (kIsWeb) {
return null;
}
// We will save it to system temporary files
Expand All @@ -96,7 +96,7 @@ class MyQuillEditor extends StatelessWidget {
return file.path;
},
embedBuilders: [
...(isWeb()
...(kIsWeb
? FlutterQuillEmbeds.editorWebBuilders()
: FlutterQuillEmbeds.editorBuilders(
imageEmbedConfigurations: QuillEditorImageEmbedConfigurations(
Expand All @@ -110,9 +110,7 @@ class MyQuillEditor extends StatelessWidget {
// only for Android, iOS and web

// We will use it only if image from network
if (isAndroid(supportWeb: false) ||
isIOS(supportWeb: false) ||
isWeb()) {
if (isAndroidApp || isIosApp || kIsWeb) {
if (isHttpBasedUrl(imageUrl)) {
return CachedNetworkImageProvider(
imageUrl,
Expand All @@ -132,7 +130,7 @@ class MyQuillEditor extends StatelessWidget {
videoEmbedConfigurations: QuillEditorVideoEmbedConfigurations(
// Loading YouTube videos on Desktop is not supported yet
// when using iframe platform view
youtubeVideoSupportMode: isDesktop(supportWeb: false)
youtubeVideoSupportMode: isDesktopApp
? YoutubeVideoSupportMode.customPlayerWithDownloadUrl
: YoutubeVideoSupportMode.iframeView,
),
Expand All @@ -141,7 +139,7 @@ class MyQuillEditor extends StatelessWidget {
],
builder: (context, rawEditor) {
// The `desktop_drop` plugin doesn't support iOS platform for now
if (isIOS(supportWeb: false)) {
if (isIosApp) {
return rawEditor;
}
return DropTarget(
Expand Down
11 changes: 5 additions & 6 deletions example/lib/screens/quill/my_quill_toolbar.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:io' as io show File;

import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_quill/extensions.dart' show isAndroid, isIOS, isWeb;
import 'package:flutter_quill/extensions.dart';
import 'package:flutter_quill/flutter_quill.dart';
import 'package:flutter_quill_extensions/flutter_quill_extensions.dart';
import 'package:google_fonts/google_fonts.dart';
Expand Down Expand Up @@ -65,7 +66,7 @@ class MyQuillToolbar extends StatelessWidget {
if (newImage == null) {
return;
}
if (isWeb()) {
if (kIsWeb) {
controller.insertImageBlock(imageSource: newImage);
return;
}
Expand All @@ -74,7 +75,7 @@ class MyQuillToolbar extends StatelessWidget {
}

Future<void> onImageInsert(String image, QuillController controller) async {
if (isWeb() || isHttpBasedUrl(image)) {
if (kIsWeb || isHttpBasedUrl(image)) {
controller.insertImageBlock(imageSource: image);
return;
}
Expand Down Expand Up @@ -295,9 +296,7 @@ class MyQuillToolbar extends StatelessWidget {
embedButtons: FlutterQuillEmbeds.toolbarButtons(
imageButtonOptions: QuillToolbarImageButtonOptions(
imageButtonConfigurations: QuillToolbarImageConfigurations(
onImageInsertCallback: isAndroid(supportWeb: false) ||
isIOS(supportWeb: false) ||
isWeb()
onImageInsertCallback: isAndroidApp || isIosApp || kIsWeb
? (image, controller) =>
onImageInsertWithCropping(image, controller, context)
: onImageInsert,
Expand Down
6 changes: 0 additions & 6 deletions example/lib/screens/settings/widgets/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ class SettingsScreen extends StatelessWidget {
actions: [
AppDialogAction(
onPressed: () => Navigator.of(context).pop(null),
options: const DialogActionOptions(
cupertinoDialogActionOptions:
CupertinoDialogActionOptions(
isDefaultAction: true,
),
),
child: const Text('Cancel'),
),
],
Expand Down
45 changes: 3 additions & 42 deletions example/lib/screens/shared/widgets/dialog_action.dart
Original file line number Diff line number Diff line change
@@ -1,62 +1,23 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_quill/extensions.dart';

@immutable
final class CupertinoDialogActionOptions {
const CupertinoDialogActionOptions({
this.isDefaultAction = false,
});

final bool isDefaultAction;
}

@immutable
final class MaterialDialogActionOptions {
const MaterialDialogActionOptions({
this.textStyle,
});

final ButtonStyle? textStyle;
}

@immutable
class DialogActionOptions {
const DialogActionOptions({
this.cupertinoDialogActionOptions,
this.materialDialogActionOptions,
});

final CupertinoDialogActionOptions? cupertinoDialogActionOptions;
final MaterialDialogActionOptions? materialDialogActionOptions;
}

class AppDialogAction extends StatelessWidget {
const AppDialogAction({
required this.child,
required this.onPressed,
this.options,
this.textStyle,
super.key,
});

final VoidCallback? onPressed;
final Widget child;

final DialogActionOptions? options;
final ButtonStyle? textStyle;

@override
Widget build(BuildContext context) {
if (isAppleOS(supportWeb: true)) {
return CupertinoDialogAction(
onPressed: onPressed,
isDefaultAction:
options?.cupertinoDialogActionOptions?.isDefaultAction ?? false,
child: child,
);
}
return TextButton(
onPressed: onPressed,
style: options?.materialDialogActionOptions?.textStyle,
style: textStyle,
child: child,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class ImageOptionsMenu extends StatelessWidget {
localizations.savedUsingLocalStorage,
};

if (isDesktop(supportWeb: false)) {
if (isDesktopApp) {
message = localizations.theImageHasBeenSavedAt(imageSource);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io' show File;

import 'package:flutter/foundation.dart';
import 'package:flutter_quill/extensions.dart';
import 'package:meta/meta.dart' show immutable;

import '../image_embed_types.dart';

Expand Down Expand Up @@ -108,11 +108,11 @@ class QuillEditorImageEmbedConfigurations {

static ImageEmbedBuilderOnRemovedCallback get defaultOnImageRemovedCallback {
return (imageUrl) async {
if (isWeb()) {
if (kIsWeb) {
return;
}

final mobile = isMobile(supportWeb: false);
final mobile = isMobileApp;
// If the platform is not mobile, return void;
// Since the mobile OS gives us a copy of the image

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SelectCameraActionDialog extends StatelessWidget {
context.loc.takeAPhotoUsingYourCamera,
),
leading: const Icon(Icons.photo_sharp),
enabled: !isDesktop(supportWeb: false),
enabled: !isDesktopApp,
onTap: () => Navigator.of(context).pop(CameraAction.image),
),
ListTile(
Expand All @@ -30,7 +30,7 @@ class SelectCameraActionDialog extends StatelessWidget {
context.loc.recordAVideoUsingYourCamera,
),
leading: const Icon(Icons.camera),
enabled: !isDesktop(supportWeb: false),
enabled: !isDesktopApp,
onTap: () => Navigator.of(context).pop(CameraAction.video),
),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_quill/extensions.dart' show isDesktop;
import 'package:flutter_quill/extensions.dart' show isDesktopApp;
import 'package:flutter_quill/translations.dart';

import '../../editor/image/image_embed_types.dart';
Expand Down Expand Up @@ -29,7 +29,7 @@ class SelectImageSourceDialog extends StatelessWidget {
context.loc.takeAPhotoUsingYourCamera,
),
leading: const Icon(Icons.camera),
enabled: !isDesktop(supportWeb: false),
enabled: !isDesktopApp,
onTap: () => Navigator.of(context).pop(InsertImageSource.camera),
),
ListTile(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_quill/extensions.dart' show isDesktop;
import 'package:flutter_quill/extensions.dart' show isDesktopApp;
import 'package:flutter_quill/translations.dart';

import 'models/video.dart';
Expand Down Expand Up @@ -27,7 +27,7 @@ class SelectVideoSourceDialog extends StatelessWidget {
title: Text(context.loc.camera),
subtitle: Text(context.loc.recordAVideoUsingYourCamera),
leading: const Icon(Icons.camera),
enabled: !isDesktop(supportWeb: false),
enabled: !isDesktopApp,
onTap: () => Navigator.of(context).pop(InsertVideoSource.camera),
),
ListTile(
Expand Down
4 changes: 4 additions & 0 deletions lib/extensions.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
library flutter_quill.extensions;

// This file contains exports that are meant to be used
// internally and are not part of the public API as
// breaking changes can happen

export 'src/common/utils/platform.dart';
export 'src/common/utils/string.dart';
export 'src/common/utils/widgets.dart';
Expand Down
Loading

0 comments on commit 151cba5

Please sign in to comment.