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"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="17" />
<bytecodeTargetLevel target="19" />
</component>
</project>

View File

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

View File

@ -1,6 +1,6 @@
<project version="4">
<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" />
</component>
<component name="ProjectType">

View File

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

View File

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_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.fillMaxSize
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.platform.LocalContext
import com.example.android_programming.composeui.Header.Header
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() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -21,6 +29,16 @@ class MainActivity : ComponentActivity() {
@Composable
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(
modifier = Modifier
.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.SignIn.LoginScreen
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.google.gson.Gson
@ -57,14 +58,14 @@ fun NavController(navController: NavHostController){
}
composable(NavItem.AboutSneaker.route) { backStackEntry ->
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 = {
navController.navigateUp() })
}
}
composable(NavItem.ChangeSneaker.route) { backStackEntry ->
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 = {
navController.navigateUp() })
}

View File

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

View File

@ -27,13 +27,16 @@ import androidx.compose.ui.res.colorResource
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.NavHostController
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.google.gson.Gson
@Composable
fun CardSneakerForChange(item: SneakerItem, navController: NavHostController) {
fun CardSneakerForChange(item: Sneaker, navController: NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
Row(
modifier = Modifier
.fillMaxWidth()
@ -44,7 +47,7 @@ fun CardSneakerForChange(item: SneakerItem, navController: NavHostController) {
horizontalArrangement = Arrangement.SpaceBetween
) {
Image(
painter = painterResource(id = item.imageId),
painter = painterResource(id = item.photo),
contentDescription = "image",
contentScale = ContentScale.FillWidth,
modifier = Modifier
@ -58,8 +61,8 @@ fun CardSneakerForChange(item: SneakerItem, navController: NavHostController) {
.weight(1f)
.padding(start = 16.dp)
) {
item.name?.let { Text(text = it, fontSize = 20.sp) }
Text(text = "${item.price} USD", color = Color.Red, fontSize = 16.sp)
item.brand?.let { Text(text = it, fontSize = 20.sp) }
Text(text = "${item.price} $", color = Color.Red, fontSize = 16.sp)
}
Button(
@ -68,6 +71,10 @@ fun CardSneakerForChange(item: SneakerItem, navController: NavHostController) {
contentColor = Color.White
),
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)
navController.navigate("changeSneaker/${sneakerItemString}") },
modifier = Modifier
@ -81,7 +88,9 @@ fun CardSneakerForChange(item: SneakerItem, navController: NavHostController) {
backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White
),
onClick = { /*TODO*/ },
onClick = {
sneakerViewModel.deleteSneaker(item)
},
modifier = Modifier
.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.itemsIndexed
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.example.android_programming.R
import com.example.android_programming.SneakerViewModel
import com.example.android_programming.model.SneakerItem
@Composable
fun ChangePanel(navHostController: NavHostController) {
fun ChangePanel(navHostController: NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
val list = sneakerViewModel.SneakerList.collectAsState(initial = emptyList()).value
Column(
modifier = Modifier
.fillMaxSize()
@ -28,12 +32,7 @@ fun ChangePanel(navHostController: NavHostController) {
modifier = Modifier
.fillMaxSize()
) {
itemsIndexed(
listOf(
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
SneakerItem(R.drawable.sneaker, "Jordan", 179.99),
)
itemsIndexed(list
){_, item->
CardSneakerForChange(item = item, navHostController)
}

View File

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

View File

@ -32,10 +32,11 @@ import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.android_programming.R
import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem
@Composable
fun AboutSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
fun AboutSneaker(sneaker: Sneaker, onBackClick: () -> Unit) {
Column(
modifier = Modifier
.fillMaxSize()
@ -55,7 +56,7 @@ fun AboutSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
)
}
Image(
painter = painterResource(id = sneaker.imageId),
painter = painterResource(id = sneaker.photo),
contentDescription = "image",
contentScale = ContentScale.FillWidth,
modifier = Modifier
@ -67,12 +68,17 @@ fun AboutSneaker(sneaker: SneakerItem, onBackClick: () -> Unit) {
modifier = Modifier.padding(16.dp)
) {
Text(
text = "${sneaker.name}",
text = "${sneaker.brand}",
fontSize = 25.sp,
fontWeight = FontWeight.Bold
)
Text(
text = "${sneaker.model}",
fontSize = 25.sp,
fontWeight = FontWeight.Bold
)
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(
colors = ButtonDefaults.buttonColors(
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.verticalScroll
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.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
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.composeui.Screens.HomeScreen.FilterByBrand.FilterByBrand
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 kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun HomeScreen(navHostController: NavHostController) {
val context = LocalContext.current
val sneakers = remember { mutableStateListOf<Sneaker>() }
Column(
modifier = Modifier
.fillMaxSize()

View File

@ -29,11 +29,12 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.example.android_programming.R
import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem
import com.google.gson.Gson
@Composable
fun CardSneaker(item: SneakerItem, navController: NavHostController) {
fun CardSneaker(item: Sneaker, navController: NavHostController) {
val maxWidth = (LocalConfiguration.current.screenWidthDp / 2).dp
Box(
@ -53,7 +54,7 @@ fun CardSneaker(item: SneakerItem, navController: NavHostController) {
.widthIn(maxWidth)
) {
Image(
painter = painterResource(id = item.imageId),
painter = painterResource(id = item.photo),
contentDescription = "image",
contentScale = ContentScale.FillWidth,
modifier = Modifier
@ -71,7 +72,10 @@ fun CardSneaker(item: SneakerItem, navController: NavHostController) {
modifier = Modifier
.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)
}
Column(

View File

@ -10,19 +10,23 @@ import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
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
fun RecyclerView(navHostController : NavHostController) {
fun RecyclerView(navHostController : NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(bottom = 60.dp)
) {
val list = getSneakers()
val list = sneakerViewModel.SneakerList.collectAsState(initial = emptyList()).value
val numColumns = 2
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)
@Update
suspend fun update(sneaker: Sneaker)
suspend fun update(sneaker: Sneaker?)
@Delete
suspend fun delete(sneaker: Sneaker)
@ -22,6 +22,6 @@ interface SneakerDao {
@Query("SELECT*FROM sneakers")
fun getAllSneakers(): Flow<List<Sneaker>>
@Query("SELECT*FROM sneakers WHERE id = :id")
suspend fun getSneakerById(id: Int)
@Query("SELECT * FROM sneakers WHERE id = :id")
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
import androidx.compose.runtime.MutableState
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@ -7,7 +8,7 @@ import androidx.room.PrimaryKey
@Entity(tableName = "sneakers")
data class Sneaker(
@PrimaryKey(autoGenerate = true)
val id: Int,
val id: Int? = null,
@ColumnInfo(name = "Brand")
val brand: String,
@ColumnInfo(name = "Model")
@ -15,5 +16,7 @@ data class Sneaker(
@ColumnInfo(name = "Description")
val description: String,
@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