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

View File

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

View File

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

View File

@ -3,10 +3,32 @@ package ru.myitschool.work.utils;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import android.content.Context;
import android.content.SharedPreferences;
public class Utils {
public static int visibleOrGone(boolean isVisible) {
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);
}
}