Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e9bc8e9792 | |||
| 0d8d318a0c | |||
| 1095dbaa47 |
@@ -76,4 +76,7 @@ dependencies {
|
||||
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
|
||||
debugImplementation("androidx.compose.ui:ui-tooling")
|
||||
debugImplementation("androidx.compose.ui:ui-test-manifest")
|
||||
|
||||
implementation ("androidx.paging:paging-compose:3.2.1")
|
||||
implementation ("androidx.paging:paging-runtime:3.2.1")
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<application
|
||||
android:name=".MyApp"
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
|
||||
12
app/src/main/java/com/example/myapplication/GlobalUser.kt
Normal file
12
app/src/main/java/com/example/myapplication/GlobalUser.kt
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.example.myapplication
|
||||
|
||||
class GlobalUser {
|
||||
var userId: Int = 0
|
||||
companion object {
|
||||
private var INSTANCE: GlobalUser? = null
|
||||
fun getInstance(): GlobalUser {
|
||||
if(INSTANCE == null) INSTANCE = GlobalUser()
|
||||
return INSTANCE!!
|
||||
}
|
||||
}
|
||||
}
|
||||
8
app/src/main/java/com/example/myapplication/MyApp.kt
Normal file
8
app/src/main/java/com/example/myapplication/MyApp.kt
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.example.myapplication
|
||||
|
||||
import android.app.Application
|
||||
import com.example.myapplication.database.AppDb
|
||||
|
||||
class MyApp : Application() {
|
||||
val db by lazy { AppDb.getInstance(this) }
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package com.example.myapplication.components
|
||||
|
||||
import ButtonNice
|
||||
import TextNice
|
||||
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.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
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
|
||||
import myColor1
|
||||
import myColor2
|
||||
|
||||
@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
|
||||
.fillMaxSize()
|
||||
.padding(start = 15.dp, end = 10.dp).verticalScroll(rememberScrollState()),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
|
||||
) {
|
||||
TextNice("Добавление товара")
|
||||
myInput("Название", nameState)
|
||||
myInput("Описание", infoState)
|
||||
myInput("Цена", priceState)
|
||||
DropDown(categories, categoryState)
|
||||
|
||||
Image(
|
||||
bitmap = img.value.asImageBitmap(),
|
||||
contentDescription = "editplaceholder",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier
|
||||
.size(350.dp)
|
||||
.padding(8.dp)
|
||||
.align(Alignment.CenterHorizontally))
|
||||
// Button(
|
||||
// onClick = {
|
||||
// launcher.launch("image/*")
|
||||
// },
|
||||
// modifier = Modifier
|
||||
// .fillMaxWidth()
|
||||
// .padding(top = 10.dp)) {
|
||||
// Text("Выбрать картинку", fontSize = 20.sp)
|
||||
// }
|
||||
ButtonNice("Выбрать картинку", myColor1){
|
||||
launcher.launch("image/*")
|
||||
}
|
||||
ButtonNice("Сохранить", myColor2){
|
||||
coroutineScope.launch {
|
||||
itemViewModel.insert(Product(null, nameState.value,infoState.value, priceState.value.toDouble(), img.value, categoryState.value!!.id!!))
|
||||
navController.navigate("main/0")
|
||||
}
|
||||
}
|
||||
// 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)
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -12,34 +13,39 @@ import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.Modifier
|
||||
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavController
|
||||
import com.example.myapplication.components.funs.ProductCardInCart
|
||||
import com.example.myapplication.components.funs.createProductCard
|
||||
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
|
||||
|
||||
@Composable
|
||||
fun Cart(navController: NavController){
|
||||
fun Cart(navController: NavController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)){
|
||||
|
||||
val context = LocalContext.current
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val products = remember { mutableStateListOf<Product>() }
|
||||
val sumPrice = remember { mutableStateOf(0.0) }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
products.clear()
|
||||
sumPrice.value = 0.0;
|
||||
AppDb.getInstance(context).userDao().getUserProductCartById(1).products.forEach {
|
||||
sumPrice.value += it.price
|
||||
products.add(it)
|
||||
}
|
||||
|
||||
userViewModel.getUserProductCartById(userViewModel.getUserId()).collect {data ->
|
||||
products.clear()
|
||||
sumPrice.value = 0.0;
|
||||
data.products.forEach {
|
||||
sumPrice.value += it.price
|
||||
products.add(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,12 +54,27 @@ fun Cart(navController: NavController){
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -1,89 +1,155 @@
|
||||
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.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
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.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 com.example.myapplication.database.entities.Product
|
||||
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.paging.compose.itemKey
|
||||
import com.example.myapplication.R
|
||||
import com.example.myapplication.components.funs.createProductCard
|
||||
import com.example.myapplication.database.AppDb
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import com.example.myapplication.database.entities.UserProductCart
|
||||
import com.example.myapplication.viewModels.ProductViewModel
|
||||
|
||||
import com.example.myapplication.viewModels.UserViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import myColor3
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun Main(navController: NavController, categoryId: Int) {
|
||||
fun Main(
|
||||
navController: NavController,
|
||||
catalogId: Int,
|
||||
productViewModel: ProductViewModel = viewModel(factory = ProductViewModel.factory),
|
||||
userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)
|
||||
) {
|
||||
|
||||
val context = LocalContext.current
|
||||
val products = remember { mutableStateListOf<Product>() }
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
|
||||
if (categoryId == 0){
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
AppDb.getInstance(context).productDao().getAll().collect { data ->
|
||||
products.clear()
|
||||
data.forEach{
|
||||
products.add(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
AppDb.getInstance(context).categoryDao().getProductsByCategory(categoryId).collect { data ->
|
||||
products.clear()
|
||||
|
||||
data.forEach {
|
||||
products.add(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val products = when (catalogId) {
|
||||
0 -> productViewModel.getAll().collectAsLazyPagingItems()
|
||||
else -> productViewModel.getByCategory(catalogId).collectAsLazyPagingItems()
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
if (userViewModel.getUserId() == 0) {
|
||||
navController.navigate("authorization")
|
||||
}
|
||||
}
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(8.dp)
|
||||
) {
|
||||
// item { OutlinedTextField(
|
||||
// value = "",
|
||||
// onValueChange = { },
|
||||
// placeholder = { Text("Поиск товара") },
|
||||
// modifier = Modifier
|
||||
// .fillMaxWidth()
|
||||
// .padding(8.dp)
|
||||
// ) }
|
||||
item {Text(
|
||||
text = "Товары:",
|
||||
fontSize = 28.sp,
|
||||
color = Color.Black,
|
||||
modifier = Modifier.padding(8.dp)
|
||||
)}
|
||||
item{
|
||||
Button(
|
||||
onClick = {navController.navigate("addProduct")},
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor= Color.Transparent,
|
||||
contentColor = myColor3
|
||||
),
|
||||
contentPadding = PaddingValues(0.dp),
|
||||
modifier = Modifier.size(30.dp).padding(top = 10.dp)
|
||||
|
||||
item{products.forEach {
|
||||
createProductCard(it.name, it.price, it.img, { navController.navigate("product/" + it.productId) } )
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(id = R.drawable.add),
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(50.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
item {
|
||||
Text(
|
||||
text = "Товары:",
|
||||
fontSize = 28.sp,
|
||||
color = Color.Black,
|
||||
modifier = Modifier.padding(8.dp)
|
||||
)
|
||||
}
|
||||
// item {
|
||||
// products.forEach {
|
||||
// createProductCard(
|
||||
// it.name,
|
||||
// it.price,
|
||||
// it.img,
|
||||
// { navController.navigate("product/" + it.productId) })
|
||||
// }
|
||||
// }
|
||||
items (
|
||||
count = products.itemCount,
|
||||
key = products.itemKey { product -> product.productId!! }
|
||||
) { index ->
|
||||
val it = products[index]!!
|
||||
createProductCard( it.name, it.price, it.img, { navController.navigate("product/" + it.productId)}){
|
||||
coroutineScope.launch {
|
||||
kotlin.runCatching {
|
||||
userViewModel.addProductCart(
|
||||
UserProductCart(
|
||||
userViewModel.getUserId(),
|
||||
it.productId!!
|
||||
)
|
||||
)
|
||||
}
|
||||
.onSuccess {
|
||||
val toast =
|
||||
Toast.makeText(context, "Добавлено", Toast.LENGTH_SHORT)
|
||||
toast.show()
|
||||
delay(500)
|
||||
toast.cancel()
|
||||
}
|
||||
.onFailure {
|
||||
val toast = Toast.makeText(
|
||||
context,
|
||||
"Уже есть в корзине",
|
||||
Toast.LENGTH_SHORT
|
||||
)
|
||||
toast.show()
|
||||
delay(500)
|
||||
toast.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.example.myapplication.components
|
||||
|
||||
import ButtonNice
|
||||
import Input
|
||||
import TextNice
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
@@ -12,57 +14,68 @@ import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
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.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.NavController
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
|
||||
import com.example.myapplication.components.common.myInput
|
||||
import com.example.myapplication.database.entities.User
|
||||
import com.example.myapplication.viewModels.UserViewModel
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import myColor1
|
||||
import myColor2
|
||||
import myColor3
|
||||
|
||||
@Composable
|
||||
fun Registration (navController: NavController){
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(start = 10.dp, end = 10.dp),
|
||||
fun Registration(
|
||||
navController: NavController,
|
||||
userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)
|
||||
) {
|
||||
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val context = LocalContext.current
|
||||
|
||||
val mailState = remember { mutableStateOf("") }
|
||||
val loginState = remember { mutableStateOf("") }
|
||||
val passState = remember { mutableStateOf("") }
|
||||
val repeatPassState = remember { mutableStateOf("") }
|
||||
|
||||
Column(modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(start = 10.dp, end = 10.dp),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
){
|
||||
Input("Имя")
|
||||
Input("Эл.почта")
|
||||
Input("Пароль")
|
||||
Input("Повторите пароль")
|
||||
|
||||
Button(
|
||||
onClick = { /*TODO*/ },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
{
|
||||
TextNice("Создать аккаунт")
|
||||
}
|
||||
|
||||
Button(
|
||||
onClick = {navController.navigate("authorization")},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
border = BorderStroke(1.dp, Color.Black),
|
||||
colors= ButtonDefaults.buttonColors(
|
||||
containerColor= Color.White,
|
||||
contentColor = Color.Gray
|
||||
))
|
||||
{
|
||||
TextNice("Авторизация")
|
||||
) {
|
||||
TextNice(text = "Регистрация")
|
||||
myInput("Email", mailState)
|
||||
myInput("Логин", loginState)
|
||||
myInput("Пароль", passState)
|
||||
myInput("Повторите Пароль", repeatPassState)
|
||||
|
||||
ButtonNice(text = "Регистрация", color = myColor1){
|
||||
coroutineScope.launch {
|
||||
if(passState.value != repeatPassState.value) {
|
||||
val toast = Toast.makeText(context, "Пароли не совпадают", Toast.LENGTH_SHORT)
|
||||
toast.show()
|
||||
delay(500)
|
||||
toast.cancel()
|
||||
return@launch
|
||||
}
|
||||
|
||||
userViewModel.insert(User(null, mailState.value, loginState.value, passState.value))
|
||||
navController.navigate("authorization")
|
||||
}
|
||||
}
|
||||
ButtonNice(text = "Авторизация", color = myColor2, onClickAction = {navController.navigate("authorization")})
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun RegistrationPreview() {
|
||||
val navController = rememberNavController()
|
||||
|
||||
Registration(navController = navController)
|
||||
}
|
||||
@@ -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") })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ fun ButtonNice(text: String, color: Color, onClickAction: () -> Unit = {}){
|
||||
shape = RoundedCornerShape(corner = CornerSize(5.dp)),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = color,
|
||||
contentColor = Color.White
|
||||
)
|
||||
) {
|
||||
Text(text, fontSize = 20.sp, color = Color.Black)
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.example.myapplication.components.common
|
||||
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextField
|
||||
import androidx.compose.material3.TextFieldDefaults
|
||||
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
|
||||
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun myInput(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
|
||||
),
|
||||
modifier = Modifier
|
||||
.border(1.dp, Color.Black, RoundedCornerShape(5.dp))
|
||||
.fillMaxWidth()
|
||||
.height(height),
|
||||
)
|
||||
}
|
||||
}
|
||||
@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(5.dp))
|
||||
.fillMaxWidth()
|
||||
.height(height),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ import myColor3
|
||||
import myColor4
|
||||
|
||||
@Composable
|
||||
fun createProductCard(name: String, price: Double, img: Bitmap, onClickAction: () -> Unit){
|
||||
fun createProductCard(name: String, price: Double, img: Bitmap, onClickAction: () -> Unit, onClickAction2: () -> Unit){
|
||||
Column () {
|
||||
Row {
|
||||
Image(
|
||||
@@ -47,7 +47,7 @@ fun createProductCard(name: String, price: Double, img: Bitmap, onClickAction: (
|
||||
ButtonNice("Инфо: " , Color.White, onClickAction)
|
||||
}
|
||||
}
|
||||
ButtonNice("Добавить в корзину" , myColor1, onClickAction)
|
||||
ButtonNice("Добавить в корзину" , myColor1, onClickAction2)
|
||||
}
|
||||
}
|
||||
@Composable
|
||||
@@ -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
|
||||
|
||||
@@ -31,7 +31,7 @@ fun CatalogItems (navController: NavController, title: String, products: Mutable
|
||||
}
|
||||
item{
|
||||
for (product in products){
|
||||
createProductCard(product.name, product.price, product.img, { })
|
||||
createProductCard(product.name, product.price, product.img, { }, { })
|
||||
}
|
||||
}
|
||||
item {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ fun ProductCard(product: ProductOld, onClickAction: () -> Unit) {
|
||||
ButtonNice(text ="Добавить в корзину" , color = myColor1, onClickAction)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun ProductCardPreview() {
|
||||
|
||||
@@ -43,33 +43,9 @@ fun Сategory(navController: NavController) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
categories.forEach{
|
||||
CategoryItem(it.name, { navController.navigate("main/" + it.id)})
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun myFun(categoryId: Int, title: String){
|
||||
|
||||
val navController = rememberNavController()
|
||||
val context = LocalContext.current
|
||||
val products = remember { mutableStateListOf<Product>() }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
AppDb.getInstance(context).categoryDao().getProductsByCategory(categoryId).collect { data ->
|
||||
products.clear()
|
||||
|
||||
data.forEach {
|
||||
products.add(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CatalogItems(navController, title, products )
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.example.myapplication.database
|
||||
|
||||
import android.content.Context
|
||||
import com.example.myapplication.database.repository.CategoryRepository
|
||||
import com.example.myapplication.database.repository.OfflineCategoryRepository
|
||||
import com.example.myapplication.database.repository.OfflineProductRepository
|
||||
import com.example.myapplication.database.repository.OfflineUserRepository
|
||||
import com.example.myapplication.database.repository.ProductRepository
|
||||
import com.example.myapplication.database.repository.UserRepository
|
||||
|
||||
interface AppContainer {
|
||||
val userRepository: UserRepository
|
||||
val productRepository: ProductRepository
|
||||
val categoryRepository: CategoryRepository
|
||||
}
|
||||
class AppDataContainer(private val context: Context) : AppContainer {
|
||||
override val userRepository: UserRepository by lazy {
|
||||
OfflineUserRepository(AppDb.getInstance(context).userDao())
|
||||
}
|
||||
override val productRepository: ProductRepository by lazy {
|
||||
OfflineProductRepository(AppDb.getInstance(context).productDao())
|
||||
}
|
||||
|
||||
override val categoryRepository: CategoryRepository by lazy {
|
||||
OfflineCategoryRepository(AppDb.getInstance(context).categoryDao())
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TIMEOUT = 5000L
|
||||
}
|
||||
}
|
||||
@@ -96,7 +96,7 @@ abstract class AppDb: RoomDatabase(){
|
||||
))
|
||||
|
||||
val userDao = database.userDao()
|
||||
userDao.insert(User(1, "Иванов И.И", "ivanov"))
|
||||
userDao.insert(User(1, "Иванов И.И", "ivanov","ivanov"))
|
||||
database.userDao().addProductCart(UserProductCart(1, 1))
|
||||
database.userDao().addProductCart(UserProductCart(1, 3))
|
||||
//database.userDao().addProductCart(UserProductCart(1, 2))
|
||||
|
||||
@@ -11,9 +11,7 @@ import kotlinx.coroutines.flow.Flow
|
||||
interface CategoryDao {
|
||||
@Query("select * from categories")
|
||||
fun getAll(): Flow<List<Category>>
|
||||
|
||||
@Insert
|
||||
suspend fun insert(category: Category)
|
||||
|
||||
@Query("select * from products where category_id = :categoryId")
|
||||
fun getProductsByCategory(categoryId: Int): Flow<List<Product>>
|
||||
}
|
||||
@@ -14,13 +14,18 @@ interface UserDao {
|
||||
fun getAll(): Flow<List<User>>
|
||||
|
||||
@Query("select * from users where users.userId = :id")
|
||||
fun getById(id: Int): User
|
||||
fun getById(id: Int): Flow<User>
|
||||
|
||||
@Query("select * from users where users.login = :login and users.password = :password")
|
||||
fun getByAuth(login: String, password: String): Flow<User?>
|
||||
|
||||
@Query("delete from userproductcart where userproductcart.userId == :userId and userproductcart.productId == :productId")
|
||||
suspend fun deleteCartProduct(userId: Int, productId: Int)
|
||||
@Insert
|
||||
suspend fun insert(user: User)
|
||||
|
||||
@Query("select * from users where users.userId = :id")
|
||||
fun getUserProductCartById(id: Int): UserWithCartProduct
|
||||
fun getUserProductCartById(id: Int): Flow<UserWithCartProduct>
|
||||
|
||||
@Insert
|
||||
suspend fun addProductCart(userProduct: UserProductCart)
|
||||
|
||||
@@ -11,7 +11,9 @@ data class User(
|
||||
@ColumnInfo(name="name")
|
||||
val name: String,
|
||||
@ColumnInfo(name="login")
|
||||
val login: String
|
||||
val login: String,
|
||||
@ColumnInfo(name="password")
|
||||
val password: String
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.example.myapplication.database.repository
|
||||
|
||||
import com.example.myapplication.database.dao.CategoryDao
|
||||
import com.example.myapplication.database.entities.Category
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class OfflineCategoryRepository(private val categoryDao: CategoryDao) : CategoryRepository {
|
||||
override fun getAll(): Flow<List<Category>> = categoryDao.getAll()
|
||||
override suspend fun insert(category: Category) = categoryDao.insert(category)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.example.myapplication.database.repository
|
||||
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import androidx.paging.PagingData
|
||||
import com.example.myapplication.database.dao.ProductDao
|
||||
import com.example.myapplication.database.entities.Product
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class OfflineProductRepository(private val productDao: ProductDao) : ProductRepository {
|
||||
override fun getAll(): Flow<PagingData<Product>> {
|
||||
return Pager(
|
||||
config = PagingConfig(
|
||||
pageSize = 8,
|
||||
prefetchDistance = 2,
|
||||
enablePlaceholders = true,
|
||||
initialLoadSize = 12,
|
||||
maxSize = 24
|
||||
),
|
||||
pagingSourceFactory = {
|
||||
productDao.getAll()
|
||||
}
|
||||
).flow
|
||||
}
|
||||
override fun getById(id: Int): Flow<Product> = productDao.getById(id)
|
||||
override fun getByCategory(category_id: Int): Flow<PagingData<Product>> {
|
||||
return Pager(
|
||||
config = PagingConfig(
|
||||
pageSize = 8,
|
||||
prefetchDistance = 2,
|
||||
enablePlaceholders = true,
|
||||
initialLoadSize = 12,
|
||||
maxSize = 24
|
||||
),
|
||||
pagingSourceFactory = {
|
||||
productDao.getByCategory(category_id)
|
||||
}
|
||||
).flow
|
||||
}
|
||||
override suspend fun insert(product: Product) = productDao.insert(product)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.myapplication.database.repository
|
||||
|
||||
import com.example.myapplication.database.dao.UserDao
|
||||
import com.example.myapplication.database.entities.User
|
||||
import com.example.myapplication.database.entities.UserProductCart
|
||||
import com.example.myapplication.database.entities.UserWithCartProduct
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class OfflineUserRepository(private val userDao: UserDao) : UserRepository {
|
||||
override fun getAll(): Flow<List<User>> = userDao.getAll()
|
||||
override fun getById(id: Int): Flow<User> = userDao.getById(id)
|
||||
override fun getByAuth(login: String, password: String): Flow<User?> = userDao.getByAuth(login, password)
|
||||
override fun getUserProductCartById(id: Int): Flow<UserWithCartProduct> = userDao.getUserProductCartById(id)
|
||||
override suspend fun deleteCartProduct(userId: Int, productId: Int) = userDao.deleteCartProduct(userId, productId)
|
||||
override suspend fun insert(user: User) = userDao.insert(user)
|
||||
override suspend fun addProductCart(userItem: UserProductCart) = userDao.addProductCart(userItem)
|
||||
}
|
||||
@@ -1,4 +1,12 @@
|
||||
package com.example.myapplication.database.repository
|
||||
|
||||
import androidx.paging.PagingData
|
||||
import com.example.myapplication.database.entities.Product
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface ProductRepository {
|
||||
fun getAll(): Flow<PagingData<Product>>
|
||||
fun getById(id: Int): Flow<Product>
|
||||
fun getByCategory(category_id: Int): Flow<PagingData<Product>>
|
||||
suspend fun insert(product: Product)
|
||||
}
|
||||
@@ -1,16 +1,17 @@
|
||||
package com.example.myapplication.database.repository
|
||||
|
||||
import com.example.myapplication.database.entities.User
|
||||
import com.example.myapplication.database.entities.UserProductCart
|
||||
import com.example.myapplication.database.entities.UserWithCartProduct
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface UserRepository {
|
||||
fun getAll(): Flow<List<User>>
|
||||
fun getById(id: Int): Flow<User>
|
||||
fun getByAuth(login: String, password: String): Flow<User?>
|
||||
fun getUserItemsCartById(id: Int): Flow<UserWithCartItems>
|
||||
suspend fun deleteCartItem(userId: Int, itemId: Int)
|
||||
suspend fun deleteFavoriteItem(userId: Int, itemId: Int)
|
||||
fun getUserProductCartById(id: Int): Flow<UserWithCartProduct>
|
||||
suspend fun deleteCartProduct(userId: Int, productId: Int)
|
||||
suspend fun insert(user: User)
|
||||
suspend fun addItemCart(userItem: UserItemCart)
|
||||
suspend fun addProductCart(userItem: UserProductCart)
|
||||
|
||||
}
|
||||
@@ -26,7 +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.myFun
|
||||
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
|
||||
@@ -96,8 +96,8 @@ fun Navbar() {
|
||||
Main(navController, it.getInt("id"))
|
||||
}
|
||||
}
|
||||
|
||||
composable("category") { Сategory(navController) }
|
||||
composable("addProduct") { AddProduct(navController) }
|
||||
composable("cart") { Cart(navController) }
|
||||
|
||||
composable(
|
||||
@@ -106,6 +106,7 @@ fun Navbar() {
|
||||
) { backStackEntry ->
|
||||
backStackEntry.arguments?.let { ProductForPage(it.getInt("id")) }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.example.myapplication.viewModels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import com.example.myapplication.MyApp
|
||||
import com.example.myapplication.database.entities.Category
|
||||
import com.example.myapplication.database.repository.CategoryRepository
|
||||
import com.example.myapplication.database.repository.OfflineCategoryRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class CategoryViewModel (
|
||||
private val categoryRepository: CategoryRepository
|
||||
): ViewModel() {
|
||||
companion object {
|
||||
val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
|
||||
override fun <T : ViewModel> create(
|
||||
modelClass: Class<T>,
|
||||
extras: CreationExtras
|
||||
): T {
|
||||
|
||||
val db =
|
||||
(checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as MyApp).db
|
||||
return CategoryViewModel(OfflineCategoryRepository(db.categoryDao())) as T
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getAll(): Flow<List<Category>> {
|
||||
return categoryRepository.getAll()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.example.myapplication.viewModels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import androidx.paging.PagingData
|
||||
import androidx.paging.cachedIn
|
||||
import com.example.myapplication.MyApp
|
||||
import com.example.myapplication.database.entities.Product
|
||||
import com.example.myapplication.database.repository.OfflineProductRepository
|
||||
import com.example.myapplication.database.repository.ProductRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class ProductViewModel(
|
||||
private val productRepository: ProductRepository
|
||||
) : ViewModel() {
|
||||
companion object {
|
||||
val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{
|
||||
override fun <T: ViewModel> create(
|
||||
modelClass: Class<T>,
|
||||
extras: CreationExtras
|
||||
) : T {
|
||||
val db = (checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as MyApp).db
|
||||
return ProductViewModel(OfflineProductRepository(db.productDao())) as T
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getAll() : Flow<PagingData<Product>> {
|
||||
return productRepository.getAll().cachedIn(viewModelScope)
|
||||
}
|
||||
|
||||
fun getById(id: Int) : Flow<Product> {
|
||||
return productRepository.getById(id)
|
||||
}
|
||||
|
||||
fun getByCategory(cId: Int): Flow<PagingData<Product>> {
|
||||
return productRepository.getByCategory(cId).cachedIn(viewModelScope)
|
||||
}
|
||||
|
||||
suspend fun insert(product: Product) {
|
||||
productRepository.insert(product)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.example.myapplication.viewModels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import com.example.myapplication.GlobalUser
|
||||
import com.example.myapplication.MyApp
|
||||
import com.example.myapplication.database.entities.User
|
||||
import com.example.myapplication.database.entities.UserProductCart
|
||||
import com.example.myapplication.database.entities.UserWithCartProduct
|
||||
import com.example.myapplication.database.repository.OfflineUserRepository
|
||||
import com.example.myapplication.database.repository.UserRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class UserViewModel(
|
||||
private val userRepository: UserRepository
|
||||
) : ViewModel() {
|
||||
companion object {
|
||||
val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{
|
||||
override fun <T: ViewModel> create(
|
||||
modelClass: Class<T>,
|
||||
extras: CreationExtras
|
||||
) : T {
|
||||
|
||||
val db = (checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as MyApp).db
|
||||
return UserViewModel(OfflineUserRepository(db.userDao())) as T
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getUserId(): Int {
|
||||
return GlobalUser.getInstance().userId
|
||||
}
|
||||
|
||||
fun setUserId(id: Int) {
|
||||
GlobalUser.getInstance().userId = id
|
||||
}
|
||||
|
||||
fun getAll(): Flow<List<User>> {
|
||||
return userRepository.getAll()
|
||||
}
|
||||
fun getById(id: Int): Flow<User> {
|
||||
return userRepository.getById(id)
|
||||
}
|
||||
fun getByAuth(login: String, password: String): Flow<User?> {
|
||||
return userRepository.getByAuth(login, password)
|
||||
}
|
||||
suspend fun deleteCartProduct(userId: Int, productId: Int) {
|
||||
userRepository.deleteCartProduct(userId, productId)
|
||||
}
|
||||
fun getUserProductCartById(id: Int): Flow<UserWithCartProduct> {
|
||||
return userRepository.getUserProductCartById(id)
|
||||
}
|
||||
|
||||
suspend fun insert(user: User) {
|
||||
userRepository.insert(user)
|
||||
}
|
||||
suspend fun addProductCart(userItem: UserProductCart) {
|
||||
userRepository.addProductCart(userItem)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user