need to do ChangeHotel

This commit is contained in:
VictoriaPresnyakova 2023-11-01 19:13:12 +04:00
parent 6a8a539858
commit edeae3587f
8 changed files with 144 additions and 86 deletions

View File

@ -12,18 +12,18 @@ data class Hotel (
@ColumnInfo(name = "Name")
val name: String,
@ColumnInfo(name = "Price")
@ColumnInfo(name = "Price")//
val price: Double,
@ColumnInfo(name = "Img")
@ColumnInfo(name = "Img")//
val img: Int,
@ColumnInfo(name = "Stars")
@ColumnInfo(name = "Stars")//
val stars: Int,
@ColumnInfo(name = "Location")
@ColumnInfo(name = "Location")//
val location: String,
@ColumnInfo(name = "Info")
@ColumnInfo(name = "Info")//
val info: String,
)

View File

@ -1,18 +1,14 @@
package com.example.androidlabs.Navigation
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.DB.viewModels.HotelViewModel
import com.example.androidlabs.DB.viewModels.OrderViewModel
import com.example.androidlabs.MyOrderScreen.MyOrderScreen
import com.example.androidlabs.R
import com.example.androidlabs.booking.BookingScreen
import com.example.androidlabs.homeScreen.CardItem.HotelCard
import com.example.androidlabs.homeScreen.HomeScreen
import com.example.androidlabs.hotelScreen.HotelInfo
import com.example.androidlabs.profileScreen.profile.Person
@ -21,6 +17,7 @@ import com.example.androidlabs.profileScreen.signIn.LoginScreen
import com.example.androidlabs.profileScreen.signUp.SignUpScreen
import com.example.androidlabs.adminPanel.AddPanel
import com.example.androidlabs.adminPanel.AdminPanel
import com.example.androidlabs.adminPanel.ChangeHotel
import com.example.androidlabs.adminPanel.ChangePanel
import com.google.gson.Gson
@ -61,6 +58,13 @@ fun NavController(navController: NavHostController) {
hotelItem?.let { BookingScreen(orderViewModel, it, navController)
}
}
composable(NavItem.ChangeHotel.route) { backStackEntry ->
val hotelItemString = backStackEntry.arguments?.getString("hotelItem")
val hotelItem = Gson().fromJson(hotelItemString, Hotel::class.java)
hotelItem?.let { ChangeHotel(it, onBackClick = {
navController.navigateUp() })
}
}
composable(NavItem.Person.route) {
Person(navController)
}
@ -68,10 +72,11 @@ fun NavController(navController: NavHostController) {
AdminPanel(navController)
}
composable(NavItem.AddPanel.route){
AddPanel()
AddPanel(navController)
}
composable(NavItem.ChangePanel.route){
ChangePanel(navController)
}
}
}

View File

@ -19,6 +19,6 @@ sealed class NavItem(val route: String, val icon: ImageVector?){
object AdminPanel : NavItem("admin", Icons.Default.Build)
object AddPanel : NavItem("add", null)
object ChangePanel : NavItem("change", null)
// object ChangeHotel : NavItem("changeHotel/{hotelItem}", null)
object ChangeHotel : NavItem("changeHotel/{hotelItem}", null)
}

View File

