Source:
public void loadLocations(final String url) { loginView.showLocationLoadingAnimation(); if (NetworkUtils.hasNetwork()) { String locationEndPoint = url + ApplicationConstants.API.REST_ENDPOINT + "location"; Call<Results<Location>> call = restApi.getLocations(locationEndPoint, "Login Location", "full"); call.enqueue(new Callback<Results<Location>>() { @Override public void onResponse(@NonNull Call<Results<Location>> call, @NonNull Response<Results<Location>> response) { if (response.isSuccessful()) { RestServiceBuilder.changeBaseUrl(url.trim()); mOpenMRS.setServerUrl(url); loginView.initLoginForm(response.body().getResults(), url); loginView.startFormListService(); loginView.setLocationErrorOccurred(false); } else { loginView.showInvalidURLSnackbar("The entered server is currently down, please select a different one."); loginView.setLocationErrorOccurred(true); loginView.initLoginForm(new ArrayList<>(), url); } loginView.hideUrlLoadingAnimation(); } @Override public void onFailure(@NonNull Call<Results<Location>> call, @NonNull Throwable t) { loginView.hideUrlLoadingAnimation(); loginView.showInvalidURLSnackbar(t.getMessage()); loginView.initLoginForm(new ArrayList<>(), url); loginView.setLocationErrorOccurred(true); } }); } else { addSubscription(locationDAO.getLocations() .observeOn(AndroidSchedulers.mainThread()) .subscribe(locations -> { if (locations.size() > 0) { loginView.initLoginForm(locations, url); loginView.setLocationErrorOccurred(false); } else { loginView.showToast("Network not available.", ToastUtil.ToastType.ERROR); loginView.setLocationErrorOccurred(true); } loginView.hideLoadingAnimation(); })); } }

Target:
public void loadLocations(final String url) { loginView.showLocationLoadingAnimation(); if (NetworkUtils.hasNetwork()) { String locationEndPoint = url + ApplicationConstants.API.REST_ENDPOINT + "location"; Call<Results<Location>> call = restApi.getLocations(locationEndPoint, "Login Location", "full"); call.enqueue(new Callback<Results<Location>>() { @Override public void onResponse(@NonNull Call<Results<Location>> call, @NonNull Response<Results<Location>> response) { if (response.isSuccessful()) { RestServiceBuilder.changeBaseUrl(url.trim()); mOpenMRS.setServerUrl(url); loginView.initLoginForm(response.body().getResults(), url); loginView.startFormListService(); loginView.setLocationErrorOccurred(false); } else { loginView.showInvalidURLSnackbar(R.string.snackbar_server_error); loginView.setLocationErrorOccurred(true); loginView.initLoginForm(new ArrayList<>(), url); } loginView.hideUrlLoadingAnimation(); } @Override public void onFailure(@NonNull Call<Results<Location>> call, @NonNull Throwable t) { loginView.hideUrlLoadingAnimation(); loginView.showInvalidURLSnackbar(t.getMessage()); loginView.initLoginForm(new ArrayList<>(), url); loginView.setLocationErrorOccurred(true); } }); } else { addSubscription(locationDAO.getLocations() .observeOn(AndroidSchedulers.mainThread()) .subscribe(locations -> { if (locations.size() > 0) { loginView.initLoginForm(locations, url); loginView.setLocationErrorOccurred(false); } else { loginView.showToast(R.string.no_internet_connection_message, ToastUtil.ToastType.ERROR); loginView.setLocationErrorOccurred(true); } loginView.hideLoadingAnimation(); })); } }