Hotfix: Fix logins (#1431)

Closes #1430

Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1431
Co-authored-by: M M Arif <mmarif@swatian.com>
Co-committed-by: M M Arif <mmarif@swatian.com>
This commit is contained in:
M M Arif
2025-04-03 10:56:24 +00:00
committed by M M Arif
parent 67f70f8815
commit d1d74407e6
2 changed files with 34 additions and 16 deletions

View File

@@ -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> 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<ServerVersion> callVersion;
callVersion =
Call<ServerVersion> callVersion =
RetrofitClient.getApiInterface(
ctx, instanceUrl.toString(), "token " + loginToken, null)
.getVersion();

View File

@@ -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)) {