Better login screen

This commit is contained in:
Bazsalanszky 2023-07-25 16:08:44 +02:00
parent 0054413e89
commit a2ad876517
3 changed files with 78 additions and 18 deletions

View File

@ -7,9 +7,11 @@ import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.util.Patterns;
import android.view.InflateException;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
@ -19,6 +21,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.textfield.TextInputEditText;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import org.json.JSONException;
import org.json.JSONObject;
@ -73,6 +76,9 @@ public class LoginActivity extends BaseActivity {
@BindView(R.id.user_login_button)
Button loginButton;
@BindView(R.id.login_progress)
ProgressBar progressBar;
@Inject
@Named("no_oauth")
RetrofitHolder mRetrofit;
@ -130,8 +136,19 @@ public class LoginActivity extends BaseActivity {
}
loginButton.setOnClickListener(view -> {
Log.i("LoginActivity", "Login button clicked");
loginButton.setEnabled(false);
progressBar.setVisibility(ProgressBar.VISIBLE);
String username = username_input.getText().toString();
String instance = instance_input.getText().toString();
String instance = correctURL(instance_input.getText().toString());
if(!Patterns.WEB_URL.matcher(instance).matches()){
instance_input.setError("Invalid instance URL");
Toast.makeText(LoginActivity.this, "Invalid instance URL", Toast.LENGTH_SHORT).show();
loginButton.setEnabled(true);
progressBar.setVisibility(ProgressBar.GONE);
return;
}
Log.i("LoginActivity", "Instance: " + instance);
AccountLoginDTO accountLoginDTO = new AccountLoginDTO(username,password_input.getText().toString(),token_2fa_input.getText().toString());
mRetrofit.setBaseURL(instance);
LemmyAPI api = mRetrofit.getRetrofit().create(LemmyAPI.class);
@ -139,15 +156,16 @@ public class LoginActivity extends BaseActivity {
accessTokenCall.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
progressBar.setVisibility(ProgressBar.GONE);
loginButton.setEnabled(true);
String accountResponse = response.body();
if (accountResponse == null) {
Log.e("LoginActivity", "Account response is null");
Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
//Handle error
return;
}
if (response.isSuccessful()) {
Log.i("test", "WIN");
String accountResponse = response.body();
if (accountResponse == null) {
//Handle error
return;
}
Log.i("test", accountResponse);
try {
JSONObject responseJSON = new JSONObject(accountResponse);
String accessToken = responseJSON.getString("jwt");
@ -187,22 +205,44 @@ public class LoginActivity extends BaseActivity {
return;
}
String accountResponse = response.body();
if (accountResponse == null) {
//Handle error
return;
try {
JSONObject responseObject = new JSONObject(accountResponse);
if(responseObject.has("error")) {
Toast.makeText(LoginActivity.this, "Error:"+responseObject.getString("error"), Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
}
Log.i("test", accountResponse);
Log.e("LoginActivity", "Failed to get access token: " + response.code() + " " + response.message() + " " + response.errorBody());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.i("test", "LOSE");
progressBar.setVisibility(ProgressBar.GONE);
loginButton.setEnabled(true);
Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show();
}
});
});
}
private static String correctURL(String url) {
if (url == null || url.isEmpty()) {
throw new IllegalArgumentException("URL cannot be null or empty");
}
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "https://" + url;
}
return url;
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {

View File

@ -54,10 +54,19 @@
android:id="@+id/instance_url_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/instance_url" />
android:hint="@string/instance_url"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="@+id/login_instance_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:text="The URL of you prefered Lemmy instance with or without the https:// prefix" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
@ -67,7 +76,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/afs_md2_popup_margin_end"
android:hint="@string/user_username" />
android:hint="@string/user_username"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
@ -80,6 +90,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/user_password"
android:maxLines="1"
android:password="true" />
</com.google.android.material.textfield.TextInputLayout>
@ -92,7 +103,8 @@
android:id="@+id/user_2fa_token_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/user_2fa_token" />
android:hint="@string/user_2fa_token"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<Button
@ -101,6 +113,13 @@
android:layout_height="wrap_content"
android:text="@string/user_login" />
<ProgressBar
android:id="@+id/login_progress"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>

View File

@ -1326,6 +1326,7 @@
<string name="reddit_api_info">Starting from July 1st, 2023, Reddit API will be pay-per-use for 3rd-party clients, which include Infinity for Reddit. The announcement from Reddit can be found here: %1$s\n\nIn order to survive this change, Infinity will become a subscription-only app after July 1st. You can learn more about the changes in this post: %2$s\n\nIt\'s required for you to update Infinity after July 1st so that you will get the new version with subscription options. None of the previous versions (including this one) will work after July 1st. But due to a tight timeline Reddit gave, the update may not be available immediately on July 1st since it requires proper testing. Thank you for your understanding!</string>
<string name="i_understand">I understand</string>
<string name="instance_url">Instance URL</string>
<string name="instance_url_hint">The URL of you preferred Lemmy instance with or without the https:// prefix</string>
<string name="user_username">Username</string>
<string name="user_password">Password</string>
<string name="user_2fa_token">2FA token (if needed)</string>