Feature: sneaker model, class for connect db, crud on sneaker

This commit is contained in:
ArtemEmelyanov 2023-10-15 13:42:11 +04:00
parent ff39c5c854
commit c6bc40d3ff
27 changed files with 286 additions and 87 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<bytecodeTargetLevel target="17" /> <bytecodeTargetLevel target="19" />
</component> </component>
</project> </project>

View File

@ -8,7 +8,7 @@
<option name="distributionType" value="DEFAULT_WRAPPED" /> <option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="$PROJECT_DIR$/../../../../Gradle/gradle-8.3" /> <option name="gradleHome" value="$PROJECT_DIR$/../../../../Gradle/gradle-8.3" />
<option name="gradleJvm" value="jbr-17" /> <option name="gradleJvm" value="19" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />

View File

@ -1,6 +1,6 @@
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View File

@ -1,7 +1,7 @@
plugins { plugins {
id("com.android.application") id("com.android.application")
id("org.jetbrains.kotlin.android") id("org.jetbrains.kotlin.android")
// id("kotlin-kapt") id ("kotlin-kapt")
} }
android { android {
@ -31,11 +31,11 @@ android {
} }
} }
compileOptions { compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8 sourceCompatibility = JavaVersion.VERSION_19
targetCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_19
} }
kotlinOptions { kotlinOptions {
jvmTarget = "1.8" jvmTarget = "19"
} }
buildFeatures { buildFeatures {
compose = true compose = true
@ -49,7 +49,7 @@ android {
} }
} }
} }
apply(plugin = "kotlin-kapt")
dependencies { dependencies {
implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.core:core-ktx:1.12.0")
@ -78,7 +78,7 @@ dependencies {
val room_version = "2.5.2" val room_version = "2.5.2"
implementation("androidx.room:room-runtime:$room_version") implementation("androidx.room:room-runtime:$room_version")
annotationProcessor("androidx.room:room-compiler:$room_version") annotationProcessor("androidx.room:room-compiler:$room_version")
// kapt("androidx.room:room-compiler:$room_version") kapt("androidx.room:room-compiler:$room_version")
implementation("androidx.room:room-ktx:$room_version") implementation("androidx.room:room-ktx:$room_version")
implementation("androidx.room:room-paging:$room_version") implementation("androidx.room:room-paging:$room_version")
} }

View File

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<application <application
android:name=".App"
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules" android:fullBackupContent="@xml/backup_rules"

View File

@ -0,0 +1,8 @@
package com.example.android_programming
import android.app.Application
import com.example.android_programming.database.AppDatabase
public class App : Application() {
val database by lazy { AppDatabase.getInstance(this)}
}

View File

@ -6,9 +6,17 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.example.android_programming.composeui.Header.Header import com.example.android_programming.composeui.Header.Header
import com.example.android_programming.composeui.Navigation.Navigate import com.example.android_programming.composeui.Navigation.Navigate
import com.example.android_programming.database.AppDatabase
import com.example.android_programming.model.Sneaker
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -21,6 +29,16 @@ class MainActivity : ComponentActivity() {
@Composable @Composable
fun MainContent() { fun MainContent() {
val context = LocalContext.current
val sneakers = remember { mutableStateListOf<Sneaker>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).sneakerDao().getAllSneakers().collect { data ->
sneakers.clear()
sneakers.addAll(data)
}
}
}
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()

View File

@ -0,0 +1,56 @@
package com.example.android_programming
import androidx.compose.runtime.mutableStateOf
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.room.Update
import com.example.android_programming.database.AppDatabase
import com.example.android_programming.model.Sneaker
import kotlinx.coroutines.launch
class SneakerViewModel(val database: AppDatabase): ViewModel() {
var brand = mutableStateOf("")
val model = mutableStateOf("")
val description = mutableStateOf("")
val price = mutableStateOf("")
val photo = mutableStateOf(R.drawable.img)
val SneakerList = database.sneakerDao().getAllSneakers()
var sneaker: Sneaker? = null
fun insertSneaker() = viewModelScope.launch {
val sneaker = Sneaker(
brand = brand.value,
model = model.value,
description = description.value,
price = price.value.toDouble(),
photo = photo.value
)
database.sneakerDao().insert(sneaker)
}
fun deleteSneaker(sneaker : Sneaker) = viewModelScope.launch {
database.sneakerDao().delete(sneaker)
}
fun getSneakerById(id: Int) = viewModelScope.launch {
database.sneakerDao().getSneakerById(id)
}
fun UpdateSneaker(sneaker: Sneaker) = viewModelScope.launch {
database.sneakerDao().update(sneaker)
}
companion object{
val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras): T {
val database = (checkNotNull(extras[APPLICATION_KEY]) as App).database
return SneakerViewModel(database) as T
}
}
}
}

