Skip to content

Commit

Permalink
login: stable realm while settings dialog (fixes open-learning-exchan…
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 and dogi authored Sep 27, 2023
1 parent d08f44c commit 1f8884b
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 100 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1057
versionName "0.10.57"
versionCode 1058
versionName "0.10.58"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
175 changes: 95 additions & 80 deletions app/src/main/java/org/ole/planet/myplanet/ui/sync/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private void becomeAMember() {

private void showGuestLoginDialog() {
try {
mRealm = Realm.getDefaultInstance(); // Open a new Realm instance
mRealm = Realm.getDefaultInstance();

editor = settings.edit();
View v = LayoutInflater.from(this).inflate(R.layout.alert_guest_login, null);
Expand Down Expand Up @@ -263,58 +263,66 @@ public void notAvailable() {
}

public void declareMoreElements() {
syncIcon = findViewById(R.id.syncIcon);
syncIcon.setImageDrawable(getResources().getDrawable(R.drawable.login_file_upload_animation));
syncIcon.getScaleType();
syncIconDrawable = (AnimationDrawable) syncIcon.getDrawable();
syncIcon.setOnClickListener(v -> {
syncIconDrawable.start();
isSync = false;
forceSync = true;
service.checkVersion(this, settings);
});
declareHideKeyboardElements();
TextView txtVersion = findViewById(R.id.lblVersion);
txtVersion.setText(getResources().getText(R.string.version) + " " + getResources().getText(R.string.app_version));
inputName = findViewById(R.id.input_name);
inputPassword = findViewById(R.id.input_password);
inputName.addTextChangedListener(new MyTextWatcher(inputName));
inputPassword.addTextChangedListener(new MyTextWatcher(inputPassword));
inputPassword.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE ||
(event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
btnSignIn.performClick();
return true;
try {
mRealm = Realm.getDefaultInstance();
syncIcon = findViewById(R.id.syncIcon);
syncIcon.setImageDrawable(getResources().getDrawable(R.drawable.login_file_upload_animation));
syncIcon.getScaleType();
syncIconDrawable = (AnimationDrawable) syncIcon.getDrawable();
syncIcon.setOnClickListener(v -> {
syncIconDrawable.start();
isSync = false;
forceSync = true;
service.checkVersion(this, settings);
});
declareHideKeyboardElements();
TextView txtVersion = findViewById(R.id.lblVersion);
txtVersion.setText(getResources().getText(R.string.version) + " " + getResources().getText(R.string.app_version));
inputName = findViewById(R.id.input_name);
inputPassword = findViewById(R.id.input_password);
inputName.addTextChangedListener(new MyTextWatcher(inputName));
inputPassword.addTextChangedListener(new MyTextWatcher(inputPassword));
inputPassword.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE ||
(event != null && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
btnSignIn.performClick();
return true;
}
return false;
});
setUplanguageButton();
if (defaultPref.getBoolean("saveUsernameAndPassword", false)) {
inputName.setText(settings.getString(getString(R.string.login_user), ""));
inputPassword.setText(settings.getString(getString(R.string.login_password), ""));
}
return false;
});
setUplanguageButton();
if (defaultPref.getBoolean("saveUsernameAndPassword", false)) {
inputName.setText(settings.getString(getString(R.string.login_user), ""));
inputPassword.setText(settings.getString(getString(R.string.login_password), ""));
}
if (NetworkUtils.isNetworkConnected()) {
service.syncPlanetServers(mRealm, success -> Utilities.toast(LoginActivity.this, success));
}

inputName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (NetworkUtils.isNetworkConnected()) {
service.syncPlanetServers(mRealm, success -> Utilities.toast(LoginActivity.this, success));
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String lowercaseText = s.toString().toLowerCase(Locale.ROOT);
if (!s.toString().equals(lowercaseText)) {
inputName.setText(lowercaseText);
inputName.setSelection(lowercaseText.length());
inputName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
}

@Override
public void afterTextChanged(Editable s) {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String lowercaseText = s.toString().toLowerCase(Locale.ROOT);
if (!s.toString().equals(lowercaseText)) {
inputName.setText(lowercaseText);
inputName.setSelection(lowercaseText.length());
}
}

@Override
public void afterTextChanged(Editable s) {
}
});
} finally {
if (mRealm != null && !mRealm.isClosed()) {
mRealm.close();
}
});
}
}

