From 0d8d318a0ce5bf69546c45996b6ca72987d300ac Mon Sep 17 00:00:00 2001 From: Ismailov_Rovshan Date: Wed, 20 Dec 2023 00:54:49 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../myapplication/components/AddProduct.kt | 139 ++++++++++++++++++ .../example/myapplication/components/Cart.kt | 28 +++- .../example/myapplication/components/Main.kt | 26 +++- .../myapplication/components/addProduct.kt | 100 ------------- .../components/common/myInput.kt | 26 +++- .../myapplication/components/fun/funs.kt | 2 +- .../components/templates/DropDown.kt | 69 +++++++++ .../myapplication/navigation/Navbar.kt | 4 +- 8 files changed, 282 insertions(+), 112 deletions(-) create mode 100644 app/src/main/java/com/example/myapplication/components/AddProduct.kt delete mode 100644 app/src/main/java/com/example/myapplication/components/addProduct.kt create mode 100644 app/src/main/java/com/example/myapplication/components/templates/DropDown.kt diff --git a/app/src/main/java/com/example/myapplication/components/AddProduct.kt b/app/src/main/java/com/example/myapplication/components/AddProduct.kt new file mode 100644 index 0000000..d70aca9 --- /dev/null +++ b/app/src/main/java/com/example/myapplication/components/AddProduct.kt @@ -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( + BitmapFactory.decodeResource(context.resources, + R.drawable.product5 + )) } + + val imageData = remember { mutableStateOf(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() } + + val nameState = remember { mutableStateOf("") } + val infoState = remember { mutableStateOf("") } + val priceState = remember { mutableStateOf("") } + val categoryState = remember { mutableStateOf(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) + } + } +} + + diff --git a/app/src/main/java/com/example/myapplication/components/Cart.kt b/app/src/main/java/com/example/myapplication/components/Cart.kt index 468b4f5..bd50393 100644 --- a/app/src/main/java/com/example/myapplication/components/Cart.kt +++ b/app/src/main/java/com/example/myapplication/components/Cart.kt @@ -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; @@ -46,7 +46,6 @@ fun Cart(navController: NavController, userViewModel: UserViewModel = viewModel( sumPrice.value += it.price products.add(it) } - } } } @@ -55,12 +54,27 @@ fun Cart(navController: NavController, userViewModel: UserViewModel = viewModel( 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) } diff --git a/app/src/main/java/com/example/myapplication/components/Main.kt b/app/src/main/java/com/example/myapplication/components/Main.kt index 4680ec6..d13ead9 100644 --- a/app/src/main/java/com/example/myapplication/components/Main.kt +++ b/app/src/main/java/com/example/myapplication/components/Main.kt @@ -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( diff --git a/app/src/main/java/com/example/myapplication/components/addProduct.kt b/app/src/main/java/com/example/myapplication/components/addProduct.kt deleted file mode 100644 index f821cbd..0000000 --- a/app/src/main/java/com/example/myapplication/components/addProduct.kt +++ /dev/null @@ -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") }) - } -} - - diff --git a/app/src/main/java/com/example/myapplication/components/common/myInput.kt b/app/src/main/java/com/example/myapplication/components/common/myInput.kt index 5cadd62..9c7739f 100644 --- a/app/src/main/java/com/example/myapplication/components/common/myInput.kt +++ b/app/src/main/java/com/example/myapplication/components/common/myInput.kt @@ -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 @@ -37,4 +38,27 @@ fun myInput(label: String, text: MutableState, height: Dp = 50.dp, modif .height(height), ) } -} \ No newline at end of file +} +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun passwordInput(label: String, text: MutableState, 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), + ) + } +} + diff --git a/app/src/main/java/com/example/myapplication/components/fun/funs.kt b/app/src/main/java/com/example/myapplication/components/fun/funs.kt index 5fd9478..e3f5edb 100644 --- a/app/src/main/java/com/example/myapplication/components/fun/funs.kt +++ b/app/src/main/java/com/example/myapplication/components/fun/funs.kt @@ -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 diff --git a/app/src/main/java/com/example/myapplication/components/templates/DropDown.kt b/app/src/main/java/com/example/myapplication/components/templates/DropDown.kt new file mode 100644 index 0000000..2eacc3c --- /dev/null +++ b/app/src/main/java/com/example/myapplication/components/templates/DropDown.kt @@ -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, selected: MutableState) { + 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 + } + ) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/myapplication/navigation/Navbar.kt b/app/src/main/java/com/example/myapplication/navigation/Navbar.kt index f621da6..d220ead 100644 --- a/app/src/main/java/com/example/myapplication/navigation/Navbar.kt +++ b/app/src/main/java/com/example/myapplication/navigation/Navbar.kt @@ -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")) } } + } } }