View File

@ -16,6 +16,7 @@ import com.example.android_programming.composeui.Screens.ProfileScreen.Profile.P
import com.example.android_programming.composeui.Screens.ProfileScreen.Profile.ProfileScreen import com.example.android_programming.composeui.Screens.ProfileScreen.Profile.ProfileScreen
import com.example.android_programming.composeui.Screens.ProfileScreen.SignIn.LoginScreen import com.example.android_programming.composeui.Screens.ProfileScreen.SignIn.LoginScreen
import com.example.android_programming.composeui.Screens.ProfileScreen.SignUp.SignUpScreen import com.example.android_programming.composeui.Screens.ProfileScreen.SignUp.SignUpScreen
import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem import com.example.android_programming.model.SneakerItem
import com.google.gson.Gson import com.google.gson.Gson
@ -57,14 +58,14 @@ fun NavController(navController: NavHostController){
} }
composable(NavItem.AboutSneaker.route) { backStackEntry -> composable(NavItem.AboutSneaker.route) { backStackEntry ->
val sneakerItemString = backStackEntry.arguments?.getString("sneakerItem") val sneakerItemString = backStackEntry.arguments?.getString("sneakerItem")
val sneakerItem = Gson().fromJson(sneakerItemString, SneakerItem::class.java) val sneakerItem = Gson().fromJson(sneakerItemString, Sneaker::class.java)
sneakerItem?.let { AboutSneaker(it, onBackClick = { sneakerItem?.let { AboutSneaker(it, onBackClick = {
navController.navigateUp() }) navController.navigateUp() })
} }
} }
composable(NavItem.ChangeSneaker.route) { backStackEntry -> composable(NavItem.ChangeSneaker.route) { backStackEntry ->
val sneakerItemString = backStackEntry.arguments?.getString("sneakerItem") val sneakerItemString = backStackEntry.arguments?.getString("sneakerItem")
val sneakerItem = Gson().fromJson(sneakerItemString, SneakerItem::class.java) val sneakerItem = Gson().fromJson(sneakerItemString, Sneaker::class.java)
sneakerItem?.let { ChangeSneaker(it, onBackClick = { sneakerItem?.let { ChangeSneaker(it, onBackClick = {
navController.navigateUp() }) navController.navigateUp() })
} }

View File

@ -1,5 +1,6 @@
package com.example.android_programming.composeui.Screens.AdminPanel package com.example.android_programming.composeui.Screens.AdminPanel
import androidx.compose.foundation.Image
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
@ -22,6 +23,7 @@ import androidx.compose.material.Text
import androidx.compose.material.TextField import androidx.compose.material.TextField
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
@ -29,23 +31,25 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.android_programming.App
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.SneakerViewModel
import com.example.android_programming.database.AppDatabase
import com.example.android_programming.model.PhotoManager
@Composable @Composable
@Preview fun AddPanel(sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)){
fun AddPanel(){ val photoManager = PhotoManager()
var brand by remember { mutableStateOf("") }
var model by remember { mutableStateOf("") }
var description by remember { mutableStateOf("") }
var price by remember { mutableStateOf("") }
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -59,20 +63,22 @@ fun AddPanel(){
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center verticalArrangement = Arrangement.Center
) { ) {
Box( Image(
painter = painterResource(id = sneakerViewModel.photo.value),
contentDescription = "image",
contentScale = ContentScale.FillHeight,
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(16.dp) .padding(16.dp)
.height(150.dp) .height(200.dp)
.background(Color.Gray) )
) {
}
Button( Button(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.figma_blue), backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
sneakerViewModel.photo.value = photoManager.changePhoto(sneakerViewModel.photo.value)
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -82,8 +88,8 @@ fun AddPanel(){
} }
TextField( TextField(
value = brand, value = sneakerViewModel.brand.value,
onValueChange = { brand = it }, onValueChange = { sneakerViewModel.brand.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -110,8 +116,8 @@ fun AddPanel(){
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = model, value = sneakerViewModel.model.value,
onValueChange = { model = it }, onValueChange = { sneakerViewModel.model.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -138,8 +144,8 @@ fun AddPanel(){
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = description, value = sneakerViewModel.description.value,
onValueChange = { description = it }, onValueChange = { sneakerViewModel.description.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(150.dp) .height(150.dp)
@ -165,8 +171,8 @@ fun AddPanel(){
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = price, value = sneakerViewModel.price.value,
onValueChange = { price = it }, onValueChange = { sneakerViewModel.price.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -196,7 +202,7 @@ fun AddPanel(){
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
sneakerViewModel.insertSneaker()
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()

View File

@ -27,13 +27,16 @@ import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.SneakerViewModel
import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem import com.example.android_programming.model.SneakerItem
import com.google.gson.Gson import com.google.gson.Gson
@Composable @Composable
fun CardSneakerForChange(item: SneakerItem, navController: NavHostController) { fun CardSneakerForChange(item: Sneaker, navController: NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -44,7 +47,7 @@ fun CardSneakerForChange(item: SneakerItem, navController: NavHostController) {
horizontalArrangement = Arrangement.SpaceBetween horizontalArrangement = Arrangement.SpaceBetween
) { ) {
Image( Image(
painter = painterResource(id = item.imageId), painter = painterResource(id = item.photo),
contentDescription = "image", contentDescription = "image",
contentScale = ContentScale.FillWidth, contentScale = ContentScale.FillWidth,
modifier = Modifier modifier = Modifier
@ -58,8 +61,8 @@ fun CardSneakerForChange(item: SneakerItem, navController: NavHostController) {
.weight(1f) .weight(1f)
.padding(start = 16.dp) .padding(start = 16.dp)
) { ) {
item.name?.let { Text(text = it, fontSize = 20.sp) } item.brand?.let { Text(text = it, fontSize = 20.sp) }
Text(text = "${item.price} USD", color = Color.Red, fontSize = 16.sp) Text(text = "${item.price} $", color = Color.Red, fontSize = 16.sp)
} }
Button( Button(
@ -68,6 +71,10 @@ fun CardSneakerForChange(item: SneakerItem, navController: NavHostController) {
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
sneakerViewModel.brand.value = item.brand ?: ""
sneakerViewModel.model.value = item.model ?: ""
sneakerViewModel.description.value = item.description ?: ""
sneakerViewModel.price.value = item.price.toString()
val sneakerItemString = Gson().toJson(item) val sneakerItemString = Gson().toJson(item)
navController.navigate("changeSneaker/${sneakerItemString}") }, navController.navigate("changeSneaker/${sneakerItemString}") },
modifier = Modifier modifier = Modifier
@ -81,7 +88,9 @@ fun CardSneakerForChange(item: SneakerItem, navController: NavHostController) {
backgroundColor = colorResource(id = R.color.figma_blue), backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White contentColor = Color.White
), ),
onClick = { /*TODO*/ }, onClick = {
sneakerViewModel.deleteSneaker(item)
},
modifier = Modifier modifier = Modifier
.padding(end = 16.dp) .padding(end = 16.dp)
) { ) {

View File

@ -8,15 +8,19 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.SneakerViewModel
import com.example.android_programming.model.SneakerItem import com.example.android_programming.model.SneakerItem
@Composable @Composable
fun ChangePanel(navHostController: NavHostController) { fun ChangePanel(navHostController: NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
val list = sneakerViewModel.SneakerList.collectAsState(initial = emptyList()).value
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
@ -28,12 +32,7 @@ fun ChangePanel(navHostController: NavHostController) {
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
) { ) {
itemsIndexed( itemsIndexed(list
listOf(
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
SneakerItem(R.drawable.sneaker, "Jordan", 179.99),
)
){_, item-> ){_, item->
CardSneakerForChange(item = item, navHostController) CardSneakerForChange(item = item, navHostController)
} }

View File

@ -4,11 +4,9 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
@ -41,15 +39,20 @@ import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.SneakerViewModel
import com.example.android_programming.model.PhotoManager
import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem import com.example.android_programming.model.SneakerItem
@Composable @Composable
fun ChangeSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) { fun ChangeSneaker(sneaker: Sneaker, onBackClick: () -> Unit, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
var brand by remember { mutableStateOf("") } val brand = remember {mutableStateOf(sneaker.brand)}
var model by remember { mutableStateOf("") } val model = remember{mutableStateOf(sneaker.model)}
var description by remember { mutableStateOf("") } val description = remember{mutableStateOf(sneaker.description)}
var price by remember { mutableStateOf("") } val price = remember{mutableStateOf(sneaker.price.toString())}
var photo by remember { mutableStateOf(sneaker.photo) }
val photoManager = PhotoManager()
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -76,7 +79,7 @@ fun ChangeSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
) )
} }
Image( Image(
painter = painterResource(id = sneaker.imageId), painter = painterResource(id = photo),
contentDescription = "image", contentDescription = "image",
contentScale = ContentScale.FillHeight, contentScale = ContentScale.FillHeight,
modifier = Modifier modifier = Modifier
@ -84,12 +87,14 @@ fun ChangeSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
.padding(16.dp) .padding(16.dp)
.height(200.dp) .height(200.dp)
) )
Button( Button(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.figma_blue), backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
photo = photoManager.changePhoto(photo)
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -99,8 +104,8 @@ fun ChangeSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
} }
TextField( TextField(
value = sneaker.name.toString(), value = brand.value,
onValueChange = { brand = it }, onValueChange = { newValue -> brand.value = newValue },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -127,8 +132,8 @@ fun ChangeSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = model, value = model.value,
onValueChange = { model = it }, onValueChange = { model.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -155,8 +160,8 @@ fun ChangeSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = description, value = description.value,
onValueChange = { description = it }, onValueChange = { description.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(150.dp) .height(150.dp)
@ -182,8 +187,8 @@ fun ChangeSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = sneaker.price.toString(), value = price.value,
onValueChange = { price = it }, onValueChange = { price.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -213,7 +218,17 @@ fun ChangeSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
sneakerViewModel.UpdateSneaker(
Sneaker(
id = sneaker.id,
brand = brand.value,
model = model.value,
description = description.value,
photo = photo,
price = price.value.toDouble()
)
)
onBackClick()
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()

View File

@ -32,10 +32,11 @@ import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem import com.example.android_programming.model.SneakerItem
@Composable @Composable
fun AboutSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) { fun AboutSneaker(sneaker: Sneaker, onBackClick: () -> Unit) {
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
@ -55,7 +56,7 @@ fun AboutSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
) )
} }
Image( Image(
painter = painterResource(id = sneaker.imageId), painter = painterResource(id = sneaker.photo),
contentDescription = "image", contentDescription = "image",
contentScale = ContentScale.FillWidth, contentScale = ContentScale.FillWidth,
modifier = Modifier modifier = Modifier
@ -67,12 +68,17 @@ fun AboutSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
modifier = Modifier.padding(16.dp) modifier = Modifier.padding(16.dp)
) { ) {
Text( Text(
text = "${sneaker.name}", text = "${sneaker.brand}",
fontSize = 25.sp,
fontWeight = FontWeight.Bold
)
Text(
text = "${sneaker.model}",
fontSize = 25.sp, fontSize = 25.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text(text = "Price: $${sneaker.price}", fontSize = 16.sp) Text(text = "Price: $${sneaker.price}", fontSize = 16.sp)
Text(text = "Description: If you wear stylish sneakers, you are doing something good for yourself. And with colors inspired by the aisles of your local beauty store (plus cloud-like Air cushioning underfoot), you'll feel anything but mediocre in these mid-cut Js. Go on, treat yourself.", fontSize = 16.sp) Text(text = "Description: ${sneaker.description}", fontSize = 16.sp)
Button( Button(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.figma_blue), backgroundColor = colorResource(id = R.color.figma_blue),
@ -89,13 +95,4 @@ fun AboutSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
} }
} }
} }
}
@Preview
@Composable
fun asds(){
val sdas = rememberNavController()
AboutSneaker(SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
onBackClick = {
sdas.navigateUp() })
} }

View File

@ -14,9 +14,13 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.composeui.Screens.HomeScreen.FilterByBrand.ItemFilterByBrand import com.example.android_programming.composeui.Screens.HomeScreen.FilterByBrand.ItemFilterByBrand
@ -26,10 +30,16 @@ import com.example.android_programming.composeui.Screens.HomeScreen.SneakerRecyc
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.composeui.Screens.HomeScreen.FilterByBrand.FilterByBrand import com.example.android_programming.composeui.Screens.HomeScreen.FilterByBrand.FilterByBrand
import com.example.android_programming.composeui.Screens.HomeScreen.SneakerRecyclerView.RecyclerView import com.example.android_programming.composeui.Screens.HomeScreen.SneakerRecyclerView.RecyclerView
import com.example.android_programming.database.AppDatabase
import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.getSneakers import com.example.android_programming.model.getSneakers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable @Composable
fun HomeScreen(navHostController: NavHostController) { fun HomeScreen(navHostController: NavHostController) {
val context = LocalContext.current
val sneakers = remember { mutableStateListOf<Sneaker>() }
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()

View File

@ -29,11 +29,12 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem import com.example.android_programming.model.SneakerItem
import com.google.gson.Gson import com.google.gson.Gson
@Composable @Composable
fun CardSneaker(item: SneakerItem, navController: NavHostController) { fun CardSneaker(item: Sneaker, navController: NavHostController) {
val maxWidth = (LocalConfiguration.current.screenWidthDp / 2).dp val maxWidth = (LocalConfiguration.current.screenWidthDp / 2).dp
Box( Box(
@ -53,7 +54,7 @@ fun CardSneaker(item: SneakerItem, navController: NavHostController) {
.widthIn(maxWidth) .widthIn(maxWidth)
) { ) {
Image( Image(
painter = painterResource(id = item.imageId), painter = painterResource(id = item.photo),
contentDescription = "image", contentDescription = "image",
contentScale = ContentScale.FillWidth, contentScale = ContentScale.FillWidth,
modifier = Modifier modifier = Modifier
@ -71,7 +72,10 @@ fun CardSneaker(item: SneakerItem, navController: NavHostController) {
modifier = Modifier modifier = Modifier
.padding(10.dp, 0.dp) .padding(10.dp, 0.dp)
) { ) {
item.name?.let { Text(text = it) } Row {
item.brand?.let { Text(text = "$it ") }
item.model?.let { Text(text = it) }
}
Text(text = item.price.toString(), color = Color.Red) Text(text = item.price.toString(), color = Color.Red)
} }
Column( Column(

View File

@ -10,19 +10,23 @@ import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.model.getSneakers import com.example.android_programming.SneakerViewModel
import com.example.android_programming.model.Sneaker
import kotlin.collections.chunked
@Composable @Composable
fun RecyclerView(navHostController : NavHostController) { fun RecyclerView(navHostController : NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.padding(bottom = 60.dp) .padding(bottom = 60.dp)
) { ) {
val list = getSneakers() val list = sneakerViewModel.SneakerList.collectAsState(initial = emptyList()).value
val numColumns = 2 val numColumns = 2
val chunkedList = list.chunked(numColumns) val chunkedList = list.chunked(numColumns)
@ -37,4 +41,4 @@ fun RecyclerView(navHostController : NavHostController) {
} }
} }
} }
} }

View File

@ -14,7 +14,7 @@ interface SneakerDao {
suspend fun insert(sneaker: Sneaker) suspend fun insert(sneaker: Sneaker)
@Update @Update
suspend fun update(sneaker: Sneaker) suspend fun update(sneaker: Sneaker?)
@Delete @Delete
suspend fun delete(sneaker: Sneaker) suspend fun delete(sneaker: Sneaker)
@ -22,6 +22,6 @@ interface SneakerDao {
@Query("SELECT*FROM sneakers") @Query("SELECT*FROM sneakers")
fun getAllSneakers(): Flow<List<Sneaker>> fun getAllSneakers(): Flow<List<Sneaker>>
@Query("SELECT*FROM sneakers WHERE id = :id") @Query("SELECT * FROM sneakers WHERE id = :id")
suspend fun getSneakerById(id: Int) suspend fun getSneakerById(id: Int): Sneaker
} }

View File

@ -0,0 +1,32 @@
package com.example.android_programming.database
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.android_programming.dao.SneakerDao
import com.example.android_programming.model.Sneaker
@Database(entities = [Sneaker::class], version = 2)
abstract class AppDatabase : RoomDatabase() {
abstract fun sneakerDao(): SneakerDao
companion object {
private const val DB_NAME: String = "my-db"
@Volatile
private var INSTANCE: AppDatabase? = null
fun getInstance(context: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
DB_NAME
).build()
INSTANCE = instance
instance
}
}
}
}

View File

@ -0,0 +1,36 @@
package com.example.android_programming.model
import android.util.Log
import com.example.android_programming.R
class PhotoManager {
private val photos = listOf(
R.drawable.img,
R.drawable.img_1,
R.drawable.img_2,
R.drawable.img_3,
R.drawable.img_4,
R.drawable.img_5,
)
private var currentIndex = 0
fun changePhoto(int: Int): Int {
currentIndex = photos.indexOf(int)
if (photos.isNotEmpty()) {
if(currentIndex + 1 >= photos.size){
currentIndex = 0
}else{
currentIndex += 1
}
return photos[currentIndex]
} else {
return -1
}
}
}

View File

@ -1,5 +1,6 @@
package com.example.android_programming.model package com.example.android_programming.model
import androidx.compose.runtime.MutableState
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
@ -7,7 +8,7 @@ import androidx.room.PrimaryKey
@Entity(tableName = "sneakers") @Entity(tableName = "sneakers")
data class Sneaker( data class Sneaker(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
val id: Int, val id: Int? = null,
@ColumnInfo(name = "Brand") @ColumnInfo(name = "Brand")
val brand: String, val brand: String,
@ColumnInfo(name = "Model") @ColumnInfo(name = "Model")
@ -15,5 +16,7 @@ data class Sneaker(
@ColumnInfo(name = "Description") @ColumnInfo(name = "Description")
val description: String, val description: String,
@ColumnInfo(name = "Price") @ColumnInfo(name = "Price")
val price: Double val price: Double,
@ColumnInfo(name = "Photo")
val photo: Int
) )

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB