Skip to content

Commit

Permalink
Adding fallback implementation for unsupported platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatth committed Apr 20, 2022
1 parent dcf9870 commit 754012c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
6 changes: 4 additions & 2 deletions rich_clipboard_macos/lib/rich_clipboard_macos.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// All functionality is implemented via a MethodChannel in Swift. This file is
// here to stop Flutter from breaking.
// Re-export the default method channel implementation so flutter code gen can
// find it.
export 'package:rich_clipboard_platform_interface/rich_clipboard_platform_interface.dart'
show MethodChannelRichClipboard;
3 changes: 2 additions & 1 deletion rich_clipboard_macos/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ flutter:
implements: rich_clipboard
platforms:
macos:
pluginClass: RichClipboardPlugin
pluginClass: RichClipboardPlugin
dartPluginClass: MethodChannelRichClipboard
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

import 'src/method_channel_rich_clipboard.dart';
import 'src/fallback_rich_clipboard.dart';
import 'src/rich_clipboard_data.dart';

export 'src/method_channel_rich_clipboard.dart' show MethodChannelRichClipboard;
export 'src/rich_clipboard_data.dart' show RichClipboardData;

abstract class RichClipboardPlatform extends PlatformInterface {
RichClipboardPlatform() : super(token: _token);

static final Object _token = Object();

static RichClipboardPlatform _instance = MethodChannelRichClipboard();
static RichClipboardPlatform _instance = FallbackRichClipboard();

static RichClipboardPlatform get instance => _instance;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:flutter/services.dart';

import '../rich_clipboard_platform_interface.dart';

/// A partial implementation of [RichClipboardPlatform] that provides
/// functionality on unsupported platforms.
///
/// This class uses Flutter's built-in [Clipboard] to provide plain text support
/// and silently discards other content types
class FallbackRichClipboard extends RichClipboardPlatform {
@override
Future<List<String>> getAvailableTypes() async =>
const [Clipboard.kTextPlain];

@override
Future<RichClipboardData> getData() => Clipboard.getData(Clipboard.kTextPlain)
.then((d) => RichClipboardData(text: d?.text));

@override
Future<void> setData(RichClipboardData data) => Clipboard.setData(data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import '../rich_clipboard_platform_interface.dart';

const MethodChannel _channel = MethodChannel('com.bringingfire.rich_clipboard');

/// A default [RichClipboardPlatform] implementation backed by a platform
/// channel.
class MethodChannelRichClipboard extends RichClipboardPlatform {
/// Registers this implementation.
static void registerWith() {
RichClipboardPlatform.instance = MethodChannelRichClipboard();
}

@override
Future<List<String>> getAvailableTypes() async {
final List<String>? result =
Expand Down

0 comments on commit 754012c

Please sign in to comment.