Fix: valid some input, delete not used import

This commit is contained in:
ArtemEmelyanov 2023-10-18 13:44:15 +04:00
parent c6bc40d3ff
commit 74007b5979
49 changed files with 792 additions and 603 deletions

View File

@ -73,6 +73,8 @@ dependencies {
implementation ("io.coil-kt:coil-compose:1.4.0") implementation ("io.coil-kt:coil-compose:1.4.0")
implementation ("com.google.code.gson:gson:2.8.8") implementation ("com.google.code.gson:gson:2.8.8")
implementation("androidx.navigation:navigation-compose:2.7.4") implementation("androidx.navigation:navigation-compose:2.7.4")
implementation ("androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0-alpha02")
//ROOM //ROOM
val room_version = "2.5.2" val room_version = "2.5.2"

View File

@ -2,7 +2,18 @@ package com.example.android_programming
import android.app.Application import android.app.Application
import com.example.android_programming.database.AppDatabase 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)} val database by lazy { AppDatabase.getInstance(this)}
override fun onCreate() {
super.onCreate()
// this.deleteDatabase("my-db")
CoroutineScope(Dispatchers.IO).launch {
AppDatabase.populateDatabase()
}
}
} }

View File

@ -6,17 +6,10 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable 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.Modifier
import androidx.compose.ui.platform.LocalContext
import com.example.android_programming.composeui.Header.Header import com.example.android_programming.composeui.Header.Header
import com.example.android_programming.composeui.Navigation.Navigate import com.example.android_programming.composeui.Navigation.Navigate
import com.example.android_programming.database.AppDatabase import com.example.android_programming.model.User
import com.example.android_programming.model.Sneaker
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -29,21 +22,33 @@ class MainActivity : ComponentActivity() {
@Composable @Composable
fun MainContent() { 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( Column(
modifier = Modifier modifier = Modifier.fillMaxSize()
.fillMaxSize()
) { ) {
Header() Header()
Navigate() 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 }
}
}
} }
} }

View File

