Skip to content

Commit

Permalink
login: remember all returning users (fixes #2605) (#2606)
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 17, 2023
1 parent c82950e commit dd4cf9f
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 96 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 1100
versionName "0.11.0"
versionCode 1101
versionName "0.11.1"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/ole/planet/myplanet/model/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ data class User(
val fullName: String,
val name: String,
val password: String,
val image: String
val image: String,
val source: String
)
206 changes: 119 additions & 87 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 @@ -145,10 +145,7 @@ protected void onCreate(Bundle savedInstanceState) {
if (settings.getBoolean("firstRun", true));

previouslyLoggedIn = findViewById(R.id.previouslyLoggedIn);
previouslyLoggedIn.setOnClickListener(view -> {
showUserList();
});

previouslyLoggedIn.setOnClickListener(view -> showUserList());
}

private void showUserList(){
Expand All @@ -160,9 +157,24 @@ private void showUserList(){
ListView lv = view.findViewById(R.id.list_user);

List<User> existingUsers = prefData.getSAVEDUSERS1();

UserListAdapter adapter = new UserListAdapter(LoginActivity.this, existingUsers);
adapter.setOnItemClickListener(this::submitForm);
adapter.setOnItemClickListener(new UserListAdapter.OnItemClickListener() {
@Override
public void onItemClickGuest(String name) {
RealmUserModel model = mRealm.copyFromRealm(RealmUserModel.createGuestUser(name, mRealm, settings));
if (model == null) {
Utilities.toast(LoginActivity.this, getString(R.string.unable_to_login));
} else {
saveUserInfoPref(settings, "", model);
onLogin();
}
}

@Override
public void onItemClickMember(String name, String password) {
submitForm(name, password);
}
});

lv.setAdapter(adapter);

Expand Down Expand Up @@ -307,80 +319,79 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
etUserName.setSelection(lowercaseText.length());
}
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);
@Override
public void afterTextChanged(Editable s) {}
});

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;
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);

boolean isValid = true;
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;

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

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 (TextUtils.isEmpty(username)) {
etUserName.setError(getString(R.string.username_cannot_be_empty));
isValid = false;
}

if (hasInvalidCharacters) {
etUserName.setError(getString(R.string.only_letters_numbers_and_are_allowed));
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 (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));
if (existingUser != null) {
if (existingUser.get_id().contains("guest")) {
showGuestDialog(username);
} else if (existingUser.get_id().contains("org.couchdb.user:")) {
showUserAlreadyMemberDialog(username);
}
} else {
saveUserInfoPref(settings, "", model);
onLogin();
RealmUserModel model = mRealm.copyFromRealm(RealmUserModel.createGuestUser(username, mRealm, settings));
if (model == null) {
Utilities.toast(LoginActivity.this, getString(R.string.unable_to_login));
} else {
saveUsers(username, "", "guest");
saveUserInfoPref(settings, "", model);
onLogin();
}
}
}
}
});
});

