Skip to content

Commit

Permalink
login: guest until sign up on third login (fixes #2522) (#2523)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Oct 12, 2023
1 parent b2a90d3 commit 6e7f4f2
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 76 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 1093
versionName "0.10.93"
versionCode 1094
versionName "0.10.94"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.ole.planet.myplanet.ui.dashboard;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
Expand All @@ -8,11 +10,13 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

Expand Down Expand Up @@ -49,6 +53,7 @@
import org.ole.planet.myplanet.ui.survey.SurveyFragment;
import org.ole.planet.myplanet.ui.sync.DashboardElementActivity;
import org.ole.planet.myplanet.ui.team.TeamFragment;
import org.ole.planet.myplanet.ui.userprofile.BecomeMemberActivity;
import org.ole.planet.myplanet.utilities.BottomNavigationViewHelper;
import org.ole.planet.myplanet.utilities.Constants;
import org.ole.planet.myplanet.utilities.KeyboardUtils;
Expand Down Expand Up @@ -122,9 +127,8 @@ protected void onCreate(Bundle savedInstanceState) {
navigationView.setVisibility(new UserProfileDbHandler(this).getUserModel().getShowTopbar() ? View.VISIBLE : View.GONE);
headerResult = getAccountHeader();
createDrawer();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
if (!(user.getId().startsWith("guest") && profileDbHandler.getOfflineVisits() >= 3) && getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
result.openDrawer();

}//Opens drawer by default
result.getStickyFooter().setPadding(0, 0, 0, 0); // moves logout button to the very bottom of the drawer. Without it, the "logout" button suspends a little.
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
Expand Down Expand Up @@ -205,8 +209,30 @@ private void checkUser() {
logout();
return;
}
if (user.getId().startsWith("guest")) {
getTheme().applyStyle(R.style.GuestStyle, true);
if(user.getId().startsWith("guest") && profileDbHandler.getOfflineVisits() >= 3 ){

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Become a member");
builder.setMessage("Trial period ended! Kindly complete registration to continue");
builder.setCancelable(false);
builder.setPositiveButton("Become a member", null);
builder.setNegativeButton("Logout", null);
AlertDialog dialog = builder.create();
dialog.show();
Button becomeMember = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button logout = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
becomeMember.setOnClickListener(view -> {
boolean guest = true;
Intent intent = new Intent(this, BecomeMemberActivity.class);
intent.putExtra("username", profileDbHandler.getUserModel().getName());
intent.putExtra("guest", guest);
setResult(Activity.RESULT_OK, intent);
startActivity(intent);
});
logout.setOnClickListener(view -> {
dialog.dismiss();
logout();
});
}
}

Expand Down
190 changes: 159 additions & 31 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 @@ -68,6 +68,7 @@
import java.util.concurrent.TimeUnit;

import io.realm.Realm;
import io.realm.RealmResults;
import io.realm.Sort;

public class LoginActivity extends SyncActivity implements Service.CheckVersionCallback, AdapterTeam.OnUserSelectedListener {
Expand Down Expand Up @@ -129,13 +130,17 @@ protected void onCreate(Bundle savedInstanceState) {
if (!Utilities.getUrl().isEmpty()) {
btnOpenCommunity.setVisibility(View.VISIBLE);
btnOpenCommunity.setOnClickListener(v -> {
inputName.setText("");
new HomeCommunityDialogFragment().show(getSupportFragmentManager(), "");
});
new HomeCommunityDialogFragment().show(getSupportFragmentManager(), "");
} else {
btnOpenCommunity.setVisibility(View.GONE);
}
findViewById(R.id.btn_feedback).setOnClickListener(view -> new FeedbackFragment().show(getSupportFragmentManager(), ""));
findViewById(R.id.btn_feedback).setOnClickListener(view -> {
inputName.setText("");
new FeedbackFragment().show(getSupportFragmentManager(), "");
});

if (settings.getBoolean("firstRun", true));

Expand Down Expand Up @@ -238,18 +243,29 @@ public void declareElements() {
});
if (!settings.contains("serverProtocol"))
settings.edit().putString("serverProtocol", "http://").commit();
findViewById(R.id.become_member).setOnClickListener(v -> becomeAMember());
imgBtnSetting.setOnClickListener(view -> settingDialog());
btnGuestLogin.setOnClickListener(view -> showGuestLoginDialog());
findViewById(R.id.become_member).setOnClickListener(v -> {
inputName.setText("");
becomeAMember();
});
imgBtnSetting.setOnClickListener(view -> {
inputName.setText("");
settingDialog();
});
btnGuestLogin.setOnClickListener(view -> {
inputName.setText("");
showGuestLoginDialog();
});
SwitchCompat switchChildMode = findViewById(R.id.switch_child_mode);
switchChildMode.setChecked(settings.getBoolean("isChild", false));
switchChildMode.setOnCheckedChangeListener((compoundButton, b) -> {
inputName.setText("");
settings.edit().putBoolean("isChild", b).commit();
recreate();
});
}

private void becomeAMember() {

if (!Utilities.getUrl().isEmpty()) {
startActivity(new Intent(this, BecomeMemberActivity.class));
} else {
Expand All @@ -261,42 +277,156 @@ private void becomeAMember() {
private void showGuestLoginDialog() {
try {
mRealm = Realm.getDefaultInstance();

mRealm.refresh();
editor = settings.edit();
View v = LayoutInflater.from(this).inflate(R.layout.alert_guest_login, null);
TextInputEditText etUserName = v.findViewById(R.id.et_user_name);
new AlertDialog.Builder(this)
.setTitle("Login As Guest")
.setView(v)
.setPositiveButton("Login", (dialogInterface, i) -> {
if (mRealm != null && !mRealm.isClosed()) {
if (mRealm.isEmpty()) {
alertDialogOkay(getString(R.string.this_device_not_configured_properly_please_check_and_sync));
return;
}
String username = etUserName.getText().toString().toLowerCase().trim();
if (username.isEmpty()) {
Utilities.toast(this, getString(R.string.username_cannot_be_empty));
return;
}
RealmUserModel model = mRealm.copyFromRealm(RealmUserModel.createGuestUser(username, mRealm, settings));
if (model == null) {
Utilities.toast(this, getString(R.string.unable_to_login));
} else {
saveUserInfoPref(settings, "", model);
onLogin();
}
etUserName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
char firstChar = s.length() > 0 ? s.charAt(0) : '\0';
boolean hasInvalidCharacters = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c != '_' && c != '.' && c != '-' && !Character.isDigit(c) && !Character.isLetter(c)) {
hasInvalidCharacters = true;
break;
}
}

if (!Character.isDigit(firstChar) && !Character.isLetter(firstChar)) {
etUserName.setError(getString(R.string.must_start_with_letter_or_number));
} else if (hasInvalidCharacters) {
etUserName.setError(getString(R.string.only_letters_numbers_and_are_allowed));
} else {
String lowercaseText = s.toString().toLowerCase(Locale.ROOT);
if (!s.toString().equals(lowercaseText)) {
etUserName.setText(lowercaseText);
etUserName.setSelection(lowercaseText.length());
}
})
.setNegativeButton("Cancel", null)
.show();
etUserName.setError(null);
}
}

@Override
public void afterTextChanged(Editable s) {}
});

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Login As Guest");
builder.setView(v);
builder.setPositiveButton("Login", null);
builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
Button login = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button cancel = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);

login.setOnClickListener(view -> {
if (mRealm.isEmpty()) {
alertDialogOkay(getString(R.string.this_device_not_configured_properly_please_check_and_sync));
return;
}
String username = etUserName.getText().toString().trim();
Character firstChar = username.isEmpty() ? null : username.charAt(0);
boolean hasInvalidCharacters = false;

boolean isValid = true;

if (TextUtils.isEmpty(username)) {
etUserName.setError(getString(R.string.username_cannot_be_empty));
isValid = false;
}

if (firstChar != null && !Character.isDigit(firstChar) && !Character.isLetter(firstChar)) {
etUserName.setError(getString(R.string.must_start_with_letter_or_number));
isValid = false;
} else {
for (char c : username.toCharArray()) {
if (c != '_' && c != '.' && c != '-' && !Character.isDigit(c) && !Character.isLetter(c)) {
hasInvalidCharacters = true;
break;
}
}

if (hasInvalidCharacters) {
etUserName.setError(getString(R.string.only_letters_numbers_and_are_allowed));
isValid = false;
}
}

if (isValid) {
RealmUserModel existingUser = mRealm.where(RealmUserModel.class).equalTo("name", username).findFirst();
dialog.dismiss();

if (existingUser != null) {
Log.d("model", String.valueOf(existingUser.get_id()));
if (existingUser.get_id().contains("guest")) {
showGuestDialog(username);
} else if (existingUser.get_id().contains("org.couchdb.user:")) {
showUserAlreadyMemberDialog(username);
}
} else {
RealmUserModel model = mRealm.copyFromRealm(RealmUserModel.createGuestUser(username, mRealm, settings));
if (model == null) {
Utilities.toast(LoginActivity.this, getString(R.string.unable_to_login));
} else {
saveUserInfoPref(settings, "", model);
onLogin();
}
}
}
});

cancel.setOnClickListener(view -> dialog.dismiss());
} finally {
if (mRealm != null && !mRealm.isClosed()) {
mRealm.close();
}
}
}

private void showGuestDialog(String username) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(username + " is already a guest");
builder.setMessage("Continue only if this is you");
builder.setCancelable(false);
builder.setNegativeButton("cancel", (dialog, which) -> dialog.dismiss());

builder.setPositiveButton("continue", (dialog, which) -> {
dialog.dismiss();
RealmUserModel model = mRealm.copyFromRealm(RealmUserModel.createGuestUser(username, mRealm, settings));
if (model == null) {
Utilities.toast(LoginActivity.this, getString(R.string.unable_to_login));
} else {
saveUserInfoPref(settings, "", model);
onLogin();
}
});

AlertDialog dialog = builder.create();
dialog.show();

}

private void showUserAlreadyMemberDialog(String username) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(username + " is already a member");
builder.setMessage("Continue to login if this is you");
builder.setCancelable(false);
builder.setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss());

builder.setPositiveButton("login", (dialog, which) -> {
dialog.dismiss();
inputName.setText(username);
});

AlertDialog dialog = builder.create();
dialog.show();
}

private void continueSync(MaterialDialog dialog) {
processedUrl = saveConfigAndContinue(dialog);
if (TextUtils.isEmpty(processedUrl)) return;
Expand Down Expand Up @@ -729,5 +859,3 @@ public String getCustomDeviceName() {
return settings.getString("customDeviceName", NetworkUtils.getDeviceName());
}
}


Loading

0 comments on commit 6e7f4f2

Please sign in to comment.