allow cancelling reload requests

Summary:
Make the ProgressDialog cancelable and cancel the network request / websocket connection when the user presses back.

public

Reviewed By: andreicoman11

Differential Revision: D2764788

fb-gh-sync-id: 0fdb87ba9431be5a3c453422724cd364292eff61
This commit is contained in:
Felix Oghina 2016-01-07 08:33:30 -08:00 committed by facebook-github-bot-3
parent 6dc6794881
commit cb94d997ca
2 changed files with 31 additions and 2 deletions

View File

@ -91,6 +91,7 @@ public class DevServerHelper {
private boolean mOnChangePollingEnabled; private boolean mOnChangePollingEnabled;
private @Nullable OkHttpClient mOnChangePollingClient; private @Nullable OkHttpClient mOnChangePollingClient;
private @Nullable OnServerContentChangeListener mOnServerContentChangeListener; private @Nullable OnServerContentChangeListener mOnServerContentChangeListener;
private @Nullable Call mDownloadBundleFromURLCall;
public DevServerHelper(DevInternalSettings settings) { public DevServerHelper(DevInternalSettings settings) {
mSettings = settings; mSettings = settings;
@ -179,15 +180,29 @@ public class DevServerHelper {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(bundleURL) .url(bundleURL)
.build(); .build();
Call call = mClient.newCall(request); mDownloadBundleFromURLCall = Assertions.assertNotNull(mClient.newCall(request));
call.enqueue(new Callback() { mDownloadBundleFromURLCall.enqueue(new Callback() {
@Override @Override
public void onFailure(Request request, IOException e) { public void onFailure(Request request, IOException e) {
// ignore callback if call was cancelled
if (mDownloadBundleFromURLCall == null || mDownloadBundleFromURLCall.isCanceled()) {
mDownloadBundleFromURLCall = null;
return;
}
mDownloadBundleFromURLCall = null;
callback.onFailure(e); callback.onFailure(e);
} }
@Override @Override
public void onResponse(Response response) throws IOException { public void onResponse(Response response) throws IOException {
// ignore callback if call was cancelled
if (mDownloadBundleFromURLCall == null || mDownloadBundleFromURLCall.isCanceled()) {
mDownloadBundleFromURLCall = null;
return;
}
mDownloadBundleFromURLCall = null;
// Check for server errors. If the server error has the expected form, fail with more info. // Check for server errors. If the server error has the expected form, fail with more info.
if (!response.isSuccessful()) { if (!response.isSuccessful()) {
String body = response.body().string(); String body = response.body().string();
@ -214,6 +229,13 @@ public class DevServerHelper {
}); });
} }
public void cancelDownloadBundleFromURL() {
if (mDownloadBundleFromURLCall != null) {
mDownloadBundleFromURLCall.cancel();
mDownloadBundleFromURLCall = null;
}
}
public void isPackagerRunning(final PackagerStatusCallback callback) { public void isPackagerRunning(final PackagerStatusCallback callback) {
String statusURL = createPackagerStatusURL(getDebugServerHost()); String statusURL = createPackagerStatusURL(getDebugServerHost());
Request request = new Request.Builder() Request request = new Request.Builder()

View File

@ -629,6 +629,13 @@ public class DevSupportManager implements NativeModuleCallExceptionHandler {
}, },
Assertions.assertNotNull(mJSAppBundleName), Assertions.assertNotNull(mJSAppBundleName),
mJSBundleTempFile); mJSBundleTempFile);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mDevServerHelper.cancelDownloadBundleFromURL();
}
});
progressDialog.setCancelable(true);
} }
private void reload() { private void reload() {