From 32991b2e1409aaa282b8366c9f2b39ac08901c2b Mon Sep 17 00:00:00 2001 From: Danya_Mochalov Date: Thu, 28 Dec 2023 00:58:34 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BC=20?= =?UTF-8?q?user=20repository?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/shawarma/AppModule.kt | 14 ++-------- .../data/api/repos/RestUserRepository.kt | 24 ---------------- .../shawarma/data/repos/OrderRepository.kt | 2 -- .../shawarma/data/repos/UserRepository.kt | 28 +++++++++---------- .../shawarma/viewmodels/UserViewModel.kt | 12 ++++---- 5 files changed, 22 insertions(+), 58 deletions(-) delete mode 100644 app/src/main/java/com/example/shawarma/data/api/repos/RestUserRepository.kt diff --git a/app/src/main/java/com/example/shawarma/AppModule.kt b/app/src/main/java/com/example/shawarma/AppModule.kt index 6170be3..8f7715f 100644 --- a/app/src/main/java/com/example/shawarma/AppModule.kt +++ b/app/src/main/java/com/example/shawarma/AppModule.kt @@ -6,7 +6,6 @@ import com.example.shawarma.data.api.MyServerService import com.example.shawarma.data.api.repos.RestOrderProductRepository import com.example.shawarma.data.api.repos.RestOrderRepository import com.example.shawarma.data.api.repos.RestProductRepository -import com.example.shawarma.data.api.repos.RestUserRepository import com.example.shawarma.data.db.AppDatabase import com.example.shawarma.data.repos.OrderProductRepository import com.example.shawarma.data.repos.OrderRepository @@ -16,7 +15,6 @@ import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent - import javax.inject.Singleton @Module @InstallIn(SingletonComponent::class) @@ -39,14 +37,8 @@ object AppModule { @Provides @Singleton - fun provideUserRepository(db: AppDatabase) : UserRepository { - return UserRepository(db.userDao()) - } - - @Provides - @Singleton - fun provideRestUserRepository(service: MyServerService) : RestUserRepository { - return RestUserRepository(service) + fun provideUserRepository(service: MyServerService) : UserRepository { + return UserRepository(service) } @Provides @@ -64,7 +56,7 @@ object AppModule { @Provides @Singleton fun provideOrderRepository(db: AppDatabase, restOrderRepository: RestOrderRepository) : OrderRepository { - return OrderRepository(db, db.orderDao(), db.productDao(), db.orderProductDao(), restOrderRepository) + return OrderRepository(db, db.orderDao(), db.orderProductDao(), restOrderRepository) } @Provides diff --git a/app/src/main/java/com/example/shawarma/data/api/repos/RestUserRepository.kt b/app/src/main/java/com/example/shawarma/data/api/repos/RestUserRepository.kt deleted file mode 100644 index 391f4f8..0000000 --- a/app/src/main/java/com/example/shawarma/data/api/repos/RestUserRepository.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.example.shawarma.data.api.repos - -import com.example.shawarma.data.api.MyServerService -import com.example.shawarma.data.api.models.TokenModelRemote -import com.example.shawarma.data.api.models.UserModelRemote -import com.example.shawarma.data.api.models.toUserModelRemote -import com.example.shawarma.data.db.models.UserModel -import javax.inject.Inject - -class RestUserRepository @Inject constructor( - private val service: MyServerService -) { - suspend fun insert(user: UserModel) { - service.createUser(user.toUserModelRemote()) - } - - suspend fun getToken(user: UserModel): TokenModelRemote { - return service.getToken(user.toUserModelRemote()) - } - - suspend fun checkLogin(user: UserModel): UserModelRemote? { - return service.checkLogin(user.toUserModelRemote()) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt index 4ca2f21..2c9df35 100644 --- a/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt @@ -13,7 +13,6 @@ import com.example.shawarma.data.api.repos.RestOrderRepository import com.example.shawarma.data.db.AppDatabase import com.example.shawarma.data.db.dao.OrderDao import com.example.shawarma.data.db.dao.OrderProductDao -import com.example.shawarma.data.db.dao.ProductDao import com.example.shawarma.data.db.models.OrderModel import com.example.shawarma.data.db.models.OrderWithProducts import kotlinx.coroutines.flow.Flow @@ -23,7 +22,6 @@ import javax.inject.Inject class OrderRepository @Inject constructor( private val database: AppDatabase, private val orderDao: OrderDao, - private val productDao: ProductDao, private val orderProductDao: OrderProductDao, private val restRepository: RestOrderRepository ){ diff --git a/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt index 00a9b37..8c57199 100644 --- a/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/repos/UserRepository.kt @@ -1,26 +1,24 @@ package com.example.shawarma.data.repos -import com.example.shawarma.data.db.dao.UserDao +import com.example.shawarma.data.api.MyServerService +import com.example.shawarma.data.api.models.TokenModelRemote +import com.example.shawarma.data.api.models.UserModelRemote +import com.example.shawarma.data.api.models.toUserModelRemote import com.example.shawarma.data.db.models.UserModel -import kotlinx.coroutines.flow.Flow import javax.inject.Inject class UserRepository @Inject constructor( - private val userDao: UserDao -) { + private val service: MyServerService +) { suspend fun insert(user: UserModel) { - return userDao.insert(user) + service.createUser(user.toUserModelRemote()) } - suspend fun update (user: UserModel) { - return userDao.update(user) + + suspend fun getToken(user: UserModel): TokenModelRemote { + return service.getToken(user.toUserModelRemote()) } - suspend fun delete (user: UserModel) { - return userDao.delete(user) - } - fun getById(id: Int): Flow { - return userDao.getById(id) - } - fun login(login: String, password: String): Flow { - return userDao.login(login, password) + + suspend fun checkLogin(user: UserModel): UserModelRemote? { + return service.checkLogin(user.toUserModelRemote()) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt index 402f46f..5623179 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt @@ -4,7 +4,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.example.shawarma.data.api.repos.RestUserRepository +import com.example.shawarma.data.repos.UserRepository import com.example.shawarma.data.db.models.UserModel import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers @@ -14,7 +14,7 @@ import javax.inject.Inject @HiltViewModel class UserViewModel @Inject constructor( - private val restUserRepository: RestUserRepository + private val userRepository: UserRepository ) : ViewModel() { private val _token = MutableLiveData() val token: LiveData @@ -27,7 +27,7 @@ class UserViewModel @Inject constructor( fun login(login: String, password: String){ viewModelScope.launch { withContext(Dispatchers.Main) { - val token_response = restUserRepository.getToken(UserModel(id = null, login = login, password = password, role = "")) + val token_response = userRepository.getToken(UserModel(id = null, login = login, password = password, role = "")) if (token_response.access_token.isNotEmpty()) { _token.postValue(token_response.access_token) _authorizationState.postValue(true) @@ -64,14 +64,14 @@ class UserViewModel @Inject constructor( } viewModelScope.launch { withContext(Dispatchers.Main) { - val user_response = restUserRepository.checkLogin(UserModel(null, login, password, "")) + val user_response = userRepository.checkLogin(UserModel(null, login, password, "")) if (user_response != null) { if (user_response.login.isNullOrEmpty()) { if (login == "admin" && password == "admin") { - restUserRepository.insert(UserModel(null, login, password, "ADMIN")) + userRepository.insert(UserModel(null, login, password, "ADMIN")) _registrationState.postValue(true) } else { - restUserRepository.insert(UserModel(null, login, password, "USER")) + userRepository.insert(UserModel(null, login, password, "USER")) _registrationState.postValue(true) } } else {