Profile done
This commit is contained in:
parent
5b005e5d63
commit
1194f4d2ea
@ -21,36 +21,6 @@ class RootActivity : AppCompatActivity() {
|
|||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_root)
|
setContentView(R.layout.activity_root)
|
||||||
val navHostFragment = supportFragmentManager
|
|
||||||
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment?
|
|
||||||
|
|
||||||
if (navHostFragment != null) {
|
|
||||||
val navController = navHostFragment.navController
|
|
||||||
navController.graph = navController.createGraph(
|
|
||||||
startDestination = LoginDestination
|
|
||||||
) {
|
|
||||||
fragment<LoginFragment, LoginDestination>()
|
|
||||||
fragment<QrScanFragment, QrScanDestination>()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onBackPressedDispatcher.addCallback(
|
|
||||||
this,
|
|
||||||
object : OnBackPressedCallback(true) {
|
|
||||||
override fun handleOnBackPressed() {
|
|
||||||
onSupportNavigateUp()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onSupportNavigateUp(): Boolean {
|
|
||||||
val navController = findNavController(R.id.nav_host_fragment)
|
|
||||||
val popBackResult = if (navController.previousBackStackEntry != null) {
|
|
||||||
navController.popBackStack()
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
return popBackResult || super.onSupportNavigateUp()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,71 @@
|
|||||||
package ru.myitschool.work.ui.profile;
|
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.fragment.app.Fragment;
|
||||||
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
|
||||||
|
import com.squareup.picasso.Picasso;
|
||||||
|
|
||||||
|
import ru.myitschool.work.R;
|
||||||
|
import ru.myitschool.work.databinding.FragmentUserBinding;
|
||||||
|
import ru.myitschool.work.domain.entities.UserEntity;
|
||||||
|
import ru.myitschool.work.utils.Utils;
|
||||||
|
|
||||||
public class UserFragment extends Fragment {
|
public class UserFragment extends Fragment {
|
||||||
|
|
||||||
|
private static final String KEY_ID = "id";
|
||||||
|
|
||||||
|
private FragmentUserBinding binding;
|
||||||
|
private UserViewModel viewModel;
|
||||||
|
|
||||||
|
public UserFragment() {
|
||||||
|
super(R.layout.fragment_user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||||
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
binding = FragmentUserBinding.bind(view);
|
||||||
|
|
||||||
|
viewModel = new ViewModelProvider(this).get(UserViewModel.class);
|
||||||
|
viewModel.stateLiveData.observe(getViewLifecycleOwner(), state -> {
|
||||||
|
UserEntity entity = state.getItem();
|
||||||
|
if (entity == null) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
binding.photo.setVisibility(Utils.visibleOrGone(entity.getPhoto() != null));
|
||||||
|
binding.position.setVisibility(Utils.visibleOrGone(entity.getPosition() != null));
|
||||||
|
binding.lastEntry.setVisibility(Utils.visibleOrGone(entity.getLast_visit() != null));
|
||||||
|
|
||||||
|
binding.fullname.setText(entity.getName());
|
||||||
|
binding.position.setText(entity.getPosition());
|
||||||
|
binding.lastEntry.setText(entity.getLast_visit());
|
||||||
|
|
||||||
|
if (entity.getPhoto() != null) {
|
||||||
|
Picasso.get().load(entity.getPhoto()).into(binding.photo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
String id = getArguments() != null ? getArguments().getString(KEY_ID) : null;
|
||||||
|
if (id == null) throw new IllegalStateException("ID is null");
|
||||||
|
viewModel.update(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
binding = null;
|
||||||
|
super.onDestroyView();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Bundle getBundle(@NonNull String id) {
|
||||||
|
Bundle bundle = new Bundle();
|
||||||
|
bundle.putString(KEY_ID, id);
|
||||||
|
return bundle;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,73 @@
|
|||||||
package ru.myitschool.work.ui.profile;
|
package ru.myitschool.work.ui.profile;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.MutableLiveData;
|
||||||
import androidx.lifecycle.ViewModel;
|
import androidx.lifecycle.ViewModel;
|
||||||
|
|
||||||
|
import ru.myitschool.work.data.UserRepositoryImplementation;
|
||||||
|
import ru.myitschool.work.domain.entities.Status;
|
||||||
|
import ru.myitschool.work.domain.entities.UserEntity;
|
||||||
|
import ru.myitschool.work.domain.user.GetUserByIdUseCase;
|
||||||
|
|
||||||
public class UserViewModel extends ViewModel {
|
public class UserViewModel extends ViewModel {
|
||||||
|
|
||||||
|
private final MutableLiveData<State> mutableStateLiveData = new MutableLiveData<State>();
|
||||||
|
public final LiveData<State> stateLiveData = mutableStateLiveData;
|
||||||
|
|
||||||
|
public final GetUserByIdUseCase getUserByIdUseCase = new GetUserByIdUseCase(
|
||||||
|
UserRepositoryImplementation.getInstance()
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public UserViewModel(String id) {
|
||||||
|
update(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(@NonNull String id) {
|
||||||
|
mutableStateLiveData.setValue(new State(null, null, true));
|
||||||
|
getUserByIdUseCase.execute(id, status -> {
|
||||||
|
mutableStateLiveData.postValue(fromStatus(status));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private State fromStatus(Status<UserEntity> status) {
|
||||||
|
return new State(
|
||||||
|
status.getErrors() != null ? status.getErrors().getLocalizedMessage(): null,
|
||||||
|
status.getValue(),
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class State {
|
||||||
|
@Nullable
|
||||||
|
private final String errorMessage;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final UserEntity item;
|
||||||
|
|
||||||
|
private final boolean isLoading;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public UserEntity getItem() {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isLoading() {
|
||||||
|
return isLoading;
|
||||||
|
}
|
||||||
|
|
||||||
|
public State(@Nullable String errorMessage, @Nullable UserEntity item, boolean isLoading) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
this.item = item;
|
||||||
|
this.isLoading = isLoading;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
13
app/src/main/java/ru/myitschool/work/utils/Utils.java
Normal file
13
app/src/main/java/ru/myitschool/work/utils/Utils.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package ru.myitschool.work.utils;
|
||||||
|
|
||||||
|
import static android.view.View.GONE;
|
||||||
|
import static android.view.View.VISIBLE;
|
||||||
|
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
public class Utils {
|
||||||
|
|
||||||
|
public static int visibleOrGone(boolean isVisible) {
|
||||||
|
return isVisible ? VISIBLE : GONE;
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent"
|
||||||
|
android:id="@+id/root"
|
||||||
<androidx.fragment.app.FragmentContainerView
|
app:navGraph="@navigation/nav_graph"
|
||||||
android:id="@+id/nav_host_fragment"
|
android:name="androidx.navigation.fragment.NavHostFragment"
|
||||||
android:name="androidx.navigation.fragment.NavHostFragment"
|
app:defaultNavHost="true" />
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
app:defaultNavHost="true" />
|
|
||||||
</FrameLayout>
|
|
||||||
|
31
app/src/main/res/navigation/nav_graph.xml
Normal file
31
app/src/main/res/navigation/nav_graph.xml
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:id="@+id/nav_graph"
|
||||||
|
app:startDestination="@id/loginFragment">
|
||||||
|
|
||||||
|
<fragment
|
||||||
|
android:id="@+id/userFragment"
|
||||||
|
android:name="ru.myitschool.work.ui.profile.UserFragment"
|
||||||
|
android:label="UserFragment" >
|
||||||
|
<action
|
||||||
|
android:id="@+id/action_userFragment_to_qrScanFragment"
|
||||||
|
app:destination="@id/qrScanFragment" />
|
||||||
|
</fragment>
|
||||||
|
<fragment
|
||||||
|
android:id="@+id/qrScanFragment"
|
||||||
|
android:name="ru.myitschool.work.ui.qr.scan.QrScanFragment"
|
||||||
|
android:label="QrScanFragment" >
|
||||||
|
<action
|
||||||
|
android:id="@+id/action_qrScanFragment_to_userFragment"
|
||||||
|
app:destination="@id/userFragment" />
|
||||||
|
</fragment>
|
||||||
|
<fragment
|
||||||
|
android:id="@+id/loginFragment"
|
||||||
|
android:name="ru.myitschool.work.ui.login.LoginFragment"
|
||||||
|
android:label="LoginFragment" >
|
||||||
|
<action
|
||||||
|
android:id="@+id/action_loginFragment_to_userFragment"
|
||||||
|
app:destination="@id/userFragment" />
|
||||||
|
</fragment>
|
||||||
|
</navigation>
|
Loading…
x
Reference in New Issue
Block a user