Merge remote-tracking branch 'origin/main'

# Conflicts:
#	app/src/main/java/ru/myitschool/work/ui/InformationFragment.java
#	app/src/main/java/ru/myitschool/work/ui/login/LoginFragment.java
#	app/src/main/res/layout/fragment_information.xml
#	app/src/main/res/layout/fragment_login.xml
This commit is contained in:
SunZar 2025-02-19 16:50:18 +03:00
commit dc5c724263
20 changed files with 878 additions and 13 deletions

View File

@ -0,0 +1,53 @@
package ru.myitschool.work.data;
import androidx.annotation.NonNull;
import java.util.function.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import ru.myitschool.work.domain.entities.Status;
public class CallToConsumer <SOURCE, DEST> implements Callback<SOURCE> {
@NonNull
private final Consumer<Status<DEST>> callback;
@NonNull
private final Mapper<SOURCE, DEST> mapper;
public CallToConsumer(
@NonNull Consumer<Status<DEST>> callback,
@NonNull Mapper<SOURCE, DEST> mapper
) {
this.callback = callback;
this.mapper = mapper;
}
@Override
public void onResponse(@NonNull Call<SOURCE> call, @NonNull Response<SOURCE> response) {
callback.accept(
new Status<>(
response.code(),
mapper.map(response.body()),
null
)
);
}
@Override
public void onFailure(@NonNull Call<SOURCE> call, @NonNull Throwable throwable) {
callback.accept(
new Status<>(
-1,
null,
throwable
)
);
}
public interface Mapper<SOURCE, DEST> {
DEST map(SOURCE source);
}
}

View File

@ -0,0 +1,90 @@
package ru.myitschool.work.data;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import ru.myitschool.work.data.dto.HistoryDto;
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.entities.HistoryEntity;
import ru.myitschool.work.domain.entities.Status;
import ru.myitschool.work.domain.entities.UserEntity;
public class UserResponseImpl {
private static UserResponseImpl INSTANCE;
private UserApi userApi = RetrofitFactory.getInstance().getUserApi();
private final CredentialsDataSource credentialsDataSource = CredentialsDataSource.getInstance();
private UserResponseImpl() {}
public static synchronized UserResponseImpl getInstance() {
if (INSTANCE == null) {
INSTANCE = new UserResponseImpl();
}
return INSTANCE;
}
public void getAllUsers(@NonNull Consumer<Status<List<UserEntity>>> callback) {
userApi.getAllUsers().enqueue(new CallToConsumer<>(
callback,
usersDto -> {
ArrayList<UserEntity> result = new ArrayList<>(usersDto.size());
for (UserDto user : usersDto) {
final String username = 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) {
result.add(new UserEntity(username, photo, lastVisit, idUser, position));
}
}
return result;
}
));
}
public void getAllUserHistory(@NonNull Integer id, @NonNull Consumer<Status<List<UserEntity>>> callback) {
userApi.getAllUserHistory(id).enqueue(new CallToConsumer<>(
callback,
historiesDto -> {
ArrayList<HistoryEntity> result = new ArrayList<>(historiesDto.size());
for (HistoryDto history : historiesDto) {
final Integer id1 = history.id;
final Integer idUser = history.idUser;
final Long time = history.time;
final String nameReader = history.nameReader;
final String type = history.type;
if (idUser != null && id1 != null){
result.add(new HistoryEntity(id1, idUser, time, nameReader, type));
}
}
return result;
}
));
}
public void login(@NonNull String username, @NonNull String password, Consumer<Status<Void>> callback) {
credentialsDataSource.updateLogin(username, password);
userApi = RetrofitFactory.getInstance().getUserApi();
userApi.login().enqueue(new CallToConsumer<>(
callback,
dto -> null
));
}
public void logout() {
credentialsDataSource.logout();
}
}

View File

@ -0,0 +1,30 @@
package ru.myitschool.work.data.dto;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class HistoryDto {
@Nullable
@SerializedName("id")
public Integer id;
@Nullable
@SerializedName("idUser")
public Integer idUser;
@Nullable
@SerializedName("time")
public Long time;
@Nullable
@SerializedName("nameReader")
public String nameReader;
@Nullable
@SerializedName("type")
public String type;
}

View File

