Создал RestProductRepository и ремоут модельки

This commit is contained in:
Данила Мочалов 2023-12-19 22:58:46 +04:00
parent 423a7c4c7e
commit f52b88e8b6
6 changed files with 45 additions and 18 deletions

View File

@ -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 {

View File

@ -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
)

View File

@ -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
) {
}

View File

@ -23,15 +23,6 @@ class ProductRepository @Inject constructor(
orderProductDao.deleteByProductId(product.id!!)
return productDao.delete(product)
}
/*fun getAll(): Flow<List<ProductModel>> {
return productDao.getAll()
}
fun getDiscounts(): Flow<List<ProductModel>> {
return productDao.getDiscounts()
}
fun getItems(): Flow<List<ProductModel>> {
return productDao.getItems()
}*/
fun getById(id: Int): Flow<ProductModel> {
return productDao.getById(id)
}

View File

@ -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

View File

@ -16,10 +16,6 @@ import javax.inject.Inject
class UserViewModel @Inject constructor(
private val restUserRepository: RestUserRepository
) : ViewModel() {
private val _userModel = MutableLiveData<UserModel?>()
val userModel: LiveData<UserModel?>
get() = _userModel
private val _token = MutableLiveData<String?>()
val token: LiveData<String?>
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)
}