private void setUplanguageButton() {
Expand Down Expand Up @@ -400,43 +408,50 @@ private void onLogin() {
}

public void settingDialog() {
MaterialDialog.Builder builder = new MaterialDialog.Builder(LoginActivity.this);
builder.title(R.string.action_settings).customView(R.layout.dialog_server_url_, true).positiveText(R.string.btn_sync).negativeText(R.string.btn_sync_cancel).neutralText(R.string.btn_sync_save).onPositive((dialog, which) -> continueSync(dialog)).onNeutral((dialog, which) -> saveConfigAndContinue(dialog));

MaterialDialog dialog = builder.build();
positiveAction = dialog.getActionButton(DialogAction.POSITIVE);
spnCloud = dialog.getCustomView().findViewById(R.id.spn_cloud);
List<RealmCommunity> communities = mRealm.where(RealmCommunity.class).sort("weight", Sort.ASCENDING).findAll();
spnCloud.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, communities));
spnCloud.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
onChangeServerUrl();
}
try {
mRealm = Realm.getDefaultInstance();
MaterialDialog.Builder builder = new MaterialDialog.Builder(LoginActivity.this);
builder.title(R.string.action_settings).customView(R.layout.dialog_server_url_, true).positiveText(R.string.btn_sync).negativeText(R.string.btn_sync_cancel).neutralText(R.string.btn_sync_save).onPositive((dialog, which) -> continueSync(dialog)).onNeutral((dialog, which) -> saveConfigAndContinue(dialog));

MaterialDialog dialog = builder.build();
positiveAction = dialog.getActionButton(DialogAction.POSITIVE);
spnCloud = dialog.getCustomView().findViewById(R.id.spn_cloud);
List<RealmCommunity> communities = mRealm.where(RealmCommunity.class).sort("weight", Sort.ASCENDING).findAll();
spnCloud.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, communities));
spnCloud.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
onChangeServerUrl();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {
@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});
protocol_checkin = dialog.getCustomView().findViewById(R.id.radio_protocol);
serverUrl = dialog.getCustomView().findViewById(R.id.input_server_url);
serverPassword = dialog.getCustomView().findViewById(R.id.input_server_Password);
switchServerUrl = dialog.getCustomView().findViewById(R.id.switch_server_url);
serverUrlProtocol = dialog.getCustomView().findViewById(R.id.input_server_url_protocol);
switchServerUrl.setOnCheckedChangeListener((compoundButton, b) -> {
settings.edit().putBoolean("switchCloudUrl", b).commit();
spnCloud.setVisibility(b ? View.VISIBLE : View.GONE);
setUrlAndPin(switchServerUrl.isChecked());
});
serverUrl.addTextChangedListener(new MyTextWatcher(serverUrl));
EditText customDeviceName = dialog.getCustomView().findViewById(R.id.deviceName);
customDeviceName.setText(getCustomDeviceName());
switchServerUrl.setChecked(settings.getBoolean("switchCloudUrl", false));
setUrlAndPin(settings.getBoolean("switchCloudUrl", false));
protocol_semantics();
dialog.show();
sync(dialog);
} finally {
if (mRealm != null && !mRealm.isClosed()) {
mRealm.close();
}
});
protocol_checkin = dialog.getCustomView().findViewById(R.id.radio_protocol);
serverUrl = dialog.getCustomView().findViewById(R.id.input_server_url);
serverPassword = dialog.getCustomView().findViewById(R.id.input_server_Password);
switchServerUrl = dialog.getCustomView().findViewById(R.id.switch_server_url);
serverUrlProtocol = dialog.getCustomView().findViewById(R.id.input_server_url_protocol);
switchServerUrl.setOnCheckedChangeListener((compoundButton, b) -> {
settings.edit().putBoolean("switchCloudUrl", b).commit();
spnCloud.setVisibility(b ? View.VISIBLE : View.GONE);
setUrlAndPin(switchServerUrl.isChecked());
});
serverUrl.addTextChangedListener(new MyTextWatcher(serverUrl));
EditText customDeviceName = dialog.getCustomView().findViewById(R.id.deviceName);
customDeviceName.setText(getCustomDeviceName());
switchServerUrl.setChecked(settings.getBoolean("switchCloudUrl", false));
setUrlAndPin(settings.getBoolean("switchCloudUrl", false));
protocol_semantics();
dialog.show();
sync(dialog);
}
}

