feat: fixed UserRepositoryImpl

This commit is contained in:
aklyushova 2025-02-19 18:04:53 +03:00 committed by SunZar
parent d0229e9515
commit 1b160281e3
3 changed files with 92 additions and 13 deletions

View File

@ -15,17 +15,17 @@ import ru.myitschool.work.domain.entities.HistoryEntity;
import ru.myitschool.work.domain.entities.Status; import ru.myitschool.work.domain.entities.Status;
import ru.myitschool.work.domain.entities.UserEntity; import ru.myitschool.work.domain.entities.UserEntity;
public class UserResponseImpl { public class UserRepositoryImpl {
private static UserResponseImpl INSTANCE; private static UserRepositoryImpl INSTANCE;
private UserApi userApi = RetrofitFactory.getInstance().getUserApi(); private UserApi userApi = RetrofitFactory.getInstance().getUserApi();
private final CredentialsDataSource credentialsDataSource = CredentialsDataSource.getInstance(); private final CredentialsDataSource credentialsDataSource = CredentialsDataSource.getInstance();
private UserResponseImpl() {} private UserRepositoryImpl() {}
public static synchronized UserResponseImpl getInstance() { public static synchronized UserRepositoryImpl getInstance() {
if (INSTANCE == null) { if (INSTANCE == null) {
INSTANCE = new UserResponseImpl(); INSTANCE = new UserRepositoryImpl();
} }
return INSTANCE; return INSTANCE;
} }
@ -52,11 +52,11 @@ public class UserResponseImpl {
} }
public void getAllUserHistory(@NonNull Integer id, @NonNull Consumer<Status<List<UserEntity>>> callback) { public void getAllUserHistory(@NonNull Integer id, @NonNull Consumer<Status<List<HistoryEntity>>> callback) {
userApi.getAllUserHistory(id).enqueue(new CallToConsumer<>( userApi.getAllUserHistory(id).enqueue(new CallToConsumer<>(
callback, callback,
historiesDto -> { historiesDto -> {
ArrayList<HistoryEntity> result = new ArrayList<>(historiesDto.size()); ArrayList<HistoryEntity> result1 = new ArrayList<>(historiesDto.size());
for (HistoryDto history : historiesDto) { for (HistoryDto history : historiesDto) {
final Integer id1 = history.id; final Integer id1 = history.id;
final Integer idUser = history.idUser; final Integer idUser = history.idUser;
@ -64,10 +64,55 @@ public class UserResponseImpl {
final String nameReader = history.nameReader; final String nameReader = history.nameReader;
final String type = history.type; final String type = history.type;
if (idUser != null && id1 != null){ if (idUser != null && id1 != null){
result.add(new HistoryEntity(id1, idUser, time, nameReader, type)); result1.add(new HistoryEntity(id1, idUser, time, nameReader, type));
} }
} }
return result; return result1;
}
));
}
/* 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;
}
};
} */
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;
}
} }
)); ));
} }

View File

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

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Поиск"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/search_button"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/search_button"
android:layout_width="64dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/worker_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/search"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>