Skip to content

Commit 28ea111

Browse files
authored
Refactor : API calls moved out of the LoginActivity (commons-app#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
1 parent 2c376da commit 28ea111

File tree

3 files changed

+71
-72
lines changed

3 files changed

+71
-72
lines changed

app/src/main/java/fr/free/nrw/commons/auth/LoginActivity.java

+43-62
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
import fr.free.nrw.commons.databinding.ActivityLoginBinding;
3131
import fr.free.nrw.commons.utils.ActivityUtils;
3232
import java.util.Locale;
33-
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse;
3433
import fr.free.nrw.commons.auth.login.LoginCallback;
3534

35+
import java.util.Objects;
3636
import javax.inject.Inject;
3737
import javax.inject.Named;
3838

@@ -46,9 +46,6 @@
4646
import fr.free.nrw.commons.utils.SystemThemeUtils;
4747
import fr.free.nrw.commons.utils.ViewUtil;
4848
import io.reactivex.disposables.CompositeDisposable;
49-
import retrofit2.Call;
50-
import retrofit2.Callback;
51-
import retrofit2.Response;
5249
import timber.log.Timber;
5350

5451
import static android.view.KeyEvent.KEYCODE_ENTER;
@@ -75,7 +72,6 @@ public class LoginActivity extends AccountAuthenticatorActivity {
7572
private AppCompatDelegate delegate;
7673
private LoginTextWatcher textWatcher = new LoginTextWatcher();
7774
private CompositeDisposable compositeDisposable = new CompositeDisposable();
78-
private Call<MwQueryResponse> loginToken;
7975
final String saveProgressDailog="ProgressDailog_state";
8076
final String saveErrorMessage ="errorMessage";
8177
final String saveUsername="username";
@@ -116,7 +112,7 @@ public void onCreate(Bundle savedInstanceState) {
116112
binding.loginCredentials.setVisibility(View.GONE);
117113
}
118114
}
119-
/**
115+
/**
120116
* Hides the keyboard if the user's focus is not on the password (hasFocus is false).
121117
* @param view The keyboard
122118
* @param hasFocus Set to true if the keyboard has focus
@@ -212,63 +208,52 @@ protected void onDestroy() {
212208

213209
public void performLogin() {
214210
Timber.d("Login to start!");
215-
final String username = binding.loginUsername.getText().toString();
216-
final String rawUsername = binding.loginUsername.getText().toString().trim();
217-
final String password = binding.loginPassword.getText().toString();
218-
String twoFactorCode = binding.loginTwoFactor.getText().toString();
211+
final String username = Objects.requireNonNull(binding.loginUsername.getText()).toString();
212+
final String password = Objects.requireNonNull(binding.loginPassword.getText()).toString();
213+
final String twoFactorCode = Objects.requireNonNull(binding.loginTwoFactor.getText()).toString();
219214

220215
showLoggingProgressBar();
221-
doLogin(username, password, twoFactorCode);
216+
loginClient.doLogin(username, password, twoFactorCode, Locale.getDefault().getLanguage(),
217+
new LoginCallback() {
218+
@Override
219+
public void success(@NonNull LoginResult loginResult) {
220+
runOnUiThread(()->{
221+
Timber.d("Login Success");
222+
hideProgress();
223+
onLoginSuccess(loginResult);
224+
});
225+
}
226+
227+
@Override
228+
public void twoFactorPrompt(@NonNull Throwable caught, @Nullable String token) {
229+
runOnUiThread(()->{
230+
Timber.d("Requesting 2FA prompt");
231+
hideProgress();
232+
askUserForTwoFactorAuth();
233+
});
234+
}
235+
236+
@Override
237+
public void passwordResetPrompt(@Nullable String token) {
238+
runOnUiThread(()->{
239+
Timber.d("Showing password reset prompt");
240+
hideProgress();
241+
showPasswordResetPrompt();
242+
});
243+
}
244+
245+
@Override
246+
public void error(@NonNull Throwable caught) {
247+
runOnUiThread(()->{
248+
Timber.e(caught);
249+
hideProgress();
250+
showMessageAndCancelDialog(caught.getLocalizedMessage());
251+
});
252+
}
253+
});
222254
}
223255

224-
private void doLogin(String username, String password, String twoFactorCode) {
225-
progressDialog.show();
226-
loginToken = loginClient.getLoginToken();
227-
loginToken.enqueue(
228-
new Callback<MwQueryResponse>() {
229-
@Override
230-
public void onResponse(Call<MwQueryResponse> call,
231-
Response<MwQueryResponse> response) {
232-
loginClient.login(username, password, null, twoFactorCode,
233-
response.body().query().loginToken(), Locale.getDefault().getLanguage(), new LoginCallback() {
234-
@Override
235-
public void success(@NonNull LoginResult result) {
236-
Timber.d("Login Success");
237-
onLoginSuccess(result);
238-
}
239-
240-
@Override
241-
public void twoFactorPrompt(@NonNull Throwable caught,
242-
@Nullable String token) {
243-
Timber.d("Requesting 2FA prompt");
244-
hideProgress();
245-
askUserForTwoFactorAuth();
246-
}
247-
248-
@Override
249-
public void passwordResetPrompt(@Nullable String token) {
250-
Timber.d("Showing password reset prompt");
251-
hideProgress();
252-
showPasswordResetPrompt();
253-
}
254-
255-
@Override
256-
public void error(@NonNull Throwable caught) {
257-
Timber.e(caught);
258-
hideProgress();
259-
showMessageAndCancelDialog(caught.getLocalizedMessage());
260-
}
261-
});
262-
}
263-
264-
@Override
265-
public void onFailure(Call<MwQueryResponse> call, Throwable t) {
266-
Timber.e(t);
267-
showMessageAndCancelDialog(t.getLocalizedMessage());
268-
}
269-
});
270256

271-
}
272257

273258
private void hideProgress() {
274259
progressDialog.dismiss();
@@ -299,10 +284,6 @@ private void showLoggingProgressBar() {
299284
}
300285

301286
private void onLoginSuccess(LoginResult loginResult) {
302-
if (!progressDialog.isShowing()) {
303-
// no longer attached to activity!
304-
return;
305-
}
306287
compositeDisposable.clear();
307288
sessionManager.setUserLoggedIn(true);
308289
sessionManager.updateAccount(loginResult);

app/src/main/java/fr/free/nrw/commons/auth/login/LoginClient.kt

+28-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class LoginClient(private val loginInterface: LoginInterface) {
2828
*/
2929
private var userLanguage = ""
3030

31-
fun getLoginToken() = loginInterface.getLoginToken()
31+
private fun getLoginToken() = loginInterface.getLoginToken()
3232

3333
fun request(userName: String, password: String, cb: LoginCallback) {
3434
cancel()
@@ -106,6 +106,33 @@ class LoginClient(private val loginInterface: LoginInterface) {
106106
})
107107
}
108108

