diff --git a/app/src/main/java/com/example/androidlabs/App.kt b/app/src/main/java/com/example/androidlabs/App.kt index 56c98a5..592737b 100644 --- a/app/src/main/java/com/example/androidlabs/App.kt +++ b/app/src/main/java/com/example/androidlabs/App.kt @@ -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() } diff --git a/app/src/main/java/com/example/androidlabs/DB/models/Order.kt b/app/src/main/java/com/example/androidlabs/DB/models/Order.kt index 671bc68..26d0267 100644 --- a/app/src/main/java/com/example/androidlabs/DB/models/Order.kt +++ b/app/src/main/java/com/example/androidlabs/DB/models/Order.kt @@ -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 ) \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/DB/viewModels/OrderViewModel.kt b/app/src/main/java/com/example/androidlabs/DB/viewModels/OrderViewModel.kt index c17eeb6..5ace80b 100644 --- a/app/src/main/java/com/example/androidlabs/DB/viewModels/OrderViewModel.kt +++ b/app/src/main/java/com/example/androidlabs/DB/viewModels/OrderViewModel.kt @@ -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) diff --git a/app/src/main/java/com/example/androidlabs/DB/viewModels/UserViewModel.kt b/app/src/main/java/com/example/androidlabs/DB/viewModels/UserViewModel.kt index 79a4603..d05e93f 100644 --- a/app/src/main/java/com/example/androidlabs/DB/viewModels/UserViewModel.kt +++ b/app/src/main/java/com/example/androidlabs/DB/viewModels/UserViewModel.kt @@ -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 { diff --git a/app/src/main/java/com/example/androidlabs/MainActivity.kt b/app/src/main/java/com/example/androidlabs/MainActivity.kt index ae8084f..4dbf286 100644 --- a/app/src/main/java/com/example/androidlabs/MainActivity.kt +++ b/app/src/main/java/com/example/androidlabs/MainActivity.kt @@ -12,7 +12,6 @@ class MainActivity : ComponentActivity() { super.onCreate(savedInstanceState) setContent { Navigate() - //HotelInfo(Hotel("hotel", R.drawable.img_1, 4, "location")) } } } diff --git a/app/src/main/java/com/example/androidlabs/MyOrderScreen/MyOrderScreen.kt b/app/src/main/java/com/example/androidlabs/MyOrderScreen/MyOrderScreen.kt new file mode 100644 index 0000000..cbaf40f --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/MyOrderScreen/MyOrderScreen.kt @@ -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? = 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) + } + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/MyOrderScreen/OrderCard.kt b/app/src/main/java/com/example/androidlabs/MyOrderScreen/OrderCard.kt new file mode 100644 index 0000000..59f6dfb --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/MyOrderScreen/OrderCard.kt @@ -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") + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidlabs/Navigation/NavController.kt b/app/src/main/java/com/example/androidlabs/Navigation/NavController.kt index a9e6447..ba05814 100644 --- a/app/src/main/java/com/example/androidlabs/Navigation/NavController.kt +++ b/app/src/main/java/com/example/androidlabs/Navigation/NavController.kt @@ -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) } diff --git a/app/src/main/java/com/example/androidlabs/Navigation/NavItem.kt b/app/src/main/java/com/example/androidlabs/Navigation/NavItem.kt index d4a2205..456c801 100644 --- a/app/src/main/java/com/example/androidlabs/Navigation/NavItem.kt +++ b/app/src/main/java/com/example/androidlabs/Navigation/NavItem.kt @@ -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)