@ -41,11 +41,15 @@ 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 androidx.navigation.NavHostController
import com.example.androidlabs.DB.models.PhotoManager
import com.example.androidlabs.DB.viewModels.HotelViewModel
import com.example.androidlabs.R
@Composable
fun AddPanel(){
fun AddPanel(navHostController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)){
val photoManager = PhotoManager()
Row(
modifier = Modifier
.fillMaxWidth()
@ -59,7 +63,7 @@ fun AddPanel(){
verticalArrangement = Arrangement.Center
) {
Image(
painter = painterResource(id = R.drawable.img),
painter = painterResource(id = hotelViewModel.img.value),
contentDescription = "image",
contentScale = ContentScale.FillHeight,
modifier = Modifier
@ -73,7 +77,8 @@ fun AddPanel(){
contentColor = Color.White
),
onClick = {
//TODO
hotelViewModel.img.value = photoManager.changePhoto(hotelViewModel.img.value)
},
modifier = Modifier
.fillMaxWidth()
@ -82,10 +87,35 @@ fun AddPanel(){
) {
Text("Add image")
}
TextField(
value = "",
onValueChange = { },
value = hotelViewModel.name.value,
onValueChange = { hotelViewModel.name.value = it },
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
.padding(16.dp, 0.dp)
.border(1.dp, Color.Gray, RoundedCornerShape(4.dp)),
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(
onNext = {
}
),
placeholder = {
Text(
text = "Name",
style = TextStyle(fontSize = 12.sp)
)
}
)
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = hotelViewModel.stars.value,
onValueChange = { hotelViewModel.stars.value = it },
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
@ -111,8 +141,8 @@ fun AddPanel(){
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = "",
onValueChange = { },
value = hotelViewModel.location.value,
onValueChange = { hotelViewModel.location.value = it },
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
@ -138,36 +168,11 @@ fun AddPanel(){
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = "",
onValueChange = { },
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
.padding(16.dp, 0.dp)
.border(1.dp, Color.Gray, RoundedCornerShape(4.dp)),
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(
onNext = {
}
),
placeholder = {
Text(
text = "Info",
style = TextStyle(fontSize = 12.sp)
)
}
)
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = "",
onValueChange = { },
value = hotelViewModel.price.value,
onValueChange = { hotelViewModel.price.value = it },
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
@ -193,8 +198,8 @@ fun AddPanel(){
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = "",
onValueChange = { },
value = hotelViewModel.info.value,
onValueChange = { hotelViewModel.info.value = it },
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
@ -224,7 +229,8 @@ fun AddPanel(){
contentColor = Color.White
),
onClick = {
//
hotelViewModel.insertHotel()
navHostController.navigate("home")
},
modifier = Modifier
.fillMaxWidth()
@ -241,6 +247,6 @@ fun AddPanel(){
@Composable
fun AddPreview() {
AddPanel()
//AddPanel()
}

View File

@ -4,6 +4,8 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
@ -16,13 +18,34 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.androidlabs.DB.models.RoleEnum
import com.example.androidlabs.GlobalUser
import com.example.androidlabs.profileScreen.signIn.LoginScreen
@Composable
fun AdminPanel(navHostController: NavHostController) {
var isAddPanelVisible by remember { mutableStateOf(false) }
var isChangePanelVisible by remember { mutableStateOf(true) }
var showDialog by remember { mutableStateOf(GlobalUser.getInstance().getUser()?.role == RoleEnum.User || GlobalUser.getInstance().getUser()?.role == null) }
if (showDialog) {
AlertDialog(
onDismissRequest = { showDialog = false },
title = {
Text("Access denied")
},
text = {
Text("You are not admin")
},
confirmButton = {
Button(
onClick = { navHostController.navigate("home") }
) {
Text("OK")
}
}
)
}
else{
Column(
modifier = Modifier
.fillMaxSize()
@ -41,13 +64,14 @@ fun AdminPanel(navHostController: NavHostController) {
)
if (isAddPanelVisible) {
AddPanel()
AddPanel(navHostController)
}
if (isChangePanelVisible) {
ChangePanel(navHostController)
}
}
}
}
@Composable

View File

@ -2,7 +2,6 @@ package com.example.androidlabs.adminPanel
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
@ -10,7 +9,13 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Create
import androidx.compose.material.icons.filled.Delete
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@ -22,15 +27,17 @@ import androidx.compose.ui.res.painterResource
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 androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.androidlabs.Hotel
import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.DB.viewModels.HotelViewModel
import com.example.androidlabs.R
import com.google.gson.Gson
@Composable
fun CardHotelForChange(item: Hotel, navController: NavHostController) {
fun CardHotelForChange(item: Hotel, navController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)) {
Row(
modifier = Modifier
.fillMaxWidth()
@ -59,28 +66,39 @@ fun CardHotelForChange(item: Hotel, navController: NavHostController) {
Text(text = "${item.location}", color = Color.Red, fontSize = 16.sp)
}
Image(
painter = painterResource(id = R.drawable.baseline_edit_24),
contentDescription = "image",
modifier = Modifier
.size(40.dp)
.padding(10.dp)
.clickable {
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White
),
onClick = {
hotelViewModel.name.value = item.name ?: ""
hotelViewModel.price.value = item.price.toString()
hotelViewModel.stars.value = item.stars.toString()
hotelViewModel.location.value = item.location ?: ""
hotelViewModel.info.value = item.info ?: ""
val hotelItemString = Gson().toJson(item)
navController.navigate("changeHotel/${hotelItemString}")
}
)
Image(
painter = painterResource(id = R.drawable.baseline_delete_24),
contentDescription = "image",
navController.navigate("changeHotel/${hotelItemString}") },
modifier = Modifier
.size(40.dp)
.padding(10.dp)
.clickable {
.padding(end = 16.dp)
) {
Icon(imageVector = Icons.Default.Create, contentDescription = "change")
}
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White
),
onClick = {
hotelViewModel.deleteHotel(item)
},
modifier = Modifier
.padding(end = 16.dp)
) {
Icon(imageVector = Icons.Default.Delete, contentDescription = "delete")
}
)
}
}
@ -91,5 +109,5 @@ fun CardHotelForChange(item: Hotel, navController: NavHostController) {
@Preview
fun CardHotelLikePreview(){
val navController = rememberNavController()
CardHotelForChange(Hotel("Hotel", R.drawable.img, 5, "location", "info", 4000), navController)
//CardHotelForChange(Hotel("Hotel", R.drawable.img, 5, "location", "info", 4000), navController)
}

View File

@ -0,0 +1,10 @@
package com.example.androidlabs.adminPanel
import androidx.compose.runtime.Composable
import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.DB.viewModels.HotelViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
@Composable
fun ChangeHotel(sneaker: Hotel, onBackClick: () -> Unit, sneakerViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)) {
//TODO
}

View File

@ -14,13 +14,14 @@ 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.androidlabs.DB.viewModels.HotelViewModel
import com.example.androidlabs.Hotel
import com.example.androidlabs.R
@Composable
fun ChangePanel(navHostController: NavHostController){
//val list = sneakerViewModel.SneakerList.collectAsState(initial = emptyList()).value
fun ChangePanel(navHostController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)){
val list = hotelViewModel.HotelList.collectAsState(initial = emptyList()).value
Column(
modifier = Modifier
.fillMaxSize()
@ -32,15 +33,9 @@ fun ChangePanel(navHostController: NavHostController){
modifier = Modifier
.fillMaxSize()
) {
itemsIndexed(
listOf(
Hotel("Hotel", R.drawable.img, 5, "location", "info", 4000),
Hotel("Hotel", R.drawable.img, 5, "location", "info", 4000),
Hotel("Hotel", R.drawable.img, 5, "location", "info", 4000)
)
itemsIndexed(list
){_, item->
CardHotelForChange(item = item, navHostController)
}
}
}