осталось список заказов и панельку админа

This commit is contained in:
VictoriaPresnyakova 2023-10-31 17:32:36 +04:00
parent 25e1d97db5
commit 4c013ac345
15 changed files with 363 additions and 180 deletions

View File

@ -0,0 +1,60 @@
package com.example.androidlabs.DB.viewModels
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 com.example.androidlabs.App
import com.example.androidlabs.DB.AppDatabase
import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.R
import kotlinx.coroutines.launch
class HotelViewModel(val database: AppDatabase): ViewModel() {
var name = mutableStateOf("")
val price = mutableStateOf("")
val location = mutableStateOf("")
val stars = mutableStateOf("")
val info = mutableStateOf("")
val img = mutableStateOf(R.drawable.img)
val HotelList = database.hotelDao().getAllHotelss()
var hotel: Hotel? = null
fun insertHotel() = viewModelScope.launch {
val hotel = Hotel(
name = name.value,
location = location.value,
price = price.value.toDouble(),
img = img.value,
stars = stars.value.toInt(),
info = info.value
)
database.hotelDao().insert(hotel)
}
fun deleteHotel(hotel : Hotel) = viewModelScope.launch {
database.hotelDao().delete(hotel)
}
fun getHotelById(id: Int) = viewModelScope.launch {
database.hotelDao().getHotelById(id)
}
fun UpdateHotel(hotel: Hotel) = viewModelScope.launch {
database.hotelDao().update(hotel)
}
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 HotelViewModel(database) as T
}
}
}
}

View File

@ -0,0 +1,62 @@
package com.example.androidlabs.DB.viewModels
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras
import com.example.androidlabs.App
import com.example.androidlabs.DB.AppDatabase
import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.DB.models.Order
import com.example.androidlabs.GlobalUser
import kotlinx.coroutines.launch
import java.util.Date
class OrderViewModel(val database: AppDatabase) : ViewModel() {
var selectedItem: Hotel? = null
val rooms = mutableStateOf("")
fun deleteOrder(order: Order) = viewModelScope.launch {
database.orderDao().delete(order)
}
fun getOrderList(id: Int) = viewModelScope.launch {
database.userDao().getUserOrders(id)
}
fun createOrder() = viewModelScope.launch {
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!!
)
val orderId = database.orderDao().createOrder(order)
rooms.value = ""
selectedItem = null
}
fun getSubTotal(): Double {
return selectedItem!!.price * rooms.value.toInt()
}
companion object{
val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras
): T {
val database = (checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as App).database
return OrderViewModel(database) as T
}
}
}
}

View File

@ -0,0 +1,54 @@
package com.example.androidlabs.DB.viewModels
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras
import com.example.androidlabs.App
import com.example.androidlabs.DB.AppDatabase
import com.example.androidlabs.DB.models.RoleEnum
import com.example.androidlabs.DB.models.User
import com.example.androidlabs.GlobalUser
import kotlinx.coroutines.launch
class UserViewModel(val database: AppDatabase): ViewModel() {
var name = mutableStateOf("")
val surname = mutableStateOf("")
val email = mutableStateOf("")
val password = mutableStateOf("")
fun createUser() = viewModelScope.launch {
val user = User(
name = name.value,
surname = surname.value,
email = email.value,
password = password.value,
role = RoleEnum.User
)
database.userDao().createUser(user)
}
fun authUser() = viewModelScope.launch {
val user = database.userDao().getUserByEmail(email.value)
if (password.value != "" && user.password == password.value) {
val globalUser = GlobalUser.getInstance()
globalUser.setUser(user)
println()
}
}
fun isValidEmail(email: String): Boolean {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
}
companion object{
val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory{
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras
): T {
val database = (checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as App).database
return UserViewModel(database) as T
}
}
}
}

View File

