Skip to content

Commit

Permalink
Merge pull request nickbutcher#86 from amilcar-andrade/snackbar-crash…
Browse files Browse the repository at this point in the history
…-dribble-login

Fixes NPE when response body is null and adds visual indicator
  • Loading branch information
nickbutcher committed May 10, 2016
2 parents 7ae5d88 + bce97dc commit 7395e1a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
31 changes: 28 additions & 3 deletions app/src/main/java/io/plaidapp/ui/DribbbleLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@
import retrofit2.converter.gson.GsonConverterFactory;

public class DribbbleLogin extends Activity {
private static final String STATE_LOGIN_FAILED = "loginFailed";

boolean isDismissing = false;
boolean isLoginFailed = false;
private ViewGroup container;
private TextView message;
private Button login;
private ProgressBar loading;
private DribbblePrefs dribbblePrefs;
private TextView loginFailed;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -78,9 +81,15 @@ protected void onCreate(Bundle savedInstanceState) {
message = (TextView) findViewById(R.id.login_message);
login = (Button) findViewById(R.id.login);
loading = (ProgressBar) findViewById(R.id.loading);
loginFailed = (TextView) container.findViewById(R.id.login_failed_message);
loading.setVisibility(View.GONE);
dribbblePrefs = DribbblePrefs.get(this);

if (savedInstanceState != null) {
isLoginFailed = savedInstanceState.getBoolean(STATE_LOGIN_FAILED, false);
loginFailed.setVisibility(isLoginFailed ? View.VISIBLE : View.GONE);
}

checkAuthCallback(getIntent());
}

Expand All @@ -106,10 +115,17 @@ public void onBackPressed() {
dismiss(null);
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean(STATE_LOGIN_FAILED, isLoginFailed);
}

private void showLoading() {
TransitionManager.beginDelayedTransition(container);
message.setVisibility(View.GONE);
login.setVisibility(View.GONE);
loginFailed.setVisibility(View.GONE);
loading.setVisibility(View.VISIBLE);
}

Expand Down Expand Up @@ -144,6 +160,11 @@ private void getAccessToken(String code) {
accessTokenCall.enqueue(new Callback<AccessToken>() {
@Override
public void onResponse(Call<AccessToken> call, Response<AccessToken> response) {
if (response.body() == null) {
showLoginFailed();
return;
}
isLoginFailed = false;
dribbblePrefs.setAccessToken(response.body().access_token);
showLoggedInUser();
setResult(Activity.RESULT_OK);
Expand All @@ -153,13 +174,17 @@ public void onResponse(Call<AccessToken> call, Response<AccessToken> response) {
@Override
public void onFailure(Call<AccessToken> call, Throwable t) {
Log.e(getClass().getCanonicalName(), t.getMessage(), t);
// TODO snackbar?
Toast.makeText(getApplicationContext(), "Login failed", Toast.LENGTH_LONG).show();
showLogin();
showLoginFailed();
}
});
}

private void showLoginFailed() {
isLoginFailed = true;
showLogin();
loginFailed.setVisibility(View.VISIBLE);
}

private void showLoggedInUser() {
final Call<User> authenticatedUser = dribbblePrefs.getApi().getAuthenticatedUser();
authenticatedUser.enqueue(new Callback<User>() {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_dribbble_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
android:drawablePadding="@dimen/spacing_micro"
android:onClick="doLogin" />

<TextView
android:id="@+id/login_failed_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="@color/error"
android:visibility="gone"
android:text="@string/dribbble_login_failed" />

<include layout="@layout/loading" />

</LinearLayout>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
<string name="sign_up">Sign up</string>
<string name="dn_login_action">Log in</string>

<!-- dribble login -->
<string name="dribbble_login_failed">Login failed</string>

<!-- designer news story -->
<string name="upvote_story">Upvote story</string>
<!-- using strings for the number of views/likes as we run them through NumberFormat -->
Expand Down

0 comments on commit 7395e1a

Please sign in to comment.