@ -1,6 +1,7 @@
package com.example.android_programming.composeui.Navigation package com.example.android_programming.composeui.Navigation
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
@ -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.AdminPanel.ChangeSneaker
import com.example.android_programming.composeui.Screens.HomeScreen.AboutSneaker 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.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.OrderScreen.OrderScreen
import com.example.android_programming.composeui.Screens.ProfileScreen.Profile.Person 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.Profile.ProfileScreen
import com.example.android_programming.composeui.Screens.ProfileScreen.SignIn.LoginScreen 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.composeui.Screens.ProfileScreen.SignUp.SignUpScreen
import com.example.android_programming.model.Sneaker 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 import com.google.gson.Gson
@Composable @Composable
fun NavController(navController: NavHostController){ fun NavController(navController: NavHostController){
var orderViewModel: OrderViewModel = viewModel(factory = OrderViewModel.factory)
NavHost( NavHost(
navController = navController, navController = navController,
startDestination = NavItem.Home.route startDestination = NavItem.Home.route
){ ){
composable(NavItem.Home.route){ composable(NavItem.Home.route){
HomeScreen(navController) HomeScreen(navController, orderViewModel)
} }
composable(NavItem.Like.route){ composable(NavItem.MyOrder.route){
LikeScreen() MyOrderScreen(orderViewModel)
} }
composable(NavItem.Order.route){ composable(NavItem.Order.route){
OrderScreen() OrderScreen(orderViewModel, navController)
} }
composable(NavItem.Profile.route){ composable(NavItem.Profile.route){
ProfileScreen(navController) ProfileScreen(navController)
@ -42,10 +44,10 @@ fun NavController(navController: NavHostController){
LoginScreen(navController) LoginScreen(navController)
} }
composable(NavItem.SignUp.route){ composable(NavItem.SignUp.route){
SignUpScreen() SignUpScreen(navController)
} }
composable(NavItem.Person.route){ composable(NavItem.Person.route){
Person() Person(navController)
} }
composable(NavItem.AdminPanel.route){ composable(NavItem.AdminPanel.route){
AdminPanel(navController) AdminPanel(navController)

View File

@ -2,7 +2,6 @@ package com.example.android_programming.composeui.Navigation
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Build 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.Home
import androidx.compose.material.icons.filled.Person import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.ShoppingCart 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?){ sealed class NavItem(val route: String, val icon: ImageVector?){
object Home : NavItem("home", Icons.Default.Home) 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 Order : NavItem("order", Icons.Default.ShoppingCart)
object Profile : NavItem("profile", Icons.Default.Person) object Profile : NavItem("profile", Icons.Default.Person)
object SignIn : NavItem("login", null) object SignIn : NavItem("login", null)

View File

@ -12,9 +12,7 @@ import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
@ -24,22 +22,23 @@ import com.example.android_programming.R
@Composable @Composable
fun Navigate() { fun Navigate() {
val navController = rememberNavController() val navController = rememberNavController()
val listItem = listOf(
val Items = listOf(
NavItem.Home, NavItem.Home,
NavItem.Like,
NavItem.Order, NavItem.Order,
NavItem.Profile, NavItem.Profile,
NavItem.AdminPanel, NavItem.AdminPanel,
) )
Scaffold(bottomBar = { Scaffold(bottomBar = {
// Оставьте код навигационного бара без изменений
BottomNavigation( BottomNavigation(
backgroundColor = Color.White backgroundColor = Color.White
) { ) {
val navBackStackEntry = navController.currentBackStackEntryAsState() val navBackStackEntry = navController.currentBackStackEntryAsState()
val currentState = navBackStackEntry.value val currentState = navBackStackEntry.value
listItem.forEach { it -> Items.forEach { it ->
val isSelected = currentState?.destination?.route == it.route val isSelected = currentState?.destination?.route == it.route
BottomNavigationItem( BottomNavigationItem(
@ -54,7 +53,6 @@ fun Navigate() {
} }
} }
navController.navigate(it.route) navController.navigate(it.route)
}, },
icon = { icon = {
val iconModifier = if (isSelected) { val iconModifier = if (isSelected) {
@ -81,9 +79,3 @@ fun Navigate() {
NavController(navController = navController) NavController(navController = navController)
} }
} }
@Composable
@Preview
fun NavigatePreview() {
Navigate()
}

View File

@ -4,11 +4,9 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
@ -22,11 +20,6 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.material.TextField import androidx.compose.material.TextField
import androidx.compose.runtime.Composable 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.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip 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.TextStyle
import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType 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.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.android_programming.App
import com.example.android_programming.R 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.model.PhotoManager
import com.example.android_programming.vmodel.SneakerViewModel
@Composable @Composable
fun AddPanel(sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)){ fun AddPanel(sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)){

View File

@ -4,6 +4,8 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
@ -14,12 +16,15 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.GlobalUser
import com.example.android_programming.model.RoleEnum
@Composable @Composable
fun AdminPanel(navHostController: NavHostController) { fun AdminPanel(navHostController: NavHostController) {
var isAddPanelVisible by remember { mutableStateOf(false) } var isAddPanelVisible by remember { mutableStateOf(false) }
var isChangePanelVisible by remember { mutableStateOf(false) } var isChangePanelVisible by remember { mutableStateOf(false) }
var showDialog by remember { mutableStateOf(GlobalUser.getInstance().getUser()?.role == RoleEnum.User || GlobalUser.getInstance().getUser()?.role == null) }
if (!showDialog) {
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
@ -36,6 +41,7 @@ fun AdminPanel(navHostController: NavHostController) {
isAddPanelVisible = false isAddPanelVisible = false
} }
) )
}
if (isAddPanelVisible) { if (isAddPanelVisible) {
AddPanel() AddPanel()
@ -45,4 +51,22 @@ fun AdminPanel(navHostController: NavHostController) {
ChangePanel(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")
}
}
)
}
} }

View File

@ -1,29 +1,19 @@
package com.example.android_programming.composeui.Screens.AdminPanel package com.example.android_programming.composeui.Screens.AdminPanel
import androidx.compose.foundation.background 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.Row
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.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.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.example.android_programming.R import com.example.android_programming.R
@Composable @Composable

View File

@ -2,7 +2,6 @@ package com.example.android_programming.composeui.Screens.AdminPanel
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
@ -30,9 +29,8 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.R 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.Sneaker
import com.example.android_programming.model.SneakerItem import com.example.android_programming.vmodel.SneakerViewModel
import com.google.gson.Gson import com.google.gson.Gson
@Composable @Composable

View File

@ -14,9 +14,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.R import com.example.android_programming.vmodel.SneakerViewModel
import com.example.android_programming.SneakerViewModel
import com.example.android_programming.model.SneakerItem
@Composable @Composable
fun ChangePanel(navHostController: NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) { fun ChangePanel(navHostController: NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {

View File

@ -41,10 +41,10 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.android_programming.R 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.PhotoManager
import com.example.android_programming.model.Sneaker import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem import com.example.android_programming.vmodel.SneakerViewModel
@Composable @Composable
fun ChangeSneaker(sneaker: Sneaker, onBackClick: () -> Unit, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) { fun ChangeSneaker(sneaker: Sneaker, onBackClick: () -> Unit, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
val brand = remember {mutableStateOf(sneaker.brand)} val brand = remember {mutableStateOf(sneaker.brand)}
@ -220,7 +220,7 @@ fun ChangeSneaker(sneaker: Sneaker, onBackClick: () -> Unit, sneakerViewModel: S
onClick = { onClick = {
sneakerViewModel.UpdateSneaker( sneakerViewModel.UpdateSneaker(
Sneaker( Sneaker(
id = sneaker.id, sneakerId = sneaker.sneakerId,
brand = brand.value, brand = brand.value,
model = model.value, model = model.value,
description = description.value, description = description.value,

View File

@ -7,9 +7,6 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults 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.Icons
import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@ -26,14 +22,10 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.model.Sneaker import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem
@Composable @Composable
fun AboutSneaker(sneaker: Sneaker, onBackClick: () -> Unit) { fun AboutSneaker(sneaker: Sneaker, onBackClick: () -> Unit) {

View File

@ -1,20 +1,15 @@
package com.example.android_programming.composeui.Screens.HomeScreen package com.example.android_programming.composeui.Screens.HomeScreen
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding 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.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier 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.platform.LocalContext
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController 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.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.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.Sneaker
import com.example.android_programming.model.getSneakers import com.example.android_programming.vmodel.OrderViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable @Composable
fun HomeScreen(navHostController: NavHostController) { fun HomeScreen(navHostController: NavHostController, orderViewModel: OrderViewModel) {
val context = LocalContext.current val context = LocalContext.current
val sneakers = remember { mutableStateListOf<Sneaker>() } val sneakers = remember { mutableStateListOf<Sneaker>() }
Column( Column(
@ -59,6 +47,6 @@ fun HomeScreen(navHostController: NavHostController) {
} }
Sales() Sales()
FilterByBrand() FilterByBrand()
RecyclerView(navHostController = navHostController) RecyclerView(navHostController = navHostController, orderViewModel)
} }
} }

View File

@ -2,10 +2,8 @@ package com.example.android_programming.composeui.Screens.HomeScreen
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale

View File

@ -10,7 +10,7 @@ import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.Icon import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search 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.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember 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.graphics.vector.ImageVector
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.example.android_programming.R import com.example.android_programming.R

View File

@ -30,11 +30,10 @@ import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.model.Sneaker import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem
import com.google.gson.Gson import com.google.gson.Gson
@Composable @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 val maxWidth = (LocalConfiguration.current.screenWidthDp / 2).dp
Box( Box(
@ -87,7 +86,9 @@ fun CardSneaker(item: Sneaker, navController: NavHostController) {
backgroundColor = colorResource(id = R.color.figma_blue), backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White contentColor = Color.White
), ),
onClick = { /*TODO*/ }, onClick = {
onItemSelected(item)
},
modifier = Modifier modifier = Modifier
.size(50.dp, 30.dp) .size(50.dp, 30.dp)
.clip(RoundedCornerShape(10.dp)) .clip(RoundedCornerShape(10.dp))

View File

@ -6,21 +6,17 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding 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.Composable
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.SneakerViewModel import com.example.android_programming.vmodel.OrderViewModel
import com.example.android_programming.model.Sneaker import com.example.android_programming.vmodel.SneakerViewModel
import kotlin.collections.chunked
@Composable @Composable
fun RecyclerView(navHostController : NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) { fun RecyclerView(navHostController : NavHostController, orderViewModel: OrderViewModel, sneakerViewModel: SneakerViewModel = viewModel(factory = SneakerViewModel.factory)) {
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
@ -36,9 +32,12 @@ fun RecyclerView(navHostController : NavHostController, sneakerViewModel: Sneake
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
) { ) {
for (item in chunkedListItem) { for (item in chunkedListItem) {
CardSneaker(item, navHostController) CardSneaker(item, navHostController, orderViewModel.selectedItems) { selectedItem ->
orderViewModel.addSelectedItem(selectedItem)
}
} }
} }
} }
} }
} }

View File

@ -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()
}

View File

@ -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)
}
}
}
}
}
}

View File

@ -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")
}
}
}
}

View File

@ -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.Image
import androidx.compose.foundation.background 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.layout.ContentScale
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import com.example.android_programming.R 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 @Composable
fun CardSneakerLike(item: SneakerItem) { fun CardSneakerLike(item: Sneaker, orderViewModel: OrderViewModel) {
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -41,7 +41,7 @@ fun CardSneakerLike(item: SneakerItem) {
horizontalArrangement = Arrangement.SpaceBetween horizontalArrangement = Arrangement.SpaceBetween
) { ) {
Image( Image(
painter = painterResource(id = item.imageId), painter = painterResource(id = item.photo),
contentDescription = "image", contentDescription = "image",
contentScale = ContentScale.FillWidth, contentScale = ContentScale.FillWidth,
modifier = Modifier modifier = Modifier
@ -55,7 +55,7 @@ fun CardSneakerLike(item: SneakerItem) {
.weight(1f) .weight(1f)
.padding(start = 16.dp) .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) Text(text = "${item.price} USD", color = Color.Red, fontSize = 16.sp)
} }
@ -64,7 +64,9 @@ fun CardSneakerLike(item: SneakerItem) {
backgroundColor = colorResource(id = R.color.figma_blue), backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White contentColor = Color.White
), ),
onClick = { /*TODO*/ }, onClick = {
orderViewModel.removeSelectedItem(item)
},
modifier = Modifier modifier = Modifier
.padding(end = 16.dp) .padding(end = 16.dp)
) { ) {
@ -72,11 +74,3 @@ fun CardSneakerLike(item: SneakerItem) {
} }
} }
} }
@Composable
@Preview
fun CardSneakerLikePreview(){
CardSneakerLike(SneakerItem(R.drawable.sneaker, "Jordan", 159.99))
}

View File

@ -15,10 +15,6 @@ import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.material.TextField import androidx.compose.material.TextField
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
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip 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.font.FontWeight
import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType 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.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.vmodel.OrderViewModel
@Composable @Composable
@Preview fun DeliveryAddress(orderViewModel: OrderViewModel) {
fun DeliveryAddress() {
var address by remember { mutableStateOf("") }
var city by remember { mutableStateOf("") }
var number by remember { mutableStateOf("") }
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -58,35 +50,8 @@ fun DeliveryAddress() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = address, value = orderViewModel.city.value,
onValueChange = { address = it }, onValueChange = { orderViewModel.city.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 = "Address",
style = TextStyle(fontSize = 12.sp)
)
}
)
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = city,
onValueChange = { city = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -112,8 +77,8 @@ fun DeliveryAddress() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = number, value = orderViewModel.street.value,
onValueChange = { number = it }, onValueChange = { orderViewModel.street.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -130,7 +95,34 @@ fun DeliveryAddress() {
), ),
placeholder = { placeholder = {
Text( 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) style = TextStyle(fontSize = 12.sp)
) )
} }

View File

@ -1,51 +1,28 @@
package com.example.android_programming.composeui.Screens.OrderScreen package com.example.android_programming.composeui.Screens.OrderScreen
import androidx.compose.foundation.background 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.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding 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.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.foundation.verticalScroll
import androidx.compose.material.Button import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.material.TextField
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
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource 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.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.R
import com.example.android_programming.composeui.Screens.LikeScreen.CardSneakerLike import com.example.android_programming.vmodel.OrderViewModel
import com.example.android_programming.model.SneakerItem
@Composable @Composable
fun OrderScreen() { fun OrderScreen(orderViewModel: OrderViewModel, navHostController: NavHostController) {
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
@ -53,16 +30,9 @@ fun OrderScreen() {
.padding(bottom = 60.dp) .padding(bottom = 60.dp)
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
){ ){
DeliveryAddress() DeliveryAddress(orderViewModel)
ShoppingList(listOf( ShoppingList(orderViewModel.selectedItems, orderViewModel)
SneakerItem(R.drawable.sneaker, "Jordan", 159.99), SubTotal(orderViewModel)
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()
Button( Button(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
@ -70,7 +40,12 @@ fun OrderScreen() {
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
if(GlobalUser.getInstance().getUser() != null){
orderViewModel.createOrder()
navHostController.navigate("home")
}else{
navHostController.navigate("login")
}
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -80,9 +55,3 @@ fun OrderScreen() {
} }
} }
} }
@Composable
@Preview
fun OrderScreenPreview(){
OrderScreen()
}

View File

@ -3,15 +3,15 @@ package com.example.android_programming.composeui.Screens.OrderScreen
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import com.example.android_programming.composeui.Screens.LikeScreen.CardSneakerLike import com.example.android_programming.model.Sneaker
import com.example.android_programming.model.SneakerItem import com.example.android_programming.vmodel.OrderViewModel
@Composable @Composable
fun ShoppingList(list : List<SneakerItem>) { fun ShoppingList(list : List<Sneaker>, orderViewModel: OrderViewModel) {
Row { Row {
Column { Column {
for(item in list){ for(item in list){
CardSneakerLike(item = item) CardSneakerLike(item = item, orderViewModel)
} }
} }
} }

View File

@ -12,14 +12,13 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.vmodel.OrderViewModel
@Composable @Composable
@Preview fun SubTotal(orderViewModel: OrderViewModel) {
fun SubTotal() {
Column( Column(
modifier = Modifier modifier = Modifier
.padding(16.dp) .padding(16.dp)
@ -42,7 +41,7 @@ fun SubTotal() {
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
horizontalArrangement = Arrangement.End horizontalArrangement = Arrangement.End
){ ){
Text(text = "319.99 $", fontSize = 15.sp) Text(text = "${orderViewModel.getSubTotal()} $", fontSize = 15.sp)
} }
} }
Row( Row(
@ -61,7 +60,7 @@ fun SubTotal() {
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
horizontalArrangement = Arrangement.End horizontalArrangement = Arrangement.End
){ ){
Text(text = "180 $", fontSize = 15.sp) Text(text = "${"%.2f".format(orderViewModel.getSubTotal() * 0.05)} $", fontSize = 15.sp)
} }
} }
Row( Row(
@ -80,7 +79,7 @@ fun SubTotal() {
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
horizontalArrangement = Arrangement.End horizontalArrangement = Arrangement.End
){ ){
Text(text = "1900 $", fontSize = 15.sp) Text(text = "${"%.2f".format(orderViewModel.getSubTotal() + orderViewModel.getSubTotal() * 0.05)} $", fontSize = 15.sp)
} }
} }
} }

View File

@ -1,44 +1,44 @@
package com.example.android_programming.composeui.Screens.ProfileScreen.Profile package com.example.android_programming.composeui.Screens.ProfileScreen.Profile
import androidx.compose.foundation.Image
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.material.Button
import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.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.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.R
@Composable @Composable
fun Person() { fun Person(navHostController: NavHostController) {
Column( Column(
modifier = Modifier modifier = Modifier
.background(Color.White) .background(Color.White)
.fillMaxSize(), .fillMaxSize(),
verticalArrangement = Arrangement.Center verticalArrangement = Arrangement.Center
) { ) {
ProfileCard() Button(
onClick = {
GlobalUser.getInstance().setUser(null)
navHostController.navigate("profile")
},
colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.white),
contentColor = Color.Black
),
modifier = Modifier
.padding(16.dp, 0.dp)
) {
Text("Exit")
}
ProfileCard(navHostController)
} }
} }
@Preview
@Composable
fun PersonPreview(){
Person()
}

View File

@ -6,32 +6,32 @@ import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row 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.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.example.android_programming.GlobalUser
import com.example.android_programming.R import com.example.android_programming.R
@Preview
@Composable @Composable
fun ProfileCard() { fun ProfileCard(navHostController: NavHostController) {
val globalUser = GlobalUser.getInstance().getUser()
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -55,17 +55,32 @@ fun ProfileCard() {
.clip(CircleShape) .clip(CircleShape)
) )
if (globalUser != null) {
Text( Text(
text = "Шайлушай", text = "${globalUser.name} ${globalUser.surname}",
fontSize = 18.sp, fontSize = 18.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text( Text(
text = "shailushai@example.com", text = globalUser.email,
fontSize = 16.sp, fontSize = 16.sp,
color = Color.Gray color = Color.Gray
) )
} }
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White
),
onClick = {
navHostController.navigate("myorder")
},
modifier = Modifier
.fillMaxWidth()
.padding(16.dp, 16.dp, 16.dp, 0.dp)
) {
Text("My order")
}
}
} }
} }

View File

@ -1,78 +1,19 @@
package com.example.android_programming.composeui.Screens.ProfileScreen.Profile; package com.example.android_programming.composeui.Screens.ProfileScreen.Profile;
import androidx.compose.foundation.background import androidx.compose.runtime.Composable
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.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.example.android_programming.R import com.example.android_programming.GlobalUser
import com.example.android_programming.composeui.Navigation.NavController
import com.example.android_programming.composeui.Screens.ProfileScreen.SignIn.LoginScreen import com.example.android_programming.composeui.Screens.ProfileScreen.SignIn.LoginScreen
@Composable @Composable
fun ProfileScreen(navController: NavHostController) { fun ProfileScreen(navController: NavHostController) {
Column( val globalUser: GlobalUser = GlobalUser.getInstance()
modifier = Modifier if(globalUser.getUser() != null){
.fillMaxSize() Person(navController)
.background(Color.White), }else{
verticalArrangement = Arrangement.Center, LoginScreen(navController = navController)
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")
}
}
} }
} }

