From 6a8a539858d54e4a98e545ab4355c78f128344c3 Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Wed, 1 Nov 2023 18:36:34 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=80=D0=B5=D0=BC=D1=8F=20=D1=81=D0=BE?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D1=8F=D0=B5=D1=82=D1=81=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../androidlabs/Calendar/DatePicker.kt | 79 +++++++++ .../example/androidlabs/DB/models/Order.kt | 4 +- .../DB/viewModels/OrderViewModel.kt | 6 +- .../androidlabs/MyOrderScreen/OrderCard.kt | 6 +- .../androidlabs/booking/BookingScreen.kt | 152 ++++++++++++++---- 5 files changed, 211 insertions(+), 36 deletions(-) create mode 100644 app/src/main/java/com/example/androidlabs/Calendar/DatePicker.kt diff --git a/app/src/main/java/com/example/androidlabs/Calendar/DatePicker.kt b/app/src/main/java/com/example/androidlabs/Calendar/DatePicker.kt new file mode 100644 index 0000000..6c89864 --- /dev/null +++ b/app/src/main/java/com/example/androidlabs/Calendar/DatePicker.kt @@ -0,0 +1,79 @@ +package com.example.androidlabs.Calendar + +import android.app.DatePickerDialog +import android.os.Bundle +import android.widget.DatePicker +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.* +import androidx.compose.material.* +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import java.util.* +@Composable +fun MyDp(){ + + // Fetching the Local Context + val mContext = LocalContext.current + + // Declaring integer values + // for year, month and day + val mYear: Int + val mMonth: Int + val mDay: Int + + // Initializing a Calendar + val mCalendar = Calendar.getInstance() + + // Fetching current year, month and day + mYear = mCalendar.get(Calendar.YEAR) + mMonth = mCalendar.get(Calendar.MONTH) + mDay = mCalendar.get(Calendar.DAY_OF_MONTH) + + mCalendar.time = Date() + + // Declaring a string value to + // store date in string format + val mDate = remember { mutableStateOf("") } + + // Declaring DatePickerDialog and setting + // initial values as current values (present year, month and day) + val mDatePickerDialog = DatePickerDialog( + mContext, + { _: DatePicker, mYear: Int, mMonth: Int, mDayOfMonth: Int -> + mDate.value = "$mDayOfMonth/${mMonth+1}/$mYear" + }, mYear, mMonth, mDay + ) + + Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally) { + + // Creating a button that on + // click displays/shows the DatePickerDialog + Button(onClick = { + mDatePickerDialog.show() + }, colors = ButtonDefaults.buttonColors(backgroundColor = Color(0XFF0F9D58)) ) { + Text(text = "Open Date Picker", color = Color.White) + } + + + // Displaying the mDate value in the Text + Text(text = "Selected Date: ${mDate.value}", fontSize = 30.sp, textAlign = TextAlign.Center) + } +} + +// For displaying preview in +// the Android Studio IDE emulator +@Preview(showBackground = true) +@Composable +fun DefaultPreview() { + MyDp() +} \ No newline at end of file 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 26d0267..2238468 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 @@ -10,9 +10,9 @@ data class Order( @PrimaryKey(autoGenerate = true) val orderId: Int? = null, @ColumnInfo(name = "DateFrom") - val dateFrom: Long, + val dateFrom: String, @ColumnInfo(name = "DateTo") - val dateTo: Long, + val dateTo: String, @ColumnInfo(name = "Rooms") val rooms: Int, @ColumnInfo(name = "Total") 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 5ace80b..0b55f6d 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 @@ -17,6 +17,8 @@ import java.util.Date class OrderViewModel(val database: AppDatabase) : ViewModel() { var selectedItem: Hotel? = null val rooms = mutableStateOf("") + var dateFrom = mutableStateOf("") + var dateTo = mutableStateOf("") fun deleteOrder(order: Order) = viewModelScope.launch { database.orderDao().delete(order) @@ -31,8 +33,8 @@ class OrderViewModel(val database: AppDatabase) : ViewModel() { Log.d("MyLog", GlobalUser.getInstance().getUser()?.userId.toString()) val order = Order( - dateFrom = Date().time, - dateTo = Date().time, + dateFrom = dateFrom.value, + dateTo = dateTo.value, rooms = rooms.value.toInt(), total = getSubTotal(), bookedHotelId = selectedItem?.hotelId!!, diff --git a/app/src/main/java/com/example/androidlabs/MyOrderScreen/OrderCard.kt b/app/src/main/java/com/example/androidlabs/MyOrderScreen/OrderCard.kt index 59f6dfb..d87996a 100644 --- a/app/src/main/java/com/example/androidlabs/MyOrderScreen/OrderCard.kt +++ b/app/src/main/java/com/example/androidlabs/MyOrderScreen/OrderCard.kt @@ -47,8 +47,10 @@ fun OrderCard(order: Order, orderViewModel: OrderViewModel){ verticalArrangement = Arrangement.Center ){ Text("№ ${order.orderId}") - Text("${Date(order.dateFrom)}") - Text("${Date(order.dateTo)}") + Text("From " + order.dateFrom) + Text("To " + order.dateTo) + Text("Sum ${order.total}") + Row(){ diff --git a/app/src/main/java/com/example/androidlabs/booking/BookingScreen.kt b/app/src/main/java/com/example/androidlabs/booking/BookingScreen.kt index 117cada..ef24f53 100644 --- a/app/src/main/java/com/example/androidlabs/booking/BookingScreen.kt +++ b/app/src/main/java/com/example/androidlabs/booking/BookingScreen.kt @@ -1,5 +1,6 @@ package com.example.androidlabs.booking +import android.widget.DatePicker import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.layout.Arrangement @@ -29,16 +30,18 @@ import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType -import androidx.compose.ui.text.input.PasswordVisualTransformation -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.navigation.NavHostController -import androidx.navigation.compose.rememberNavController import com.example.androidlabs.DB.models.Hotel import com.example.androidlabs.DB.viewModels.OrderViewModel import com.example.androidlabs.GlobalUser import com.example.androidlabs.R +import android.app.DatePickerDialog +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.text.style.TextAlign +import java.util.Calendar +import java.util.Date @Composable fun BookingScreen(orderViewModel: OrderViewModel, hotel: Hotel, navHostController: NavHostController) { @@ -51,8 +54,6 @@ fun BookingScreen(orderViewModel: OrderViewModel, hotel: Hotel, navHostControlle verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { - var rooms by remember { mutableStateOf("") } - var date by remember { mutableStateOf("") } Text( text = "Booking", @@ -90,47 +91,138 @@ fun BookingScreen(orderViewModel: OrderViewModel, hotel: Hotel, navHostControlle Spacer(modifier = Modifier.height(16.dp)) - TextField( - value = date, - onValueChange = { date = it }, +// Fetching the Local Context + val mContext = LocalContext.current + + // Declaring integer values + // for year, month and day + val mYearFrom: Int + val mMonthFrom: Int + val mDayFrom: Int + + // Initializing a Calendar + val mCalendarFrom = Calendar.getInstance() + + // Fetching current year, month and day + mYearFrom = mCalendarFrom.get(Calendar.YEAR) + mMonthFrom = mCalendarFrom.get(Calendar.MONTH) + mDayFrom = mCalendarFrom.get(Calendar.DAY_OF_MONTH) + + mCalendarFrom.time = Date() + + // Declaring a string value to + // store date in string format + val mDateFrom = remember { mutableStateOf("") } + + // Declaring DatePickerDialog and setting + // initial values as current values (present year, month and day) + val mDatePickerDialogFrom = DatePickerDialog( + mContext, + { _: DatePicker, mYear: Int, mMonth: Int, mDayOfMonth: Int -> + mDateFrom.value = "$mDayOfMonth/${mMonth+1}/$mYear" + }, mYearFrom, mMonthFrom, mDayFrom + ) + + + // Creating a button that on + // click displays/shows the DatePickerDialog + Button( + colors = ButtonDefaults.buttonColors( + backgroundColor = (colorResource(id = R.color.figma_blue)), + contentColor = Color.White + ), + onClick = { + mDatePickerDialogFrom.show() + + }, modifier = Modifier .fillMaxWidth() + .padding(16.dp, 16.dp, 16.dp, 0.dp) .height(50.dp) - .padding(16.dp, 0.dp) - .border(1.dp, Color.Gray, RoundedCornerShape(4.dp)), - singleLine = true, - visualTransformation = PasswordVisualTransformation(), - keyboardOptions = KeyboardOptions( - keyboardType = KeyboardType.Text, - imeAction = ImeAction.Next - ), + ) { + Text("Open Date Picker") + } - keyboardActions = KeyboardActions( - onNext = { - } - ), - placeholder = { - Text( - text = "Date", - style = TextStyle(fontSize = 12.sp) - ) - } + + // Displaying the mDate value in the Text + Text(text = "Selected Date From: ${mDateFrom.value}", fontSize = 15.sp) + + + Spacer(modifier = Modifier.height(16.dp)) + + +// Fetching the Local Context + + // Declaring integer values + // for year, month and day + val mYear: Int + val mMonth: Int + val mDay: Int + + // Initializing a Calendar + val mCalendar = Calendar.getInstance() + + // Fetching current year, month and day + mYear = mCalendar.get(Calendar.YEAR) + mMonth = mCalendar.get(Calendar.MONTH) + mDay = mCalendar.get(Calendar.DAY_OF_MONTH) + + mCalendar.time = Date() + + // Declaring a string value to + // store date in string format + val mDate = remember { mutableStateOf("") } + + // Declaring DatePickerDialog and setting + // initial values as current values (present year, month and day) + val mDatePickerDialog = DatePickerDialog( + mContext, + { _: DatePicker, mYear: Int, mMonth: Int, mDayOfMonth: Int -> + mDate.value = "$mDayOfMonth/${mMonth+1}/$mYear" + }, mYear, mMonth, mDay ) + + // Creating a button that on + // click displays/shows the DatePickerDialog + Button( colors = ButtonDefaults.buttonColors( backgroundColor = (colorResource(id = R.color.figma_blue)), contentColor = Color.White ), onClick = { - //if(GlobalUser.getInstance().getUser() != null){ + mDatePickerDialog.show() + + }, + modifier = Modifier + .fillMaxWidth() + .padding(16.dp, 16.dp, 16.dp, 0.dp) + .height(50.dp) + ) { + Text("Open Date Picker") + } + + // Displaying the mDate value in the Text + Text(text = "Selected Date To: ${mDate.value}", fontSize = 15.sp,) + + + Button( + colors = ButtonDefaults.buttonColors( + backgroundColor = (colorResource(id = R.color.figma_blue)), + contentColor = Color.White + ), + onClick = { + if(GlobalUser.getInstance().getUser() != null){ orderViewModel.selectedItem = hotel + orderViewModel.dateFrom = mDateFrom + orderViewModel.dateTo = mDate orderViewModel.createOrder() navHostController.navigate("home") - //}else{ - // navHostController.navigate("login") - // } + }else{ + navHostController.navigate("login") + } }, modifier = Modifier .fillMaxWidth()