Skip to content

Commit

Permalink
feat: more advance method to check the platform support check allowin…
Browse files Browse the repository at this point in the history
…g each platform to have it's own check, publish new experimental version of quill_native_version
  • Loading branch information
EchoEllet committed Sep 26, 2024
1 parent d3e9357 commit 7321ef5
Show file tree
Hide file tree
Showing 31 changed files with 344 additions and 170 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/services.dart' show Uint8List;
import 'package:meta/meta.dart' show experimental;
import 'package:quill_native_bridge/quill_native_bridge.dart'
show QuillNativeBridge, QuillNativeBridgePlatformFeature;
show QuillNativeBridge, QuillNativeBridgeFeature;

import 'clipboard_service.dart';

Expand All @@ -11,31 +11,35 @@ import 'clipboard_service.dart';
class DefaultClipboardService extends ClipboardService {
@override
Future<String?> getHtmlText() async {
if (QuillNativeBridgePlatformFeature.getClipboardHtml.isUnsupported) {
if (!(await QuillNativeBridge.isSupported(
QuillNativeBridgeFeature.getClipboardHtml))) {
return null;
}
return await QuillNativeBridge.getClipboardHtml();
}

@override
Future<Uint8List?> getImageFile() async {
if (QuillNativeBridgePlatformFeature.getClipboardImage.isUnsupported) {
if (!(await QuillNativeBridge.isSupported(
QuillNativeBridgeFeature.getClipboardImage))) {
return null;
}
return await QuillNativeBridge.getClipboardImage();
}

@override
Future<void> copyImageToClipboard(Uint8List imageBytes) async {
if (QuillNativeBridgePlatformFeature.copyImageToClipboard.isUnsupported) {
if (!(await QuillNativeBridge.isSupported(
QuillNativeBridgeFeature.copyImageToClipboard))) {
return;
}
await QuillNativeBridge.copyImageToClipboard(imageBytes);
}

@override
Future<Uint8List?> getGifFile() async {
if (QuillNativeBridgePlatformFeature.getClipboardGif.isUnsupported) {
if (!(await QuillNativeBridge.isSupported(
QuillNativeBridgeFeature.getClipboardGif))) {
return null;
}
return QuillNativeBridge.getClipboardGif();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ dependencies:
# Plugins
url_launcher: ^6.2.4
flutter_keyboard_visibility: ^6.0.0
quill_native_bridge: ^10.7.6
quill_native_bridge: ^10.7.7

dev_dependencies:
flutter_lints: ^4.0.0
Expand Down
4 changes: 4 additions & 0 deletions quill_native_bridge/quill_native_bridge/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## 10.7.7

- Highly experimental changes in https://github.com/singerdmx/flutter-quill/pull/2230 (WIP). Not intended for public use as breaking changes will occur. Not stable yet.

## 10.7.6

- Highly experimental changes in https://github.com/singerdmx/flutter-quill/pull/2230 (WIP). Not intended for public use as breaking changes will occur. Not stable yet.
Expand Down
11 changes: 10 additions & 1 deletion quill_native_bridge/quill_native_bridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@
An internal plugin for [`flutter_quill`](https://pub.dev/packages/flutter_quill) package to access platform-specific APIs.

> [!NOTE]
> **Internal Use Only**: Exclusively for `flutter_quill`. Breaking changes may occur.
> **Internal Use Only**: Exclusively for `flutter_quill`. Breaking changes may occur.
| Feature | iOS | Android | macOS | Windows | Linux | Web | Description |
|------------------------------|------|---------|-------|---------|-------|--------|---------------------------------------------------------------------------------------------------------|
| **isIOSSimulator** | Yes | No | No | No | No | No | Checks if the code is running in an iOS simulator. |
| **getClipboardHtml** | Yes | Yes | Yes | Yes | Yes | Yes | Retrieves HTML content from the system clipboard. |
| **copyHtmlToClipboard** | Yes | Yes | Yes | No | Yes | Yes | Copies HTML content to the system clipboard. |
| **copyImageToClipboard** | Yes | Yes | Yes | No | Yes | Yes | Copies an image to the system clipboard. |
| **getClipboardImage** | Yes | Yes | Yes | No | Yes | Yes | Retrieves an image from the system clipboard. |
| **getClipboardGif** | Yes | Yes | No | No | No | No | Retrieves a GIF from the system clipboard. |
36 changes: 19 additions & 17 deletions quill_native_bridge/quill_native_bridge/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/foundation.dart' show defaultTargetPlatform, kIsWeb;
import 'package:flutter/material.dart';
import 'package:quill_native_bridge/quill_native_bridge.dart'
show QuillNativeBridge, QuillNativeBridgePlatformFeature;
show QuillNativeBridge, QuillNativeBridgeFeature;

import 'assets.dart';

Expand Down Expand Up @@ -42,47 +42,47 @@ class Buttons extends StatelessWidget {
const SizedBox(height: 50),
ElevatedButton.icon(
onPressed: () => _onButtonClick(
QuillNativeBridgePlatformFeature.isIOSSimulator,
QuillNativeBridgeFeature.isIOSSimulator,
context: context,
),
label: const Text('Is iOS Simulator'),
icon: const Icon(Icons.apple),
),
ElevatedButton.icon(
onPressed: () => _onButtonClick(
QuillNativeBridgePlatformFeature.getClipboardHtml,
QuillNativeBridgeFeature.getClipboardHtml,
context: context,
),
label: const Text('Get HTML from Clipboard'),
icon: const Icon(Icons.html),
),
ElevatedButton.icon(
onPressed: () => _onButtonClick(
QuillNativeBridgePlatformFeature.copyHtmlToClipboard,
QuillNativeBridgeFeature.copyHtmlToClipboard,
context: context,
),
label: const Text('Copy HTML to Clipboard'),
icon: const Icon(Icons.copy),
),
ElevatedButton.icon(
onPressed: () => _onButtonClick(
QuillNativeBridgePlatformFeature.copyImageToClipboard,
QuillNativeBridgeFeature.copyImageToClipboard,
context: context,
),
label: const Text('Copy Image to Clipboard'),
icon: const Icon(Icons.copy),
),
ElevatedButton.icon(
onPressed: () => _onButtonClick(
QuillNativeBridgePlatformFeature.getClipboardImage,
QuillNativeBridgeFeature.getClipboardImage,
context: context,
),
label: const Text('Retrieve Image from Clipboard'),
icon: const Icon(Icons.image),
),
ElevatedButton.icon(
onPressed: () => _onButtonClick(
QuillNativeBridgePlatformFeature.getClipboardGif,
QuillNativeBridgeFeature.getClipboardGif,
context: context,
),
label: const Text('Retrieve Gif from Clipboard'),
Expand All @@ -93,14 +93,16 @@ class Buttons extends StatelessWidget {
}

Future<void> _onButtonClick(
QuillNativeBridgePlatformFeature platformFeature, {
QuillNativeBridgeFeature feature, {
required BuildContext context,
}) async {
final isFeatureUnsupported = platformFeature.isUnsupported;
final isFeatureWebUnsupported = !platformFeature.hasWebSupport && kIsWeb;
final scaffoldMessenger = ScaffoldMessenger.of(context);
switch (platformFeature) {
case QuillNativeBridgePlatformFeature.isIOSSimulator:

final isFeatureUnsupported =
!(await QuillNativeBridge.isSupported(feature));
final isFeatureWebUnsupported = isFeatureUnsupported && kIsWeb;
switch (feature) {
case QuillNativeBridgeFeature.isIOSSimulator:
if (isFeatureUnsupported) {
scaffoldMessenger.showText(
isFeatureWebUnsupported
Expand All @@ -114,7 +116,7 @@ class Buttons extends StatelessWidget {
? "You're running the app on iOS simulator"
: "You're running the app on real iOS device.");
break;
case QuillNativeBridgePlatformFeature.getClipboardHtml:
case QuillNativeBridgeFeature.getClipboardHtml:
if (isFeatureUnsupported) {
scaffoldMessenger.showText(
isFeatureWebUnsupported
Expand All @@ -135,7 +137,7 @@ class Buttons extends StatelessWidget {
);
debugPrint('HTML from the clipboard: $result');
break;
case QuillNativeBridgePlatformFeature.copyHtmlToClipboard:
case QuillNativeBridgeFeature.copyHtmlToClipboard:
if (isFeatureUnsupported) {
scaffoldMessenger.showText(
isFeatureWebUnsupported
Expand All @@ -156,7 +158,7 @@ class Buttons extends StatelessWidget {
'HTML copied to the clipboard: $html',
);
break;
case QuillNativeBridgePlatformFeature.copyImageToClipboard:
case QuillNativeBridgeFeature.copyImageToClipboard:
if (isFeatureUnsupported) {
scaffoldMessenger.showText(
isFeatureWebUnsupported
Expand Down Expand Up @@ -185,7 +187,7 @@ class Buttons extends StatelessWidget {
'Image has been copied to the clipboard.',
);
break;
case QuillNativeBridgePlatformFeature.getClipboardImage:
case QuillNativeBridgeFeature.getClipboardImage:
if (isFeatureUnsupported) {
scaffoldMessenger.showText(
isFeatureWebUnsupported
Expand All @@ -211,7 +213,7 @@ class Buttons extends StatelessWidget {
),
);
break;
case QuillNativeBridgePlatformFeature.getClipboardGif:
case QuillNativeBridgeFeature.getClipboardGif:
if (isFeatureUnsupported) {
scaffoldMessenger.showText(
isFeatureWebUnsupported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import 'package:flutter/foundation.dart'

import 'package:quill_native_bridge_platform_interface/quill_native_bridge_platform_interface.dart';

// TODO: Might move platform feature check outside of quill_native_bridge_platform_interface
// to allow the implementation of QuillNativeBridgePlatform to have a different check.
export 'package:quill_native_bridge_platform_interface/src/platform_feature.dart';
export 'package:quill_native_bridge_platform_interface/src/platform_feature.dart'
show QuillNativeBridgeFeature;

/// An internal plugin for [`flutter_quill`](https://pub.dev/packages/flutter_quill)
/// package to access platform-specific APIs.
Expand All @@ -25,6 +24,23 @@ class QuillNativeBridge {
/// is [TargetPlatform.iOS] and [kIsWeb] is `false`.
static Future<bool> isIOSSimulator() => _platform.isIOSSimulator();

/// Checks if the specified [feature] is supported in the current implementation.
///
/// Will verify if this is supported in the platform itself:
///
/// - If [feature] is supported on **Android API 21** (as an example) and the
/// current Android API is `19` then will return `false`
/// - If [feature] is supported on the web if Clipboard API (as another example)
/// available in the current browser, and the current browser doesn't support it,
/// will return `false` too. For this specific example, you will need
/// to fallback to **Clipboard events** on **Firefox** or browsers that doesn't
/// support **Clipboard API**.
///
/// Always check the docs of the method you're calling to see if there
/// are special notes.
static Future<bool> isSupported(QuillNativeBridgeFeature feature) =>
_platform.isSupported(feature);

/// Return HTML from the Clipboard.
///
/// **Important for web**: If [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API)
Expand Down
16 changes: 8 additions & 8 deletions quill_native_bridge/quill_native_bridge/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: quill_native_bridge
description: "An internal plugin for flutter_quill package to access platform-specific APIs"
version: 10.7.6
version: 10.7.7
homepage: https://github.com/singerdmx/flutter-quill/tree/master/quill_native_bridge/quill_native_bridge
repository: https://github.com/singerdmx/flutter-quill/tree/master/quill_native_bridge/quill_native_bridge
issue_tracker: https://github.com/singerdmx/flutter-quill/issues/
Expand All @@ -21,13 +21,13 @@ environment:
dependencies:
flutter:
sdk: flutter
quill_native_bridge_android: ^0.0.1-dev.0
quill_native_bridge_platform_interface: ^0.0.1-dev.0
quill_native_bridge_web: ^0.0.1-dev.1
quill_native_bridge_windows: ^0.0.1-dev.0
quill_native_bridge_linux: ^0.0.1-dev.0
quill_native_bridge_ios: ^0.0.1-dev.0
quill_native_bridge_macos: ^0.0.1-dev.0
quill_native_bridge_android: ^0.0.1-dev.1
quill_native_bridge_platform_interface: ^0.0.1-dev.2
quill_native_bridge_web: ^0.0.1-dev.2
quill_native_bridge_windows: ^0.0.1-dev.1
quill_native_bridge_linux: ^0.0.1-dev.1
quill_native_bridge_ios: ^0.0.1-dev.1
quill_native_bridge_macos: ^0.0.1-dev.1

dev_dependencies:
flutter_test:
Expand Down
4 changes: 4 additions & 0 deletions quill_native_bridge/quill_native_bridge_android/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## 0.0.1-dev.1

- Highly experimental changes in https://github.com/singerdmx/flutter-quill/pull/2230 (WIP). Not intended for public use as breaking changes will occur. Not stable yet.

## 0.0.1-dev.0

- Initial experimental release. WIP in https://github.com/singerdmx/flutter-quill/pull/2230. Not intended for public use as breaking changes will occur.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ class QuillNativeBridgeAndroid extends QuillNativeBridgePlatform {
QuillNativeBridgePlatform.instance = QuillNativeBridgeAndroid._();
}

@override
Future<bool> isSupported(QuillNativeBridgeFeature feature) async {
switch (feature) {
case QuillNativeBridgeFeature.isIOSSimulator:
return false;
case QuillNativeBridgeFeature.getClipboardHtml:
case QuillNativeBridgeFeature.copyHtmlToClipboard:
case QuillNativeBridgeFeature.copyImageToClipboard:
case QuillNativeBridgeFeature.getClipboardImage:
case QuillNativeBridgeFeature.getClipboardGif:
return true;
// Without this default check, adding new item to the enum will be a breaking change
default:
throw UnimplementedError(
'Checking if `${feature.name}` is supported on Android is not covered.',
);
}
}

@override
Future<String?> getClipboardHtml() async => _hostApi.getClipboardHtml();

Expand Down
4 changes: 2 additions & 2 deletions quill_native_bridge/quill_native_bridge_android/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: quill_native_bridge_android
description: "Android implementation of the quill_native_bridge plugin."
version: 0.0.1-dev.0
version: 0.0.1-dev.1
homepage: https://github.com/singerdmx/flutter-quill/tree/master/quill_native_bridge/quill_native_bridge_android
repository: https://github.com/singerdmx/flutter-quill/tree/master/quill_native_bridge/quill_native_bridge_android
issue_tracker: https://github.com/singerdmx/flutter-quill/issues/
Expand All @@ -13,7 +13,7 @@ environment:
dependencies:
flutter:
sdk: flutter
quill_native_bridge_platform_interface: ^0.0.1-dev.0
quill_native_bridge_platform_interface: ^0.0.1-dev.2

dev_dependencies:
flutter_test:
Expand Down
4 changes: 4 additions & 0 deletions quill_native_bridge/quill_native_bridge_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## 0.0.1-dev.1

- Highly experimental changes in https://github.com/singerdmx/flutter-quill/pull/2230 (WIP). Not intended for public use as breaking changes will occur. Not stable yet.

## 0.0.1-dev.0

- Initial experimental release. WIP in https://github.com/singerdmx/flutter-quill/pull/2230. Not intended for public use as breaking changes will occur.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ class QuillNativeBridgeIos extends QuillNativeBridgePlatform {
QuillNativeBridgePlatform.instance = QuillNativeBridgeIos._();
}

@override
Future<bool> isSupported(QuillNativeBridgeFeature feature) async {
switch (feature) {
case QuillNativeBridgeFeature.isIOSSimulator:
case QuillNativeBridgeFeature.getClipboardHtml:
case QuillNativeBridgeFeature.copyHtmlToClipboard:
case QuillNativeBridgeFeature.copyImageToClipboard:
case QuillNativeBridgeFeature.getClipboardImage:
case QuillNativeBridgeFeature.getClipboardGif:
return true;
// Without this default check, adding new item to the enum will be a breaking change
default:
throw UnimplementedError(
'Checking if `${feature.name}` is supported on iOS is not covered.',
);
}
}

@override
Future<bool> isIOSSimulator() => _hostApi.isIosSimulator();

Expand Down
4 changes: 2 additions & 2 deletions quill_native_bridge/quill_native_bridge_ios/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: quill_native_bridge_ios
description: "iOS implementation of the quill_native_bridge plugin."
version: 0.0.1-dev.0
version: 0.0.1-dev.1
homepage: https://github.com/singerdmx/flutter-quill/tree/master/quill_native_bridge/quill_native_bridge_ios
repository: https://github.com/singerdmx/flutter-quill/tree/master/quill_native_bridge/quill_native_bridge_ios
issue_tracker: https://github.com/singerdmx/flutter-quill/issues/
Expand All @@ -13,7 +13,7 @@ environment:
dependencies:
flutter:
sdk: flutter
quill_native_bridge_platform_interface: ^0.0.1-dev.0
quill_native_bridge_platform_interface: ^0.0.1-dev.2

dev_dependencies:
flutter_test:
Expand Down
3 changes: 3 additions & 0 deletions quill_native_bridge/quill_native_bridge_linux/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to this project will be documented in this file.

## 0.0.1-dev.1

- Highly experimental changes in https://github.com/singerdmx/flutter-quill/pull/2230 (WIP). Not intended for public use as breaking changes will occur. Not stable yet.

## 0.0.1-dev.0

Expand Down
Loading

0 comments on commit 7321ef5

Please sign in to comment.