added ProfileFragment ProfileViewModel Utils
fragment_profile.xml
This commit is contained in:
parent
9e0f181f56
commit
86cbb6b87d
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<State> mutableStateLiveData = new MutableLiveData<>();
|
||||
|
||||
public final LiveData<State> 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;
|
||||
}
|
||||
}
|
||||
}
|
9
app/src/main/java/ru/myitschool/work/utils/Utils.java
Normal file
9
app/src/main/java/ru/myitschool/work/utils/Utils.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -1,6 +1,45 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
android:layout_height="match_parent"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<ImageView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/photo"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/username"
|
||||
app:layout_constraintTop_toTopOf="parent"/>
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/username"
|
||||
app:layout_constraintTop_toBottomOf="@id/photo"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"/>
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/position"
|
||||
app:layout_constraintTop_toBottomOf="@id/username"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"/>
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/lastTime"
|
||||
app:layout_constraintTop_toBottomOf="@id/position"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"/>
|
||||
<Button
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/exit"
|
||||
android:text="выход"
|
||||
app:layout_constraintTop_toBottomOf="@id/lastTime"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"/>
|
||||
|
||||
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
x
Reference in New Issue
Block a user