@ -1,10 +1,14 @@
package com.example.androidlabs.Navigation package com.example.androidlabs.Navigation
import android.util.Log
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
import com.example.androidlabs.Hotel 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.R import com.example.androidlabs.R
import com.example.androidlabs.booking.BookingScreen import com.example.androidlabs.booking.BookingScreen
import com.example.androidlabs.homeScreen.CardItem.HotelCard import com.example.androidlabs.homeScreen.CardItem.HotelCard
@ -17,15 +21,22 @@ import com.example.androidlabs.profileScreen.signUp.SignUpScreen
import com.example.androidlabs.adminPanel.AddPanel import com.example.androidlabs.adminPanel.AddPanel
import com.example.androidlabs.adminPanel.AdminPanel import com.example.androidlabs.adminPanel.AdminPanel
import com.example.androidlabs.adminPanel.ChangePanel import com.example.androidlabs.adminPanel.ChangePanel
import com.google.gson.Gson
@Composable @Composable
fun NavController(navController: NavHostController) { fun NavController(navController: NavHostController) {
var orderViewModel: OrderViewModel = viewModel(factory = OrderViewModel.factory)
NavHost( NavHost(
navController = navController, navController = navController,
startDestination = NavItem.Home.route startDestination = NavItem.Home.route
) { ) {
composable(NavItem.HotelInfo.route) { composable(NavItem.HotelInfo.route) {
HotelInfo(Hotel("hotel", R.drawable.img_1, 4, "location", "info", 4000), navController) //
backStackEntry ->
val hotelItemString = backStackEntry.arguments?.getString("hotelItem")
val hotelItem = Gson().fromJson(hotelItemString, Hotel::class.java)
hotelItem?.let { HotelInfo(it, navController)
}
} }
composable(NavItem.Home.route) { composable(NavItem.Home.route) {
HomeScreen(navController) HomeScreen(navController)
@ -37,13 +48,17 @@ fun NavController(navController: NavHostController) {
LoginScreen(navController) LoginScreen(navController)
} }
composable(NavItem.SignUp.route){ composable(NavItem.SignUp.route){
SignUpScreen() SignUpScreen(navController)
} }
composable(NavItem.Booking.route) { composable(NavItem.Booking.route) {
BookingScreen() backStackEntry ->
val hotelItemString = backStackEntry.arguments?.getString("hotelItem")
val hotelItem = Gson().fromJson(hotelItemString, Hotel::class.java)
hotelItem?.let { BookingScreen(orderViewModel, it, navController)
}
} }
composable(NavItem.Person.route) { composable(NavItem.Person.route) {
Person() Person(navController)
} }
composable(NavItem.AdminPanel.route){ composable(NavItem.AdminPanel.route){
AdminPanel(navController) AdminPanel(navController)

View File

@ -12,8 +12,8 @@ sealed class NavItem(val route: String, val icon: ImageVector?){
object Profile : NavItem("profile", Icons.Default.Person) object Profile : NavItem("profile", Icons.Default.Person)
object SignIn : NavItem("login", null) object SignIn : NavItem("login", null)
object SignUp : NavItem("signup", null) object SignUp : NavItem("signup", null)
object HotelInfo : NavItem("HotelInfo", null) object HotelInfo : NavItem("HotelInfo/{hotelItem}", null)
object Booking : NavItem("booking", null) object Booking : NavItem("booking/{hotelItem}", null)
object Person : NavItem("person", null) object Person : NavItem("person", null)
object AdminPanel : NavItem("admin", Icons.Default.Build) object AdminPanel : NavItem("admin", Icons.Default.Build)
object AddPanel : NavItem("add", null) object AddPanel : NavItem("add", null)

View File

@ -10,7 +10,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Button import androidx.compose.material.Button
@ -34,12 +33,15 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController 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 com.example.androidlabs.R
import com.example.androidlabs.profileScreen.signIn.LoginScreen
@Composable @Composable
fun BookingScreen() { fun BookingScreen(orderViewModel: OrderViewModel, hotel: Hotel, navHostController: NavHostController) {
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
@ -49,7 +51,7 @@ fun BookingScreen() {
verticalArrangement = Arrangement.Center, verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
var room by remember { mutableStateOf("") } var rooms by remember { mutableStateOf("") }
var date by remember { mutableStateOf("") } var date by remember { mutableStateOf("") }
Text( Text(
@ -61,8 +63,8 @@ fun BookingScreen() {
) )
TextField( TextField(
value = room, value = orderViewModel.rooms.value,
onValueChange = { room = it }, onValueChange = { orderViewModel.rooms.value = it},
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -80,7 +82,7 @@ fun BookingScreen() {
), ),
placeholder = { placeholder = {
Text( Text(
text = "Room", text = "Rooms",
style = TextStyle(fontSize = 12.sp) style = TextStyle(fontSize = 12.sp)
) )
} }
@ -122,21 +124,27 @@ fun BookingScreen() {
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
//if(GlobalUser.getInstance().getUser() != null){
orderViewModel.selectedItem = hotel
orderViewModel.createOrder()
navHostController.navigate("home")
//}else{
// navHostController.navigate("login")
// }
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(16.dp, 16.dp, 16.dp, 0.dp) .padding(16.dp, 16.dp, 16.dp, 0.dp)
.height(50.dp) .height(50.dp)
) { ) {
Text("Payment") Text("Book")
} }
} }
} }
@Composable //@Composable
@Preview(showBackground = true) //@Preview(showBackground = true)
fun BookingScreenPreview(){ //fun BookingScreenPreview(){
val navController = rememberNavController() // val navController = rememberNavController()
BookingScreen() // BookingScreen()
} //}

View File

@ -26,8 +26,9 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.androidlabs.Hotel import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.R import com.example.androidlabs.R
import com.google.gson.Gson
@Composable @Composable
fun HotelCard (hotel: Hotel, navController: NavHostController){ fun HotelCard (hotel: Hotel, navController: NavHostController){
@ -36,7 +37,8 @@ fun HotelCard (hotel: Hotel, navController: NavHostController){
.fillMaxWidth() .fillMaxWidth()
.padding(10.dp) .padding(10.dp)
.clickable { .clickable {
navController.navigate("HotelInfo") val hotelItemString = Gson().toJson(hotel)
navController.navigate("HotelInfo/${hotelItemString}")
}, },
shape = RoundedCornerShape(15.dp), shape = RoundedCornerShape(15.dp),
elevation = 5.dp elevation = 5.dp

View File

@ -9,6 +9,8 @@ import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
@ -21,9 +23,11 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.androidlabs.DB.AppDatabase import com.example.androidlabs.DB.AppDatabase
import com.example.androidlabs.DB.viewModels.HotelViewModel
import com.example.androidlabs.R import com.example.androidlabs.R
import com.example.androidlabs.homeScreen.CardItem.HotelCard import com.example.androidlabs.homeScreen.CardItem.HotelCard
import com.example.androidlabs.Hotel import com.example.androidlabs.Hotel
@ -31,14 +35,14 @@ import com.example.androidlabs.homeScreen.SearchField.SearchField
import kotlinx.coroutines.flow.count import kotlinx.coroutines.flow.count
@Composable @Composable
fun HomeScreen(navController: NavHostController) { fun HomeScreen(navController: NavHostController, hotelViewModel: HotelViewModel = viewModel(factory = HotelViewModel.factory)) {
val context = LocalContext.current val list = hotelViewModel.HotelList.collectAsState(initial = emptyList()).value
val List = AppDatabase.getInstance(context).hotelDao().getAllHotelss().collectAsState(initial = emptyList()).value //Log.d("MyLog", list.toString())
Log.d("MyLog", List.toString())
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.background(Color.White) .background(Color.White)
//.padding(bottom = 60.dp)
) { ) {
Box(modifier = Modifier Box(modifier = Modifier
.background(colorResource(id = R.color.figma_blue)) .background(colorResource(id = R.color.figma_blue))
@ -62,12 +66,14 @@ fun HomeScreen(navController: NavHostController) {
} }
} }
} }
LazyColumn ( Column (
modifier = Modifier modifier = Modifier
//.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
.padding(bottom = 60.dp)
){ ){
items(count = 100 ){ for (item in list){
HotelCard(Hotel("hotel", R.drawable.img, it % 6, "location", "info", 4000), navController) HotelCard(item, navController)
} }
} }

View File

@ -1,5 +1,6 @@
package com.example.androidlabs.hotelScreen package com.example.androidlabs.hotelScreen
import android.util.Log
import androidx.compose.animation.shrinkVertically import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.ScrollState import androidx.compose.foundation.ScrollState
@ -34,12 +35,15 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.androidlabs.Hotel import com.example.androidlabs.DB.models.Hotel
import com.example.androidlabs.R import com.example.androidlabs.R
import com.example.androidlabs.homeScreen.CardItem.HotelCard import com.example.androidlabs.homeScreen.CardItem.HotelCard
import com.google.gson.Gson
@Composable @Composable
fun HotelInfo(hotel: Hotel, navController: NavHostController) { fun HotelInfo(hotel: Hotel, navController: NavHostController) {
Log.d("MyLog", hotel.toString())
Column ( Column (
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -143,7 +147,8 @@ fun HotelInfo(hotel: Hotel, navController: NavHostController) {
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
navController.navigate("booking") val hotelItemString = Gson().toJson(hotel)
navController.navigate("booking/${hotelItemString}")
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -161,6 +166,6 @@ fun HotelInfo(hotel: Hotel, navController: NavHostController) {
@Composable @Composable
fun HotelInfoPreview() { fun HotelInfoPreview() {
val navController = rememberNavController() val navController = rememberNavController()
HotelInfo(Hotel("hotel", R.drawable.img_1, 4, "location", "info", 4000), navController) // HotelInfo(Hotel("hotel", R.drawable.img_1, 4, "location", "info", 4000), navController)
} }

View File

@ -11,6 +11,8 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
@ -18,26 +20,46 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.androidlabs.GlobalUser
import com.example.androidlabs.R
@Composable @Composable
fun Person() { fun Person(navHostController: NavHostController) {
Column( Column(
modifier = Modifier modifier = Modifier
.background(Color.White) .background(Color.White)
.fillMaxSize(), .fillMaxSize(),
verticalArrangement = Arrangement.Center verticalArrangement = Arrangement.Center
) { ) {
ProfileCard() Button(
onClick = {
GlobalUser.getInstance().setUser(null)
navHostController.navigate("profile")
},
colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.white),
contentColor = Color.Black
),
modifier = Modifier
.padding(16.dp, 0.dp)
) {
Text("Exit")
}
ProfileCard(navHostController)
} }
} }
@Preview @Preview
@Composable @Composable
fun PersonPreview(){ fun PersonPreview(){
Person() val navController = rememberNavController()
Person(navController)
} }

View File

@ -13,6 +13,8 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
@ -27,17 +29,19 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.example.androidlabs.GlobalUser
import com.example.androidlabs.R import com.example.androidlabs.R
@Preview
@Composable @Composable
fun ProfileCard() { fun ProfileCard(navHostController: NavHostController) {
val globalUser = GlobalUser.getInstance().getUser()
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(16.dp) .padding(16.dp)
.clip(RoundedCornerShape(16.dp)) .clip(RoundedCornerShape(16.dp))
.background(Color.LightGray) .background(colorResource(id = R.color.figma))
){ ){
Column( Column(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
@ -55,17 +59,32 @@ fun ProfileCard() {
.clip(CircleShape) .clip(CircleShape)
) )
Text( if (globalUser != null) {
text = "User", Text(
fontSize = 18.sp, text = "${globalUser.name} ${globalUser.surname}",
fontWeight = FontWeight.Bold fontSize = 18.sp,
) fontWeight = FontWeight.Bold
)
Text( Text(
text = "user@example.com", text = globalUser.email,
fontSize = 16.sp, fontSize = 16.sp,
color = Color.Gray color = Color.Gray
) )
}
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White
),
onClick = {
navHostController.navigate("myorder")
},
modifier = Modifier
.fillMaxWidth()
.padding(16.dp, 16.dp, 16.dp, 0.dp)
) {
Text("My order")
}
} }
} }
} }

View File

@ -25,78 +25,22 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import com.example.androidlabs.GlobalUser
import com.example.androidlabs.R import com.example.androidlabs.R
import com.example.androidlabs.profileScreen.signIn.LoginScreen
@Composable @Composable
fun ProfileScreen(navController: NavHostController) { fun ProfileScreen(navController: NavHostController) {
Column( val globalUser: GlobalUser = GlobalUser.getInstance()
modifier = Modifier if(globalUser.getUser() != null){
.fillMaxWidth() Person(navController)
.fillMaxHeight() }else{
.padding(16.dp), LoginScreen(navController = navController)
verticalArrangement = Arrangement.Center,
){
Row(
){
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = (colorResource(id = R.color.figma_blue)),
contentColor = Color.White
),
onClick = {
navController.navigate("person")
},
modifier = Modifier
.fillMaxWidth()
.padding(16.dp, 0.dp, 16.dp, 0.dp)
.height(50.dp)
) {
Text(text = "Profile")
}
}
Spacer(modifier = Modifier.height(16.dp))
Row(
){
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = (colorResource(id = R.color.figma_blue)),
contentColor = Color.White
),
onClick = {
navController.navigate("login")
},
modifier = Modifier
.fillMaxWidth()
.padding(16.dp, 0.dp, 16.dp, 0.dp)
.height(50.dp)
) {
Text(text = "Sign In")
}
}
Spacer(modifier = Modifier.height(16.dp))
Row(
){
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = (colorResource(id = R.color.figma_blue)),
contentColor = Color.White
),
onClick = {
navController.navigate("signup")
},
modifier = Modifier
.fillMaxWidth()
.padding(16.dp, 0.dp, 16.dp, 0.dp)
.height(50.dp)
) {
Text(text = "Sign Up")
}
}
} }
} }
@Composable @Composable
@Preview(showBackground = true) @Preview
fun ProfileScreenPreview(){ fun ProfileScreenPreview(){
val navController = rememberNavController() val navController = rememberNavController()
ProfileScreen(navController = navController) ProfileScreen(navController = navController)

View File

@ -37,12 +37,14 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.androidlabs.DB.viewModels.UserViewModel
import com.example.androidlabs.R import com.example.androidlabs.R
@Composable @Composable
fun SignInCard(navController: NavHostController) { fun SignInCard(navController: NavHostController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)) {
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -55,8 +57,8 @@ fun SignInCard(navController: NavHostController) {
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center verticalArrangement = Arrangement.Center
){ ){
var username by remember { mutableStateOf("") } var isEmailValid by remember { mutableStateOf(true) }
var password by remember { mutableStateOf("") } var isPasswordValid by remember { mutableStateOf(true) }
Text( Text(
text = "Sign In", text = "Sign In",
@ -67,8 +69,10 @@ fun SignInCard(navController: NavHostController) {
) )
TextField( TextField(
value = username, value = userViewModel.email.value,
onValueChange = { username = it }, onValueChange = {
userViewModel.email.value = it
isEmailValid = userViewModel.isValidEmail(it)},
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -91,12 +95,21 @@ fun SignInCard(navController: NavHostController) {
) )
} }
) )
if (!isEmailValid) {
Text(
text = "Invalid email format",
color = Color.Red,
style = TextStyle(fontSize = 12.sp)
)
}
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = password, value = userViewModel.password.value,
onValueChange = { password = it }, onValueChange = {
userViewModel.password.value = it
isPasswordValid = it.isNotEmpty()
},
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -121,14 +134,21 @@ fun SignInCard(navController: NavHostController) {
) )
} }
) )
if (!isPasswordValid) {
Text(
text = "Password is required",
color = Color.Red,
style = TextStyle(fontSize = 12.sp)
)
}
Button( Button(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = (colorResource(id = R.color.figma_blue)), backgroundColor = (colorResource(id = R.color.figma_blue)),
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
userViewModel.authUser()
navController.navigate("person")
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()

View File

@ -34,18 +34,15 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.androidlabs.DB.viewModels.UserViewModel
import com.example.androidlabs.R import com.example.androidlabs.R
import com.example.androidlabs.profileScreen.signIn.LoginScreen import com.example.androidlabs.profileScreen.signIn.LoginScreen
@Composable @Composable
@Preview fun SignUpCard(navHostController: NavHostController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)) {
fun SignUpCard() {
var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var sex by remember { mutableStateOf("") }
var name by remember { mutableStateOf("") }
var surname by remember { mutableStateOf("") }
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -68,36 +65,8 @@ fun SignUpCard() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = username, value = userViewModel.name.value,
onValueChange = { username = it }, onValueChange = { userViewModel.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 = "Username",
style = TextStyle(fontSize = 12.sp)
)
}
)
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = name,
onValueChange = { name = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -124,8 +93,8 @@ fun SignUpCard() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = surname, value = userViewModel.surname.value,
onValueChange = { surname = it }, onValueChange = { userViewModel.surname.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -152,8 +121,8 @@ fun SignUpCard() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = sex, value = userViewModel.email.value,
onValueChange = { sex = it }, onValueChange = { userViewModel.email.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -171,7 +140,7 @@ fun SignUpCard() {
), ),
placeholder = { placeholder = {
Text( Text(
text = "Sex", text = "Email",
style = TextStyle(fontSize = 12.sp) style = TextStyle(fontSize = 12.sp)
) )
} }
@ -180,20 +149,18 @@ fun SignUpCard() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = password, value = userViewModel.password.value,
onValueChange = { password = it }, onValueChange = { userViewModel.password.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
.padding(16.dp, 0.dp) .padding(16.dp, 0.dp)
.border(1.dp, Color.Gray, RoundedCornerShape(4.dp)), .border(1.dp, Color.Gray, RoundedCornerShape(4.dp)),
singleLine = true, singleLine = true,
visualTransformation = PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions( keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Text, keyboardType = KeyboardType.Text,
imeAction = ImeAction.Next imeAction = ImeAction.Next
), ),
keyboardActions = KeyboardActions( keyboardActions = KeyboardActions(
onNext = { onNext = {
@ -207,13 +174,16 @@ fun SignUpCard() {
} }
) )
Spacer(modifier = Modifier.height(16.dp))
Button( Button(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = (colorResource(id = R.color.figma_blue)), backgroundColor = (colorResource(id = R.color.figma_blue)),
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
userViewModel.createUser()
navHostController.navigate("login")
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -226,8 +196,3 @@ fun SignUpCard() {
} }
} }
@Composable
@Preview(showBackground = true)
fun SignUpScreenPreview(){
SignUpScreen()
}

View File

@ -34,14 +34,15 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
@Composable @Composable
fun SignUpScreen() { fun SignUpScreen(navHostController: NavHostController) {
Column(modifier = Modifier Column(modifier = Modifier
.fillMaxSize() .fillMaxSize()
.background(Color.White), .background(Color.White),
verticalArrangement = Arrangement.Center verticalArrangement = Arrangement.Center
) { ) {
SignUpCard() SignUpCard(navHostController)
} }
} }