109+
fun doLogin(
110+
username: String,
111+
password: String,
112+
twoFactorCode: String,
113+
userLanguage: String,
114+
loginCallback: LoginCallback
115+
) {
116+
getLoginToken().enqueue(object :Callback<MwQueryResponse?>{
117+
override fun onResponse(
118+
call: Call<MwQueryResponse?>,
119+
response: Response<MwQueryResponse?>
120+
) = if (response.isSuccessful){
121+
val loginToken = response.body()?.query()?.loginToken()
122+
loginToken?.let {
123+
login(username, password, null, twoFactorCode, it, userLanguage, loginCallback)
124+
} ?: run {
125+
loginCallback.error(IOException("Failed to retrieve login token"))
126+
}
127+
} else {
128+
loginCallback.error(IOException("Failed to retrieve login token"))
129+
}
130+
131+
override fun onFailure(call: Call<MwQueryResponse?>, t: Throwable) {
132+
loginCallback.error(t)
133+
}
134+
})
135+
}
109136
@Throws(Throwable::class)
110137
fun loginBlocking(userName: String, password: String, twoFactorCode: String?) {
111138
val tokenResponse = getLoginToken().execute()

app/src/main/res/values-yue-hant/error.xml

-9
This file was deleted.

0 commit comments

Comments
 (0)