From f52b88e8b65e78439d5393b6915ba45567013c81 Mon Sep 17 00:00:00 2001 From: Danya_Mochalov Date: Tue, 19 Dec 2023 22:58:46 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=20RestProd?= =?UTF-8?q?uctRepository=20=D0=B8=20=D1=80=D0=B5=D0=BC=D0=BE=D1=83=D1=82?= =?UTF-8?q?=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D1=8C=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/shawarma/AppModule.kt | 7 +++++++ .../data/api/models/ProductModelRemote.kt | 20 +++++++++++++++++++ .../data/api/repos/RestProductRepository.kt | 10 ++++++++++ .../shawarma/data/repos/ProductRepository.kt | 9 --------- .../authorization/AuthorizationScreen.kt | 7 +++++++ .../shawarma/viewmodels/UserViewModel.kt | 10 +--------- 6 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt create mode 100644 app/src/main/java/com/example/shawarma/data/api/repos/RestProductRepository.kt diff --git a/app/src/main/java/com/example/shawarma/AppModule.kt b/app/src/main/java/com/example/shawarma/AppModule.kt index 8d847fc..13fd664 100644 --- a/app/src/main/java/com/example/shawarma/AppModule.kt +++ b/app/src/main/java/com/example/shawarma/AppModule.kt @@ -3,6 +3,7 @@ package com.example.shawarma import android.app.Application import androidx.room.Room import com.example.shawarma.data.api.MyServerService +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 @@ -52,6 +53,12 @@ object AppModule { return ProductRepository(db.productDao(), db.orderProductDao()) } + @Provides + @Singleton + fun provideRestProductRepository(service: MyServerService) : RestProductRepository { + return RestProductRepository(service) + } + @Provides @Singleton fun provideOrderRepository(db: AppDatabase) : OrderRepository { diff --git a/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt b/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt new file mode 100644 index 0000000..602f866 --- /dev/null +++ b/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt @@ -0,0 +1,20 @@ +package com.example.shawarma.data.api.models + +import com.example.shawarma.data.models.ProductModel +import kotlinx.serialization.Serializable + +@Serializable +data class ProductModelRemote( + val id: Int = 0, + val title: String = "", + val price: Int = 0, + val oldPrice: Int? = null +) + +fun ProductModelRemote.toProductModel(): ProductModel = ProductModel( + id, title, price, oldPrice +) + +fun ProductModel.toProductModelRemote(): ProductModelRemote = ProductModelRemote( + title = title, price = price, oldPrice = oldPrice +) diff --git a/app/src/main/java/com/example/shawarma/data/api/repos/RestProductRepository.kt b/app/src/main/java/com/example/shawarma/data/api/repos/RestProductRepository.kt new file mode 100644 index 0000000..2221813 --- /dev/null +++ b/app/src/main/java/com/example/shawarma/data/api/repos/RestProductRepository.kt @@ -0,0 +1,10 @@ +package com.example.shawarma.data.api.repos + +import com.example.shawarma.data.api.MyServerService +import javax.inject.Inject + +class RestProductRepository @Inject constructor( + private val service: MyServerService +) { + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt index 2db5efe..7a13b64 100644 --- a/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/repos/ProductRepository.kt @@ -23,15 +23,6 @@ class ProductRepository @Inject constructor( orderProductDao.deleteByProductId(product.id!!) return productDao.delete(product) } - /*fun getAll(): Flow> { - return productDao.getAll() - } - fun getDiscounts(): Flow> { - return productDao.getDiscounts() - } - fun getItems(): Flow> { - return productDao.getItems() - }*/ fun getById(id: Int): Flow { return productDao.getById(id) } diff --git a/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt b/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt index 9717520..b2f344a 100644 --- a/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/authorization/AuthorizationScreen.kt @@ -59,6 +59,13 @@ fun AuthorizationCard(navHostController: NavHostController) { if (userViewModel.token.observeAsState().value != null) { preferencesManager.saveData("token", userViewModel.token.value.toString()) + if (login.value.text == "admin") { + preferencesManager.saveData("user_role", "ADMIN") + } + else { + preferencesManager.saveData("user_role", "USER") + } + navHostController.navigate(ScreenPaths.home.name) { popUpTo(ScreenPaths.authorization.name) { inclusive = true 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 be0a215..9ac6d82 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt @@ -16,10 +16,6 @@ import javax.inject.Inject class UserViewModel @Inject constructor( private val restUserRepository: RestUserRepository ) : ViewModel() { - private val _userModel = MutableLiveData() - val userModel: LiveData - get() = _userModel - private val _token = MutableLiveData() val token: LiveData get() = _token @@ -31,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 = "USER")) + val token_response = restUserRepository.getToken(UserModel(id = null, login = login, password = password, role = "")) if (token_response.access_token.isNotEmpty()) { _token.postValue(token_response.access_token) _authorizationState.postValue(true) @@ -44,8 +40,6 @@ class UserViewModel @Inject constructor( } } } - - fun calmAuthorizationState() { _authorizationState.postValue(null) } @@ -89,8 +83,6 @@ class UserViewModel @Inject constructor( } } } - - fun calmRegistrationState() { _registrationState.postValue(null) }