feat: Changed Login and UserApi

This commit is contained in:
SunZar 2025-02-19 18:58:38 +03:00
parent 1b160281e3
commit 8f683c1f0c
18 changed files with 354 additions and 85 deletions

View File

@ -11,11 +11,13 @@ import ru.myitschool.work.data.dto.UserDto;
import ru.myitschool.work.data.network.RetrofitFactory;
import ru.myitschool.work.data.source.CredentialsDataSource;
import ru.myitschool.work.data.source.UserApi;
import ru.myitschool.work.domain.UserRepository;
import ru.myitschool.work.domain.entities.HistoryEntity;
import ru.myitschool.work.domain.entities.Status;
import ru.myitschool.work.domain.entities.UserEntity;
import ru.myitschool.work.domain.sign.SignUserRepository;
public class UserRepositoryImpl {
public class UserRepositoryImpl implements UserRepository, SignUserRepository {
private static UserRepositoryImpl INSTANCE;
private UserApi userApi = RetrofitFactory.getInstance().getUserApi();
@ -31,6 +33,7 @@ public class UserRepositoryImpl {
}
/*@Override
public void getAllUsers(@NonNull Consumer<Status<List<UserEntity>>> callback) {
userApi.getAllUsers().enqueue(new CallToConsumer<>(
callback,
@ -50,10 +53,11 @@ public class UserRepositoryImpl {
}
));
}
}*/
public void getAllUserHistory(@NonNull Integer id, @NonNull Consumer<Status<List<HistoryEntity>>> callback) {
userApi.getAllUserHistory(id).enqueue(new CallToConsumer<>(
@Override
public void getAllUserHistory(@NonNull String username, @NonNull Consumer<Status<List<HistoryEntity>>> callback) {
userApi.getAllUserHistory(username).enqueue(new CallToConsumer<>(
callback,
historiesDto -> {
ArrayList<HistoryEntity> result1 = new ArrayList<>(historiesDto.size());
@ -73,49 +77,59 @@ public class UserRepositoryImpl {
}
/* public void getHistoryById(@NonNull Integer id, @NonNull Consumer<Status<List<HistoryEntity>>> callback){
userApi.getHistoryById(id).enqueue(new CallToConsumer<>(
@Override
public void isExistUser(@NonNull String username, Consumer<Status<Void>> callback) {
userApi.isExistUser(username).enqueue(new CallToConsumer<>(
callback,
historiesDto -> {
final Integer id1 = historiesDto.id;
final Integer idUser = historiesDto.idUser;
final Long time = historiesDto.time;
final String nameReader = historiesDto.nameReader;
final String type = historiesDto.type;
if (id1 != null && idUser != null){
HistoryEntity historyEntity = new HistoryEntity(id1, idUser, time, nameReader, type);
return historyEntity;
dto -> null
));
}
/* public void getHistoryById(@NonNull Integer id, @NonNull Consumer<Status<List<HistoryEntity>>> callback){
userApi.getHistoryById(id).enqueue(new CallToConsumer<>(
callback,
historiesDto -> {
final Integer id1 = historiesDto.id;
final Integer idUser = historiesDto.idUser;
final Long time = historiesDto.time;
final String nameReader = historiesDto.nameReader;
final String type = historiesDto.type;
if (id1 != null && idUser != null){
HistoryEntity historyEntity = new HistoryEntity(id1, idUser, time, nameReader, type);
return historyEntity;
}
else{
return null;
}
};
} */
@Override
public void getUserByUsername(@NonNull String username, @NonNull Consumer<Status<UserEntity>> callback) {
userApi.getUserByUsername(username).enqueue(new CallToConsumer<>(
callback,
user -> {
if (user != null) {
final String username1 = user.username;
final String photo = user.photo;
final Long lastVisit = user.lastVisit;
final Integer idUser = user.idUser;
final String position = user.position;
if (idUser != null && username != null){
return new UserEntity(username1, photo, lastVisit, idUser, position);
}
else{
return null;
}
}
else{
return null;
}
}
));
};
} */
public void isExistUser(@NonNull String username, Consumer<Status<UserEntity>> callback) {
userApi.isUserExist(username).enqueue(new CallToConsumer<>(
callback,
user -> {
if (user != null) {
final String username1 = user.username;
final String photo = user.photo;
final Long lastVisit = user.lastVisit;
final Integer idUser = user.idUser;
final String position = user.position;
if (idUser != null && username != null){
return new UserEntity(username1, photo, lastVisit, idUser, position);
}
else{
return null;
}
}
else{
return null;
}
}
));
}
}
public void login(@NonNull String username, @NonNull String password, Consumer<Status<Void>> callback) {
credentialsDataSource.updateLogin(username, password);

View File

@ -15,14 +15,14 @@ import ru.myitschool.work.ui.History;
public interface UserApi {
@GET("api/user")
Call<List<UserDto>> getAllUsers();
/* @GET("api/user/{id}")
Call<UserDto> getUserById(@Path("id") Integer id); */
@GET("api/user/{id}")
Call<UserDto> getUserByUsername(@Path("username") String Username);
@GET("api/user/username/{username}")
Call<UserDto> isUserExist(@Path("username") String username);
Call<UserDto> isExistUser(@Path("username") String username);
@GET("api/user/login")
Call<Void> login();
@GET("api/history/user/{id}")
Call<List<HistoryDto>> getAllUserHistory(@Path("id") Integer id);
Call<List<HistoryDto>> getAllUserHistory(@Path("id") String username);
/* @GET("api/history/{id}")
Call<HistoryDto> getHistoryById(@Path("id") Integer id);
*/

View File

@ -0,0 +1,21 @@
package ru.myitschool.work.domain;
import androidx.annotation.NonNull;
import java.util.List;
import java.util.function.Consumer;
import ru.myitschool.work.domain.entities.HistoryEntity;
import ru.myitschool.work.domain.entities.Status;
public class GetAllUserHistoryUseCase {
private final UserRepository repo;
public GetAllUserHistoryUseCase(UserRepository repo) {
this.repo = repo;
}
public void execute(@NonNull String username, @NonNull Consumer<Status<List<HistoryEntity>>> callback) {
repo.getAllUserHistory(username, callback);
}
}

View File

@ -0,0 +1,21 @@
package ru.myitschool.work.domain;
import androidx.annotation.NonNull;
import java.util.function.Consumer;
import ru.myitschool.work.domain.entities.Status;
import ru.myitschool.work.domain.entities.UserEntity;
import ru.myitschool.work.domain.sign.SignUserRepository;
public class GetUserByUsernameUseCase {
private final SignUserRepository repo;
public GetUserByUsernameUseCase(SignUserRepository repo) {
this.repo = repo;
}
public void execute(@NonNull String username, @NonNull Consumer<Status<UserEntity>> callback) {
repo.getUserByUsername(username, callback);
}
}

View File

@ -0,0 +1,22 @@
package ru.myitschool.work.domain;
import androidx.annotation.NonNull;
import java.util.List;
import java.util.function.Consumer;
import ru.myitschool.work.domain.entities.HistoryEntity;
import ru.myitschool.work.domain.entities.Status;
import ru.myitschool.work.domain.entities.UserEntity;
public interface UserRepository {
//void getAllUsers(@NonNull Consumer<Status<List<UserEntity>>> callback);
//void getUser(@NonNull Integer id, @NonNull Consumer<Status<UserEntity>> callback);
void getAllUserHistory(@NonNull String username, @NonNull Consumer<Status<List<HistoryEntity>>> callback);
//void getVolunteerCenterById(@NonNull Integer id, @NonNull Consumer<Status<HistoryEntity>> callback);
//void isExist(@NonNull String username, @NonNull Consumer<Status<Void>> callback);
//void register(@NonNull String id, @NonNull Consumer<Status<FullUserEntity>> callback);
void login(@NonNull String username, @NonNull String password, @NonNull Consumer<Status<Void>> callback);
}

View File

@ -5,11 +5,15 @@ import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class UserEntity {
@Nullable
@SerializedName("id")
public Integer id;
@Nullable
@SerializedName("username")
public String username;
@Nullable
@SerializedName("photo")
public String photo;
@ -18,20 +22,15 @@ public class UserEntity {
@SerializedName("lastVisit")
public Long lastVisit;
@Nullable
@SerializedName("idUser")
public Integer idUser;
@Nullable
@SerializedName("position")
public String position;
public UserEntity(@Nullable String username, @Nullable String photo, @Nullable Long lastVisit, @Nullable Integer idUser, @Nullable String position) {
public UserEntity(@Nullable String username, @Nullable String photo, @Nullable Long lastVisit, @Nullable Integer id, @Nullable String position) {
this.username = username;
this.photo = photo;
this.lastVisit = lastVisit;
this.idUser = idUser;
this.id = id;
this.position = position;
}
@ -72,14 +71,12 @@ public class UserEntity {
}
@Nullable
public Integer getIdUser() {
return idUser;
public Integer getId() {
return id;
}
public void setIdUser(@Nullable Integer idUser) {
this.idUser = idUser;
public void setId(@Nullable Integer id) {
this.id = id;
}
}

View File

@ -0,0 +1,30 @@
package ru.myitschool.work.domain.sign;
import androidx.annotation.NonNull;
import java.util.function.Consumer;
import ru.myitschool.work.domain.entities.UserEntity;
import ru.myitschool.work.domain.entities.Status;
public class IsUserExistUseCase {
private final SignUserRepository repo;
public IsUserExistUseCase(SignUserRepository repo) {
this.repo = repo;
}
public void execute(@NonNull String username, Consumer<Status<Boolean>> callback) {
//repo.isExistUser(username, callback);
repo.isExistUser(username, status -> {
boolean isAvailable = status.getStatusCode() == 200 || status.getStatusCode() == 404;
callback.accept(
new Status<>(
status.getStatusCode(),
isAvailable ? status.getStatusCode() == 200 : null,
status.getErrors()
)
);
});
}
}

View File

@ -0,0 +1,27 @@
package ru.myitschool.work.domain.sign;
import androidx.annotation.NonNull;
import java.util.function.Consumer;
import ru.myitschool.work.domain.entities.Status;
public class LoginUserUseCase {
private final SignUserRepository repo;
public LoginUserUseCase(SignUserRepository repo) {
this.repo = repo;
}
public void execute(
@NonNull String username,
@NonNull String password,
Consumer<Status<Void>> callback
) {
repo.login(username, password, (status) -> {
if (status.getStatusCode() != 200) repo.logout();
callback.accept(status);
});
}
}

View File

@ -0,0 +1,22 @@
package ru.myitschool.work.domain.sign;
import androidx.annotation.NonNull;
import java.util.function.Consumer;
import ru.myitschool.work.domain.entities.UserEntity;
import ru.myitschool.work.domain.entities.Status;
public interface SignUserRepository {
void getUserByUsername(@NonNull String username, Consumer<Status<UserEntity>> callback);
void isExistUser(@NonNull String login, Consumer<Status<Void>> callback);
void login(
@NonNull String username,
@NonNull String password,
Consumer<Status<Void>> callback
);
void logout();
}

View File

@ -1,6 +0,0 @@
package ru.myitschool.work.ui;
public class Constants {
public static String KEY_LOGIN = "login";
public static String KEY_RESULT = "result";
}

View File

@ -21,6 +21,7 @@ import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import ru.myitschool.work.R;
import ru.myitschool.work.databinding.FragmentResultBinding;
import ru.myitschool.work.utils.Constants;
public class ResultFragment extends Fragment {
FragmentResultBinding binding;

View File

@ -1,6 +1,5 @@
package ru.myitschool.work.ui;
package ru.myitschool.work.ui.information;
import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -12,6 +11,7 @@ import java.util.ArrayList;
import java.util.List;
import ru.myitschool.work.databinding.ItemHistoryBinding;
import ru.myitschool.work.ui.History;
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {

View File

@ -1,4 +1,4 @@
package ru.myitschool.work.ui;
package ru.myitschool.work.ui.information;
import android.content.Context;
import android.content.SharedPreferences;
@ -6,12 +6,10 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentResultListener;
import androidx.navigation.Navigation;
import com.squareup.picasso.Picasso;
@ -25,8 +23,11 @@ import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import ru.myitschool.work.R;
import ru.myitschool.work.databinding.FragmentInformationBinding;
import ru.myitschool.work.utils.Constants;
import ru.myitschool.work.ui.History;
import ru.myitschool.work.ui.StoreAPI;
import ru.myitschool.work.ui.User;
import ru.myitschool.work.ui.qr.scan.QrScanDestination;
import ru.myitschool.work.ui.qr.scan.QrScanFragment;
public class InformationFragment extends Fragment {
FragmentInformationBinding binding;
@ -49,11 +50,11 @@ public class InformationFragment extends Fragment {
getInformation();
binding.scan.setOnClickListener(view1 -> {
onClickScan();
onClickScan(view);
});
binding.logout.setOnClickListener(view2 -> {
onClickLogout();
onClickLogout(view);
});
binding.refreshLayout.setOnRefreshListener(() -> {
@ -95,7 +96,7 @@ public class InformationFragment extends Fragment {
//getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new LoginFragment()).commit();
}
private void getInformation() {
/*private void getInformation() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ru.myitschool.work.core.Constants.SERVER_ADDRESS)
.addConverterFactory(GsonConverterFactory.create())
@ -134,7 +135,7 @@ public class InformationFragment extends Fragment {
takeError();
}
});
}
}*/
private void takeError() {
//binding.photo.setVisibility(View.GONE);

View File

@ -0,0 +1,117 @@
package ru.myitschool.work.ui.information;
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 java.util.List;
import ru.myitschool.work.data.UserRepositoryImpl;
import ru.myitschool.work.domain.GetAllUserHistoryUseCase;
import ru.myitschool.work.domain.sign.IsUserExistUseCase;
import ru.sicampus.bootcamp2025.data.UserRepositoryImpl;
import ru.sicampus.bootcamp2025.domain.GetUsersListUseCase;
import ru.sicampus.bootcamp2025.domain.GetVolunteerCenterByIdUseCase;
import ru.sicampus.bootcamp2025.domain.GetVolunteerCentersListUseCase;
import ru.sicampus.bootcamp2025.domain.entites.FullVolunteerCenterEntity;
import ru.sicampus.bootcamp2025.domain.entites.ItemVolunteerCenterEntity;
import ru.sicampus.bootcamp2025.domain.entites.Status;
public class InformationViewModel extends ViewModel {
private final MutableLiveData<State> mutableStateCentersLiveData = new MutableLiveData<>();
public final LiveData<State> stateCentersLiveData = mutableStateCentersLiveData;
private final MutableLiveData<State> mutableStateVolunteerCenterLiveData = new MutableLiveData<>();
public final LiveData<State> stateVolunteerCenterLiveData = mutableStateVolunteerCenterLiveData;
public final GetAllUserHistoryUseCase getAllUserHistoryUseCase = new GetAllUserHistoryUseCase(
UserRepositoryImpl.getInstance()
);
/*public ListViewModel() {
update();
}*/
private Integer volunteerCenterId;
public void changeSelectedVCId(@NonNull Integer volunteerCenterId) {
this.volunteerCenterId = volunteerCenterId;
}
public void load() {
mutableStateCentersLiveData.setValue(new ListViewModel.State(null, null, null, true));
getAllUserHistoryUseCase.execute(status -> {
mutableStateCentersLiveData.postValue(fromStatus(status));
});
}
public void getVCById() {
getVolunteerCenterByIdUseCase.execute(volunteerCenterId, status -> {
Log.d("taggis", status.getStatusCode() + " " + status.getErrors());
if (status.getStatusCode() == 200 && status.getErrors() == null) {
mutableStateVolunteerCenterLiveData.postValue(new ListViewModel.State(
status.getErrors() != null ? status.getErrors().getLocalizedMessage() : null,
null,
status.getValue(),
false
));
} else {
//mutableErrorLiveData.postValue("Something wrong");
}
});
}
private ListViewModel.State fromStatus(Status<List<ItemVolunteerCenterEntity>> status) {
return new ListViewModel.State(
status.getErrors() != null ? status.getErrors().getLocalizedMessage() : null,
status.getValue(),
null,
false
);
}
public class State {
@Nullable
private final String errorMessage;
@Nullable
private final List<ItemVolunteerCenterEntity> itemsVolunteerCenters;
@Nullable
private final FullVolunteerCenterEntity volunteerCenter;
private final boolean isLoading;
public State(@Nullable String errorMessage, @Nullable List<ItemVolunteerCenterEntity> itemsVolunteerCenters, FullVolunteerCenterEntity volunteerCenter, boolean isLoading) {
this.errorMessage = errorMessage;
this.itemsVolunteerCenters = itemsVolunteerCenters;
this.volunteerCenter = volunteerCenter;
this.isLoading = isLoading;
}
@Nullable
public String getErrorMessage() {
return errorMessage;
}
@Nullable
public List<ItemVolunteerCenterEntity> getItemsVolunteerCenters() {
return itemsVolunteerCenters;
}
@Nullable
public FullVolunteerCenterEntity getVolunteerCenter() {
return volunteerCenter;
}
public boolean isLoading() {
return isLoading;
}
}
}

View File

@ -1,4 +1,4 @@
package ru.myitschool.work.ui;
package ru.myitschool.work.ui.login;
import android.content.Context;
import android.content.SharedPreferences;
@ -26,6 +26,7 @@ import ru.myitschool.work.R;
import ru.myitschool.work.core.Constants;
import ru.myitschool.work.databinding.FragmentLoginBinding;
import ru.myitschool.work.ui.StoreAPI;
import ru.myitschool.work.ui.login.LoginViewModel;
import ru.myitschool.work.utils.OnChangeText;
import ru.myitschool.work.utils.PreferenceManager;
@ -102,7 +103,7 @@ public class LoginFragment extends Fragment {
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
viewModel.changePassword(s.toString());
viewModel.changePassword(charSequence.toString());
if (binding.password.getText().length() >= 5) {
binding.login.setEnabled(true);
} else {
@ -150,7 +151,7 @@ public class LoginFragment extends Fragment {
public void onResponse(Call<Boolean> call, Response<Boolean> response) {
if (response.isSuccessful()) {
if (response.body() != null && response.body()) {
preferenceManager.putString(ru.myitschool.work.ui.Constants.KEY_LOGIN, binding.username.getText().toString());
preferenceManager.putString(ru.myitschool.work.utils.Constants.KEY_LOGIN, binding.username.getText().toString());
Navigation.findNavController(view).navigate(R.id.action_loginFragment_to_informationFragment);
//getFragmentManager().beginTransaction().replace(R.id.nav_host_fragment, new InformationFragment()).commit();
} else {

View File

@ -11,17 +11,16 @@ import androidx.lifecycle.ViewModel;
import ru.myitschool.work.R;
import ru.myitschool.work.data.UserRepositoryImpl;
import ru.myitschool.work.domain.sign.CreateUserUseCase;
import ru.myitschool.work.domain.sign.IsUserExistUseCase;
import ru.myitschool.work.domain.sign.LoginUserUseCase;
public class LoginViewModel extends ViewModel {
private final State INIT_STATE = new State(R.string.title_init, R.string.button_init, false);
private final MutableLiveData<State> mutableStateLiveData = new MutableLiveData<>(
//private final State INIT_STATE = new State(R.string.title_init, R.string.button_init, false);
/*private final MutableLiveData<State> mutableStateLiveData = new MutableLiveData<>(
INIT_STATE
);
public final LiveData<State> stateLiveData = mutableStateLiveData;
);*/
//public final LiveData<State> stateLiveData = mutableStateLiveData;
private final MutableLiveData<String> mutableErrorLiveData = new MutableLiveData<>();
public final LiveData<String> errorLiveData = mutableErrorLiveData;
@ -51,7 +50,7 @@ public class LoginViewModel extends ViewModel {
this.username = username;
if (userCheckCompleted) {
userCheckCompleted = false;
mutableStateLiveData.postValue(INIT_STATE);
//mutableStateLiveData.postValue(INIT_STATE);
}
}

View File

@ -3,4 +3,6 @@ package ru.myitschool.work.utils;
public class Constants {
public static final String KEY_PREFERENCE_NAME = "App";
public static final String KEY_USER_USERNAME = "userUsername";
public static String KEY_LOGIN = "login";
public static String KEY_RESULT = "result";
}

View File

@ -6,7 +6,7 @@
app:startDestination="@id/loginFragment">
<fragment
android:id="@+id/informationFragment"
android:name="ru.myitschool.work.ui.InformationFragment"
android:name="ru.myitschool.work.ui.information.InformationFragment"
android:label="HistoryList"
tools:layout="@layout/fragment_information">
<action