From 831e9d36ec2ac4cb1bf08a50ac8f27eab442f798 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Tue, 7 Apr 2020 11:05:42 +0500 Subject: [PATCH] Fix repos loading on switching accounts --- .../mian/gitnex/activities/LoginActivity.java | 58 ++++++++++++++++++- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java b/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java index 11db7653..cc5b9672 100644 --- a/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java @@ -724,9 +724,61 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener } else { - tinyDb.putBoolean("loggedInMode", true); - startActivity(new Intent(LoginActivity.this, MainActivity.class)); - finish(); + String instanceToken = "token " + tinyDb.getString(loginUid + "-token"); + + Call callGetUsername = RetrofitClient + .getInstance(instanceUrl, getApplicationContext()) + .getApiInterface() + .getUserInfo(instanceToken); + + callGetUsername.enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + + UserInfo userDetails = response.body(); + + if (response.isSuccessful()) { + + if (response.code() == 200) { + + assert userDetails != null; + tinyDb.putString("userLogin", userDetails.getUsername()); + + tinyDb.putBoolean("loggedInMode", true); + startActivity(new Intent(LoginActivity.this, MainActivity.class)); + finish(); + + } + + } + else if(response.code() == 401) { + + SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.unauthorizedApiError)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + + } + else { + + SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.genericApiStatusError) + response.code()); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + + } + + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + + Log.e("onFailure", t.toString()); + SnackBar.error(getApplicationContext(), layoutView, getResources().getString(R.string.genericError)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + + } + }); }