Skip to content

Commit

Permalink
refactor(share_plus)!: two-package federated architecture (#1238)
Browse files Browse the repository at this point in the history
Co-authored-by: Miguel Beltran <m@beltran.work>
  • Loading branch information
nohli and miquelbeltran committed Oct 14, 2022
1 parent eccdc58 commit 47dd8be
Show file tree
Hide file tree
Showing 45 changed files with 159 additions and 777 deletions.
18 changes: 0 additions & 18 deletions packages/share_plus/share_plus/example/ios/Flutter/Flutter.podspec

This file was deleted.

6 changes: 4 additions & 2 deletions packages/share_plus/share_plus/example/lib/main.dart
Expand Up @@ -5,10 +5,11 @@
// ignore_for_file: public_member_api_docs

import 'dart:io';

import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
import 'package:image_picker/image_picker.dart';
import 'package:share_plus/share_plus.dart';

import 'image_previews.dart';

Expand Down Expand Up @@ -164,6 +165,7 @@ class DemoAppState extends State<DemoApp> {

void _onShareWithResult(BuildContext context) async {
final box = context.findRenderObject() as RenderBox?;
final scaffoldMessenger = ScaffoldMessenger.of(context);
ShareResult result;
if (imagePaths.isNotEmpty) {
final files = <XFile>[];
Expand All @@ -179,7 +181,7 @@ class DemoAppState extends State<DemoApp> {
subject: subject,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
}
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
scaffoldMessenger.showSnackBar(SnackBar(
content: Text("Share result: ${result.status}"),
));
}
Expand Down
12 changes: 2 additions & 10 deletions packages/share_plus/share_plus/example/pubspec.yaml
Expand Up @@ -10,25 +10,17 @@ dependencies:
file_selector: ^0.9.2+1

dependency_overrides:
share_plus_linux:
path: ../../share_plus_linux
share_plus_macos:
path: ../../share_plus_macos
share_plus_platform_interface:
path: ../../share_plus_platform_interface
share_plus_web:
path: ../../share_plus_web
share_plus_windows:
path: ../../share_plus_windows

dev_dependencies:
flutter_driver:
sdk: flutter
flutter_test:
sdk: flutter
integration_test:
sdk: flutter
flutter_lints: ^2.0.1
flutter_test:
sdk: flutter

flutter:
uses-material-design: true
Expand Down
4 changes: 4 additions & 0 deletions packages/share_plus/share_plus/lib/share_plus.dart
Expand Up @@ -11,6 +11,10 @@ import 'package:share_plus_platform_interface/share_plus_platform_interface.dart
export 'package:share_plus_platform_interface/share_plus_platform_interface.dart'
show ShareResult, ShareResultStatus;

export 'src/share_plus_linux.dart';
export 'src/share_plus_windows.dart'
if (dart.library.html) 'src/share_plus_web.dart';

/// Plugin for summoning a platform share sheet.
class Share {
static SharePlatform get _platform => SharePlatform.instance;
Expand Down
Expand Up @@ -2,16 +2,16 @@
library share_plus_linux;

import 'dart:ui';
import 'package:cross_file/cross_file.dart';
import 'package:url_launcher/url_launcher.dart';

import 'package:cross_file/cross_file.dart';
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
import 'package:url_launcher/url_launcher.dart';

/// The Linux implementation of SharePlatform.
class ShareLinux extends SharePlatform {
class SharePlusLinuxPlugin extends SharePlatform {
/// Register this dart class as the platform implementation for linux
static void registerWith() {
SharePlatform.instance = ShareLinux();
SharePlatform.instance = SharePlusLinuxPlugin();
}

/// Share text.
Expand Down
Expand Up @@ -9,16 +9,16 @@ import 'package:share_plus_platform_interface/share_plus_platform_interface.dart
import 'package:url_launcher/url_launcher.dart';

/// The web implementation of [SharePlatform].
class SharePlusPlugin extends SharePlatform {
class SharePlusWebPlugin extends SharePlatform {
/// Registers this class as the default instance of [SharePlatform].
static void registerWith(Registrar registrar) {
SharePlatform.instance = SharePlusPlugin();
SharePlatform.instance = SharePlusWebPlugin();
}

final html.Navigator _navigator;

/// A constructor that allows tests to override the window object used by the plugin.
SharePlusPlugin({@visibleForTesting html.Navigator? debugNavigator})
SharePlusWebPlugin({@visibleForTesting html.Navigator? debugNavigator})
: _navigator = debugNavigator ?? html.window.navigator;

/// Share text
Expand Down
Expand Up @@ -2,20 +2,20 @@
library share_plus_windows;

import 'dart:ui';
import 'package:cross_file/cross_file.dart';
import 'package:url_launcher/url_launcher.dart';

import 'package:share_plus_windows/src/version_helper.dart';
import 'package:cross_file/cross_file.dart';
import 'package:share_plus/src/windows_version_helper.dart';
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
import 'package:url_launcher/url_launcher.dart';

/// The fallback Windows implementation of [SharePlatform], for older Windows versions.
///
class ShareWindows extends SharePlatform {
class SharePlusWindowsPlugin extends SharePlatform {
/// If the modern Share UI i.e. `DataTransferManager` is not available, then use this Dart class instead of platform specific implementation.
///
static void registerWith() {
if (!VersionHelper.instance.isWindows10RS5OrGreater) {
SharePlatform.instance = ShareWindows();
SharePlatform.instance = SharePlusWindowsPlugin();
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/share_plus/share_plus/macos/share_plus.podspec
Expand Up @@ -15,6 +15,7 @@ https://github.com/flutter/flutter/issues/46618
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.public_header_files = 'Classes/**/*.h'
s.dependency 'FlutterMacOS'

s.platform = :osx
s.osx.deployment_target = '10.11'
Expand Down
21 changes: 13 additions & 8 deletions packages/share_plus/share_plus/pubspec.yaml
Expand Up @@ -14,30 +14,35 @@ flutter:
ios:
pluginClass: FLTSharePlusPlugin
linux:
default_package: share_plus_linux
dartPluginClass: SharePlusLinuxPlugin
macos:
default_package: share_plus_macos
pluginClass: SharePlusMacosPlugin
web:
default_package: share_plus_web
pluginClass: SharePlusWebPlugin
fileName: src/share_plus_web.dart
windows:
default_package: share_plus_windows
dartPluginClass: SharePlusWindowsPlugin
pluginClass: SharePlusWindowsPluginCApi

dependencies:
cross_file: ^0.3.3+2
meta: ^1.7.0
mime: ^1.0.2
flutter:
sdk: flutter
flutter_web_plugins:
sdk: flutter
share_plus_platform_interface: ^3.1.1
share_plus_linux: ^3.0.1
share_plus_macos: ^3.0.1
share_plus_windows: ^4.0.0
share_plus_web: ^3.1.0
file: ^6.0.0
url_launcher: ^6.1.2
ffi: ^2.0.1
win32: ^3.0.0

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.1
url_launcher_platform_interface: ^2.0.2

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
@@ -1,19 +1,19 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:share_plus_linux/share_plus_linux.dart';
import 'package:share_plus/share_plus.dart';
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

void main() {
test('registered instance', () {
ShareLinux.registerWith();
expect(SharePlatform.instance, isA<ShareLinux>());
SharePlusLinuxPlugin.registerWith();
expect(SharePlatform.instance, isA<SharePlusLinuxPlugin>());
});
test('url encoding is correct for &', () async {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

await ShareLinux().share('foo&bar', subject: 'bar&foo');
await SharePlusLinuxPlugin().share('foo&bar', subject: 'bar&foo');

expect(mock.url, 'mailto:?subject=bar%26foo&body=foo%26bar');
});
Expand All @@ -23,7 +23,7 @@ void main() {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

await ShareLinux().share('foo bar', subject: 'bar foo');
await SharePlusLinuxPlugin().share('foo bar', subject: 'bar foo');

expect(mock.url, 'mailto:?subject=bar%20foo&body=foo%20bar');
});
Expand All @@ -33,7 +33,8 @@ void main() {
mock.canLaunchMockValue = false;
UrlLauncherPlatform.instance = mock;

expect(() async => await ShareLinux().share('foo bar'), throwsException);
expect(() async => await SharePlusLinuxPlugin().share('foo bar'),
throwsException);
});
}

Expand Down
@@ -1,26 +1,25 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:share_plus_windows/share_plus_windows.dart';
import 'package:share_plus_windows/src/version_helper.dart';
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
import 'package:share_plus/src/share_plus_windows.dart';
import 'package:share_plus/src/windows_version_helper.dart';
import 'package:share_plus_platform_interface/method_channel/method_channel_share.dart';

import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
import 'package:share_plus_platform_interface/share_plus_platform_interface.dart';
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

void main() {
test(
'registered instance',
() {
ShareWindows.registerWith();
expect(SharePlatform.instance, isA<ShareWindows>());
SharePlusWindowsPlugin.registerWith();
expect(SharePlatform.instance, isA<SharePlusWindowsPlugin>());
},
skip: VersionHelper.instance.isWindows10RS5OrGreater,
);

test(
'registered instance',
() {
ShareWindows.registerWith();
SharePlusWindowsPlugin.registerWith();
expect(SharePlatform.instance, isA<MethodChannelShare>());
},
skip: !VersionHelper.instance.isWindows10RS5OrGreater,
Expand All @@ -34,7 +33,7 @@ void main() {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

await ShareWindows().share('foo&bar', subject: 'bar&foo');
await SharePlusWindowsPlugin().share('foo&bar', subject: 'bar&foo');

expect(mock.url, 'mailto:?subject=bar%26foo&body=foo%26bar');
},
Expand All @@ -48,7 +47,7 @@ void main() {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

await ShareWindows().share('foo bar', subject: 'bar foo');
await SharePlusWindowsPlugin().share('foo bar', subject: 'bar foo');

expect(mock.url, 'mailto:?subject=bar%20foo&body=foo%20bar');
},
Expand All @@ -62,8 +61,8 @@ void main() {
mock.canLaunchMockValue = false;
UrlLauncherPlatform.instance = mock;

expect(
() async => await ShareWindows().share('foo bar'), throwsException);
expect(() async => await SharePlusWindowsPlugin().share('foo bar'),
throwsException);
},
skip: VersionHelper.instance.isWindows10RS5OrGreater,
);
Expand Down
Expand Up @@ -5,24 +5,24 @@
cmake_minimum_required(VERSION 3.14)

# Project-level configuration.
set(PROJECT_NAME "share_plus_windows")
set(PROJECT_NAME "share_plus")
project(${PROJECT_NAME} LANGUAGES CXX)

# This value is used when generating builds using this plugin, so it must
# not be changed
set(PLUGIN_NAME "share_plus_windows_plugin")
set(PLUGIN_NAME "share_plus_plugin")

# Any new source files that you add to the plugin should be added here.
list(APPEND PLUGIN_SOURCES
"share_plus_windows_plugin.cpp"
"share_plus_plugin.cpp"
"share_plus_windows_plugin.h"
)

# Define the plugin library target. Its name must not be changed (see comment
# on PLUGIN_NAME above).
add_library(${PLUGIN_NAME} SHARED
"include/share_plus_windows/share_plus_windows_plugin_c_api.h"
"share_plus_windows_plugin_c_api.cpp"
"include/share_plus/share_plus_windows_plugin_c_api.h"
"share_plus_plugin_c_api.cpp"
${PLUGIN_SOURCES}
)

Expand All @@ -47,7 +47,7 @@ target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)
# List of absolute paths to libraries that should be bundled with the plugin.
# This list could contain prebuilt libraries, or libraries created by an
# external build triggered from this build file.
set(share_plus_windows_bundled_libraries
set(share_plus_bundled_libraries
""
PARENT_SCOPE
)
Expand Up @@ -17,7 +17,7 @@ FLUTTER_PLUGIN_EXPORT void SharePlusWindowsPluginCApiRegisterWithRegistrar(
FlutterDesktopPluginRegistrarRef registrar);

#if defined(__cplusplus)
} // extern "C"
} // extern "C"
#endif

#endif // FLUTTER_PLUGIN_SHARE_PLUS_WINDOWS_PLUGIN_C_API_H_
#endif // FLUTTER_PLUGIN_SHARE_PLUS_WINDOWS_PLUGIN_C_API_H_

0 comments on commit 47dd8be

Please sign in to comment.