Готово

This commit is contained in:
Ismailov_Rovshan 2023-12-20 00:54:49 +04:00
parent 1095dbaa47
commit 0d8d318a0c
8 changed files with 282 additions and 112 deletions

View File

@ -0,0 +1,139 @@
package com.example.myapplication.components
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ImageDecoder
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.example.myapplication.R
import com.example.myapplication.components.common.myInput
import com.example.myapplication.components.templates.DropDown
import com.example.myapplication.database.entities.Category
import com.example.myapplication.database.entities.Product
import com.example.myapplication.viewModels.CategoryViewModel
import com.example.myapplication.viewModels.ProductViewModel
import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddProduct(
navController: NavController,
id: Int = 0,
itemViewModel: ProductViewModel = viewModel(factory = ProductViewModel.factory),
categoryViewModel: CategoryViewModel = viewModel(factory = CategoryViewModel.factory)
)
{
val coroutineScope = rememberCoroutineScope()
val context = LocalContext.current
val img = remember { mutableStateOf<Bitmap>(
BitmapFactory.decodeResource(context.resources,
R.drawable.product5
)) }
val imageData = remember { mutableStateOf<Uri?>(null) }
val launcher =
rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
imageData.value = uri
}
imageData.value?.let {
if (Build.VERSION.SDK_INT < 28) {
img.value = MediaStore.Images
.Media.getBitmap(context.contentResolver, imageData.value)
} else {
val source = ImageDecoder
.createSource(context.contentResolver, imageData.value!!)
img.value = ImageDecoder.decodeBitmap(source)
}
}
val categories = remember{ mutableListOf<Category>() }
val nameState = remember { mutableStateOf("") }
val infoState = remember { mutableStateOf("") }
val priceState = remember { mutableStateOf("") }
val categoryState = remember { mutableStateOf<Category?>(null) }
LaunchedEffect(Unit) {
categoryViewModel.getAll().collect{categs ->
categories.clear()
categs.forEach{
categories.add(it)
}
}
if(id != 0) {
itemViewModel.getById(id).collect{
nameState.value = it.name
}
}
}
Column(modifier = Modifier
.padding(start = 30.dp, end = 30.dp, top = 100.dp)
.verticalScroll(rememberScrollState())) {
myInput("Название", nameState)
myInput("Описание", infoState)
myInput("Цена", priceState)
DropDown(categories, categoryState)
Image(
bitmap = img.value.asImageBitmap(),
contentDescription = "editplaceholder",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(384.dp)
.padding(8.dp)
.align(Alignment.CenterHorizontally))
Button(
onClick = {
launcher.launch("image/*")
},
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)) {
Text("Выбрать картинку", fontSize = 20.sp)
}
Button(
onClick = {
coroutineScope.launch {
itemViewModel.insert(Product(null, nameState.value,infoState.value, priceState.value.toDouble(), img.value, categoryState.value!!.id!!))
navController.navigate("main/0")
}
},
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)) {
Text("Сохранить", fontSize = 20.sp)
}
}
}

View File

