diff --git a/app/src/main/java/ru/myitschool/work/ui/profile/ProfileFragment.java b/app/src/main/java/ru/myitschool/work/ui/profile/ProfileFragment.java new file mode 100644 index 0000000..cdf10a1 --- /dev/null +++ b/app/src/main/java/ru/myitschool/work/ui/profile/ProfileFragment.java @@ -0,0 +1,70 @@ +package ru.myitschool.work.ui.profile; + +import android.os.Bundle; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; +import androidx.navigation.Navigation; + +import ru.myitschool.work.R; +import ru.myitschool.work.databinding.FragmentProfileBinding; +import ru.myitschool.work.domain.entities.HistoryEntity; +import ru.myitschool.work.domain.entities.UserEntity; +import ru.myitschool.work.ui.History; +import ru.myitschool.work.utils.Constants; +import ru.myitschool.work.utils.PreferenceManager; +import ru.myitschool.work.utils.Utils; + +public class ProfileFragment extends Fragment { + private static final String KEY_USER_USERNAME = "username"; + + private FragmentProfileBinding binding; + private PreferenceManager preferenceManager; + private ProfileViewModel viewModel; + + public ProfileFragment() { + super(R.layout.fragment_profile); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + binding = FragmentProfileBinding.bind(view); + preferenceManager = new PreferenceManager(this.getContext()); + viewModel = new ViewModelProvider(this).get(ProfileViewModel.class); + viewModel.stateLiveData.observe(getViewLifecycleOwner(), state -> { + final UserEntity userEntity = state.getUser(); + + binding.username.setText(userEntity.getUsername()); + binding.position.setText(userEntity.getPosition()); + binding.lastTime.setText(userEntity.getLastVisit().toString()); + + }); + + binding.exit.setOnClickListener(view2 -> { + preferenceManager.clear(); + view.getRootView().findViewById(R.id.nav_view).setVisibility(View.GONE); + Navigation.findNavController(view).navigate(R.id.action_profileFragment_to_informationFragment); + }); + + viewModel.load(preferenceManager.getString(Constants.KEY_USER_USERNAME)); + + } + + @Override + public void onDestroyView() { + binding = null; + super.onDestroyView(); + } + + public static Bundle getBundle(@NonNull String id) { + Bundle bundle = new Bundle(); + bundle.putString(KEY_USER_USERNAME, id); + return bundle; + } +} + + diff --git a/app/src/main/java/ru/myitschool/work/ui/profile/ProfileViewModel.java b/app/src/main/java/ru/myitschool/work/ui/profile/ProfileViewModel.java new file mode 100644 index 0000000..5897765 --- /dev/null +++ b/app/src/main/java/ru/myitschool/work/ui/profile/ProfileViewModel.java @@ -0,0 +1,118 @@ +package ru.myitschool.work.ui.profile; + +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +import ru.myitschool.work.data.UserRepositoryImpl; +import ru.myitschool.work.domain.GetAllUserHistoryUseCase; +import ru.myitschool.work.domain.GetUserByUsernameUseCase; +import ru.myitschool.work.domain.entities.HistoryEntity; +import ru.myitschool.work.domain.entities.UserEntity; +import ru.myitschool.work.domain.sign.IsUserExistUseCase; + +public class ProfileViewModel extends ViewModel { + private final MutableLiveData mutableStateLiveData = new MutableLiveData<>(); + + public final LiveData stateLiveData = mutableStateLiveData; + + public final GetUserByUsernameUseCase getUserByUsernameUseCase = new GetUserByUsernameUseCase( + UserRepositoryImpl.getInstance() + ); + + public final GetAllUserHistoryUseCase getAllUserHistoryListUseCase = new GetAllUserHistoryUseCase( + UserRepositoryImpl.getInstance() + ); + private final IsUserExistUseCase isUserExistUseCase = new IsUserExistUseCase( + UserRepositoryImpl.getInstance() + ); + + public void load(@NonNull String username) { + mutableStateLiveData.setValue(new State(null, null, null, true)); + postUserByUsername(username); + } + + private void postUserByUsername(String username) { + final String currentUsername = username; + if (currentUsername == null || currentUsername.isEmpty()) { + return; + } + isUserExistUseCase.execute(currentUsername, status -> { + if (status.getValue() == null || status.getErrors() != null) { + Log.d("tagg", status.getErrors().toString()); + //mutableErrorLiveData.postValue("Something wrong. Try later =(" + status.getErrors()); + return; + } + //userCheckCompleted = true; + if (status.getStatusCode() == 200) { + if (status.getValue().g != null) { + GetAllUserHistoryUseCase.execute(status.getValue().getStatus(), status2 -> { + Log.d("taggis", status.getStatusCode() + " " + status.getErrors()); + if (status.getStatusCode() == 200 && status.getErrors() == null) { + mutableStateLiveData.postValue(new ProfileViewModel.State( + status.getErrors() != null ? status.getErrors().getLocalizedMessage() : null, + status.getValue(), + status2.getValue(), + false + )); + } else { + //mutableErrorLiveData.postValue("Something wrong"); + } + }); + } else { + mutableStateLiveData.postValue(new State( + status.getErrors() != null ? status.getErrors().getLocalizedMessage() : null, + status.getValue(), + null, + false + )); + } + } + }); + } + + + + + public class State { + @Nullable + private final String errorMessage; + + @Nullable + private final UserEntity user; + @Nullable + private final HistoryEntity historyEntity; + + private final boolean isLoading; + + public State(@Nullable String errorMessage, @Nullable UserEntity user, @Nullable HistoryEntity historyEntity, boolean isLoading) { + this.errorMessage = errorMessage; + this.user = user; + this.historyEntity = historyEntity; + this.isLoading = isLoading; + } + + @Nullable + public String getErrorMessage() { + return errorMessage; + } + + @Nullable + public UserEntity getUser() { + return user; + } + + @Nullable + public HistoryEntity getVolunteerCenter() { + return historyEntity; + } + + public boolean isLoading() { + return isLoading; + } + } +} diff --git a/app/src/main/java/ru/myitschool/work/utils/Utils.java b/app/src/main/java/ru/myitschool/work/utils/Utils.java new file mode 100644 index 0000000..63a5e47 --- /dev/null +++ b/app/src/main/java/ru/myitschool/work/utils/Utils.java @@ -0,0 +1,9 @@ +package ru.myitschool.work.utils; + +import android.view.View; + +public class Utils { + public static int visibleOrGone(boolean isVisible) { + return isVisible ? View.VISIBLE : View.GONE; + } +} diff --git a/app/src/main/res/layout/fragment_profile.xml b/app/src/main/res/layout/fragment_profile.xml index 77d9ef6..9121f28 100644 --- a/app/src/main/res/layout/fragment_profile.xml +++ b/app/src/main/res/layout/fragment_profile.xml @@ -1,6 +1,45 @@ + android:layout_height="match_parent" + xmlns:app="http://schemas.android.com/apk/res-auto"> + + + + +