cancel.setOnClickListener(view -> dialog.dismiss());
cancel.setOnClickListener(view -> dialog.dismiss());
} finally {
if (mRealm != null && !mRealm.isClosed()) {
mRealm.close();
Expand Down Expand Up @@ -547,7 +558,7 @@ private void submitForm(String name, String password) {
if (isLoggedIn) {
Toast.makeText(getApplicationContext(), getString(R.string.thank_you), Toast.LENGTH_SHORT).show();
onLogin();
saveUsers(inputName.getText().toString(), inputPassword.getText().toString());
saveUsers(inputName.getText().toString(), inputPassword.getText().toString(), "member");
} else {
ManagerSync.getInstance().login(name, password, new SyncListener() {
@Override
Expand All @@ -564,7 +575,7 @@ public void onSyncComplete() {
if (log) {
Toast.makeText(getApplicationContext(), getString(R.string.thank_you), Toast.LENGTH_SHORT).show();
onLogin();
saveUsers(inputName.getText().toString(), inputPassword.getText().toString());
saveUsers(inputName.getText().toString(), inputPassword.getText().toString(), "member");
} else {
alertDialogOkay(getString(R.string.err_msg_login));
}
Expand All @@ -584,31 +595,52 @@ public void onSyncFailed(String msg) {
editor.commit();
}

private void saveUsers(String name, String password) {
String userProfile = profileDbHandler.getUserModel().getUserImage();
String fullName = profileDbHandler.getUserModel().getFullName();
private void saveUsers(String name, String password, String source) {
if(source == "guest"){
User newUser = new User("", name, password, "", "guest");
List<User> existingUsers = new ArrayList<>(prefData.getSAVEDUSERS1());

if (userProfile == null) {
userProfile = "";
}
boolean newUserExists = false;

if (fullName.trim().length() == 0) {
fullName = profileDbHandler.getUserModel().getName();
}
User newUser = new User(fullName, name, password, userProfile);
List<User> existingUsers = new ArrayList<>(prefData.getSAVEDUSERS1());
boolean newUserExists = false;

for (User user : existingUsers) {
if (user.getFullName().equals(newUser.getFullName().trim())) {
newUserExists = true;
break;
for (User user : existingUsers) {
if (user.getName().equals(newUser.getName().trim())) {
newUserExists = true;
break;
}
}

if (!newUserExists) {
existingUsers.add(newUser);
prefData.setSAVEDUSERS1(existingUsers);
}
} else if(source == "member"){
String userProfile = profileDbHandler.getUserModel().getUserImage();
String fullName = profileDbHandler.getUserModel().getFullName();

if (userProfile == null) {
userProfile = "";
}

if (fullName.trim().length() == 0) {
fullName = profileDbHandler.getUserModel().getName();
}

User newUser = new User(fullName, name, password, userProfile, "member");
List<User> existingUsers = new ArrayList<>(prefData.getSAVEDUSERS1());

boolean newUserExists = false;

for (User user : existingUsers) {
if (user.getFullName().equals(newUser.getFullName().trim())) {
newUserExists = true;
break;
}
}
}

if (!newUserExists) {
existingUsers.add(newUser);
prefData.setSAVEDUSERS1(existingUsers);
if (!newUserExists) {
existingUsers.add(newUser);
prefData.setSAVEDUSERS1(existingUsers);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class UserListAdapter extends BaseAdapter implements Filterable {
private OnItemClickListener onItemClickListener;

public interface OnItemClickListener {
void onItemClick(String name, String password);
void onItemClickGuest(String name);
void onItemClickMember(String name, String password);
}

public UserListAdapter(Context context, List<User> userList) {
Expand Down Expand Up @@ -67,8 +68,12 @@ public View getView(int position, View convertView, ViewGroup parent) {
CircleImageView userProfile = view.findViewById(R.id.userProfile);

User user = filteredUserList.get(position);
userNameTextView.setText(user.getFullName());
user.getImage();
if (user.getSource().equals("guest")){
userNameTextView.setText(user.getName());
} else if (user.getSource().equals("member")){
userNameTextView.setText(user.getFullName());
}

if (!user.getImage().isEmpty()) {
Glide.with(context)
.load(user.getImage())
Expand All @@ -79,7 +84,11 @@ public View getView(int position, View convertView, ViewGroup parent) {

view.setOnClickListener(v -> {
if (onItemClickListener != null) {
onItemClickListener.onItemClick(user.getName(), user.getPassword());
if (user.getSource().equals("guest")) {
onItemClickListener.onItemClickGuest(user.getName());
} else if (user.getSource().equals("member")) {
onItemClickListener.onItemClickMember(user.getName(), user.getPassword());
}
}
});

Expand All @@ -95,7 +104,6 @@ protected FilterResults performFiltering(CharSequence constraint) {
List<User> filteredList = new ArrayList<>();

if (constraint == null || constraint.length() == 0) {
// No filter applied, return the full list
filteredList.addAll(userList);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class SharedPrefManager(context: Context) {
val name = userJson.getString("name")
val password = userJson.getString("password")
val image = userJson.getString("image")
val user = User(fullName, name, password, image)
val source = userJson.getString("source")
val user = User(fullName, name, password, image, source)
userList.add(user)
}
userList
Expand All @@ -54,6 +55,7 @@ class SharedPrefManager(context: Context) {
userJson.put("name", user.name)
userJson.put("password", user.password)
userJson.put("image", user.image)
userJson.put("source", user.source)
jsonArray.put(userJson)
}
editor.putString(SAVEDUSERS, jsonArray.toString())
Expand Down

0 comments on commit dd4cf9f

Please sign in to comment.