@Override
  public void onBlocksLoaded(Response response) {
    Logger.v("Response code:", "Response Code: " + response.getResponseCode() + response.getBody());
    try {

      Gson gson = new Gson();

      if (response.getResponseCode() == 200) {
        BlocksList blocksList = gson.fromJson(response.getBody(), BlocksList.class);
        mBlocks = blocksList.getBlocks();
        BlocksAdapter blocksAdapter = new BlocksAdapter(mBlocks, this);
        mRecyclerView.setAdapter(blocksAdapter);
        mRecyclerView.setVisibility(View.VISIBLE);
        mProgressWheel.setVisibility(View.GONE);
        mConnectionContainer.setVisibility(View.GONE);
        mFragmentLoader.onFragmentLoaded();
      } else {
        onBlocksLoadFailed(response);
      }

    } catch (Exception e) {
      e.printStackTrace();
      couldNotLoadBlocks();
    }
  }
Пример #2
0
  /**
   * Registers the user with the given data at the server
   *
   * @param firstName the user's first name
   * @param lastName the user's last name
   * @param email the user's registerEmail address
   * @param password the user's password
   */
  private void registerUser(
      final String firstName, final String lastName, final String email, final String password) {

    // UI: Disable register button and show progress bar
    registerButton.setEnabled(false);
    registerProgressBar.setVisibility(View.VISIBLE);

    // Create a new user on the server
    UserDescription userDescription = new UserDescription(email, firstName, lastName, password);
    usersResource
        .registerUser(userDescription)
        .compose(new DefaultTransformer<User>())
        .subscribe(
            new Action1<User>() {

              @Override
              public void call(User user) {
                // REGISTER SUCCESS

                // UI: Hide progress bar
                registerProgressBar.setVisibility(View.GONE);

                Timber.i("Successfully registered user with registerEmail " + user.getEmail());

                // Finally log the user in locally and start the main app
                loginAndRedirect(user, password);
              }
            },
            new CrashCallback(
                this,
                "failed to register user",
                new Action1<Throwable>() {
                  @Override
                  public void call(Throwable throwable) {
                    // UI: Re-enable register button and hide progress bar
                    registerButton.setEnabled(true);
                    registerProgressBar.setVisibility(View.GONE);

                    Response response = ((RetrofitError) throwable).getResponse();

                    String message;
                    if (response != null && response.getStatus() == 409) { // Conflict
                      message = getString(R.string.registration_error_conflict);
                    } else {
                      message = getString(R.string.registration_error_general);
                    }

                    // Show an error message
                    Toast.makeText(LoginActivity.this, message, Toast.LENGTH_LONG).show();
                    Timber.e("Registration failed with error:\n" + throwable.getMessage());
                  }
                }));
  }
Пример #3
0
  /**
   * Logs a user in by connecting to the server and checking the given registerEmail and password
   *
   * @param email the user's registerEmail address
   * @param password the user's password
   */
  private void loginUser(final String email, final String password) {
    // UI: Show progress bar, disable login button
    loginButton.setEnabled(false);
    loginProgressBar.setVisibility(View.VISIBLE);
    loginErrorTextView.setVisibility(View.GONE);

    // Create new adapter to set "custom" auth header (user not officially logged in yet)
    UsersResource usersResource =
        new RestAdapter.Builder()
            .setEndpoint(getString(R.string.server_address))
            .setRequestInterceptor(
                new RequestInterceptor() {
                  @Override
                  public void intercept(RequestFacade request) {
                    AccountManager.addAuthorizationHeader(email, password, request);
                  }
                })
            .setConverter(new JacksonConverter())
            .build()
            .create(UsersResource.class);

    // Make the actual call to the server to receive the correct user object
    usersResource
        .getUser()
        .compose(new DefaultTransformer<User>())
        .subscribe(
            new Action1<User>() {

              @Override
              public void call(User user) {
                // LOGIN SUCCESS
                Timber.d("Successful Login as " + user.getFirstName() + " " + user.getLastName());
                // UI: Hide progress bar
                loginProgressBar.setVisibility(View.GONE);

                // Finally log the user in locally and start the main app
                loginAndRedirect(user, password);
              }
            },
            new CrashCallback(this, "failed to get user") {

              @Override
              public void call(Throwable throwable) {
                super.call(throwable);
                // ERROR

                // UI: Hide progress bar, enable login button again
                loginProgressBar.setVisibility(View.GONE);
                loginButton.setEnabled(true);

                Response response = ((RetrofitError) throwable).getResponse();

                if (response != null && response.getStatus() == 401) { // Not Authorized
                  // Show error message for invalid login
                  loginErrorTextView.setVisibility(View.VISIBLE);
                } else {
                  // Show an error for general errors e.g. connection issues
                  String errorMessage = LoginActivity.this.getString(R.string.login_error_general);
                  Toast.makeText(LoginActivity.this, errorMessage, Toast.LENGTH_LONG).show();
                }

                Timber.e("Logging the user in failed with error:\n" + throwable.getMessage());
              }
            });
  }
 private void onRetryButtonClicked() {
   mNoInternetLL.setVisibility(View.GONE);
   mProgressWheel.setVisibility(View.VISIBLE);
   loadBlocks();
 }
 private void onNoInternet() {
   mConnectionContainer.setVisibility(View.VISIBLE);
   mNoInternetLL.setVisibility(View.VISIBLE);
   mProgressWheel.setVisibility(View.GONE);
   mRecyclerView.setVisibility(View.GONE);
 }
 @Override
 public void hideProgress() {
   inProgress = false;
   progress.setVisibility(View.GONE);
 }
 @Override
 public void showProgress() {
   inProgress = true;
   progress.setVisibility(View.VISIBLE);
 }