Feat[launcher]: user-friendlier checks for local mode username

This commit is contained in:
Maksim Belov 2024-10-16 23:10:06 +03:00 committed by Maksim Belov
parent ca6c7ff3ed
commit ce2d81eaae
2 changed files with 12 additions and 5 deletions

View File

@ -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;
@ -20,17 +21,23 @@ import java.util.regex.Pattern;
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(), "" });
@ -45,13 +52,11 @@ public class LocalLoginFragment extends Fragment {
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()
);
}

View File

@ -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 316 characters long, and must only contain latin letters, arabic numerals and underscores.</string>
</resources>