private void onChangeServerUrl() {
Expand Down
54 changes: 36 additions & 18 deletions app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,25 @@ private void setSpinnerVisibility(boolean isChecked) {
}

public void setUpChildMode() {
if (!settings.getBoolean("isChild", false)) return;
RecyclerView rvTeams = findViewById(R.id.rv_teams);
TextView tvNodata = findViewById(R.id.tv_nodata);

List<RealmMyTeam> teams = mRealm.where(RealmMyTeam.class).isEmpty("teamId").findAll();
rvTeams.setLayoutManager(new GridLayoutManager(this, 3));
rvTeams.setAdapter(new AdapterTeam(this, teams, mRealm));
if (teams.size() > 0) {
tvNodata.setVisibility(View.GONE);
} else {
tvNodata.setText(R.string.no_team_available);
tvNodata.setVisibility(View.VISIBLE);
try {
mRealm = Realm.getDefaultInstance();
if (!settings.getBoolean("isChild", false)) return;
RecyclerView rvTeams = findViewById(R.id.rv_teams);
TextView tvNodata = findViewById(R.id.tv_nodata);

List<RealmMyTeam> teams = mRealm.where(RealmMyTeam.class).isEmpty("teamId").findAll();
rvTeams.setLayoutManager(new GridLayoutManager(this, 3));
rvTeams.setAdapter(new AdapterTeam(this, teams, mRealm));
if (teams.size() > 0) {
tvNodata.setVisibility(View.GONE);
} else {
tvNodata.setText(R.string.no_team_available);
tvNodata.setVisibility(View.VISIBLE);
}
} finally {
if (mRealm != null && !mRealm.isClosed()) {
mRealm.close();
}
}
}

Expand Down Expand Up @@ -197,18 +204,26 @@ public void saveSyncInfoToPreference() {
}

public boolean authenticateUser(SharedPreferences settings, String username, String password, boolean isManagerMode) {
this.settings = settings;
if (mRealm.isEmpty()) {
alertDialogOkay(getString(R.string.server_not_configured_properly_connect_this_device_with_planet_server));
return false;
} else {
return checkName(username, password, isManagerMode);
try {
mRealm = Realm.getDefaultInstance();
this.settings = settings;
if (mRealm.isEmpty()) {
alertDialogOkay(getString(R.string.server_not_configured_properly_connect_this_device_with_planet_server));
return false;
} else {
return checkName(username, password, isManagerMode);
}
} finally {
if (mRealm != null && !mRealm.isClosed()) {
mRealm.close();
}
}
}

@Nullable
private Boolean checkName(String username, String password, boolean isManagerMode) {
try {
mRealm = Realm.getDefaultInstance();
AndroidDecrypter decrypt = new AndroidDecrypter();
RealmResults<RealmUserModel> db_users = mRealm.where(RealmUserModel.class).equalTo("name", username).findAll();
for (RealmUserModel user : db_users) {
Expand All @@ -227,6 +242,9 @@ private Boolean checkName(String username, String password, boolean isManagerMod
}
} catch (Exception err) {
err.printStackTrace();
if (mRealm != null && !mRealm.isClosed()) {
mRealm.close();
}
return false;
}
return false;
Expand Down

0 comments on commit 1f8884b

Please sign in to comment.