Fix: valid some input, delete not used import
This commit is contained in:
parent
c6bc40d3ff
commit
74007b5979
@ -73,6 +73,8 @@ dependencies {
|
||||
implementation ("io.coil-kt:coil-compose:1.4.0")
|
||||
implementation ("com.google.code.gson:gson:2.8.8")
|
||||
implementation("androidx.navigation:navigation-compose:2.7.4")
|
||||
implementation ("androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0-alpha02")
|
||||
|
||||
|
||||
//ROOM
|
||||
val room_version = "2.5.2"
|
||||
|
@ -2,7 +2,18 @@ package com.example.android_programming
|
||||
|
||||
import android.app.Application
|
||||
import com.example.android_programming.database.AppDatabase
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
public class App : Application() {
|
||||
class App : Application() {
|
||||
val database by lazy { AppDatabase.getInstance(this)}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
// this.deleteDatabase("my-db")
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
AppDatabase.populateDatabase()
|
||||
}
|
||||
}
|
||||
}
|
@ -6,17 +6,10 @@ import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import com.example.android_programming.composeui.Header.Header
|
||||
import com.example.android_programming.composeui.Navigation.Navigate
|
||||
import com.example.android_programming.database.AppDatabase
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import com.example.android_programming.model.User
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@ -29,21 +22,33 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
@Composable
|
||||
fun MainContent() {
|
||||
val context = LocalContext.current
|
||||
val sneakers = remember { mutableStateListOf<Sneaker>() }
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).sneakerDao().getAllSneakers().collect { data ->
|
||||
sneakers.clear()
|
||||
sneakers.addAll(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
Header()
|
||||
Navigate()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class GlobalUser private constructor() {
|
||||
private var user: User? = null
|
||||
|
||||
fun setUser(user: User?) {
|
||||
this.user = user
|
||||
}
|
||||
|
||||
fun getUser(): User? {
|
||||
return user
|
||||
}
|
||||
|
||||
companion object {
|
||||
private var instance: GlobalUser? = null
|
||||
|
||||
fun getInstance(): GlobalUser {
|
||||
return instance ?: synchronized(this) {
|
||||
instance ?: GlobalUser().also { instance = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.example.android_programming.composeui.Navigation
|
||||
|
||||
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
|
||||
@ -10,30 +11,31 @@ import com.example.android_programming.composeui.Screens.AdminPanel.ChangePanel
|
||||
import com.example.android_programming.composeui.Screens.AdminPanel.ChangeSneaker
|
||||
import com.example.android_programming.composeui.Screens.HomeScreen.AboutSneaker
|
||||
import com.example.android_programming.composeui.Screens.HomeScreen.HomeScreen
|
||||
import com.example.android_programming.composeui.Screens.LikeScreen.LikeScreen
|
||||
import com.example.android_programming.composeui.Screens.MyOrderScreen.MyOrderScreen
|
||||
import com.example.android_programming.composeui.Screens.OrderScreen.OrderScreen
|
||||
import com.example.android_programming.composeui.Screens.ProfileScreen.Profile.Person
|
||||
import com.example.android_programming.composeui.Screens.ProfileScreen.Profile.ProfileScreen
|
||||
import com.example.android_programming.composeui.Screens.ProfileScreen.SignIn.LoginScreen
|
||||
import com.example.android_programming.composeui.Screens.ProfileScreen.SignUp.SignUpScreen
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.model.SneakerItem
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
import com.google.gson.Gson
|
||||
|
||||
@Composable
|
||||
fun NavController(navController: NavHostController){
|
||||
var orderViewModel: OrderViewModel = viewModel(factory = OrderViewModel.factory)
|
||||
NavHost(
|
||||
navController = navController,
|
||||
startDestination = NavItem.Home.route
|
||||
){
|
||||
composable(NavItem.Home.route){
|
||||
HomeScreen(navController)
|
||||
HomeScreen(navController, orderViewModel)
|
||||
}
|
||||
composable(NavItem.Like.route){
|
||||
LikeScreen()
|
||||
composable(NavItem.MyOrder.route){
|
||||
MyOrderScreen(orderViewModel)
|
||||
}
|
||||
composable(NavItem.Order.route){
|
||||
OrderScreen()
|
||||
OrderScreen(orderViewModel, navController)
|
||||
}
|
||||
composable(NavItem.Profile.route){
|
||||
ProfileScreen(navController)
|
||||
@ -42,10 +44,10 @@ fun NavController(navController: NavHostController){
|
||||
LoginScreen(navController)
|
||||
}
|
||||
composable(NavItem.SignUp.route){
|
||||
SignUpScreen()
|
||||
SignUpScreen(navController)
|
||||
}
|
||||
composable(NavItem.Person.route){
|
||||
Person()
|
||||
Person(navController)
|
||||
}
|
||||
composable(NavItem.AdminPanel.route){
|
||||
AdminPanel(navController)
|
||||
|
@ -2,7 +2,6 @@ package com.example.android_programming.composeui.Navigation
|
||||
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Build
|
||||
import androidx.compose.material.icons.filled.Favorite
|
||||
import androidx.compose.material.icons.filled.Home
|
||||
import androidx.compose.material.icons.filled.Person
|
||||
import androidx.compose.material.icons.filled.ShoppingCart
|
||||
@ -10,7 +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 Like : NavItem("like", Icons.Default.Favorite)
|
||||
object MyOrder : NavItem("myorder", null)
|
||||
object Order : NavItem("order", Icons.Default.ShoppingCart)
|
||||
object Profile : NavItem("profile", Icons.Default.Person)
|
||||
object SignIn : NavItem("login", null)
|
||||
|
@ -12,9 +12,7 @@ import androidx.compose.material.Scaffold
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.ColorFilter
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
@ -24,22 +22,23 @@ import com.example.android_programming.R
|
||||
@Composable
|
||||
fun Navigate() {
|
||||
val navController = rememberNavController()
|
||||
val listItem = listOf(
|
||||
|
||||
val Items = listOf(
|
||||
NavItem.Home,
|
||||
NavItem.Like,
|
||||
NavItem.Order,
|
||||
NavItem.Profile,
|
||||
NavItem.AdminPanel,
|
||||
)
|
||||
|
||||
Scaffold(bottomBar = {
|
||||
// Оставьте код навигационного бара без изменений
|
||||
BottomNavigation(
|
||||
backgroundColor = Color.White
|
||||
) {
|
||||
val navBackStackEntry = navController.currentBackStackEntryAsState()
|
||||
val currentState = navBackStackEntry.value
|
||||
|
||||
listItem.forEach { it ->
|
||||
Items.forEach { it ->
|
||||
val isSelected = currentState?.destination?.route == it.route
|
||||
|
||||
BottomNavigationItem(
|
||||
@ -54,7 +53,6 @@ fun Navigate() {
|
||||
}
|
||||
}
|
||||
navController.navigate(it.route)
|
||||
|
||||
},
|
||||
icon = {
|
||||
val iconModifier = if (isSelected) {
|
||||
@ -81,9 +79,3 @@ fun Navigate() {
|
||||
NavController(navController = navController)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
fun NavigatePreview() {
|
||||
Navigate()
|
||||
}
|
||||
|
@ -4,11 +4,9 @@ import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
@ -22,11 +20,6 @@ import androidx.compose.material.ButtonDefaults
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.material.TextField
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
@ -37,15 +30,12 @@ import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
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 com.example.android_programming.App
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.SneakerViewModel
|
||||
import com.example.android_programming.database.AppDatabase
|
||||
import com.example.android_programming.model.PhotoManager
|
||||
import com.example.android_programming.vmodel.SneakerViewModel
|
||||
|
||||
@Composable
|
||||
fun AddPanel(sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)){
|
||||
|
@ -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
|
||||
@ -14,28 +16,32 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.model.RoleEnum
|
||||
|
||||
@Composable
|
||||
fun AdminPanel(navHostController: NavHostController) {
|
||||
var isAddPanelVisible by remember { mutableStateOf(false) }
|
||||
var isChangePanelVisible by remember { mutableStateOf(false) }
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(Color.White)
|
||||
.padding(bottom = 50.dp)
|
||||
) {
|
||||
ButtonAdmin(
|
||||
onAddClick = {
|
||||
isAddPanelVisible = true
|
||||
isChangePanelVisible = false
|
||||
},
|
||||
onChangeClick = {
|
||||
isChangePanelVisible = true
|
||||
isAddPanelVisible = false
|
||||
}
|
||||
)
|
||||
var showDialog by remember { mutableStateOf(GlobalUser.getInstance().getUser()?.role == RoleEnum.User || GlobalUser.getInstance().getUser()?.role == null) }
|
||||
if (!showDialog) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(Color.White)
|
||||
.padding(bottom = 50.dp)
|
||||
) {
|
||||
ButtonAdmin(
|
||||
onAddClick = {
|
||||
isAddPanelVisible = true
|
||||
isChangePanelVisible = false
|
||||
},
|
||||
onChangeClick = {
|
||||
isChangePanelVisible = true
|
||||
isAddPanelVisible = false
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (isAddPanelVisible) {
|
||||
AddPanel()
|
||||
@ -45,4 +51,22 @@ fun AdminPanel(navHostController: NavHostController) {
|
||||
ChangePanel(navHostController)
|
||||
}
|
||||
}
|
||||
if (showDialog) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showDialog = false },
|
||||
title = {
|
||||
Text("Access denied")
|
||||
},
|
||||
text = {
|
||||
Text("You are not admin")
|
||||
},
|
||||
confirmButton = {
|
||||
Button(
|
||||
onClick = { navHostController.navigate("home") }
|
||||
) {
|
||||
Text("OK")
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
@ -1,29 +1,19 @@
|
||||
package com.example.android_programming.composeui.Screens.AdminPanel
|
||||
|
||||
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.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.ButtonDefaults
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.R
|
||||
|
||||
@Composable
|
||||
|
@ -2,7 +2,6 @@ package com.example.android_programming.composeui.Screens.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
|
||||
@ -30,9 +29,8 @@ import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.SneakerViewModel
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.model.SneakerItem
|
||||
import com.example.android_programming.vmodel.SneakerViewModel
|
||||
import com.google.gson.Gson
|
||||
|
||||
@Composable
|
||||
|
@ -14,9 +14,7 @@ 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.android_programming.R
|
||||
import com.example.android_programming.SneakerViewModel
|
||||
import com.example.android_programming.model.SneakerItem
|
||||
import com.example.android_programming.vmodel.SneakerViewModel
|
||||
|
||||
@Composable
|
||||
fun ChangePanel(navHostController: NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
|
||||
|
@ -41,10 +41,10 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.SneakerViewModel
|
||||
import com.example.android_programming.model.PhotoManager
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.model.SneakerItem
|
||||
import com.example.android_programming.vmodel.SneakerViewModel
|
||||
|
||||
@Composable
|
||||
fun ChangeSneaker(sneaker: Sneaker, onBackClick: () -> Unit, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
|
||||
val brand = remember {mutableStateOf(sneaker.brand)}
|
||||
@ -220,7 +220,7 @@ fun ChangeSneaker(sneaker: Sneaker, onBackClick: () -> Unit, sneakerViewModel: S
|
||||
onClick = {
|
||||
sneakerViewModel.UpdateSneaker(
|
||||
Sneaker(
|
||||
id = sneaker.id,
|
||||
sneakerId = sneaker.sneakerId,
|
||||
brand = brand.value,
|
||||
model = model.value,
|
||||
description = description.value,
|
||||
|
@ -7,9 +7,6 @@ import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.ButtonDefaults
|
||||
@ -18,7 +15,6 @@ import androidx.compose.material.Text
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.ArrowBack
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
@ -26,14 +22,10 @@ import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
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.android_programming.R
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.model.SneakerItem
|
||||
|
||||
@Composable
|
||||
fun AboutSneaker(sneaker: Sneaker, onBackClick: () -> Unit) {
|
||||
|
@ -1,20 +1,15 @@
|
||||
package com.example.android_programming.composeui.Screens.HomeScreen
|
||||
|
||||
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.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
@ -23,21 +18,14 @@ import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.composeui.Screens.HomeScreen.FilterByBrand.ItemFilterByBrand
|
||||
import com.example.android_programming.composeui.Screens.HomeScreen.FilterByBrand.ItemRow
|
||||
import com.example.android_programming.composeui.Screens.HomeScreen.SearchField.SearchField
|
||||
import com.example.android_programming.composeui.Screens.HomeScreen.SneakerRecyclerView.CardSneaker
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.composeui.Screens.HomeScreen.FilterByBrand.FilterByBrand
|
||||
import com.example.android_programming.composeui.Screens.HomeScreen.SearchField.SearchField
|
||||
import com.example.android_programming.composeui.Screens.HomeScreen.SneakerRecyclerView.RecyclerView
|
||||
import com.example.android_programming.database.AppDatabase
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.model.getSneakers
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
@Composable
|
||||
fun HomeScreen(navHostController: NavHostController) {
|
||||
fun HomeScreen(navHostController: NavHostController, orderViewModel: OrderViewModel) {
|
||||
val context = LocalContext.current
|
||||
val sneakers = remember { mutableStateListOf<Sneaker>() }
|
||||
Column(
|
||||
@ -59,6 +47,6 @@ fun HomeScreen(navHostController: NavHostController) {
|
||||
}
|
||||
Sales()
|
||||
FilterByBrand()
|
||||
RecyclerView(navHostController = navHostController)
|
||||
RecyclerView(navHostController = navHostController, orderViewModel)
|
||||
}
|
||||
}
|
@ -2,10 +2,8 @@ package com.example.android_programming.composeui.Screens.HomeScreen
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
|
@ -10,7 +10,7 @@ import androidx.compose.foundation.text.BasicTextField
|
||||
import androidx.compose.material.Icon
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Search
|
||||
import androidx.compose.runtime.Composable;
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@ -21,7 +21,6 @@ import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.example.android_programming.R
|
||||
|
||||
|
@ -30,11 +30,10 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.model.SneakerItem
|
||||
import com.google.gson.Gson
|
||||
|
||||
@Composable
|
||||
fun CardSneaker(item: Sneaker, navController: NavHostController) {
|
||||
fun CardSneaker(item: Sneaker, navController: NavHostController, selectedItems: List<Sneaker>, onItemSelected: (Sneaker) -> Unit) {
|
||||
val maxWidth = (LocalConfiguration.current.screenWidthDp / 2).dp
|
||||
|
||||
Box(
|
||||
@ -87,7 +86,9 @@ fun CardSneaker(item: Sneaker, navController: NavHostController) {
|
||||
backgroundColor = colorResource(id = R.color.figma_blue),
|
||||
contentColor = Color.White
|
||||
),
|
||||
onClick = { /*TODO*/ },
|
||||
onClick = {
|
||||
onItemSelected(item)
|
||||
},
|
||||
modifier = Modifier
|
||||
.size(50.dp, 30.dp)
|
||||
.clip(RoundedCornerShape(10.dp))
|
||||
|
@ -6,21 +6,17 @@ import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.SneakerViewModel
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import kotlin.collections.chunked
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
import com.example.android_programming.vmodel.SneakerViewModel
|
||||
|
||||
@Composable
|
||||
fun RecyclerView(navHostController : NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
|
||||
fun RecyclerView(navHostController : NavHostController, orderViewModel: OrderViewModel, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@ -36,9 +32,12 @@ fun RecyclerView(navHostController : NavHostController, sneakerViewModel: Sneake
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
for (item in chunkedListItem) {
|
||||
CardSneaker(item, navHostController)
|
||||
CardSneaker(item, navHostController, orderViewModel.selectedItems) { selectedItem ->
|
||||
orderViewModel.addSelectedItem(selectedItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,59 +0,0 @@
|
||||
package com.example.android_programming.composeui.Screens.LikeScreen;
|
||||
|
||||
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.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
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.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.model.SneakerItem
|
||||
|
||||
@Composable
|
||||
fun LikeScreen() {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(Color.White)
|
||||
){
|
||||
Text(
|
||||
text = "Favorites",
|
||||
style = MaterialTheme.typography.h5,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier
|
||||
.padding(10.dp, 10.dp)
|
||||
)
|
||||
Row {
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
) {
|
||||
itemsIndexed(
|
||||
listOf(
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
)
|
||||
){_, item->
|
||||
CardSneakerLike(item = item)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
fun LikeScreenPreview(){
|
||||
LikeScreen()
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.example.android_programming.composeui.Screens.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.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
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.android_programming.GlobalUser
|
||||
import com.example.android_programming.model.Order
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
@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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package com.example.android_programming.composeui.Screens.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.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
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.res.colorResource
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.model.Order
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
import java.util.Date
|
||||
|
||||
@Composable
|
||||
fun OrderCard(order: Order, orderViewModel: OrderViewModel){
|
||||
|
||||
val SneakerList = order.orderId?.let {
|
||||
orderViewModel.database.orderDao().getOrderWithSneakers(
|
||||
it
|
||||
)
|
||||
}
|
||||
val sneakerWithOrder by SneakerList!!.collectAsState(null)
|
||||
|
||||
val sneakerList: List<Sneaker>? = sneakerWithOrder?.sneakers
|
||||
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.date)}")
|
||||
Row(){
|
||||
if (sneakerList != null) {
|
||||
for(sneaker in sneakerList){
|
||||
Image(
|
||||
contentScale = ContentScale.FillBounds,
|
||||
painter = painterResource(id = sneaker.photo),
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.example.android_programming.composeui.Screens.LikeScreen
|
||||
package com.example.android_programming.composeui.Screens.OrderScreen
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
@ -23,14 +23,14 @@ import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.res.colorResource
|
||||
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 com.example.android_programming.R
|
||||
import com.example.android_programming.model.SneakerItem
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
@Composable
|
||||
fun CardSneakerLike(item: SneakerItem) {
|
||||
fun CardSneakerLike(item: Sneaker, orderViewModel: OrderViewModel) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -41,7 +41,7 @@ fun CardSneakerLike(item: SneakerItem) {
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Image(
|
||||
painter = painterResource(id = item.imageId),
|
||||
painter = painterResource(id = item.photo),
|
||||
contentDescription = "image",
|
||||
contentScale = ContentScale.FillWidth,
|
||||
modifier = Modifier
|
||||
@ -55,7 +55,7 @@ fun CardSneakerLike(item: SneakerItem) {
|
||||
.weight(1f)
|
||||
.padding(start = 16.dp)
|
||||
) {
|
||||
item.name?.let { Text(text = it, fontSize = 20.sp) }
|
||||
item.brand?.let { Text(text = it, fontSize = 20.sp) }
|
||||
Text(text = "${item.price} USD", color = Color.Red, fontSize = 16.sp)
|
||||
}
|
||||
|
||||
@ -64,19 +64,13 @@ fun CardSneakerLike(item: SneakerItem) {
|
||||
backgroundColor = colorResource(id = R.color.figma_blue),
|
||||
contentColor = Color.White
|
||||
),
|
||||
onClick = { /*TODO*/ },
|
||||
onClick = {
|
||||
orderViewModel.removeSelectedItem(item)
|
||||
},
|
||||
modifier = Modifier
|
||||
.padding(end = 16.dp)
|
||||
) {
|
||||
Icon(imageVector = Icons.Default.Delete, contentDescription = "delete")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
fun CardSneakerLikePreview(){
|
||||
CardSneakerLike(SneakerItem(R.drawable.sneaker, "Jordan", 159.99))
|
||||
}
|
@ -15,10 +15,6 @@ import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.material.TextField
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
@ -28,17 +24,13 @@ 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.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
fun DeliveryAddress() {
|
||||
var address by remember { mutableStateOf("") }
|
||||
var city by remember { mutableStateOf("") }
|
||||
var number by remember { mutableStateOf("") }
|
||||
fun DeliveryAddress(orderViewModel: OrderViewModel) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -58,35 +50,8 @@ fun DeliveryAddress() {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
TextField(
|
||||
value = address,
|
||||
onValueChange = { address = it },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(50.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 = "Address",
|
||||
style = TextStyle(fontSize = 12.sp)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
TextField(
|
||||
value = city,
|
||||
onValueChange = { city = it },
|
||||
value = orderViewModel.city.value,
|
||||
onValueChange = { orderViewModel.city.value = it },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(50.dp)
|
||||
@ -112,8 +77,8 @@ fun DeliveryAddress() {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
TextField(
|
||||
value = number,
|
||||
onValueChange = { number = it },
|
||||
value = orderViewModel.street.value,
|
||||
onValueChange = { orderViewModel.street.value = it },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(50.dp)
|
||||
@ -130,7 +95,34 @@ fun DeliveryAddress() {
|
||||
),
|
||||
placeholder = {
|
||||
Text(
|
||||
text = "Number",
|
||||
text = "Street",
|
||||
style = TextStyle(fontSize = 12.sp)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
TextField(
|
||||
value = orderViewModel.house.value,
|
||||
onValueChange = { orderViewModel.house.value = it },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(50.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 = "House",
|
||||
style = TextStyle(fontSize = 12.sp)
|
||||
)
|
||||
}
|
||||
|
@ -1,51 +1,28 @@
|
||||
package com.example.android_programming.composeui.Screens.OrderScreen
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.gestures.scrollable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.ButtonDefaults
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.material.TextField
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.colorResource
|
||||
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.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.composeui.Screens.LikeScreen.CardSneakerLike
|
||||
import com.example.android_programming.model.SneakerItem
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
|
||||
@Composable
|
||||
fun OrderScreen() {
|
||||
fun OrderScreen(orderViewModel: OrderViewModel, navHostController: NavHostController) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@ -53,16 +30,9 @@ fun OrderScreen() {
|
||||
.padding(bottom = 60.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
){
|
||||
DeliveryAddress()
|
||||
ShoppingList(listOf(
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
))
|
||||
SubTotal()
|
||||
DeliveryAddress(orderViewModel)
|
||||
ShoppingList(orderViewModel.selectedItems, orderViewModel)
|
||||
SubTotal(orderViewModel)
|
||||
|
||||
Button(
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
@ -70,7 +40,12 @@ fun OrderScreen() {
|
||||
contentColor = Color.White
|
||||
),
|
||||
onClick = {
|
||||
|
||||
if(GlobalUser.getInstance().getUser() != null){
|
||||
orderViewModel.createOrder()
|
||||
navHostController.navigate("home")
|
||||
}else{
|
||||
navHostController.navigate("login")
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -79,10 +54,4 @@ fun OrderScreen() {
|
||||
Text("Confirm order")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
fun OrderScreenPreview(){
|
||||
OrderScreen()
|
||||
}
|
@ -3,15 +3,15 @@ package com.example.android_programming.composeui.Screens.OrderScreen
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.runtime.Composable
|
||||
import com.example.android_programming.composeui.Screens.LikeScreen.CardSneakerLike
|
||||
import com.example.android_programming.model.SneakerItem
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
@Composable
|
||||
fun ShoppingList(list : List<SneakerItem>) {
|
||||
fun ShoppingList(list : List<Sneaker>, orderViewModel: OrderViewModel) {
|
||||
Row {
|
||||
Column {
|
||||
for(item in list){
|
||||
CardSneakerLike(item = item)
|
||||
CardSneakerLike(item = item, orderViewModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,14 +12,13 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
fun SubTotal() {
|
||||
fun SubTotal(orderViewModel: OrderViewModel) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.padding(16.dp)
|
||||
@ -42,7 +41,7 @@ fun SubTotal() {
|
||||
modifier = Modifier.weight(1f),
|
||||
horizontalArrangement = Arrangement.End
|
||||
){
|
||||
Text(text = "319.99 $", fontSize = 15.sp)
|
||||
Text(text = "${orderViewModel.getSubTotal()} $", fontSize = 15.sp)
|
||||
}
|
||||
}
|
||||
Row(
|
||||
@ -61,7 +60,7 @@ fun SubTotal() {
|
||||
modifier = Modifier.weight(1f),
|
||||
horizontalArrangement = Arrangement.End
|
||||
){
|
||||
Text(text = "180 $", fontSize = 15.sp)
|
||||
Text(text = "${"%.2f".format(orderViewModel.getSubTotal() * 0.05)} $", fontSize = 15.sp)
|
||||
}
|
||||
}
|
||||
Row(
|
||||
@ -80,7 +79,7 @@ fun SubTotal() {
|
||||
modifier = Modifier.weight(1f),
|
||||
horizontalArrangement = Arrangement.End
|
||||
){
|
||||
Text(text = "1900 $", fontSize = 15.sp)
|
||||
Text(text = "${"%.2f".format(orderViewModel.getSubTotal() + orderViewModel.getSubTotal() * 0.05)} $", fontSize = 15.sp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,44 +1,44 @@
|
||||
package com.example.android_programming.composeui.Screens.ProfileScreen.Profile
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.ButtonDefaults
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
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.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.R
|
||||
|
||||
@Composable
|
||||
fun Person() {
|
||||
fun Person(navHostController: NavHostController) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.background(Color.White)
|
||||
.fillMaxSize(),
|
||||
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
|
||||
@Composable
|
||||
fun PersonPreview(){
|
||||
Person()
|
||||
}
|
@ -6,32 +6,32 @@ import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.aspectRatio
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
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.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.draw.drawWithContent
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.R
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
fun ProfileCard() {
|
||||
fun ProfileCard(navHostController: NavHostController) {
|
||||
val globalUser = GlobalUser.getInstance().getUser()
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -55,17 +55,32 @@ fun ProfileCard() {
|
||||
.clip(CircleShape)
|
||||
)
|
||||
|
||||
Text(
|
||||
text = "Шайлушай",
|
||||
fontSize = 18.sp,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
Text(
|
||||
text = "shailushai@example.com",
|
||||
fontSize = 16.sp,
|
||||
color = Color.Gray
|
||||
)
|
||||
if (globalUser != null) {
|
||||
Text(
|
||||
text = "${globalUser.name} ${globalUser.surname}",
|
||||
fontSize = 18.sp,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = globalUser.email,
|
||||
fontSize = 16.sp,
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,78 +1,19 @@
|
||||
package com.example.android_programming.composeui.Screens.ProfileScreen.Profile;
|
||||
|
||||
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.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.requiredSize
|
||||
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.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.runtime.Composable
|
||||
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.android_programming.R
|
||||
import com.example.android_programming.composeui.Navigation.NavController
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.composeui.Screens.ProfileScreen.SignIn.LoginScreen
|
||||
|
||||
@Composable
|
||||
fun ProfileScreen(navController: NavHostController) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(Color.White),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
){
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(20.dp))
|
||||
){
|
||||
Button(
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
backgroundColor = colorResource(id = R.color.figma_blue),
|
||||
contentColor = Color.White
|
||||
),
|
||||
onClick = {
|
||||
navController.navigate("person")
|
||||
},
|
||||
modifier = Modifier
|
||||
.requiredSize(300.dp, 40.dp)
|
||||
) {
|
||||
Text(text = "Profile")
|
||||
}
|
||||
}
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(20.dp))
|
||||
){
|
||||
Button(
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
backgroundColor = colorResource(id = R.color.figma_blue),
|
||||
contentColor = Color.White
|
||||
),
|
||||
onClick = {
|
||||
navController.navigate("login")
|
||||
},
|
||||
modifier = Modifier
|
||||
.requiredSize(300.dp, 40.dp)
|
||||
) {
|
||||
Text(text = "Sign In")
|
||||
}
|
||||
}
|
||||
val globalUser: GlobalUser = GlobalUser.getInstance()
|
||||
if(globalUser.getUser() != null){
|
||||
Person(navController)
|
||||
}else{
|
||||
LoginScreen(navController = navController)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,6 @@ import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
@ -34,14 +33,15 @@ 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.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.vmodel.UserViewModel
|
||||
|
||||
@Composable
|
||||
fun SignInCard(navController: NavHostController) {
|
||||
fun SignInCard(navController: NavHostController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -54,8 +54,8 @@ fun SignInCard(navController: NavHostController) {
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center
|
||||
){
|
||||
var username by remember { mutableStateOf("") }
|
||||
var password by remember { mutableStateOf("") }
|
||||
var isEmailValid by remember { mutableStateOf(true) }
|
||||
var isPasswordValid by remember { mutableStateOf(true) }
|
||||
|
||||
Text(
|
||||
text = "Sign In",
|
||||
@ -66,8 +66,10 @@ fun SignInCard(navController: NavHostController) {
|
||||
)
|
||||
|
||||
TextField(
|
||||
value = username,
|
||||
onValueChange = { username = it },
|
||||
value = userViewModel.email.value,
|
||||
onValueChange = {
|
||||
userViewModel.email.value = it
|
||||
isEmailValid = userViewModel.isValidEmail(it)},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(50.dp)
|
||||
@ -85,17 +87,28 @@ fun SignInCard(navController: NavHostController) {
|
||||
),
|
||||
placeholder = {
|
||||
Text(
|
||||
text = "Username",
|
||||
text = "Email",
|
||||
style = TextStyle(fontSize = 12.sp)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
if (!isEmailValid) {
|
||||
Text(
|
||||
text = "Invalid email format",
|
||||
color = Color.Red,
|
||||
style = TextStyle(fontSize = 12.sp)
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
TextField(
|
||||
value = password,
|
||||
onValueChange = { password = it },
|
||||
value = userViewModel.password.value,
|
||||
onValueChange = {
|
||||
userViewModel.password.value = it
|
||||
isPasswordValid = it.isNotEmpty()
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(50.dp)
|
||||
@ -121,13 +134,22 @@ fun SignInCard(navController: NavHostController) {
|
||||
}
|
||||
)
|
||||
|
||||
if (!isPasswordValid) {
|
||||
Text(
|
||||
text = "Password is required",
|
||||
color = Color.Red,
|
||||
style = TextStyle(fontSize = 12.sp)
|
||||
)
|
||||
}
|
||||
|
||||
Button(
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
backgroundColor = colorResource(id = R.color.figma_blue),
|
||||
contentColor = Color.White
|
||||
),
|
||||
onClick = {
|
||||
|
||||
userViewModel.authUser()
|
||||
navController.navigate("profile")
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
|
@ -1,37 +1,13 @@
|
||||
package com.example.android_programming.composeui.Screens.ProfileScreen.SignIn
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.BasicTextField
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.ButtonDefaults
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.material.TextField
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.input.pointer.PointerIcon.Companion.Text
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.semantics.SemanticsProperties.Text
|
||||
import androidx.compose.ui.text.Placeholder
|
||||
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.NavController
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.R
|
||||
import androidx.compose.material.Text as Text1
|
||||
|
||||
@Composable
|
||||
fun LoginScreen(navController: NavHostController) {
|
||||
|
@ -17,10 +17,6 @@ import androidx.compose.material.ButtonDefaults
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.material.TextField
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
@ -31,19 +27,15 @@ 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.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.vmodel.UserViewModel
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
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("") }
|
||||
fun SignUpCard(navHostController: NavHostController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -67,36 +59,8 @@ fun SignUpCard() {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
TextField(
|
||||
value = username,
|
||||
onValueChange = { username = 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 },
|
||||
value = userViewModel.name.value,
|
||||
onValueChange = { userViewModel.name.value = it },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(50.dp)
|
||||
@ -123,8 +87,8 @@ fun SignUpCard() {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
TextField(
|
||||
value = surname,
|
||||
onValueChange = { surname = it },
|
||||
value = userViewModel.surname.value,
|
||||
onValueChange = { userViewModel.surname.value = it },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(50.dp)
|
||||
@ -151,8 +115,8 @@ fun SignUpCard() {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
TextField(
|
||||
value = sex,
|
||||
onValueChange = { sex = it },
|
||||
value = userViewModel.email.value,
|
||||
onValueChange = { userViewModel.email.value = it },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(50.dp)
|
||||
@ -170,7 +134,7 @@ fun SignUpCard() {
|
||||
),
|
||||
placeholder = {
|
||||
Text(
|
||||
text = "Sex",
|
||||
text = "Email",
|
||||
style = TextStyle(fontSize = 12.sp)
|
||||
)
|
||||
}
|
||||
@ -179,8 +143,8 @@ fun SignUpCard() {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
TextField(
|
||||
value = password,
|
||||
onValueChange = { password = it },
|
||||
value = userViewModel.password.value,
|
||||
onValueChange = { userViewModel.password.value = it },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(50.dp)
|
||||
@ -212,7 +176,8 @@ fun SignUpCard() {
|
||||
contentColor = Color.White
|
||||
),
|
||||
onClick = {
|
||||
|
||||
userViewModel.createUser()
|
||||
navHostController.navigate("login")
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
|
@ -1,51 +1,21 @@
|
||||
package com.example.android_programming.composeui.Screens.ProfileScreen.SignUp
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.BasicTextField
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.DropdownMenu
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.material.TextField
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.colorResource
|
||||
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 com.example.android_programming.composeui.Screens.ProfileScreen.SignIn.LoginScreen
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.composeui.Screens.ProfileScreen.SignIn.SignInCard
|
||||
import androidx.navigation.NavHostController
|
||||
|
||||
@Composable
|
||||
fun SignUpScreen() {
|
||||
fun SignUpScreen(navHostController: NavHostController) {
|
||||
Column(modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(Color.White),
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
SignUpCard()
|
||||
SignUpCard(navHostController)
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.example.android_programming.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import com.example.android_programming.model.Order
|
||||
import com.example.android_programming.model.OrderSneaker
|
||||
import com.example.android_programming.model.OrderWithSneakers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface OrderDao {
|
||||
|
||||
@Insert
|
||||
suspend fun createOrder(order: Order): Long
|
||||
|
||||
@Insert
|
||||
suspend fun insertOrderSneaker(orderSneaker: OrderSneaker)
|
||||
|
||||
@Query("SELECT * FROM 'Order' WHERE orderId = :id")
|
||||
fun getOrderWithSneakers(id: Int): Flow<OrderWithSneakers>
|
||||
|
||||
@Query("SELECT * FROM `Order`")
|
||||
fun getAllOrder(): Flow<List<Order>>
|
||||
|
||||
@Delete
|
||||
suspend fun delete(order: Order)
|
||||
}
|
@ -14,14 +14,14 @@ interface SneakerDao {
|
||||
suspend fun insert(sneaker: Sneaker)
|
||||
|
||||
@Update
|
||||
suspend fun update(sneaker: Sneaker?)
|
||||
suspend fun update(sneaker: Sneaker)
|
||||
|
||||
@Delete
|
||||
suspend fun delete(sneaker: Sneaker)
|
||||
|
||||
@Query("SELECT*FROM sneakers")
|
||||
@Query("SELECT*FROM Sneaker")
|
||||
fun getAllSneakers(): Flow<List<Sneaker>>
|
||||
|
||||
@Query("SELECT * FROM sneakers WHERE id = :id")
|
||||
@Query("SELECT * FROM Sneaker WHERE sneakerId = :id")
|
||||
suspend fun getSneakerById(id: Int): Sneaker
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.example.android_programming.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import com.example.android_programming.model.User
|
||||
import com.example.android_programming.model.UserWithOrder
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface UserDao {
|
||||
@Insert
|
||||
suspend fun createUser(user: User)
|
||||
|
||||
@Update
|
||||
suspend fun updateUser(user: User)
|
||||
|
||||
@Delete
|
||||
suspend fun deleteUser(user: User)
|
||||
|
||||
@Query("SELECT * FROM users WHERE userId = :id")
|
||||
suspend fun getUserById(id: Int): User
|
||||
|
||||
@Query("SELECT * FROM users WHERE email = :email")
|
||||
suspend fun getUserByEmail(email: String): User
|
||||
|
||||
@Query("SELECT * FROM users WHERE userId =:id")
|
||||
fun getUserOrders(id: Int) : Flow<UserWithOrder>
|
||||
}
|
@ -4,12 +4,25 @@ import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.dao.OrderDao
|
||||
import com.example.android_programming.dao.SneakerDao
|
||||
import com.example.android_programming.dao.UserDao
|
||||
import com.example.android_programming.model.Order
|
||||
import com.example.android_programming.model.OrderSneaker
|
||||
import com.example.android_programming.model.RoleEnum
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.model.User
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Database(entities = [Sneaker::class], version = 2)
|
||||
@Database(entities = [Sneaker::class, User::class, Order::class, OrderSneaker::class], version = 5)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
abstract fun sneakerDao(): SneakerDao
|
||||
abstract fun userDao(): UserDao
|
||||
abstract fun orderDao(): OrderDao
|
||||
|
||||
companion object {
|
||||
private const val DB_NAME: String = "my-db"
|
||||
@ -17,15 +30,48 @@ abstract class AppDatabase : RoomDatabase() {
|
||||
@Volatile
|
||||
private var INSTANCE: AppDatabase? = null
|
||||
|
||||
fun getInstance(context: Context): AppDatabase {
|
||||
suspend fun populateDatabase() {
|
||||
INSTANCE?.let { database ->
|
||||
// User
|
||||
val userDao = database.userDao()
|
||||
val user1 = User(null, "Artem", "Emelyanov", "artem@mail.ru", "123", RoleEnum.Admin)
|
||||
val user2 = User(null, "Danil", "Markov", "danil@mail.ru", "123", RoleEnum.User)
|
||||
val user3 = User(null, "Viktoria", "Presnyakova", "vika@mail.ru", "123", RoleEnum.User)
|
||||
userDao.createUser(user1)
|
||||
userDao.createUser(user2)
|
||||
userDao.createUser(user3)
|
||||
// Sneaker
|
||||
val sneakerDao = database.sneakerDao()
|
||||
val sneaker1 = Sneaker(null, "Nike", "Air Force 1", "nice", 159.99, R.drawable.img_1)
|
||||
val sneaker2 = Sneaker(null, "Adidas", "ZX 750", "beautiful", 169.99, R.drawable.img_2)
|
||||
val sneaker3 = Sneaker(null, "Reebok", "Classic", "amazing", 179.99, R.drawable.img_3)
|
||||
val sneaker4 = Sneaker(null, "Puma", "Classic", "normal", 189.99, R.drawable.img_4)
|
||||
sneakerDao.insert(sneaker1)
|
||||
sneakerDao.insert(sneaker2)
|
||||
sneakerDao.insert(sneaker3)
|
||||
sneakerDao.insert(sneaker4)
|
||||
// Order
|
||||
}
|
||||
}
|
||||
|
||||
fun getInstance(appContext: Context): AppDatabase {
|
||||
return INSTANCE ?: synchronized(this) {
|
||||
val instance = Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
Room.databaseBuilder(
|
||||
appContext,
|
||||
AppDatabase::class.java,
|
||||
DB_NAME
|
||||
).build()
|
||||
INSTANCE = instance
|
||||
instance
|
||||
)
|
||||
.addCallback(object : Callback() {
|
||||
override fun onCreate(db: SupportSQLiteDatabase) {
|
||||
super.onCreate(db)
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
populateDatabase()
|
||||
}
|
||||
}
|
||||
})
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
.also { INSTANCE = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity
|
||||
data class Order(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val orderId: Int? = null,
|
||||
@ColumnInfo(name = "Date")
|
||||
val date: Long,
|
||||
@ColumnInfo(name = "City")
|
||||
val city: String,
|
||||
@ColumnInfo(name = "Street")
|
||||
val street: String,
|
||||
@ColumnInfo(name = "House")
|
||||
val house: String,
|
||||
@ColumnInfo(name = "Sub Total")
|
||||
val subtotal: Double,
|
||||
@ColumnInfo(name = "Taxes")
|
||||
val taxes: Double,
|
||||
@ColumnInfo(name = "Total")
|
||||
val total: Double,
|
||||
@ColumnInfo(name = "CreatorUserId")
|
||||
val creatorUserId: Int
|
||||
)
|
@ -0,0 +1,9 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import androidx.room.Entity
|
||||
|
||||
@Entity(primaryKeys = ["orderId", "sneakerId"])
|
||||
data class OrderSneaker(
|
||||
val orderId: Int,
|
||||
val sneakerId: Int
|
||||
)
|
@ -0,0 +1,17 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Junction
|
||||
import androidx.room.Relation
|
||||
|
||||
data class OrderWithSneakers(
|
||||
@Embedded val order: Order,
|
||||
@Relation(
|
||||
parentColumn = "orderId",
|
||||
entityColumn = "sneakerId",
|
||||
associateBy = Junction(OrderSneaker::class)
|
||||
)
|
||||
val sneakers: List<Sneaker>
|
||||
)
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import android.util.Log
|
||||
import com.example.android_programming.R
|
||||
|
||||
class PhotoManager {
|
||||
|
@ -0,0 +1,6 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
enum class RoleEnum {
|
||||
Admin,
|
||||
User
|
||||
}
|
@ -1,14 +1,13 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "sneakers")
|
||||
@Entity
|
||||
data class Sneaker(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val id: Int? = null,
|
||||
val sneakerId: Int? = null,
|
||||
@ColumnInfo(name = "Brand")
|
||||
val brand: String,
|
||||
@ColumnInfo(name = "Model")
|
||||
|
@ -1,62 +0,0 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import com.example.android_programming.R
|
||||
|
||||
|
||||
data class SneakerItem(
|
||||
val imageId: Int,
|
||||
val name: String?,
|
||||
val price: Double
|
||||
) : Parcelable {
|
||||
constructor(parcel: Parcel) : this(
|
||||
parcel.readInt(),
|
||||
parcel.readString(),
|
||||
parcel.readDouble()
|
||||
) {
|
||||
}
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.writeInt(imageId)
|
||||
parcel.writeString(name)
|
||||
parcel.writeDouble(price)
|
||||
}
|
||||
|
||||
override fun describeContents(): Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
companion object CREATOR : Parcelable.Creator<SneakerItem> {
|
||||
override fun createFromParcel(parcel: Parcel): SneakerItem {
|
||||
return SneakerItem(parcel)
|
||||
}
|
||||
|
||||
override fun newArray(size: Int): Array<SneakerItem?> {
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getSneakers(): List<SneakerItem> {
|
||||
return listOf(
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
SneakerItem(R.drawable.sneaker, "Jordan", 159.99),
|
||||
)
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "users")
|
||||
data class User(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val userId: Int? = null,
|
||||
@ColumnInfo(name = "Name")
|
||||
val name: String,
|
||||
@ColumnInfo(name = "Surname")
|
||||
val surname: String,
|
||||
@ColumnInfo(name = "Email")
|
||||
val email: String,
|
||||
@ColumnInfo(name = "Password")
|
||||
val password: String,
|
||||
@ColumnInfo(name = "Role")
|
||||
val role: RoleEnum,
|
||||
@ColumnInfo(name = "Photo")
|
||||
val photo: Int? = null,
|
||||
)
|
@ -0,0 +1,13 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Relation
|
||||
|
||||
data class UserWithOrder(
|
||||
@Embedded val user: User,
|
||||
@Relation(
|
||||
parentColumn = "userId",
|
||||
entityColumn = "CreatorUserId"
|
||||
)
|
||||
val orders: List<Order>
|
||||
)
|
@ -0,0 +1,81 @@
|
||||
package com.example.android_programming.vmodel
|
||||
|
||||
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.android_programming.App
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.database.AppDatabase
|
||||
import com.example.android_programming.model.Order
|
||||
import com.example.android_programming.model.OrderSneaker
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.Date
|
||||
|
||||
class OrderViewModel(val database: AppDatabase) : ViewModel() {
|
||||
private var _selectedItems = mutableStateOf<List<Sneaker>>(emptyList())
|
||||
val selectedItems get() = _selectedItems.value
|
||||
var city = mutableStateOf("")
|
||||
val street = mutableStateOf("")
|
||||
val house = mutableStateOf("")
|
||||
|
||||
fun addSelectedItem(item: Sneaker) {
|
||||
_selectedItems.value = _selectedItems.value + item
|
||||
}
|
||||
|
||||
fun deleteOrder(order: Order) = viewModelScope.launch {
|
||||
database.orderDao().delete(order)
|
||||
}
|
||||
|
||||
fun getOrderList(id: Int) = viewModelScope.launch {
|
||||
database.userDao().getUserOrders(id)
|
||||
}
|
||||
|
||||
fun removeSelectedItem(item: Sneaker) {
|
||||
val updatedItems = _selectedItems.value.toMutableList()
|
||||
updatedItems.remove(item)
|
||||
_selectedItems.value = updatedItems
|
||||
}
|
||||
|
||||
fun createOrder() = viewModelScope.launch {
|
||||
val order = Order(
|
||||
date = Date().time,
|
||||
city = city.value,
|
||||
street = street.value,
|
||||
house = house.value,
|
||||
subtotal = getSubTotal(),
|
||||
taxes = "%.2f".format(getSubTotal() * 0.05).toDouble(),
|
||||
total = "%.2f".format(getSubTotal() * 0.05 + getSubTotal()).toDouble(),
|
||||
creatorUserId = GlobalUser.getInstance().getUser()?.userId!!
|
||||
)
|
||||
|
||||
val orderId = database.orderDao().createOrder(order)
|
||||
|
||||
for (sneaker in selectedItems) {
|
||||
val orderSneaker = OrderSneaker( orderId.toInt(), sneaker.sneakerId!!)
|
||||
database.orderDao().insertOrderSneaker(orderSneaker)
|
||||
}
|
||||
city.value = ""
|
||||
street.value = ""
|
||||
house.value = ""
|
||||
_selectedItems = mutableStateOf(emptyList())
|
||||
}
|
||||
|
||||
fun getSubTotal(): Double {
|
||||
return selectedItems.sumOf { it.price }
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.example.android_programming
|
||||
package com.example.android_programming.vmodel
|
||||
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.lifecycle.ViewModel
|
||||
@ -6,7 +6,8 @@ import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
import androidx.room.Update
|
||||
import com.example.android_programming.App
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.database.AppDatabase
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import kotlinx.coroutines.launch
|
@ -0,0 +1,54 @@
|
||||
package com.example.android_programming.vmodel
|
||||
|
||||
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.android_programming.App
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.database.AppDatabase
|
||||
import com.example.android_programming.model.RoleEnum
|
||||
import com.example.android_programming.model.User
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user