Создал 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 android.app.Application
import androidx.room.Room import androidx.room.Room
import com.example.shawarma.data.api.MyServerService 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.api.repos.RestUserRepository
import com.example.shawarma.data.db.AppDatabase import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.repos.OrderProductRepository import com.example.shawarma.data.repos.OrderProductRepository
@ -52,6 +53,12 @@ object AppModule {
return ProductRepository(db.productDao(), db.orderProductDao()) return ProductRepository(db.productDao(), db.orderProductDao())
} }
@Provides
@Singleton
fun provideRestProductRepository(service: MyServerService) : RestProductRepository {
return RestProductRepository(service)
}
@Provides @Provides
@Singleton @Singleton
fun provideOrderRepository(db: AppDatabase) : OrderRepository { 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!!) orderProductDao.deleteByProductId(product.id!!)
return productDao.delete(product) 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> { fun getById(id: Int): Flow<ProductModel> {
return productDao.getById(id) return productDao.getById(id)
} }

View File

@ -59,6 +59,13 @@ fun AuthorizationCard(navHostController: NavHostController) {
if (userViewModel.token.observeAsState().value != null) { if (userViewModel.token.observeAsState().value != null) {
preferencesManager.saveData("token", userViewModel.token.value.toString()) 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) { navHostController.navigate(ScreenPaths.home.name) {
popUpTo(ScreenPaths.authorization.name) { popUpTo(ScreenPaths.authorization.name) {
inclusive = true inclusive = true

View File

@ -16,10 +16,6 @@ import javax.inject.Inject
class UserViewModel @Inject constructor( class UserViewModel @Inject constructor(
private val restUserRepository: RestUserRepository private val restUserRepository: RestUserRepository
) : ViewModel() { ) : ViewModel() {
private val _userModel = MutableLiveData<UserModel?>()
val userModel: LiveData<UserModel?>
get() = _userModel
private val _token = MutableLiveData<String?>() private val _token = MutableLiveData<String?>()
val token: LiveData<String?> val token: LiveData<String?>
get() = _token get() = _token
@ -31,7 +27,7 @@ class UserViewModel @Inject constructor(
fun login(login: String, password: String){ fun login(login: String, password: String){
viewModelScope.launch { viewModelScope.launch {
withContext(Dispatchers.Main) { 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()) { if (token_response.access_token.isNotEmpty()) {
_token.postValue(token_response.access_token) _token.postValue(token_response.access_token)
_authorizationState.postValue(true) _authorizationState.postValue(true)
@ -44,8 +40,6 @@ class UserViewModel @Inject constructor(
} }
} }
} }
fun calmAuthorizationState() { fun calmAuthorizationState() {
_authorizationState.postValue(null) _authorizationState.postValue(null)
} }
@ -89,8 +83,6 @@ class UserViewModel @Inject constructor(
} }
} }
} }
fun calmRegistrationState() { fun calmRegistrationState() {
_registrationState.postValue(null) _registrationState.postValue(null)
} }