From 162430901e4295adba5d4db8395c2153094d7625 Mon Sep 17 00:00:00 2001 From: Florian Obernberger Date: Mon, 30 Sep 2024 22:08:50 +0200 Subject: [PATCH] feat(share_plus): add option to add custom link title on Android --- .../dev/fluttercommunity/plus/share/MethodCallHandler.kt | 2 ++ .../main/kotlin/dev/fluttercommunity/plus/share/Share.kt | 7 +++++-- packages/share_plus/share_plus/lib/share_plus.dart | 5 +++++ .../share_plus/share_plus/lib/src/share_plus_linux.dart | 1 + packages/share_plus/share_plus/lib/src/share_plus_web.dart | 1 + .../share_plus/share_plus/lib/src/share_plus_windows.dart | 1 + .../lib/method_channel/method_channel_share.dart | 5 +++++ .../lib/platform_interface/share_plus_platform.dart | 2 ++ 8 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt index d4488922e5..9da0cf7f29 100644 --- a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt +++ b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt @@ -29,6 +29,7 @@ internal class MethodCallHandler( call.argument("uri") as String, subject = null, withResult = isWithResult, + call.argument("title") as String?, ) success(isWithResult, result) } @@ -38,6 +39,7 @@ internal class MethodCallHandler( call.argument("text") as String, call.argument("subject") as String?, isWithResult, + null, ) success(isWithResult, result) } diff --git a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt index 59bdb1e914..39f63c36c2 100644 --- a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt +++ b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt @@ -55,7 +55,7 @@ internal class Share( this.activity = activity } - fun share(text: String, subject: String?, withResult: Boolean) { + fun share(text: String, subject: String?, withResult: Boolean, title: String?) { val shareIntent = Intent().apply { action = Intent.ACTION_SEND type = "text/plain" @@ -63,6 +63,9 @@ internal class Share( if (subject != null) { putExtra(Intent.EXTRA_SUBJECT, subject) } + if (title != null) { + putExtra(Intent.EXTRA_TITLE, title) + } } // If we dont want the result we use the old 'createChooser' val chooserIntent = @@ -97,7 +100,7 @@ internal class Share( val shareIntent = Intent() when { (fileUris.isEmpty() && !text.isNullOrBlank()) -> { - share(text, subject, withResult) + share(text, subject, withResult, null) return } diff --git a/packages/share_plus/share_plus/lib/share_plus.dart b/packages/share_plus/share_plus/lib/share_plus.dart index 52e50d624d..1f8ee6ff0a 100644 --- a/packages/share_plus/share_plus/lib/share_plus.dart +++ b/packages/share_plus/share_plus/lib/share_plus.dart @@ -30,6 +30,9 @@ class Share { /// origin rect for the share sheet to popover from on iPads and Macs. It has no effect /// on other devices. /// + /// The optional [title] parameter can be used to specify a custom title for + /// the uri on Android. It has no effect on other devices. + /// /// May throw [PlatformException] /// from [MethodChannel]. /// @@ -37,10 +40,12 @@ class Share { static Future shareUri( Uri uri, { Rect? sharePositionOrigin, + String? title, }) async { return _platform.shareUri( uri, sharePositionOrigin: sharePositionOrigin, + title: title, ); } diff --git a/packages/share_plus/share_plus/lib/src/share_plus_linux.dart b/packages/share_plus/share_plus/lib/src/share_plus_linux.dart index 03132bddc0..16ea33645a 100644 --- a/packages/share_plus/share_plus/lib/src/share_plus_linux.dart +++ b/packages/share_plus/share_plus/lib/src/share_plus_linux.dart @@ -24,6 +24,7 @@ class SharePlusLinuxPlugin extends SharePlatform { String? subject, String? text, Rect? sharePositionOrigin, + String? title, }) async { throw UnimplementedError( 'shareUri() has not been implemented on Linux. Use share().'); diff --git a/packages/share_plus/share_plus/lib/src/share_plus_web.dart b/packages/share_plus/share_plus/lib/src/share_plus_web.dart index 45d7968aee..ff5af51c19 100644 --- a/packages/share_plus/share_plus/lib/src/share_plus_web.dart +++ b/packages/share_plus/share_plus/lib/src/share_plus_web.dart @@ -32,6 +32,7 @@ class SharePlusWebPlugin extends SharePlatform { Future shareUri( Uri uri, { Rect? sharePositionOrigin, + String? title, }) async { final data = ShareData( url: uri.toString(), diff --git a/packages/share_plus/share_plus/lib/src/share_plus_windows.dart b/packages/share_plus/share_plus/lib/src/share_plus_windows.dart index 5a660e4763..1e4dfd574b 100644 --- a/packages/share_plus/share_plus/lib/src/share_plus_windows.dart +++ b/packages/share_plus/share_plus/lib/src/share_plus_windows.dart @@ -29,6 +29,7 @@ class SharePlusWindowsPlugin extends SharePlatform { String? subject, String? text, Rect? sharePositionOrigin, + String? title, }) async { throw UnimplementedError( 'shareUri() has not been implemented on Windows. Use share().'); diff --git a/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart b/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart index 20edd2108b..e8676f9de6 100644 --- a/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart +++ b/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart @@ -27,6 +27,7 @@ class MethodChannelShare extends SharePlatform { Future shareUri( Uri uri, { Rect? sharePositionOrigin, + String? title, }) async { final params = {'uri': uri.toString()}; @@ -37,6 +38,10 @@ class MethodChannelShare extends SharePlatform { params['originHeight'] = sharePositionOrigin.height; } + if (title != null) { + params['title'] = title; + } + final result = await channel.invokeMethod('shareUri', params) ?? 'dev.fluttercommunity.plus/share/unavailable'; diff --git a/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart b/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart index f1840624ef..1c396c2046 100644 --- a/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart +++ b/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart @@ -35,10 +35,12 @@ class SharePlatform extends PlatformInterface { Future shareUri( Uri uri, { Rect? sharePositionOrigin, + String? title, }) { return _instance.shareUri( uri, sharePositionOrigin: sharePositionOrigin, + title: title, ); }