Skip to content

Commit

Permalink
Feat[launcher]: user-friendlier checks for local mode username
Browse files Browse the repository at this point in the history
  • Loading branch information
artdeell committed Oct 16, 2024
1 parent ca6c7ff commit 68deaa5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.kdt.pojavlaunch.fragments;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
Expand All @@ -20,17 +21,23 @@
public class LocalLoginFragment extends Fragment {
public static final String TAG = "LOCAL_LOGIN_FRAGMENT";

private final Pattern mUsernameValidationPattern;
private EditText mUsernameEditText;

public LocalLoginFragment(){
super(R.layout.fragment_local_login);
mUsernameValidationPattern = Pattern.compile("^[a-zA-Z0-9_]*$");
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
mUsernameEditText = view.findViewById(R.id.login_edit_email);
view.findViewById(R.id.login_button).setOnClickListener(v -> {
if(!checkEditText()) return;
if(!checkEditText()) {
Context context = v.getContext();
Tools.dialog(context, context.getString(R.string.local_login_bad_username_title), context.getString(R.string.local_login_bad_username_text));
return;
}

ExtraCore.setValue(ExtraConstants.MOJANG_LOGIN_TODO, new String[]{
mUsernameEditText.getText().toString(), "" });
Expand All @@ -45,13 +52,11 @@ private boolean checkEditText(){

String text = mUsernameEditText.getText().toString();

Pattern pattern = Pattern.compile("[^a-zA-Z0-9_]");
Matcher matcher = pattern.matcher(text);

Matcher matcher = mUsernameValidationPattern.matcher(text);
return !(text.isEmpty()
|| text.length() < 3
|| text.length() > 16
|| matcher.find()
|| !matcher.find()
|| new File(Tools.DIR_ACCOUNT_NEW + "/" + text + ".json").exists()
);
}
Expand Down
2 changes: 2 additions & 0 deletions app_pojavlauncher/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,6 @@
<string name="preference_remap_controller_description">Allows you to modify the keyboard keys bound to each controller button</string>
<string name="mcl_button_discord">Discord</string>
<string name="discord_invite" translatable="false">https://discord.gg/pojavlauncher-724163890803638273</string>
<string name="local_login_bad_username_title">Unsuitable username</string>
<string name="local_login_bad_username_text">The username must be between 3–16 characters long, and must only contain latin letters, arabic numerals and underscores.</string>
</resources>

0 comments on commit 68deaa5

Please sign in to comment.