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 03a2f92b..5ebf8038 100644 --- a/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java @@ -53,6 +53,7 @@ public class LoginActivity extends BaseActivity { private int maxResponseItems = 50; private int defaultPagingNumber = 25; private final String DATABASE_NAME = "gitnex"; + private boolean hasShownInitialNetworkError = false; @Override public void onCreate(Bundle savedInstanceState) { @@ -86,6 +87,12 @@ public class LoginActivity extends BaseActivity { } }); + if (AppUtil.hasNetworkConnection(ctx)) { + enableProcessButton(); + } else { + disableProcessButton(); + } + networkStatusObserver.registerNetworkStatusListener( hasNetworkConnection -> runOnUiThread( @@ -96,11 +103,14 @@ public class LoginActivity extends BaseActivity { disableProcessButton(); activityLoginBinding.loginButton.setText( getResources().getString(R.string.btnLogin)); - SnackBar.error( - ctx, - findViewById(android.R.id.content), - getString(R.string.checkNetConnection)); + if (hasShownInitialNetworkError) { + SnackBar.error( + ctx, + findViewById(android.R.id.content), + getString(R.string.checkNetConnection)); + } } + hasShownInitialNetworkError = true; })); activityLoginBinding.loginButton.setOnClickListener( @@ -185,7 +195,7 @@ public class LoginActivity extends BaseActivity { } versionCheck(loginToken); - serverPageLimitSettings(); + serverPageLimitSettings(String.valueOf(instanceUrl), loginToken); } catch (Exception e) { @@ -195,10 +205,10 @@ public class LoginActivity extends BaseActivity { } } - private void serverPageLimitSettings() { - + private void serverPageLimitSettings(String instanceUrl, String loginToken) { Call generalAPISettings = - RetrofitClient.getApiInterface(ctx).getGeneralAPISettings(); + RetrofitClient.getApiInterface(ctx, instanceUrl, "token " + loginToken, null) + .getGeneralAPISettings(); generalAPISettings.enqueue( new Callback<>() { @@ -229,9 +239,7 @@ public class LoginActivity extends BaseActivity { private void versionCheck(final String loginToken) { - Call callVersion; - - callVersion = + Call callVersion = RetrofitClient.getApiInterface( ctx, instanceUrl.toString(), "token " + loginToken, null) .getVersion(); diff --git a/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java b/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java index 0f0fbc88..1cbfe2de 100644 --- a/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java +++ b/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java @@ -141,6 +141,8 @@ public class RetrofitClient { .addInterceptor(auth) .addInterceptor(cacheInterceptor) .addNetworkInterceptor(forceCacheInterceptor); + } else { + okHttpClient.addInterceptor(auth); } return okHttpClient.build(); @@ -172,6 +174,12 @@ public class RetrofitClient { } public static ApiInterface getApiInterface(Context context) { + if (!(context instanceof BaseActivity) + || ((BaseActivity) context).getAccount() == null + || ((BaseActivity) context).getAccount().getAccount() == null) { + throw new IllegalStateException( + "No active account available. Use explicit URL and token."); + } return getApiInterface( context, ((BaseActivity) context).getAccount().getAccount().getInstanceUrl(), @@ -199,14 +207,16 @@ public class RetrofitClient { public static ApiInterface getApiInterface( Context context, String url, String token, File cacheFile) { - String key = token.hashCode() + "@" + url; - if (!apiInterfaces.containsKey(key)) { + String key = (token != null ? token.hashCode() : 0) + "@" + url; + if (cacheFile == null || !apiInterfaces.containsKey(key)) { synchronized (RetrofitClient.class) { - if (!apiInterfaces.containsKey(key)) { + if (cacheFile == null || !apiInterfaces.containsKey(key)) { ApiInterface apiInterface = Objects.requireNonNull(createRetrofit(context, url, token, cacheFile)) .create(ApiInterface.class); - apiInterfaces.put(key, apiInterface); + if (cacheFile != null) { + apiInterfaces.put(key, apiInterface); + } return apiInterface; } } @@ -216,7 +226,7 @@ public class RetrofitClient { public static WebApi getWebInterface( Context context, String url, String token, File cacheFile) { - String key = token.hashCode() + "@" + url; + String key = (token != null ? token.hashCode() : 0) + "@" + url; if (!webInterfaces.containsKey(key)) { synchronized (RetrofitClient.class) { if (!webInterfaces.containsKey(key)) {