Really final commit

This commit is contained in:
A1pha 2024-11-27 22:58:52 +03:00
parent 432796e731
commit 7ae4445969
5 changed files with 60 additions and 49 deletions

View File

@ -2,4 +2,5 @@ package ru.myitschool.work.core
// БЕРИТЕ И ИЗМЕНЯЙТЕ ХОСТ ТОЛЬКО ЗДЕСЬ И НЕ БЕРИТЕ ИЗ ДРУГИХ МЕСТ. ФАЙЛ ПЕРЕМЕЩАТЬ НЕЛЬЗЯ // БЕРИТЕ И ИЗМЕНЯЙТЕ ХОСТ ТОЛЬКО ЗДЕСЬ И НЕ БЕРИТЕ ИЗ ДРУГИХ МЕСТ. ФАЙЛ ПЕРЕМЕЩАТЬ НЕЛЬЗЯ
object Constants { object Constants {
const val SERVER_ADDRESS = "http://192.168.1.2:8080" const val SERVER_ADDRESS = "http://192.168.1.2:8080"
const val RESPONSE_KEY = "RESPONSE_KEY"
} }

View File

@ -1,7 +1,5 @@
package ru.myitschool.work.ui.login; package ru.myitschool.work.ui.login;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.view.View; import android.view.View;
@ -31,12 +29,9 @@ public class LoginFragment extends Fragment {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
if (getContext() != null) { if (getContext() != null) {
SharedPreferences sharedPreferences = getContext().getSharedPreferences("login", Context.MODE_PRIVATE); if (Utils.getLogin(getContext()) != null && getView() != null)
if (sharedPreferences.getString("login", null) != null && getView() != null) {
Navigation.findNavController(getView()).navigate( Navigation.findNavController(getView()).navigate(
R.id.action_loginFragment_to_userFragment); R.id.action_loginFragment_to_userFragment);
}
} }
binding = FragmentLoginBinding.bind(view); binding = FragmentLoginBinding.bind(view);
@ -71,10 +66,7 @@ public class LoginFragment extends Fragment {
viewModel.openProfileLiveData.observe(getViewLifecycleOwner(), (unused) -> { viewModel.openProfileLiveData.observe(getViewLifecycleOwner(), (unused) -> {
if (getContext() != null) { if (getContext() != null) {
SharedPreferences sharedPreferences = getContext().getSharedPreferences("login", Context.MODE_PRIVATE); Utils.saveLogin(binding.username.getText().toString(), getContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("login", binding.username.getText().toString());
editor.commit();
} }
if (getView() == null) return; if (getView() == null) return;

View File

@ -1,8 +1,9 @@
package ru.myitschool.work.ui.profile; package ru.myitschool.work.ui.profile;
import android.content.Context; import static ru.myitschool.work.core.Constants.RESPONSE_KEY;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -64,29 +65,26 @@ public class UserFragment extends Fragment {
} }
}); });
if (getContext() != null) { if (getContext() != null && Utils.getLogin(getContext()) != null) {
SharedPreferences preferences = getContext().getSharedPreferences( Log.d("login", Utils.getLogin(getContext()));
"login", Context.MODE_PRIVATE); viewModel.update(Utils.getLogin(getContext()));
viewModel.update(preferences.getString("login", ""));
} }
binding.refresh.setOnClickListener(v -> { binding.refresh.setOnClickListener(v -> {
if (getContext() != null) { if (getContext() != null && Utils.getLogin(getContext()) != null) {
SharedPreferences preferences = getContext().getSharedPreferences( viewModel.update(Utils.getLogin(getContext()));
"login", Context.MODE_PRIVATE);
viewModel.update(preferences.getString("login", ""));
} }
}); });
binding.scan.setOnClickListener(v -> Navigation.findNavController(getView()).navigate( binding.scan.setOnClickListener(v -> {
R.id.action_userFragment_to_qrScanFragment)); if (getView() != null)
Navigation.findNavController(getView()).navigate(
R.id.action_userFragment_to_qrScanFragment);
});
binding.logout.setOnClickListener(v -> { binding.logout.setOnClickListener(v -> {
if (getContext() != null) { if (getContext() != null) {
SharedPreferences sharedPreferences = getContext().getSharedPreferences("login", Context.MODE_PRIVATE); Utils.deleteLogin(getContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("login", null);
editor.commit();
} }
if (getView() != null) { if (getView() != null) {
Navigation.findNavController(getView()).navigate( Navigation.findNavController(getView()).navigate(
@ -98,9 +96,10 @@ public class UserFragment extends Fragment {
@Override @Override
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) { public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
if (QrScanDestination.INSTANCE.getDataIfExist(result) != null) { if (QrScanDestination.INSTANCE.getDataIfExist(result) != null) {
getParentFragmentManager().setFragmentResult(QrScanDestination.REQUEST_KEY, result); getParentFragmentManager().setFragmentResult(RESPONSE_KEY, result);
Navigation.findNavController(getView()).navigate( if (getView() != null)
R.id.action_userFragment_to_qrResultFragment); Navigation.findNavController(getView()).navigate(
R.id.action_userFragment_to_qrResultFragment);
} }
} }
}); });

View File

@ -1,7 +1,7 @@
package ru.myitschool.work.ui.qr.result; package ru.myitschool.work.ui.qr.result;
import android.content.Context; import static ru.myitschool.work.core.Constants.RESPONSE_KEY;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
@ -16,6 +16,7 @@ import androidx.navigation.Navigation;
import ru.myitschool.work.R; import ru.myitschool.work.R;
import ru.myitschool.work.databinding.FragmentQrResultBinding; import ru.myitschool.work.databinding.FragmentQrResultBinding;
import ru.myitschool.work.ui.qr.scan.QrScanDestination; import ru.myitschool.work.ui.qr.scan.QrScanDestination;
import ru.myitschool.work.utils.Utils;
public class QrResultFragment extends Fragment { public class QrResultFragment extends Fragment {
@ -33,30 +34,26 @@ public class QrResultFragment extends Fragment {
binding = FragmentQrResultBinding.bind(view); binding = FragmentQrResultBinding.bind(view);
viewModel = new ViewModelProvider(this).get(QrResultViewModel.class); viewModel = new ViewModelProvider(this).get(QrResultViewModel.class);
getParentFragmentManager().setFragmentResultListener(QrScanDestination.REQUEST_KEY, this, new FragmentResultListener() { getParentFragmentManager().setFragmentResultListener(RESPONSE_KEY, this, new FragmentResultListener() {
@Override @Override
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) { public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
resultQr = QrScanDestination.INSTANCE.getDataIfExist(result); resultQr = QrScanDestination.INSTANCE.getDataIfExist(result);
if (resultQr == null) { if (getContext() != null && Utils.getLogin(getContext()) != null) {
binding.result.setText(R.string.door_closed); viewModel.update(Utils.getLogin(getContext()), resultQr);
binding.close.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.warn_button, getContext().getTheme()));
} }
}
});
viewModel.stateLiveData.observe(getViewLifecycleOwner(), state -> { viewModel.stateLiveData.observe(getViewLifecycleOwner(), state -> {
if (state.getErrorMessage() == null && state.isOpened()) { if (state.getErrorMessage() == null && state.isOpened()) {
binding.result.setText(R.string.door_opened); binding.result.setText(R.string.door_opened);
} else if (state.getErrorMessage() != null) { binding.close.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.main_button, getContext().getTheme()));
binding.result.setText(R.string.error); } else if (state.getErrorMessage() != null) {
} binding.result.setText(R.string.error);
}); } else if (resultQr == null) {
binding.result.setText(R.string.door_closed);
SharedPreferences sharedPreferences = getContext().getSharedPreferences("login", Context.MODE_PRIVATE); binding.close.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.warn_button, getContext().getTheme()));
String login = sharedPreferences.getString("login", null);
if (login != null) {
viewModel.update(login, resultQr);
}
} }
}); });

View File

@ -3,10 +3,32 @@ package ru.myitschool.work.utils;
import static android.view.View.GONE; import static android.view.View.GONE;
import static android.view.View.VISIBLE; import static android.view.View.VISIBLE;
import android.content.Context;
import android.content.SharedPreferences;
public class Utils { public class Utils {
public static int visibleOrGone(boolean isVisible) { public static int visibleOrGone(boolean isVisible) {
return isVisible ? VISIBLE : GONE; return isVisible ? VISIBLE : GONE;
} }
public static void saveLogin(String login, Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("login", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("login", login);
editor.commit();
}
public static void deleteLogin(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("login", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("login", null);
editor.commit();
}
public static String getLogin(Context context) {
SharedPreferences preferences = context.getSharedPreferences(
"login", Context.MODE_PRIVATE);
return preferences.getString("login", null);
}
} }