@ -0,0 +1,17 @@
package ru.myitschool.work.data.dto;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class ReaderDto {
@Nullable
@SerializedName("idReader")
public Integer idReader;
@Nullable
@SerializedName("typeReader")
public Integer typeReader;
}

View File

@ -0,0 +1,18 @@
package ru.myitschool.work.data.dto;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class RoleDto {
@Nullable
@SerializedName("idUser")
public Integer idUser;
@Nullable
@SerializedName("status")
public int status;
}

View File

@ -0,0 +1,116 @@
package ru.myitschool.work.data.dto;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class UserDto {
@Nullable
@SerializedName("username")
public String username;
@Nullable
@SerializedName("password")
public String password;
@Nullable
@SerializedName("photo")
public String photo;
@Nullable
@SerializedName("lastVisit")
public Long lastVisit;
@Nullable
@SerializedName("idUser")
public Integer idUser;
@Nullable
@SerializedName("status")
public int status;
@Nullable
@SerializedName("position")
public String position;
public UserDto(@Nullable String username, @Nullable String password, @Nullable String photo, @Nullable Long lastVisit, @Nullable Integer idUser, int status, @Nullable String position) {
this.username = username;
this.password = password;
this.photo = photo;
this.lastVisit = lastVisit;
this.idUser = idUser;
this.status = status;
this.position = position;
}
@Nullable
public String getPosition() {
return position;
}
public void setPosition(@Nullable String position) {
this.position = position;
}
@Nullable
public String getUsername() {
return username;
}
public void setUsername(@Nullable String username) {
this.username = username;
}
@Nullable
public String getPassword() {
return password;
}
public void setPassword(@Nullable String password) {
this.password = password;
}
@Nullable
public String getPhoto() {
return photo;
}
public void setPhoto(@Nullable String photo) {
this.photo = photo;
}
@Nullable
public Long getLastVisit() {
return lastVisit;
}
public void setLastVisit(@Nullable Long lastVisit) {
this.lastVisit = lastVisit;
}
@Nullable
public Integer getIdUser() {
return idUser;
}
public void setIdUser(@Nullable Integer idUser) {
this.idUser = idUser;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}

View File

@ -0,0 +1,49 @@
package ru.myitschool.work.data.network;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import ru.myitschool.work.data.source.CredentialsDataSource;
import ru.myitschool.work.data.source.UserApi;
public class RetrofitFactory {
private static RetrofitFactory INSTANCE;
private RetrofitFactory() {}
public static synchronized RetrofitFactory getInstance() {
if (INSTANCE == null) {
INSTANCE = new RetrofitFactory();
}
return INSTANCE;
}
private final OkHttpClient.Builder client = new OkHttpClient.Builder()
.addInterceptor(chain -> {
String authData = CredentialsDataSource.getInstance().getAuthData();
if (authData == null) {
return chain.proceed(chain.request());
} else {
Request request = chain.request()
.newBuilder()
.addHeader("Authorization", authData)
.build();
return chain.proceed(request);
}
}
);
private final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:8080/")
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
public UserApi getUserApi() {
return retrofit.create(UserApi.class);
}
}

View File

@ -0,0 +1,36 @@
package ru.myitschool.work.data.source;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import okhttp3.Credentials;
public class CredentialsDataSource {
private static CredentialsDataSource INSTANCE;
private CredentialsDataSource() {}
public static synchronized CredentialsDataSource getInstance() {
if (INSTANCE == null) {
INSTANCE = new CredentialsDataSource();
}
return INSTANCE;
}
@Nullable
private String authData = null;
@Nullable
public String getAuthData() {
return authData;
}
public void updateLogin(@NonNull String username, @NonNull String password) {
authData = Credentials.basic(username, password);
}
public void logout() {
authData = null;
}
}

View File

@ -0,0 +1,29 @@
package ru.myitschool.work.data.source;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import ru.myitschool.work.data.dto.HistoryDto;
import ru.myitschool.work.data.dto.UserDto;
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/username/{username}")
Call<UserDto> isUserExist(@Path("username") String username);
@GET("api/user/login")
Call<Void> login();
@GET("api/history/user/{id}")
Call<List<HistoryDto>> getAllUserHistory(@Path("id") Integer id);
@GET("api/history/{id}")
Call<HistoryDto> getHistoryById(@Path("id") Integer id);
}

View File

@ -0,0 +1,81 @@
package ru.myitschool.work.domain.entities;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class HistoryEntity {
@Nullable
@SerializedName("id")
public Integer id;
@Nullable
public Integer getId() {
return id;
}
public void setId(@Nullable Integer id) {
this.id = id;
}
@Nullable
public Integer getIdUser() {
return idUser;
}
public void setIdUser(@Nullable Integer idUser) {
this.idUser = idUser;
}
@Nullable
public Long getTime() {
return time;
}
public void setTime(@Nullable Long time) {
this.time = time;
}
@Nullable
public String getNameReader() {
return nameReader;
}
public void setNameReader(@Nullable String nameReader) {
this.nameReader = nameReader;
}
@Nullable
public String getType() {
return type;
}
public void setType(@Nullable String type) {
this.type = type;
}
public HistoryEntity(@Nullable Integer id, @Nullable Integer idUser, @Nullable Long time, @Nullable String nameReader, @Nullable String type) {
this.id = id;
this.idUser = idUser;
this.time = time;
this.nameReader = nameReader;
this.type = type;
}
@Nullable
@SerializedName("idUser")
public Integer idUser;
@Nullable
@SerializedName("time")
public Long time;
@Nullable
@SerializedName("nameReader")
public String nameReader;
@Nullable
@SerializedName("type")
public String type;
}

View File

@ -0,0 +1,39 @@
package ru.myitschool.work.domain.entities;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class RoleEntity {
@Nullable
@SerializedName("idUser")
public Integer idUser;
@Nullable
@SerializedName("status")
public int status;
public RoleEntity(@Nullable Integer idUser, int status) {
this.idUser = idUser;
this.status = status;
}
@Nullable
public Integer getIdUser() {
return idUser;
}
public void setIdUser(@Nullable Integer idUser) {
this.idUser = idUser;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}

View File

@ -0,0 +1,35 @@
package ru.myitschool.work.domain.entities;
import androidx.annotation.Nullable;
public class Status<T>{
private final int statusCode;
@Nullable
private final T value;
@Nullable
private final Throwable errors;
public Status(int statusCode, @Nullable T value, @Nullable Throwable errors) {
this.statusCode = statusCode;
this.value = value;
this.errors = errors;
}
public int getStatusCode() {
return statusCode;
}
@Nullable
public T getValue() {
return value;
}
@Nullable
public Throwable getErrors() {
return errors;
}
}

View File

@ -0,0 +1,85 @@
package ru.myitschool.work.domain.entities;
import androidx.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class UserEntity {
@Nullable
@SerializedName("username")
public String username;
@Nullable
@SerializedName("photo")
public String photo;
@Nullable
@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) {
this.username = username;
this.photo = photo;
this.lastVisit = lastVisit;
this.idUser = idUser;
this.position = position;
}
@Nullable
public String getPosition() {
return position;
}
public void setPosition(@Nullable String position) {
this.position = position;
}
@Nullable
public String getUsername() {
return username;
}
public void setUsername(@Nullable String username) {
this.username = username;
}
@Nullable
public String getPhoto() {
return photo;
}
public void setPhoto(@Nullable String photo) {
this.photo = photo;
}
@Nullable
public Long getLastVisit() {
return lastVisit;
}
public void setLastVisit(@Nullable Long lastVisit) {
this.lastVisit = lastVisit;
}
@Nullable
public Integer getIdUser() {
return idUser;
}
public void setIdUser(@Nullable Integer idUser) {
this.idUser = idUser;
}
}

View File

@ -0,0 +1,41 @@
package ru.myitschool.work.ui;
public class History {
private Long time;
private String nameReader;
private int type; //1 - qr 2 - карта
public History(Long time, String nameReader, int passageType) {
this.time = time;
this.nameReader = nameReader;
this.type = passageType;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
public String getNameReader() {
return nameReader;
}
public void setNameReader(String nameReader) {
this.nameReader = nameReader;
}
public int getPassageType() {
return type;
}
public void setPassageType(int passageType) {
this.type = passageType;
}
}

View File

@ -0,0 +1,69 @@
package ru.myitschool.work.ui;
import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import ru.myitschool.work.databinding.ItemHistoryBinding;
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {
private final List<History> data = new ArrayList<>();
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(
ItemHistoryBinding.inflate(
LayoutInflater.from(parent.getContext()),
parent,
false
).getRoot()
);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.bind(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
public void updateData(List<History> newData) {
data.clear();
data.addAll(newData);
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final ItemHistoryBinding binding;
public ViewHolder(@NonNull View itemView) {
super(itemView);
binding = ItemHistoryBinding.bind(itemView);
}
public void bind(History item) {
binding.time.setText(item.getTime().toString().substring(0, 10) + " "
+ item.getTime().toString().substring(11, 16));
binding.nameReader.setText(item.getNameReader());
if (item.getPassageType() == 1){
binding.type.setText("QR-code");
}
else{
binding.type.setText("Карта");
}
}
}
}

View File

@ -13,10 +13,11 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentResultListener;
import androidx.navigation.Navigation;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@ -30,6 +31,7 @@ import ru.myitschool.work.ui.qr.scan.QrScanFragment;
public class InformationFragment extends Fragment {
FragmentInformationBinding binding;
SharedPreferences sharedPreferences;
ArrayList<History> history = new ArrayList<>();
@Nullable
@Override
@ -47,16 +49,20 @@ public class InformationFragment extends Fragment {
getInformation();
binding.scan.setOnClickListener(view1 -> {
onClickScan(view);
onClickScan();
});
binding.logout.setOnClickListener(view2 -> {
onClickLogout(view);
onClickLogout();
});
binding.refreshLayout.setOnRefreshListener(() -> {
onClickRefresh();
});
//TODO: SERVER ZAGRUZKA LISTAAAAAAAAAAAAAAA
HistoryAdapter adapter = new HistoryAdapter();
binding.historylist.setAdapter(adapter);
adapter.updateData(history);
//getParentFragmentManager()
requireActivity().getSupportFragmentManager().setFragmentResultListener(QrScanDestination.REQUEST_KEY, getViewLifecycleOwner(), (requestKey, result) -> {

View File

@ -1,6 +1,7 @@
package ru.myitschool.work.ui;
public class User {
public class
User {
private int id;
private String login;
@ -8,14 +9,16 @@ public class User {
private String photo;
private String position;
private String lastVisit;
private int role; // 1 - сотрудник 2 - админ
public User(int id, String login, String name, String photo, String position, String lastVisit) {
public User(int id, String login, String name, String photo, String position, String lastVisit, int role) {
this.id = id;
this.login = login;
this.name = name;
this.photo = photo;
this.position = position;
this.lastVisit = lastVisit;
this.role = role;
}
public int getId() {
@ -65,4 +68,8 @@ public class User {
public void setLastVisit(String lastVisit) {
this.lastVisit = lastVisit;
}
public int getRole(int role){return role;}
public void setRole(int role){this.role = role;}
}

View File

@ -1,10 +1,11 @@
package ru.myitschool.work.ui.login;
package ru.myitschool.work.ui;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -66,13 +67,6 @@ public class LoginFragment extends Fragment {
}
}
});
binding.password.addTextChangedListener(new OnChangeText() {
@Override
public void afterTextChanged(Editable s) {
super.afterTextChanged(s);
viewModel.changePassword(s.toString());
}
});
binding.username.addTextChangedListener(new TextWatcher() {
@Override
@ -100,6 +94,30 @@ public class LoginFragment extends Fragment {
}
});
binding.password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
viewModel.changePassword(s.toString());
if (binding.password.getText().length() >= 5) {
binding.login.setEnabled(true);
} else {
binding.login.setEnabled(false);
}
if (binding.error.getVisibility() == View.VISIBLE) {
binding.error.setVisibility(View.GONE);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
binding.login.setOnClickListener(view1 -> {
viewModel.confirm();

View File

@ -79,6 +79,13 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/historylist"
android:layout_gravity="center"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

View File

@ -0,0 +1,39 @@
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:tag="iuroi"/>
<TextView
android:id="@+id/nameReader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:text="kijt"
app:layout_constraintBottom_toBottomOf="@+id/time"
app:layout_constraintStart_toEndOf="@id/time"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:text="hgfdfbjdhf"
app:layout_constraintBottom_toBottomOf="@+id/time"
app:layout_constraintStart_toEndOf="@id/nameReader"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>