Создание продукта + фикс экранов
This commit is contained in:
parent
7e60a88069
commit
73f4b57547
@ -3,6 +3,7 @@ package com.example.shawarma
|
||||
import android.app.Application
|
||||
import androidx.room.Room
|
||||
import com.example.shawarma.data.db.AppDatabase
|
||||
import com.example.shawarma.data.repos.ProductRepository
|
||||
import com.example.shawarma.data.repos.UserRepository
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
@ -28,4 +29,10 @@ object AppModule {
|
||||
fun provideUserRepository(db: AppDatabase) : UserRepository {
|
||||
return UserRepository(db.userDao())
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideProductRepository(db: AppDatabase) : ProductRepository {
|
||||
return ProductRepository(db.productDao())
|
||||
}
|
||||
}
|
@ -37,6 +37,7 @@ import androidx.compose.ui.zIndex
|
||||
import com.example.shawarma.R
|
||||
import com.example.shawarma.data.db.AppDatabase
|
||||
import com.example.shawarma.data.models.ProductModel
|
||||
import com.example.shawarma.screens.home.ProductCard
|
||||
import com.example.shawarma.ui.theme.MarckFamily
|
||||
import com.example.shawarma.ui.theme.MyLightYellow
|
||||
import com.example.shawarma.ui.theme.MyMainBackground
|
||||
@ -94,15 +95,27 @@ fun DiscountList(){
|
||||
)
|
||||
{
|
||||
items(products.size) { index ->
|
||||
if (index % 2 != 1) {
|
||||
if (index % 2 == 0 && index == products.size - 1) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.SpaceAround,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(top = 10.dp)
|
||||
) {
|
||||
DiscountCard(products[index])
|
||||
DiscountCard(products[index+1])
|
||||
ProductCard(products[index])
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (index % 2 != 1) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.SpaceAround,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(top = 10.dp)
|
||||
) {
|
||||
DiscountCard(products[index])
|
||||
DiscountCard(products[index+1])
|
||||
}
|
||||
}
|
||||
}
|
||||
if (index == products.size - 1) {
|
||||
|
@ -16,7 +16,6 @@ import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.ButtonColors
|
||||
import androidx.compose.material.ButtonDefaults
|
||||
import androidx.compose.material.Card
|
||||
import androidx.compose.material.Text
|
||||
@ -37,7 +36,6 @@ import androidx.compose.ui.zIndex
|
||||
import com.example.shawarma.R
|
||||
import com.example.shawarma.data.db.AppDatabase
|
||||
import com.example.shawarma.data.models.ProductModel
|
||||
import com.example.shawarma.data.models.getProducts
|
||||
import com.example.shawarma.ui.theme.MarckFamily
|
||||
import com.example.shawarma.ui.theme.MyLightYellow
|
||||
import com.example.shawarma.ui.theme.MyMainBackground
|
||||
@ -93,7 +91,7 @@ fun HomeList(){
|
||||
)
|
||||
{
|
||||
items(products.size) { index ->
|
||||
if (index % 2 != 1) {
|
||||
if (index % 2 == 0 && index == products.size - 1) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.SpaceAround,
|
||||
modifier = Modifier
|
||||
@ -101,7 +99,19 @@ fun HomeList(){
|
||||
.padding(top = 10.dp)
|
||||
) {
|
||||
ProductCard(products[index])
|
||||
ProductCard(products[index + 1])
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (index % 2 != 1) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.SpaceAround,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(top = 10.dp)
|
||||
) {
|
||||
ProductCard(products[index])
|
||||
ProductCard(products[index + 1])
|
||||
}
|
||||
}
|
||||
}
|
||||
if (index == products.size - 1) {
|
||||
|
@ -4,19 +4,18 @@ import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.ButtonDefaults
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
@ -28,6 +27,7 @@ import androidx.compose.ui.text.input.TextFieldValue
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.ui.zIndex
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.shawarma.data.db.AppDatabase
|
||||
import com.example.shawarma.data.models.ProductModel
|
||||
@ -36,6 +36,8 @@ import com.example.shawarma.ui.theme.MyLightYellow
|
||||
import com.example.shawarma.ui.theme.MyMainBackground
|
||||
import com.example.shawarma.ui.theme.MyOrange
|
||||
import com.example.shawarma.ui.theme.NunitoFamily
|
||||
import com.example.shawarma.utils.ScreenPaths
|
||||
import com.example.shawarma.viewmodels.ProductViewModel
|
||||
import com.example.shawarma.widgets.MyTextField
|
||||
import com.example.shawarma.widgets.ShawarmaLogo2
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@ -57,6 +59,16 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
|
||||
val price = remember { mutableStateOf(TextFieldValue(""))}
|
||||
val oldPrice = remember { mutableStateOf(TextFieldValue(""))}
|
||||
|
||||
val productViewModel: ProductViewModel = hiltViewModel<ProductViewModel>()
|
||||
|
||||
if (productViewModel.addingProductState.observeAsState().value == true) {
|
||||
navHostController.navigate(ScreenPaths.products.name) {
|
||||
popUpTo(ScreenPaths.product.name) {
|
||||
inclusive = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (productId != 0 && productId != null) {
|
||||
val context = LocalContext.current
|
||||
var product = remember {
|
||||
@ -92,7 +104,8 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
|
||||
)
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.padding(top = 60.dp),
|
||||
.padding(top = 60.dp)
|
||||
.verticalScroll(rememberScrollState()),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
MyTextField(
|
||||
@ -138,8 +151,7 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
|
||||
border = BorderStroke(2.dp, color = MyOrange),
|
||||
shape = RoundedCornerShape(20.dp),
|
||||
onClick = {
|
||||
// TODO
|
||||
navHostController.popBackStack()
|
||||
productViewModel.addProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null)
|
||||
}
|
||||
) {
|
||||
Text(
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.example.shawarma.viewmodels
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.shawarma.data.models.ProductModel
|
||||
import com.example.shawarma.data.repos.ProductRepository
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class ProductViewModel @Inject constructor(
|
||||
private val productRepository: ProductRepository
|
||||
) : ViewModel() {
|
||||
private val _addingProductState = MutableLiveData<Boolean>(false)
|
||||
val addingProductState: LiveData<Boolean?>
|
||||
get() = _addingProductState
|
||||
|
||||
fun addProduct(name: String, price: Int, oldPrice: Int? = null) {
|
||||
viewModelScope.launch {
|
||||
productRepository.insert(ProductModel(null, name, price, oldPrice))
|
||||
_addingProductState.postValue(true)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user