-
-
Notifications
You must be signed in to change notification settings - Fork 871
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
fix(share_plus): Recover ShareSuccessManager state after error #2817
Conversation
@@ -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, |
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
expectMapArguments(call) | ||
|
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.
Moved to the top since it is common on all methods
"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 comment
The 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.
...plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt
Outdated
Show resolved
Hide resolved
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) |
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.
Just moved all the result.success()
code to a common method to avoid code duplication in the switch
} catch (e: Throwable) { | ||
manager.clear() | ||
result.error("Share failed", e.message, e) | ||
} |
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.
Catch all errors from share methods, pass them up to the Flutter layer, but before also call to manager.clear()
to avoid the deadlock state in ShareSuccessManager
.
This can be tested simply by calling
await Share.shareXFiles(
[XFile('/wrong/file')],
text: text,
);
then the user sees something like
E/flutter (29015): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Share failed, /wrong/file: The source file doesn't exist.,
however, they will be able to use the share methods again later on because manager.clear()
was called.
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.
Thanks for tackling this bug
I believe it also closes #2515 even though the report didn't bother to provide meaningful details requested. |
Description
ShareSuccessManager
was in a bad state which blocked users from continuing using the plugin.ShareSuccessManager
checks the state of the current callback state, and fails if a callback was never executed. This is problematic because causes this state to be never recover after an error happened.onActivityResult
is never called, so the state is never restored.clear()
method that will reset the state, only call to this method in cases we catch an error.MethodCallHandler
.shareXFiles
with badXFile
(wrong paths), which causes an exception. Then I call theshare
methods normally and still works, both with result and without.ShareSuccessManager
is in a bad state, we can use the clear method in those edge cases to clean up the state.Related Issues
Checklist
CHANGELOG.md
nor the plugin version inpubspec.yaml
files.flutter analyze
) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?
!
in the title as explained in Conventional Commits).