@ -3,6 +3,7 @@ package com.example.myapplication.components
import ButtonNice
import TextNice
import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
@ -25,6 +26,7 @@ import com.example.myapplication.database.AppDb
import com.example.myapplication.database.entities.Product
import com.example.myapplication.viewModels.UserViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import myColor4
@ -36,9 +38,7 @@ fun Cart(navController: NavController, userViewModel: UserViewModel = viewModel(
val sumPrice = remember { mutableStateOf(0.0) }
LaunchedEffect(Unit) {
if (userViewModel.getUserId() == 0) {
navController.navigate("authorization")
} else {
userViewModel.getUserProductCartById(userViewModel.getUserId()).collect {data ->
products.clear()
sumPrice.value = 0.0;
@ -48,18 +48,32 @@ fun Cart(navController: NavController, userViewModel: UserViewModel = viewModel(
}
}
}
}
LazyColumn (contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp)){
item {
TextNice("Корзина")
}
item {
for (product in products){
ProductCardInCart(product.name, product.price, product.img, { navController.navigate("product/" + product.productId)})
// item {
// for (product in products){
// ProductCardInCart(product.name, product.price, product.img){
// coroutineScope.launch {
// userViewModel.deleteCartProduct(userViewModel.getUserId(), product.productId!!)
// }
// }
// }
// }
item{
products.forEach{
ProductCardInCart(it.name, it.price, it.img){
coroutineScope.launch {
Log.d("delete", "мама макса б")
userViewModel.deleteCartProduct(userViewModel.getUserId(), it.productId!!)
}
}
}
}
item {
ButtonNice(text = "Оплатить: " + sumPrice.value.toString() + "Р", color = myColor4)

View File

@ -2,20 +2,24 @@ package com.example.myapplication.components
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
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.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
@ -30,11 +34,13 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.paging.compose.itemKey
import com.example.myapplication.R
import com.example.myapplication.components.funs.createProductCard
import com.example.myapplication.database.entities.UserProductCart
import com.example.myapplication.viewModels.ProductViewModel
@ -45,6 +51,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import myColor3
@OptIn(ExperimentalMaterial3Api::class)
@Composable
@ -68,13 +75,29 @@ fun Main(
navController.navigate("authorization")
}
}
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(8.dp)
) {
item{
Button(
onClick = {navController.navigate("addProduct")},
colors = ButtonDefaults.buttonColors(
containerColor= Color.Transparent,
contentColor = myColor3
),
contentPadding = PaddingValues(0.dp),
modifier = Modifier.size(50.dp).padding(top = 10.dp)
) {
Image(
painter = painterResource(id = R.drawable.addphoto),
contentDescription = null,
modifier = Modifier.size(50.dp)
)
}
}
item {
Text(
text = "Товары:",
@ -83,7 +106,6 @@ fun Main(
modifier = Modifier.padding(8.dp)
)
}
// item {
// products.forEach {
// createProductCard(

View File

@ -1,100 +0,0 @@
package com.example.myapplication.components
import ButtonNice
import TextNice
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.example.myapplication.R
import myColor2
import myColor3
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun addProduct(navController: NavController) {
Column(
modifier = Modifier
.fillMaxSize().padding(15.dp)
)
{
Box(
modifier = Modifier.padding(bottom = 10.dp).fillMaxWidth(),
contentAlignment = Alignment.Center
) {
TextNice("Добавление товара")
}
OutlinedTextField(
value = "",
onValueChange = { },
placeholder = { Text("Название товара") },
modifier = Modifier
.fillMaxWidth()
.padding(top = 5.dp, bottom = 5.dp)
)
OutlinedTextField(
value = "",
onValueChange = { },
placeholder = { Text("Описание товара") },
modifier = Modifier
.fillMaxWidth()
.padding(top = 5.dp, bottom = 5.dp)
.height(200.dp)
)
OutlinedTextField(
value = "",
onValueChange = { },
placeholder = { Text("Цена") },
modifier = Modifier
.fillMaxWidth()
.padding(top = 5.dp, bottom = 20.dp)
)
Button(
onClick = {navController.navigate("addProduct")},
colors = ButtonDefaults.buttonColors(
containerColor= Color.Transparent,
contentColor = myColor3
),
contentPadding = PaddingValues(0.dp),
modifier = Modifier.size(50.dp).padding(top = 10.dp)
) {
Image(
painter = painterResource(id = R.drawable.addphoto),
contentDescription = null,
modifier = Modifier.size(50.dp)
)
}
ButtonNice(
text = "Добавить",
color = myColor2,
onClickAction = { navController.navigate("category") })
ButtonNice(
text = "Отмена",
color = Color.White,
onClickAction = { navController.navigate("category") })
}
}

View File

@ -13,6 +13,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
@ -38,3 +39,26 @@ fun myInput(label: String, text: MutableState<String>, height: Dp = 50.dp, modif
)
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun passwordInput(label: String, text: MutableState<String>, height: Dp = 50.dp, modifier: Modifier = Modifier) {
Column(modifier=modifier) {
Text(label)
TextField(
value = text.value,
onValueChange = {newText -> text.value = newText},
colors= TextFieldDefaults.outlinedTextFieldColors(
focusedBorderColor = Color.Transparent,
disabledBorderColor = Color.Transparent,
unfocusedBorderColor = Color.Transparent,
errorBorderColor = Color.Transparent
),
visualTransformation = PasswordVisualTransformation(),
modifier = Modifier
.border(1.dp, Color.Black, RoundedCornerShape(10.dp))
.fillMaxWidth()
.height(height),
)
}
}

View File

@ -64,7 +64,7 @@ fun ProductCardInCart(name: String, price: Double, img: Bitmap, onClickAction: (
Text(text = price.toString() + "", fontSize = 16.sp, color = Color.Black)
Button(
onClick = { onClickAction },
onClick = onClickAction,
colors = ButtonDefaults.buttonColors(
containerColor = Color.Transparent,
contentColor = myColor3

View File

@ -0,0 +1,69 @@
package com.example.myapplication.components.templates
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.myapplication.database.entities.Category
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DropDown(items: List<Category>, selected: MutableState<Category?>) {
val expanded = remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
modifier = Modifier
.padding(top = 7.dp),
expanded = expanded.value,
onExpandedChange = {
expanded.value = !expanded.value
}
) {
TextField(
value = when(selected.value) {
null -> "Значение не выбрано"
else -> selected.value!!.name
},
onValueChange = {},
readOnly = true,
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded.value)
},
modifier = Modifier
.fillMaxWidth()
.menuAnchor()
)
DropdownMenu(
expanded = expanded.value,
onDismissRequest = { expanded.value = false },
modifier = Modifier
.background(Color.White)
.exposedDropdownSize()
) {
items.forEach {
DropdownMenuItem(
text = {
Text(text = it.name)
},
onClick = {
selected.value = it
expanded.value = false
}
)
}
}
}
}

View File

@ -26,6 +26,7 @@ import com.example.myapplication.components.Authorization
import com.example.myapplication.components.Cart
import com.example.myapplication.components.Main
import com.example.myapplication.components.Registration
import com.example.myapplication.components.AddProduct
import com.example.myapplication.components.templates.ProductForPage
import com.example.myapplication.components.Сategory
import com.example.myapplication.ui.theme.MyApplicationTheme
@ -95,8 +96,8 @@ fun Navbar() {
Main(navController, it.getInt("id"))
}
}
composable("category") { Сategory(navController) }
composable("addProduct") { AddProduct(navController) }
composable("cart") { Cart(navController) }
composable(
@ -105,6 +106,7 @@ fun Navbar() {
) { backStackEntry ->
backStackEntry.arguments?.let { ProductForPage(it.getInt("id")) }
}
}
}
}