fix LoginFragment

This commit is contained in:
aklyushova 2025-02-20 15:36:25 +03:00 committed by SunZar
parent 1daf04e716
commit bc48b83103

@ -47,10 +47,9 @@ public class LoginFragment extends Fragment {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//binding.log.setText("created");
preferenceManager = new PreferenceManager(this.getContext());
viewModel = new ViewModelProvider(this).get(LoginViewModel.class);
//sharedPreferences.edit().putString(ru.myitschool.work.ui.Constants.KEY_LOGIN, "5user").apply();
binding.login.setEnabled(false);
binding.username.addTextChangedListener(new OnChangeText() {
@ -58,7 +57,7 @@ public class LoginFragment extends Fragment {
public void afterTextChanged(Editable s) {
super.afterTextChanged(s);
viewModel.changeUsername(s.toString());
if (binding.username.getText().length() >= 3 && binding.username.getText().toString().matches("[0-9a-zA-Z]+") && (binding.username.getText().toString().charAt(0) + "").matches("[0-9]+")) {
if (binding.password.getText().length() >= 5 && binding.username.getText().length() >= 3 && binding.username.getText().toString().matches("[0-9a-zA-Z]+") && (binding.username.getText().toString().charAt(0) + "").matches("[0-9]+")) {
binding.login.setEnabled(true);
} else {
binding.login.setEnabled(false);
@ -69,32 +68,7 @@ public class LoginFragment extends Fragment {
}
});
binding.username.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (binding.username.getText().length() >= 3 && binding.username.getText().toString().matches("[0-9a-zA-Z]+") && (binding.username.getText().toString().charAt(0) + "").matches("[0-9]+")) {
binding.login.setEnabled(true);
//binding.log.setText("changed true");
} else {
binding.login.setEnabled(false);
//binding.log.setText("changed false");
}
//Log.d("tagg", "changed");
if (binding.error.getVisibility() == View.VISIBLE) {
binding.error.setVisibility(View.GONE);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
binding.password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
@ -104,7 +78,7 @@ public class LoginFragment extends Fragment {
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
viewModel.changePassword(charSequence.toString());
if (binding.password.getText().length() >= 5) {
if (binding.password.getText().length() >= 5 && binding.username.getText().length() >= 3 && binding.username.getText().toString().matches("[0-9a-zA-Z]+") && (binding.username.getText().toString().charAt(0) + "").matches("[0-9]+")) {
binding.login.setEnabled(true);
} else {
binding.login.setEnabled(false);
@ -122,66 +96,19 @@ public class LoginFragment extends Fragment {
binding.login.setOnClickListener(view1 -> {
viewModel.confirm();
/*if (binding.login.isEnabled()) {
if (binding.error.getVisibility() == View.VISIBLE) {
binding.error.setVisibility(View.GONE);
//binding.log.setText("click enabled");
}
//binding.log.setText("click");
onClickLogin(view);
}*/
});
subscribe(viewModel);
}
/*private void onClickLogin(View view) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.SERVER_ADDRESS)
.addConverterFactory(GsonConverterFactory.create())
.build();
StoreAPI storeApi = retrofit.create(StoreAPI.class);
String login = binding.username.getText().toString();
Call<Boolean> call = storeApi.authenticateUser(login);
call.enqueue(new Callback<Boolean>() {
@Override
public void onResponse(Call<Boolean> call, Response<Boolean> response) {
if (response.isSuccessful()) {
if (response.body() != null && response.body()) {
preferenceManager.putString(ru.myitschool.work.utils.Constants.KEY_LOGIN, binding.username.getText().toString());
Navigation.findNavController(view).navigate(R.id.action_loginFragment_to_informationFragment);
//getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new InformationFragment()).commit();
} else {
binding.error.setVisibility(View.VISIBLE);
}
} else if (response.code() == 401) {
binding.error.setVisibility(View.VISIBLE);
} else if (response.code() == 400) {
binding.error.setVisibility(View.VISIBLE);
} else {
binding.error.setVisibility(View.VISIBLE);
}
}
@Override
public void onFailure(Call<Boolean> call, Throwable t) {
binding.error.setVisibility(View.VISIBLE);
}
});
}*/
private void subscribe(LoginViewModel viewModel) {
viewModel.errorLiveData.observe(getViewLifecycleOwner(), error -> {
Toast.makeText(getContext(), error, Toast.LENGTH_SHORT).show();
});
/*viewModel.stateLiveData.observe(getViewLifecycleOwner(), state -> {
binding.confirm.setText(state.getButton());
binding.title.setText(state.getTitle());
binding.password.setVisibility(Utils.visibleOrGone(state.isPasswordEnabled()));
});*/
viewModel.openListLiveData.observe(getViewLifecycleOwner(), username -> {
final View view = getView();
Toast.makeText(getContext(), "FFFFFFFFF", Toast.LENGTH_SHORT).show();