From dd227ee6a93e9625a0d776f954f9ce67f899390e Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Thu, 28 Sep 2023 19:43:20 +0300 Subject: [PATCH] login: better sync and realm handling (fixes #2541) (#2543) Co-authored-by: dogi --- app/build.gradle | 4 +- .../myplanet/ui/sync/LoginActivity.java | 73 ++++++++++++------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ddf605c47d..5647808b3c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1061 - versionName "0.10.61" + versionCode 1062 + versionName "0.10.62" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/sync/LoginActivity.java b/app/src/main/java/org/ole/planet/myplanet/ui/sync/LoginActivity.java index c0fe9e0acb..c3904d8deb 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/sync/LoginActivity.java +++ b/app/src/main/java/org/ole/planet/myplanet/ui/sync/LoginActivity.java @@ -455,16 +455,23 @@ public void onNothingSelected(AdapterView adapterView) { } private void onChangeServerUrl() { - RealmCommunity selected = (RealmCommunity) spnCloud.getSelectedItem(); - Utilities.log((selected == null) + " selected "); - if (selected == null) { - return; + try { + mRealm = Realm.getDefaultInstance(); + RealmCommunity selected = (RealmCommunity) spnCloud.getSelectedItem(); + Utilities.log((selected == null) + " selected "); + if (selected == null) { + return; + } + serverUrl.setText(selected.getLocalDomain()); + protocol_checkin.check(R.id.radio_https); + settings.getString("serverProtocol", "https://"); + serverPassword.setText(selected.getWeight() == 0 ? "0660" : ""); + serverPassword.setEnabled(selected.getWeight() != 0); + } finally { + if (mRealm != null && !mRealm.isClosed()) { + mRealm.close(); + } } - serverUrl.setText(selected.getLocalDomain()); - protocol_checkin.check(R.id.radio_https); - settings.getString("serverProtocol", "https://"); - serverPassword.setText(selected.getWeight() == 0 ? "0660" : ""); - serverPassword.setEnabled(selected.getWeight() != 0); } private void setUrlAndPin(boolean checked) { @@ -512,14 +519,21 @@ public void onSuccess(String s) { @Override public void onUpdateAvailable(MyPlanet info, boolean cancelable) { - AlertDialog.Builder builder = DialogUtils.getUpdateDialog(this, info, progressDialog); - if (cancelable || NetworkUtils.getCustomDeviceName(this).endsWith("###")) { - builder.setNegativeButton(R.string.update_later, (dialogInterface, i) -> continueSyncProcess()); - } else { - mRealm.executeTransactionAsync(realm -> realm.deleteAll()); + try { + mRealm = Realm.getDefaultInstance(); + AlertDialog.Builder builder = DialogUtils.getUpdateDialog(this, info, progressDialog); + if (cancelable || NetworkUtils.getCustomDeviceName(this).endsWith("###")) { + builder.setNegativeButton(R.string.update_later, (dialogInterface, i) -> continueSyncProcess()); + } else { + mRealm.executeTransactionAsync(realm -> realm.deleteAll()); + } + builder.setCancelable(cancelable); + builder.show(); + } finally { + if (mRealm != null && !mRealm.isClosed()) { + mRealm.close(); + } } - builder.setCancelable(cancelable); - builder.show(); } @Override @@ -565,17 +579,24 @@ public void continueSyncProcess() { @Override public void onSelectedUser(RealmUserModel userModel) { - View v = getLayoutInflater().inflate(R.layout.layout_child_login, null); - EditText et = v.findViewById(R.id.et_child_password); - new AlertDialog.Builder(this).setView(v).setTitle(R.string.please_enter_your_password).setPositiveButton(R.string.login, (dialogInterface, i) -> { - String password = et.getText().toString(); - if (authenticateUser(settings, userModel.getName(), password, false)) { - Toast.makeText(getApplicationContext(), getString(R.string.thank_you), Toast.LENGTH_SHORT).show(); - onLogin(); - } else { - alertDialogOkay(getString(R.string.err_msg_login)); + try { + mRealm = Realm.getDefaultInstance(); + View v = getLayoutInflater().inflate(R.layout.layout_child_login, null); + EditText et = v.findViewById(R.id.et_child_password); + new AlertDialog.Builder(this).setView(v).setTitle(R.string.please_enter_your_password).setPositiveButton(R.string.login, (dialogInterface, i) -> { + String password = et.getText().toString(); + if (authenticateUser(settings, userModel.getName(), password, false)) { + Toast.makeText(getApplicationContext(), getString(R.string.thank_you), Toast.LENGTH_SHORT).show(); + onLogin(); + } else { + alertDialogOkay(getString(R.string.err_msg_login)); + } + }).setNegativeButton(R.string.cancel, null).show(); + } finally { + if (mRealm != null && !mRealm.isClosed()) { + mRealm.close(); } - }).setNegativeButton(R.string.cancel, null).show(); + } } @Override