diff --git a/app/build.gradle.kts b/app/build.gradle.kts index e332b66..2e979cc 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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" diff --git a/app/src/main/java/com/example/android_programming/App.kt b/app/src/main/java/com/example/android_programming/App.kt index fce5d41..3cb42ef 100644 --- a/app/src/main/java/com/example/android_programming/App.kt +++ b/app/src/main/java/com/example/android_programming/App.kt @@ -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() + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/MainActivity.kt b/app/src/main/java/com/example/android_programming/MainActivity.kt index a640841..63b35fb 100644 --- a/app/src/main/java/com/example/android_programming/MainActivity.kt +++ b/app/src/main/java/com/example/android_programming/MainActivity.kt @@ -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() } - 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 } + } + } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/composeui/Navigation/NavController.kt b/app/src/main/java/com/example/android_programming/composeui/Navigation/NavController.kt index 2ddd298..1c34571 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Navigation/NavController.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Navigation/NavController.kt @@ -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) diff --git a/app/src/main/java/com/example/android_programming/composeui/Navigation/NavItem.kt b/app/src/main/java/com/example/android_programming/composeui/Navigation/NavItem.kt index 4f457d2..56b45c8 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Navigation/NavItem.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Navigation/NavItem.kt @@ -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) diff --git a/app/src/main/java/com/example/android_programming/composeui/Navigation/Navigate.kt b/app/src/main/java/com/example/android_programming/composeui/Navigation/Navigate.kt index 2050c1f..58e956e 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Navigation/Navigate.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Navigation/Navigate.kt @@ -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() -} diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/AddPanel.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/AddPanel.kt index 56c2376..e133f75 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/AddPanel.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/AddPanel.kt @@ -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)){ diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/AdminPanel.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/AdminPanel.kt index a7ecb7a..6dd90aa 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/AdminPanel.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/AdminPanel.kt @@ -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") + } + } + ) + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/Button.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/Button.kt index 2a24000..db247a0 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/Button.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/Button.kt @@ -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 diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/CardSneakerForChange.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/CardSneakerForChange.kt index 8562698..9340e52 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/CardSneakerForChange.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/CardSneakerForChange.kt @@ -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 diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangePanel.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangePanel.kt index 560a411..0e275ef 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangePanel.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangePanel.kt @@ -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)) { diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangeSneaker.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangeSneaker.kt index e390420..337c19e 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangeSneaker.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/AdminPanel/ChangeSneaker.kt @@ -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, diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/AboutSneaker.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/AboutSneaker.kt index 142cf87..7f786b6 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/AboutSneaker.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/AboutSneaker.kt @@ -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) { diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt index 12a43b0..bdd30e9 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt @@ -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() } Column( @@ -59,6 +47,6 @@ fun HomeScreen(navHostController: NavHostController) { } Sales() FilterByBrand() - RecyclerView(navHostController = navHostController) + RecyclerView(navHostController = navHostController, orderViewModel) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/Sales.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/Sales.kt index 6638942..3b884dc 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/Sales.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/Sales.kt @@ -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 diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SearchField/SearchField.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SearchField/SearchField.kt index f2e5db3..8cfa2d2 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SearchField/SearchField.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SearchField/SearchField.kt @@ -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 diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/CardSneaker.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/CardSneaker.kt index 1b02b9d..f85f7de 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/CardSneaker.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/CardSneaker.kt @@ -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, 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)) diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt index 9f195ad..b8d0d94 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt @@ -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) + } } } } } } + diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/LikeScreen/LikeScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/LikeScreen/LikeScreen.kt deleted file mode 100644 index 05672e1..0000000 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/LikeScreen/LikeScreen.kt +++ /dev/null @@ -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() -} diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/MyOrderScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/MyOrderScreen.kt new file mode 100644 index 0000000..1e6cce9 --- /dev/null +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/MyOrderScreen.kt @@ -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? = 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) + } + } + } + } + } +} diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/OrderCard.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/OrderCard.kt new file mode 100644 index 0000000..2b7b882 --- /dev/null +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/OrderCard.kt @@ -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? = 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") + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/LikeScreen/CardSneaker.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/CardSneaker.kt similarity index 82% rename from app/src/main/java/com/example/android_programming/composeui/Screens/LikeScreen/CardSneaker.kt rename to app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/CardSneaker.kt index a0e538c..72f4f60 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/LikeScreen/CardSneaker.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/CardSneaker.kt @@ -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)) } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/DeliveryAddress.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/DeliveryAddress.kt index d33d757..4e85400 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/DeliveryAddress.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/DeliveryAddress.kt @@ -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) ) } diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/OrderScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/OrderScreen.kt index b1710ac..9bc230a 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/OrderScreen.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/OrderScreen.kt @@ -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() } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/ShoppingList.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/ShoppingList.kt index ddd71bd..c5d69eb 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/ShoppingList.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/ShoppingList.kt @@ -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) { +fun ShoppingList(list : List, orderViewModel: OrderViewModel) { Row { Column { for(item in list){ - CardSneakerLike(item = item) + CardSneakerLike(item = item, orderViewModel) } } } diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/SubTotal.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/SubTotal.kt index 68a4be9..8e2d1b0 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/SubTotal.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/SubTotal.kt @@ -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) } } } diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/Profile.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/Profile.kt index a0dca25..8fb2f1f 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/Profile.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/Profile.kt @@ -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() } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileCard.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileCard.kt index ee56e32..84f4923 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileCard.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileCard.kt @@ -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") + } } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileScreen.kt index 65340f1..e9f44cd 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileScreen.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileScreen.kt @@ -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) } } diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInCard.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInCard.kt index 16558e5..06d8a71 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInCard.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInCard.kt @@ -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() diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInScreen.kt index 784e199..324ab37 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInScreen.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInScreen.kt @@ -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) { diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignUp/SignUpCard.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignUp/SignUpCard.kt index 357a5c2..f13387e 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignUp/SignUpCard.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignUp/SignUpCard.kt @@ -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() diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignUp/SignUpScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignUp/SignUpScreen.kt index 71a30b4..321a319 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignUp/SignUpScreen.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignUp/SignUpScreen.kt @@ -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) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/dao/OrderDao.kt b/app/src/main/java/com/example/android_programming/dao/OrderDao.kt new file mode 100644 index 0000000..566b1e1 --- /dev/null +++ b/app/src/main/java/com/example/android_programming/dao/OrderDao.kt @@ -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 + + @Query("SELECT * FROM `Order`") + fun getAllOrder(): Flow> + + @Delete + suspend fun delete(order: Order) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/dao/SneakerDao.kt b/app/src/main/java/com/example/android_programming/dao/SneakerDao.kt index 604d9a7..f505a22 100644 --- a/app/src/main/java/com/example/android_programming/dao/SneakerDao.kt +++ b/app/src/main/java/com/example/android_programming/dao/SneakerDao.kt @@ -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> - @Query("SELECT * FROM sneakers WHERE id = :id") + @Query("SELECT * FROM Sneaker WHERE sneakerId = :id") suspend fun getSneakerById(id: Int): Sneaker } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/dao/UserDao.kt b/app/src/main/java/com/example/android_programming/dao/UserDao.kt new file mode 100644 index 0000000..2450b3d --- /dev/null +++ b/app/src/main/java/com/example/android_programming/dao/UserDao.kt @@ -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 +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/database/AppDatabase.kt b/app/src/main/java/com/example/android_programming/database/AppDatabase.kt index d6d35df..0d2a619 100644 --- a/app/src/main/java/com/example/android_programming/database/AppDatabase.kt +++ b/app/src/main/java/com/example/android_programming/database/AppDatabase.kt @@ -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 } } } } diff --git a/app/src/main/java/com/example/android_programming/model/Order.kt b/app/src/main/java/com/example/android_programming/model/Order.kt new file mode 100644 index 0000000..6713162 --- /dev/null +++ b/app/src/main/java/com/example/android_programming/model/Order.kt @@ -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 +) diff --git a/app/src/main/java/com/example/android_programming/model/OrderSneaker.kt b/app/src/main/java/com/example/android_programming/model/OrderSneaker.kt new file mode 100644 index 0000000..2fc6243 --- /dev/null +++ b/app/src/main/java/com/example/android_programming/model/OrderSneaker.kt @@ -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 +) \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/model/OrderWithSneakers.kt b/app/src/main/java/com/example/android_programming/model/OrderWithSneakers.kt new file mode 100644 index 0000000..48143fb --- /dev/null +++ b/app/src/main/java/com/example/android_programming/model/OrderWithSneakers.kt @@ -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 +) + + diff --git a/app/src/main/java/com/example/android_programming/model/PhotoManager.kt b/app/src/main/java/com/example/android_programming/model/PhotoManager.kt index 56ab92a..ca58ee7 100644 --- a/app/src/main/java/com/example/android_programming/model/PhotoManager.kt +++ b/app/src/main/java/com/example/android_programming/model/PhotoManager.kt @@ -1,6 +1,5 @@ package com.example.android_programming.model -import android.util.Log import com.example.android_programming.R class PhotoManager { diff --git a/app/src/main/java/com/example/android_programming/model/RoleEnum.kt b/app/src/main/java/com/example/android_programming/model/RoleEnum.kt new file mode 100644 index 0000000..b51b104 --- /dev/null +++ b/app/src/main/java/com/example/android_programming/model/RoleEnum.kt @@ -0,0 +1,6 @@ +package com.example.android_programming.model + +enum class RoleEnum { + Admin, + User +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/model/Sneaker.kt b/app/src/main/java/com/example/android_programming/model/Sneaker.kt index 833f3ee..c7f4c9b 100644 --- a/app/src/main/java/com/example/android_programming/model/Sneaker.kt +++ b/app/src/main/java/com/example/android_programming/model/Sneaker.kt @@ -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") diff --git a/app/src/main/java/com/example/android_programming/model/SneakerItem.kt b/app/src/main/java/com/example/android_programming/model/SneakerItem.kt deleted file mode 100644 index 440871f..0000000 --- a/app/src/main/java/com/example/android_programming/model/SneakerItem.kt +++ /dev/null @@ -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 { - override fun createFromParcel(parcel: Parcel): SneakerItem { - return SneakerItem(parcel) - } - - override fun newArray(size: Int): Array { - return arrayOfNulls(size) - } - } -} - -fun getSneakers(): List { - 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), - ) -} diff --git a/app/src/main/java/com/example/android_programming/model/User.kt b/app/src/main/java/com/example/android_programming/model/User.kt new file mode 100644 index 0000000..5cdcacd --- /dev/null +++ b/app/src/main/java/com/example/android_programming/model/User.kt @@ -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, +) diff --git a/app/src/main/java/com/example/android_programming/model/UserWithOrder.kt b/app/src/main/java/com/example/android_programming/model/UserWithOrder.kt new file mode 100644 index 0000000..298003e --- /dev/null +++ b/app/src/main/java/com/example/android_programming/model/UserWithOrder.kt @@ -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 +) \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/vmodel/OrderViewModel.kt b/app/src/main/java/com/example/android_programming/vmodel/OrderViewModel.kt new file mode 100644 index 0000000..ca7f62c --- /dev/null +++ b/app/src/main/java/com/example/android_programming/vmodel/OrderViewModel.kt @@ -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>(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 create( + modelClass: Class, + extras: CreationExtras + ): T { + val database = (checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as App).database + return OrderViewModel(database) as T + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/SneakerViewModel.kt b/app/src/main/java/com/example/android_programming/vmodel/SneakerViewModel.kt similarity index 93% rename from app/src/main/java/com/example/android_programming/SneakerViewModel.kt rename to app/src/main/java/com/example/android_programming/vmodel/SneakerViewModel.kt index 9776c6a..a2fe811 100644 --- a/app/src/main/java/com/example/android_programming/SneakerViewModel.kt +++ b/app/src/main/java/com/example/android_programming/vmodel/SneakerViewModel.kt @@ -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 diff --git a/app/src/main/java/com/example/android_programming/vmodel/UserViewModel.kt b/app/src/main/java/com/example/android_programming/vmodel/UserViewModel.kt new file mode 100644 index 0000000..ea54c21 --- /dev/null +++ b/app/src/main/java/com/example/android_programming/vmodel/UserViewModel.kt @@ -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 create( + modelClass: Class, + extras: CreationExtras + ): T { + val database = (checkNotNull(extras[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]) as App).database + return UserViewModel(database) as T + } + } + } +} \ No newline at end of file