-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(share_plus): Recover ShareSuccessManager state after error #2817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,72 +8,68 @@ import java.io.IOException | |
| /** Handles the method calls for the plugin. */ | ||
| internal class MethodCallHandler( | ||
| private val share: Share, | ||
| private val manager: ShareSuccessManager | ||
| private val manager: ShareSuccessManager, | ||
| ) : MethodChannel.MethodCallHandler { | ||
|
|
||
| override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { | ||
| expectMapArguments(call) | ||
|
|
||
|
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to the top since it is common on all methods |
||
| // The user used a *WithResult method | ||
| val isResultRequested = call.method.endsWith("WithResult") | ||
| // We don't attempt to return a result if the current API version doesn't support it | ||
| val isWithResult = isResultRequested && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 | ||
| val isWithResult = | ||
| isResultRequested && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 | ||
|
|
||
| when (call.method) { | ||
| "shareUri" -> { | ||
| expectMapArguments(call) | ||
| share.share( | ||
| call.argument<Any>("uri") as String, | ||
| subject = null, | ||
| withResult= false | ||
| ) | ||
| if (!isWithResult) { | ||
| result.success(null) | ||
| } | ||
| } | ||
| "share", "shareWithResult" -> { | ||
| expectMapArguments(call) | ||
| if (isWithResult && !manager.setCallback(result)) return | ||
| if (isWithResult && !manager.setCallback(result)) return | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to the top since it is common for all methods as well. |
||
|
|
||
| // Android does not support showing the share sheet at a particular point on screen. | ||
| share.share( | ||
| call.argument<Any>("text") as String, | ||
| call.argument<Any>("subject") as String?, | ||
| isWithResult, | ||
| ) | ||
| try { | ||
| when (call.method) { | ||
| "shareUri" -> { | ||
| share.share( | ||
| call.argument<Any>("uri") as String, subject = null, withResult = false | ||
| ) | ||
| success(isWithResult, isResultRequested, result) | ||
| } | ||
|
|
||
| if (!isWithResult) { | ||
| if (isResultRequested) { | ||
| result.success("dev.fluttercommunity.plus/share/unavailable") | ||
| } else { | ||
| result.success(null) | ||
| } | ||
| "share", "shareWithResult" -> { | ||
| share.share( | ||
| call.argument<Any>("text") as String, | ||
| call.argument<Any>("subject") as String?, | ||
| isWithResult, | ||
| ) | ||
| success(isWithResult, isResultRequested, result) | ||
| } | ||
| } | ||
| "shareFiles", "shareFilesWithResult" -> { | ||
| expectMapArguments(call) | ||
| if (isWithResult && !manager.setCallback(result)) return | ||
|
|
||
| // Android does not support showing the share sheet at a particular point on screen. | ||
| try { | ||
| "shareFiles", "shareFilesWithResult" -> { | ||
| share.shareFiles( | ||
| call.argument<List<String>>("paths")!!, | ||
| call.argument<List<String>?>("mimeTypes"), | ||
| call.argument<String?>("text"), | ||
| call.argument<String?>("subject"), | ||
| isWithResult, | ||
| ) | ||
|
|
||
| if (!isWithResult) { | ||
| if (isResultRequested) { | ||
| result.success("dev.fluttercommunity.plus/share/unavailable") | ||
| } else { | ||
| result.success(null) | ||
| } | ||
| } | ||
| } catch (e: IOException) { | ||
| result.error("Share failed", e.message, null) | ||
| success(isWithResult, isResultRequested, result) | ||
| } | ||
|
|
||
| else -> result.notImplemented() | ||
| } | ||
| } catch (e: Throwable) { | ||
| manager.clear() | ||
| result.error("Share failed", e.message, e) | ||
| } | ||
|
Comment on lines
+56
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Catch all errors from share methods, pass them up to the Flutter layer, but before also call to This can be tested simply by calling await Share.shareXFiles(
[XFile('/wrong/file')],
text: text,
);then the user sees something like however, they will be able to use the share methods again later on because |
||
| } | ||
|
|
||
| private fun success( | ||
| isWithResult: Boolean, | ||
| isResultRequested: Boolean, | ||
| result: MethodChannel.Result | ||
| ) { | ||
| if (!isWithResult) { | ||
| if (isResultRequested) { | ||
| result.success("dev.fluttercommunity.plus/share/unavailable") | ||
| } else { | ||
| result.success(null) | ||
|
Comment on lines
+62
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just moved all the |
||
| } | ||
| else -> result.notImplemented() | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the tailing comma to avoid code formatting to a single line