Skip to content
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(android): run downloadFile asynchronously #2287

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ItsChaceD
Copy link
Contributor

Closes #6861

This PR runs downloadFile in an async task so it does not block other plugin calls.

@ItsChaceD ItsChaceD requested review from jcesarmobile and a team December 20, 2024 14:01
Copy link

Released dev build of filesystem with dev version: 7.0.0-dev-2287-20241220T140459.0

Comment on lines +310 to +311
public void downloadFile(PluginCall call, Bridge bridge, HttpRequestHandler.ProgressEmitter emitter,
FilesystemDownloadCallback callback) throws IOException, URISyntaxException, JSONException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this method no longer throws the mentioned exceptions, but rather returns them in FilesystemDownloadCallback, therefore we should update the signature

Suggested change
public void downloadFile(PluginCall call, Bridge bridge, HttpRequestHandler.ProgressEmitter emitter,
FilesystemDownloadCallback callback) throws IOException, URISyntaxException, JSONException {
public void downloadFile(PluginCall call, Bridge bridge, HttpRequestHandler.ProgressEmitter emitter,
FilesystemDownloadCallback callback) {

Comment on lines +317 to +324
AsyncTaskResult result = doDownloadInBackground(urlString, call, bridge, emitter);
handler.post(() -> {
if (result.error != null) {
callback.onError(result.error);
} else {
callback.onSuccess(result.result);
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that it is unnecessary to have this AsyncTaskResult, since it either just wraps the result or error if it exists.

I feel like we could lift the try-catch out of doDownloadInBackground, and have it return the JSObject directly

Suggested change
AsyncTaskResult result = doDownloadInBackground(urlString, call, bridge, emitter);
handler.post(() -> {
if (result.error != null) {
callback.onError(result.error);
} else {
callback.onSuccess(result.result);
}
});
try {
JSObject result = doDownloadInBackground(urlString, call, bridge, emitter);
handler.post(() -> callback.onSuccess(result));
} catch (Exception error) {
handler.post(() -> callback.onError(error));
}

Also, if you do this change, add the throws IOException, URISyntaxException, JSONException to the doDownloadInBackground signature.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants