From b4d40eed911a75a4c35456a535c28c693a84e095 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 8 May 2017 10:17:41 +0000 Subject: [PATCH] Add loading on login activity while fetching tokens --- .../keylesspalace/tusky/LoginActivity.java | 22 +++++ app/src/main/res/layout/activity_login.xml | 80 ++++++++++++------- app/src/main/res/values-fr/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 4 files changed, 78 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java b/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java index 8ec47748..9226dc6c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java @@ -32,6 +32,7 @@ import android.text.method.LinkMovementMethod; import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.TextView; import com.keylesspalace.tusky.entity.AccessToken; @@ -62,6 +63,9 @@ public class LoginActivity extends AppCompatActivity { private String clientId; private String clientSecret; + @BindView(R.id.login_input) LinearLayout input; + @BindView(R.id.login_loading) LinearLayout loading; + @BindView(R.id.edit_text_domain) EditText editText; @BindView(R.id.button_login) Button button; @BindView(R.id.whats_an_instance) TextView whatsAnInstance; @@ -326,6 +330,8 @@ public class LoginActivity extends AppCompatActivity { domain = preferences.getString("domain", null); clientId = preferences.getString("clientId", null); clientSecret = preferences.getString("clientSecret", null); + + setLoading(true); /* Since authorization has succeeded, the final step to log in is to exchange * the authorization code for an access token. */ Callback callback = new Callback() { @@ -334,6 +340,8 @@ public class LoginActivity extends AppCompatActivity { if (response.isSuccessful()) { onLoginSuccess(response.body().accessToken); } else { + setLoading(false); + editText.setError(getString(R.string.error_retrieving_oauth_token)); Log.e(TAG, String.format("%s %s", getString(R.string.error_retrieving_oauth_token), @@ -343,6 +351,7 @@ public class LoginActivity extends AppCompatActivity { @Override public void onFailure(Call call, Throwable t) { + setLoading(false); editText.setError(getString(R.string.error_retrieving_oauth_token)); Log.e(TAG, String.format("%s %s", getString(R.string.error_retrieving_oauth_token), @@ -355,21 +364,34 @@ public class LoginActivity extends AppCompatActivity { } else if (error != null) { /* Authorization failed. Put the error response where the user can read it and they * can try again. */ + setLoading(false); editText.setError(getString(R.string.error_authorization_denied)); Log.e(TAG, getString(R.string.error_authorization_denied) + error); } else { + setLoading(false); // This case means a junk response was received somehow. editText.setError(getString(R.string.error_authorization_unknown)); } } } + private void setLoading(boolean loadingState) { + if (loadingState) { + loading.setVisibility(View.VISIBLE); + input.setVisibility(View.GONE); + } else { + loading.setVisibility(View.GONE); + input.setVisibility(View.VISIBLE); + } + } + private void onLoginSuccess(String accessToken) { boolean committed = preferences.edit() .putString("domain", domain) .putString("accessToken", accessToken) .commit(); if (!committed) { + setLoading(false); editText.setError(getString(R.string.error_retrieving_oauth_token)); return; } diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 8df45c40..f65930e6 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -20,39 +20,63 @@ android:src="@drawable/elephant_friend" android:contentDescription="@null" /> - - + - - -