How to properly recover a recovery from a crowded unsigned request using RoboSpice

I have a simple snippet that handles logins for my application. Since I'm dealing with login requests, I don't want to cache them. This strategy works fine until I introduce a temporary or orientation change in the middle of the request. When the user clicks the login button, I show the ProgressDialog. This disappears when I receive a response (success or failure). If I go to the home screen and return to my application in the middle of a login request, my listener will never receive a notification and as a result, my ProgressDialog will not be fired and my application will be frozen. I tried adding spiceManager.getFromCache to my onStart. It helps, but the result is always zero when the application tries to repair ... it makes sense since the results are not cached.What's the correct way to set up my listener for notification in this scenario?

// using Jackson2SpringAndroidSpiceService

public void onStart() {
    super.onStart();
    spiceManager.start(getActivity());
    spiceManager.addListenerIfPending(AccessTokenResponse.class, null,
            new AccessTokenResponseRequestListener());

    //spiceManager.getFromCache(AccessTokenResponse.class,
    //        null, DurationInMillis.ALWAYS_EXPIRED,
    //        new AccessTokenResponseRequestListener());
}


private void performRequest(String username, String password) {
    progressDialog = ProgressDialog.show(getActivity(), "", "Logging in...", true);
    LoginFragment.this.getActivity().setProgressBarIndeterminateVisibility(true);
    LoginRequest request = new LoginRequest(username, password);
    spiceManager.execute(request, null, DurationInMillis.ALWAYS_EXPIRED, new AccessTokenResponseRequestListener());
}


private class AccessTokenResponseRequestListener implements RequestListener<AccessTokenResponse> {

    @Override
    public void onRequestFailure(SpiceException e) {
        //update your UI
        if(progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        buttonLogin.setEnabled(true);
        Log.e(TAG, "Login unsuccessful");
        if(e.getCause() instanceof HttpClientErrorException)
        {
            HttpClientErrorException exception = (HttpClientErrorException)e.getCause();
            if(exception.getStatusCode().equals(HttpStatus.BAD_REQUEST))
            {
                Log.e(TAG, "Login unsuccessful");
                Toast.makeText(getActivity().getApplicationContext(),
                        "Wrong username/password combo!",
                        Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(getActivity().getApplicationContext(),
                        "Login unsuccessful! If the problem persists, please contact support.",
                        Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(getActivity().getApplicationContext(),
                "Login unsuccessful! If the problem persists, please contact support.",
                Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onRequestSuccess(AccessTokenResponse accessToken) {
        //update  UI
        if(progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        buttonLogin.setEnabled(true);

        if (accessToken != null) { 
            OnAuthenticatedListener listener = (OnAuthenticatedListener) getActivity();
            listener.userLoggedIn(editTextUsername.getText().toString(), accessToken);
        }

    }
}

      

+3


source to share


1 answer


Use cache. Execute a request using a cache key

spiceManager.execute(request, "your_cache_key", DurationInMillis.ALWAYS_EXPIRED, new AccessTokenResponseRequestListener());

      

and in listening, remove the response to that request from the cache if it returned successfully before switching to another action as you don't want to cache the account information as per your requirement.



@Override
public void onRequestFailure(SpiceException e) {
    ....
    spiceManager.removeDataFromCache(AccessTokenResponse.class);
    ....
}

@Override
public void onRequestSuccess(AccessTokenResponse accessToken) {
    if (accessToken == null) {
        return;
    }
    ....
    spiceManager.removeDataFromCache(AccessTokenResponse.class);
    ....
}

      

In onStart, try to get a cached response if you switched to another activity and are now returning to the previous activity. This return response that comes after calling spiceManager.shouldStop (). Returns null otherwise.

spiceManager.getFromCache(AccessTokenResponse.class, "your_cache_key", DurationInMillis.ALWAYS_RETURNED, new AccessTokenResponseRequestListener());

      

+3


source







All Articles