mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Refactor : API calls moved out of the LoginActivity (#5599)
* Refactor :API calls separated from activity added to LoginClient * getLoginToken() modifier set to private * Code Cleanup : removed non-null from twofactor and Locale import * Indentation fix
This commit is contained in:
parent
2c376da46e
commit
28ea11127c
3 changed files with 71 additions and 72 deletions
|
|
@ -30,9 +30,9 @@ import fr.free.nrw.commons.auth.login.LoginResult;
|
||||||
import fr.free.nrw.commons.databinding.ActivityLoginBinding;
|
import fr.free.nrw.commons.databinding.ActivityLoginBinding;
|
||||||
import fr.free.nrw.commons.utils.ActivityUtils;
|
import fr.free.nrw.commons.utils.ActivityUtils;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse;
|
|
||||||
import fr.free.nrw.commons.auth.login.LoginCallback;
|
import fr.free.nrw.commons.auth.login.LoginCallback;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
|
|
||||||
|
|
@ -46,9 +46,6 @@ import fr.free.nrw.commons.utils.ConfigUtils;
|
||||||
import fr.free.nrw.commons.utils.SystemThemeUtils;
|
import fr.free.nrw.commons.utils.SystemThemeUtils;
|
||||||
import fr.free.nrw.commons.utils.ViewUtil;
|
import fr.free.nrw.commons.utils.ViewUtil;
|
||||||
import io.reactivex.disposables.CompositeDisposable;
|
import io.reactivex.disposables.CompositeDisposable;
|
||||||
import retrofit2.Call;
|
|
||||||
import retrofit2.Callback;
|
|
||||||
import retrofit2.Response;
|
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
import static android.view.KeyEvent.KEYCODE_ENTER;
|
import static android.view.KeyEvent.KEYCODE_ENTER;
|
||||||
|
|
@ -75,7 +72,6 @@ public class LoginActivity extends AccountAuthenticatorActivity {
|
||||||
private AppCompatDelegate delegate;
|
private AppCompatDelegate delegate;
|
||||||
private LoginTextWatcher textWatcher = new LoginTextWatcher();
|
private LoginTextWatcher textWatcher = new LoginTextWatcher();
|
||||||
private CompositeDisposable compositeDisposable = new CompositeDisposable();
|
private CompositeDisposable compositeDisposable = new CompositeDisposable();
|
||||||
private Call<MwQueryResponse> loginToken;
|
|
||||||
final String saveProgressDailog="ProgressDailog_state";
|
final String saveProgressDailog="ProgressDailog_state";
|
||||||
final String saveErrorMessage ="errorMessage";
|
final String saveErrorMessage ="errorMessage";
|
||||||
final String saveUsername="username";
|
final String saveUsername="username";
|
||||||
|
|
@ -116,7 +112,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
|
||||||
binding.loginCredentials.setVisibility(View.GONE);
|
binding.loginCredentials.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Hides the keyboard if the user's focus is not on the password (hasFocus is false).
|
* Hides the keyboard if the user's focus is not on the password (hasFocus is false).
|
||||||
* @param view The keyboard
|
* @param view The keyboard
|
||||||
* @param hasFocus Set to true if the keyboard has focus
|
* @param hasFocus Set to true if the keyboard has focus
|
||||||
|
|
@ -212,63 +208,52 @@ public class LoginActivity extends AccountAuthenticatorActivity {
|
||||||
|
|
||||||
public void performLogin() {
|
public void performLogin() {
|
||||||
Timber.d("Login to start!");
|
Timber.d("Login to start!");
|
||||||
final String username = binding.loginUsername.getText().toString();
|
final String username = Objects.requireNonNull(binding.loginUsername.getText()).toString();
|
||||||
final String rawUsername = binding.loginUsername.getText().toString().trim();
|
final String password = Objects.requireNonNull(binding.loginPassword.getText()).toString();
|
||||||
final String password = binding.loginPassword.getText().toString();
|
final String twoFactorCode = Objects.requireNonNull(binding.loginTwoFactor.getText()).toString();
|
||||||
String twoFactorCode = binding.loginTwoFactor.getText().toString();
|
|
||||||
|
|
||||||
showLoggingProgressBar();
|
showLoggingProgressBar();
|
||||||
doLogin(username, password, twoFactorCode);
|
loginClient.doLogin(username, password, twoFactorCode, Locale.getDefault().getLanguage(),
|
||||||
|
new LoginCallback() {
|
||||||
|
@Override
|
||||||
|
public void success(@NonNull LoginResult loginResult) {
|
||||||
|
runOnUiThread(()->{
|
||||||
|
Timber.d("Login Success");
|
||||||
|
hideProgress();
|
||||||
|
onLoginSuccess(loginResult);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void twoFactorPrompt(@NonNull Throwable caught, @Nullable String token) {
|
||||||
|
runOnUiThread(()->{
|
||||||
|
Timber.d("Requesting 2FA prompt");
|
||||||
|
hideProgress();
|
||||||
|
askUserForTwoFactorAuth();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void passwordResetPrompt(@Nullable String token) {
|
||||||
|
runOnUiThread(()->{
|
||||||
|
Timber.d("Showing password reset prompt");
|
||||||
|
hideProgress();
|
||||||
|
showPasswordResetPrompt();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void error(@NonNull Throwable caught) {
|
||||||
|
runOnUiThread(()->{
|
||||||
|
Timber.e(caught);
|
||||||
|
hideProgress();
|
||||||
|
showMessageAndCancelDialog(caught.getLocalizedMessage());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doLogin(String username, String password, String twoFactorCode) {
|
|
||||||
progressDialog.show();
|
|
||||||
loginToken = loginClient.getLoginToken();
|
|
||||||
loginToken.enqueue(
|
|
||||||
new Callback<MwQueryResponse>() {
|
|
||||||
@Override
|
|
||||||
public void onResponse(Call<MwQueryResponse> call,
|
|
||||||
Response<MwQueryResponse> response) {
|
|
||||||
loginClient.login(username, password, null, twoFactorCode,
|
|
||||||
response.body().query().loginToken(), Locale.getDefault().getLanguage(), new LoginCallback() {
|
|
||||||
@Override
|
|
||||||
public void success(@NonNull LoginResult result) {
|
|
||||||
Timber.d("Login Success");
|
|
||||||
onLoginSuccess(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void twoFactorPrompt(@NonNull Throwable caught,
|
|
||||||
@Nullable String token) {
|
|
||||||
Timber.d("Requesting 2FA prompt");
|
|
||||||
hideProgress();
|
|
||||||
askUserForTwoFactorAuth();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void passwordResetPrompt(@Nullable String token) {
|
|
||||||
Timber.d("Showing password reset prompt");
|
|
||||||
hideProgress();
|
|
||||||
showPasswordResetPrompt();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void error(@NonNull Throwable caught) {
|
|
||||||
Timber.e(caught);
|
|
||||||
hideProgress();
|
|
||||||
showMessageAndCancelDialog(caught.getLocalizedMessage());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(Call<MwQueryResponse> call, Throwable t) {
|
|
||||||
Timber.e(t);
|
|
||||||
showMessageAndCancelDialog(t.getLocalizedMessage());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void hideProgress() {
|
private void hideProgress() {
|
||||||
progressDialog.dismiss();
|
progressDialog.dismiss();
|
||||||
|
|
@ -299,10 +284,6 @@ public class LoginActivity extends AccountAuthenticatorActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onLoginSuccess(LoginResult loginResult) {
|
private void onLoginSuccess(LoginResult loginResult) {
|
||||||
if (!progressDialog.isShowing()) {
|
|
||||||
// no longer attached to activity!
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
compositeDisposable.clear();
|
compositeDisposable.clear();
|
||||||
sessionManager.setUserLoggedIn(true);
|
sessionManager.setUserLoggedIn(true);
|
||||||
sessionManager.updateAccount(loginResult);
|
sessionManager.updateAccount(loginResult);
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ class LoginClient(private val loginInterface: LoginInterface) {
|
||||||
*/
|
*/
|
||||||
private var userLanguage = ""
|
private var userLanguage = ""
|
||||||
|
|
||||||
fun getLoginToken() = loginInterface.getLoginToken()
|
private fun getLoginToken() = loginInterface.getLoginToken()
|
||||||
|
|
||||||
fun request(userName: String, password: String, cb: LoginCallback) {
|
fun request(userName: String, password: String, cb: LoginCallback) {
|
||||||
cancel()
|
cancel()
|
||||||
|
|
@ -106,6 +106,33 @@ class LoginClient(private val loginInterface: LoginInterface) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun doLogin(
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
twoFactorCode: String,
|
||||||
|
userLanguage: String,
|
||||||
|
loginCallback: LoginCallback
|
||||||
|
) {
|
||||||
|
getLoginToken().enqueue(object :Callback<MwQueryResponse?>{
|
||||||
|
override fun onResponse(
|
||||||
|
call: Call<MwQueryResponse?>,
|
||||||
|
response: Response<MwQueryResponse?>
|
||||||
|
) = if (response.isSuccessful){
|
||||||
|
val loginToken = response.body()?.query()?.loginToken()
|
||||||
|
loginToken?.let {
|
||||||
|
login(username, password, null, twoFactorCode, it, userLanguage, loginCallback)
|
||||||
|
} ?: run {
|
||||||
|
loginCallback.error(IOException("Failed to retrieve login token"))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
loginCallback.error(IOException("Failed to retrieve login token"))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailure(call: Call<MwQueryResponse?>, t: Throwable) {
|
||||||
|
loginCallback.error(t)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@Throws(Throwable::class)
|
@Throws(Throwable::class)
|
||||||
fun loginBlocking(userName: String, password: String, twoFactorCode: String?) {
|
fun loginBlocking(userName: String, password: String, twoFactorCode: String?) {
|
||||||
val tokenResponse = getLoginToken().execute()
|
val tokenResponse = getLoginToken().execute()
|
||||||
|
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!-- Authors:
|
|
||||||
* Winston Sung
|
|
||||||
-->
|
|
||||||
<resources>
|
|
||||||
<string name="crash_dialog_title">同享壞咗</string>
|
|
||||||
<string name="crash_dialog_text">哎呀。出咗錯!</string>
|
|
||||||
<string name="crash_dialog_ok_toast">多謝你!</string>
|
|
||||||
</resources>
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue