заказы показываются

This commit is contained in:
VictoriaPresnyakova 2023-11-01 13:58:14 +04:00
parent 4c013ac345
commit 71743e9166
9 changed files with 157 additions and 5 deletions

View File

@ -11,7 +11,7 @@ class App : Application() {
override fun onCreate() {
super.onCreate()
// this.deleteDatabase("my-db")
//this.deleteDatabase("my-db")
CoroutineScope(Dispatchers.IO).launch {
AppDatabase.populateDatabase()
}

View File

@ -1,6 +1,7 @@
package com.example.androidlabs.DB.models
import androidx.room.ColumnInfo
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
@ -19,5 +20,7 @@ data class Order(
@ColumnInfo(name = "CreatorUserId")
val creatorUserId: Int,
@ColumnInfo(name = "BookedHotelId")
val bookedHotelId: Int
val bookedHotelId: Int,
@Embedded
val hotel: Hotel
)

View File

@ -1,5 +1,6 @@
package com.example.androidlabs.DB.viewModels
import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
@ -27,14 +28,16 @@ class OrderViewModel(val database: AppDatabase) : ViewModel() {
fun createOrder() = viewModelScope.launch {
Log.d("MyLog", GlobalUser.getInstance().getUser()?.userId.toString())
val order = Order(
dateFrom = Date().time,
dateTo = Date().time,
rooms = rooms.value.toInt(),
total = getSubTotal(),
bookedHotelId = selectedItem?.hotelId!!,
//creatorUserId = 1
creatorUserId = GlobalUser.getInstance().getUser()?.userId!!
creatorUserId = GlobalUser.getInstance().getUser()?.userId!!,
hotel = selectedItem!!
)
val orderId = database.orderDao().createOrder(order)

View File

@ -1,5 +1,6 @@
package com.example.androidlabs.DB.viewModels
import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
@ -33,7 +34,9 @@ class UserViewModel(val database: AppDatabase): ViewModel() {
if (password.value != "" && user.password == password.value) {
val globalUser = GlobalUser.getInstance()
globalUser.setUser(user)
Log.d("MyLog", GlobalUser.getInstance().getUser()?.userId.toString())
println()
}
}
fun isValidEmail(email: String): Boolean {

View File

@ -12,7 +12,6 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
setContent {
Navigate()
//HotelInfo(Hotel("hotel", R.drawable.img_1, 4, "location"))
}
}
}

View File

@ -0,0 +1,57 @@
package com.example.androidlabs.MyOrderScreen
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.example.androidlabs.DB.models.Order
import com.example.androidlabs.DB.viewModels.OrderViewModel
import com.example.androidlabs.GlobalUser
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
@Composable
fun MyOrderScreen(orderViewModel: OrderViewModel) {
val userWithOrder by orderViewModel.database.userDao().getUserOrders(GlobalUser.getInstance().getUser()?.userId!!).collectAsState(null)
val orderList: List<Order>? = userWithOrder?.orders
println()
Column(
modifier = Modifier
.padding(bottom = 50.dp)
.fillMaxSize()
.background(Color.White)
.verticalScroll(rememberScrollState())
){
Text(
text = "My order",
style = MaterialTheme.typography.h5,
fontWeight = FontWeight.Bold,
modifier = Modifier
.padding(10.dp, 10.dp)
)
Row {
Column(
modifier = Modifier
.fillMaxSize()
) {
if (orderList != null) {
for (item in orderList) {
OrderCard(item, orderViewModel)
}
}
}
}
}
}

View File

@ -0,0 +1,82 @@
package com.example.androidlabs.MyOrderScreen
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
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.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.example.androidlabs.DB.AppDatabase
import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.DB.models.Order
import com.example.androidlabs.DB.viewModels.OrderViewModel
import com.example.androidlabs.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.Date
@Composable
fun OrderCard(order: Order, orderViewModel: OrderViewModel){
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp, 16.dp, 16.dp, 0.dp)
.clip(RoundedCornerShape(16.dp))
.background(colorResource(id = R.color.figma))
){
Column(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
verticalArrangement = Arrangement.Center
){
Text("${order.orderId}")
Text("${Date(order.dateFrom)}")
Text("${Date(order.dateTo)}")
Row(){
Image(
contentScale = ContentScale.FillBounds,
painter = painterResource(id =order.hotel.img),
contentDescription = null,
modifier = Modifier
.size(70.dp)
.padding(0.dp, 10.dp, 10.dp, 10.dp)
)
}
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White
),
onClick = {
orderViewModel.deleteOrder(order)
},
modifier = Modifier
.fillMaxWidth()
.padding(16.dp, 16.dp, 16.dp, 0.dp)
) {
Text("Cancel")
}
}
}
}

View File

@ -9,6 +9,7 @@ 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
@ -41,6 +42,9 @@ fun NavController(navController: NavHostController) {
composable(NavItem.Home.route) {
HomeScreen(navController)
}
composable(NavItem.MyOrder.route){
MyOrderScreen(orderViewModel)
}
composable(NavItem.Profile.route) {
ProfileScreen(navController)
}

View File

@ -9,6 +9,7 @@ import androidx.compose.ui.graphics.vector.ImageVector
sealed class NavItem(val route: String, val icon: ImageVector?){
object Home : NavItem("home", Icons.Default.Home)
object MyOrder : NavItem("myorder", null)
object Profile : NavItem("profile", Icons.Default.Person)
object SignIn : NavItem("login", null)
object SignUp : NavItem("signup", null)