View File

@ -6,7 +6,6 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
@ -34,14 +33,15 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation 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.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.android_programming.R import com.example.android_programming.R
import com.example.android_programming.vmodel.UserViewModel
@Composable @Composable
fun SignInCard(navController: NavHostController) { fun SignInCard(navController: NavHostController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)) {
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -54,8 +54,8 @@ fun SignInCard(navController: NavHostController) {
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center verticalArrangement = Arrangement.Center
){ ){
var username by remember { mutableStateOf("") } var isEmailValid by remember { mutableStateOf(true) }
var password by remember { mutableStateOf("") } var isPasswordValid by remember { mutableStateOf(true) }
Text( Text(
text = "Sign In", text = "Sign In",
@ -66,8 +66,10 @@ fun SignInCard(navController: NavHostController) {
) )
TextField( TextField(
value = username, value = userViewModel.email.value,
onValueChange = { username = it }, onValueChange = {
userViewModel.email.value = it
isEmailValid = userViewModel.isValidEmail(it)},
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -85,17 +87,28 @@ fun SignInCard(navController: NavHostController) {
), ),
placeholder = { placeholder = {
Text( Text(
text = "Username", text = "Email",
style = TextStyle(fontSize = 12.sp) 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)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = password, value = userViewModel.password.value,
onValueChange = { password = it }, onValueChange = {
userViewModel.password.value = it
isPasswordValid = it.isNotEmpty()
},
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -121,13 +134,22 @@ fun SignInCard(navController: NavHostController) {
} }
) )
if (!isPasswordValid) {
Text(
text = "Password is required",
color = Color.Red,
style = TextStyle(fontSize = 12.sp)
)
}
Button( Button(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = colorResource(id = R.color.figma_blue), backgroundColor = colorResource(id = R.color.figma_blue),
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
userViewModel.authUser()
navController.navigate("profile")
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()

View File

@ -1,37 +1,13 @@
package com.example.android_programming.composeui.Screens.ProfileScreen.SignIn package com.example.android_programming.composeui.Screens.ProfileScreen.SignIn
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.border import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.BasicTextField import androidx.compose.runtime.Composable
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.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color 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 androidx.navigation.NavHostController
import com.example.android_programming.R
import androidx.compose.material.Text as Text1
@Composable @Composable
fun LoginScreen(navController: NavHostController) { fun LoginScreen(navController: NavHostController) {

View File

@ -17,10 +17,6 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.material.TextField import androidx.compose.material.TextField
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
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip 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.ImeAction
import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation 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.dp
import androidx.compose.ui.unit.sp 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.R
import com.example.android_programming.vmodel.UserViewModel
@Composable @Composable
@Preview fun SignUpCard(navHostController: NavHostController, userViewModel: UserViewModel = viewModel(factory = UserViewModel.factory)) {
fun SignUpCard() {
var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var sex by remember { mutableStateOf("") }
var name by remember { mutableStateOf("") }
var surname by remember { mutableStateOf("") }
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -67,36 +59,8 @@ fun SignUpCard() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = username, value = userViewModel.name.value,
onValueChange = { username = it }, onValueChange = { userViewModel.name.value = it },
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
.padding(16.dp, 0.dp)
.border(1.dp, Color.Gray, RoundedCornerShape(4.dp)),
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(
onNext = {
}
),
placeholder = {
Text(
text = "Username",
style = TextStyle(fontSize = 12.sp)
)
}
)
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = name,
onValueChange = { name = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -123,8 +87,8 @@ fun SignUpCard() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = surname, value = userViewModel.surname.value,
onValueChange = { surname = it }, onValueChange = { userViewModel.surname.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -151,8 +115,8 @@ fun SignUpCard() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = sex, value = userViewModel.email.value,
onValueChange = { sex = it }, onValueChange = { userViewModel.email.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -170,7 +134,7 @@ fun SignUpCard() {
), ),
placeholder = { placeholder = {
Text( Text(
text = "Sex", text = "Email",
style = TextStyle(fontSize = 12.sp) style = TextStyle(fontSize = 12.sp)
) )
} }
@ -179,8 +143,8 @@ fun SignUpCard() {
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
TextField( TextField(
value = password, value = userViewModel.password.value,
onValueChange = { password = it }, onValueChange = { userViewModel.password.value = it },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.height(50.dp) .height(50.dp)
@ -212,7 +176,8 @@ fun SignUpCard() {
contentColor = Color.White contentColor = Color.White
), ),
onClick = { onClick = {
userViewModel.createUser()
navHostController.navigate("login")
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()

View File

@ -1,51 +1,21 @@
package com.example.android_programming.composeui.Screens.ProfileScreen.SignUp package com.example.android_programming.composeui.Screens.ProfileScreen.SignUp
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize 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.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.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource import androidx.navigation.NavHostController
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
@Composable @Composable
fun SignUpScreen() { fun SignUpScreen(navHostController: NavHostController) {
Column(modifier = Modifier Column(modifier = Modifier
.fillMaxSize() .fillMaxSize()
.background(Color.White), .background(Color.White),
verticalArrangement = Arrangement.Center verticalArrangement = Arrangement.Center
) { ) {
SignUpCard() SignUpCard(navHostController)
} }
} }

View File

@ -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)
}

View File

@ -14,14 +14,14 @@ interface SneakerDao {
suspend fun insert(sneaker: Sneaker) suspend fun insert(sneaker: Sneaker)
@Update @Update
suspend fun update(sneaker: Sneaker?) suspend fun update(sneaker: Sneaker)
@Delete @Delete
suspend fun delete(sneaker: Sneaker) suspend fun delete(sneaker: Sneaker)
@Query("SELECT*FROM sneakers") @Query("SELECT*FROM Sneaker")
fun getAllSneakers(): Flow<List<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 suspend fun getSneakerById(id: Int): Sneaker
} }

View File

@ -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>
}

View File

@ -4,12 +4,25 @@ import android.content.Context
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase 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.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.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 class AppDatabase : RoomDatabase() {
abstract fun sneakerDao(): SneakerDao abstract fun sneakerDao(): SneakerDao
abstract fun userDao(): UserDao
abstract fun orderDao(): OrderDao
companion object { companion object {
private const val DB_NAME: String = "my-db" private const val DB_NAME: String = "my-db"
@ -17,15 +30,48 @@ abstract class AppDatabase : RoomDatabase() {
@Volatile @Volatile
private var INSTANCE: AppDatabase? = null 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) { return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder( Room.databaseBuilder(
context.applicationContext, appContext,
AppDatabase::class.java, AppDatabase::class.java,
DB_NAME DB_NAME
).build() )
INSTANCE = instance .addCallback(object : Callback() {
instance override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
CoroutineScope(Dispatchers.IO).launch {
populateDatabase()
}
}
})
.fallbackToDestructiveMigration()
.build()
.also { INSTANCE = it }
} }
} }
} }

View File

@ -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
)

View File

@ -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
)

View File

@ -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>
)

View File

@ -1,6 +1,5 @@
package com.example.android_programming.model package com.example.android_programming.model
import android.util.Log
import com.example.android_programming.R import com.example.android_programming.R
class PhotoManager { class PhotoManager {

View File

@ -0,0 +1,6 @@
package com.example.android_programming.model
enum class RoleEnum {
Admin,
User
}

View File

@ -1,14 +1,13 @@
package com.example.android_programming.model package com.example.android_programming.model
import androidx.compose.runtime.MutableState
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
@Entity(tableName = "sneakers") @Entity
data class Sneaker( data class Sneaker(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
val id: Int? = null, val sneakerId: Int? = null,
@ColumnInfo(name = "Brand") @ColumnInfo(name = "Brand")
val brand: String, val brand: String,
@ColumnInfo(name = "Model") @ColumnInfo(name = "Model")

View File

@ -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),
)
}

View File

@ -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,
)

View File

@ -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>
)

View File

@ -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
}
}
}
}

View File

@ -1,4 +1,4 @@
package com.example.android_programming package com.example.android_programming.vmodel
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
@ -6,7 +6,8 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras 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.database.AppDatabase
import com.example.android_programming.model.Sneaker import com.example.android_programming.model.Sneaker
import kotlinx.coroutines.launch import kotlinx.coroutines.launch

View File

@ -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
}
}
}
}