Feature: add basket model, refactor orderviewmodel
This commit is contained in:
parent
14dabf5f60
commit
52d4438a41
@ -1,5 +1,6 @@
|
||||
package com.example.android_programming
|
||||
|
||||
import com.example.android_programming.repository.BasketRepository
|
||||
import com.example.android_programming.repository.OrderRepository
|
||||
import com.example.android_programming.repository.SneakerRepository
|
||||
import com.example.android_programming.repository.UserRepository
|
||||
@ -8,4 +9,5 @@ interface AppContainer {
|
||||
val sneakerRepo: SneakerRepository
|
||||
val userRepo: UserRepository
|
||||
val orderRepo: OrderRepository
|
||||
val basketRepo: BasketRepository
|
||||
}
|
@ -2,6 +2,8 @@ package com.example.android_programming
|
||||
|
||||
import android.content.Context
|
||||
import com.example.android_programming.database.AppDatabase
|
||||
import com.example.android_programming.repository.BasketRepoImpl
|
||||
import com.example.android_programming.repository.BasketRepository
|
||||
import com.example.android_programming.repository.OrderRepoImpl
|
||||
import com.example.android_programming.repository.OrderRepository
|
||||
import com.example.android_programming.repository.SneakerRepoImpl
|
||||
@ -19,4 +21,7 @@ class AppDataContainer(private val context: Context) : AppContainer {
|
||||
override val orderRepo: OrderRepository by lazy {
|
||||
OrderRepoImpl(AppDatabase.getInstance(context).orderDao())
|
||||
}
|
||||
override val basketRepo: BasketRepository by lazy {
|
||||
BasketRepoImpl(AppDatabase.getInstance(context).basketDao())
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ import com.example.android_programming.model.User
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
// this.deleteDatabase("my-db")
|
||||
setContent {
|
||||
MainContent()
|
||||
}
|
||||
|
@ -24,19 +24,18 @@ import com.google.gson.Gson
|
||||
|
||||
@Composable
|
||||
fun NavController(navController: NavHostController){
|
||||
var orderViewModel: OrderViewModel = viewModel(factory = AppViewModelProvider.Factory)
|
||||
NavHost(
|
||||
navController = navController,
|
||||
startDestination = NavItem.Home.route
|
||||
){
|
||||
composable(NavItem.Home.route){
|
||||
HomeScreen(navController, orderViewModel)
|
||||
HomeScreen(navController)
|
||||
}
|
||||
composable(NavItem.MyOrder.route){
|
||||
MyOrderScreen(orderViewModel)
|
||||
MyOrderScreen()
|
||||
}
|
||||
composable(NavItem.Order.route){
|
||||
OrderScreen(navController, orderViewModel)
|
||||
OrderScreen(navController)
|
||||
}
|
||||
composable(NavItem.Profile.route){
|
||||
ProfileScreen(navController)
|
||||
|
@ -27,7 +27,7 @@ import com.example.android_programming.vmodel.AppViewModelProvider
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
@Composable
|
||||
fun HomeScreen(navHostController: NavHostController, orderViewModel: OrderViewModel) {
|
||||
fun HomeScreen(navHostController: NavHostController) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@ -47,6 +47,6 @@ fun HomeScreen(navHostController: NavHostController, orderViewModel: OrderViewMo
|
||||
}
|
||||
Sales()
|
||||
FilterByBrand()
|
||||
RecyclerView(navHostController = navHostController, orderViewModel)
|
||||
RecyclerView(navHostController = navHostController)
|
||||
}
|
||||
}
|
@ -27,13 +27,19 @@ import androidx.compose.ui.platform.LocalConfiguration
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.model.BasketSneakers
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.vmodel.AppViewModelProvider
|
||||
import com.example.android_programming.vmodel.BasketViewModel
|
||||
import com.example.android_programming.vmodel.SneakerViewModel
|
||||
import com.google.gson.Gson
|
||||
|
||||
@Composable
|
||||
fun CardSneaker(item: Sneaker, navController: NavHostController, selectedItems: List<Sneaker>, onItemSelected: (Sneaker) -> Unit) {
|
||||
fun CardSneaker(item: Sneaker, navController: NavHostController, basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
val maxWidth = (LocalConfiguration.current.screenWidthDp / 2).dp
|
||||
|
||||
Box(
|
||||
@ -87,7 +93,11 @@ fun CardSneaker(item: Sneaker, navController: NavHostController, selectedItems:
|
||||
contentColor = Color.White
|
||||
),
|
||||
onClick = {
|
||||
onItemSelected(item)
|
||||
if(GlobalUser.getInstance().getUser() == null){
|
||||
navController.navigate("login")
|
||||
}else{
|
||||
basketViewModel.addToBasket(BasketSneakers(GlobalUser.getInstance().getUser()?.userId!!, item.sneakerId!!))
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
.size(50.dp, 30.dp)
|
||||
|
@ -18,7 +18,7 @@ import com.example.android_programming.vmodel.OrderViewModel
|
||||
import com.example.android_programming.vmodel.SneakerViewModel
|
||||
|
||||
@Composable
|
||||
fun RecyclerView(navHostController: NavHostController, orderViewModel: OrderViewModel, sneakerViewModel: SneakerViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
fun RecyclerView(navHostController: NavHostController, sneakerViewModel: SneakerViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@ -35,9 +35,7 @@ fun RecyclerView(navHostController: NavHostController, orderViewModel: OrderView
|
||||
) { index: Int ->
|
||||
val sneaker: Sneaker? = sneakerLazyPagingItems[index]
|
||||
if (sneaker != null) {
|
||||
CardSneaker(sneaker, navHostController, orderViewModel.selectedItems) { selectedItem ->
|
||||
orderViewModel.addSelectedItem(selectedItem)
|
||||
}
|
||||
CardSneaker(sneaker, navHostController)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,8 +25,7 @@ import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Composable
|
||||
fun MyOrderScreen(orderViewModel: OrderViewModel) {
|
||||
/*val userWithOrder by orderViewModel.database.userDao().getUserOrders(GlobalUser.getInstance().getUser()?.userId!!).collectAsState(null)*/
|
||||
fun MyOrderScreen(orderViewModel: OrderViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
val userId = GlobalUser.getInstance().getUser()?.userId
|
||||
val userWithOrder = orderViewModel.getOrderList(userId!!).collectAsState(null).value?.orders
|
||||
|
||||
@ -52,7 +51,7 @@ fun MyOrderScreen(orderViewModel: OrderViewModel) {
|
||||
) {
|
||||
if (userWithOrder != null) {
|
||||
for (item in userWithOrder) {
|
||||
OrderCard(item, orderViewModel)
|
||||
OrderCard(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,19 +22,21 @@ 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 androidx.lifecycle.viewmodel.compose.viewModel
|
||||
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.AppViewModelProvider
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
import java.util.Date
|
||||
|
||||
@Composable
|
||||
fun OrderCard(order: Order, orderViewModel: OrderViewModel){
|
||||
|
||||
val SneakerList = order?.orderId?.let { orderViewModel.getOrderWithSneakers(it) }
|
||||
fun OrderCard(order: Order, orderViewModel: OrderViewModel = viewModel(factory = AppViewModelProvider.Factory)){
|
||||
|
||||
val SneakerList = order?.orderId?.let {
|
||||
orderViewModel.getOrderWithSneakers(it)
|
||||
}
|
||||
val sneakerWithOrder by SneakerList!!.collectAsState(null)
|
||||
|
||||
val sneakerList: List<Sneaker>? = sneakerWithOrder?.sneakers
|
||||
Row(
|
||||
modifier = Modifier
|
||||
@ -71,7 +73,7 @@ fun OrderCard(order: Order, orderViewModel: OrderViewModel){
|
||||
contentColor = Color.White
|
||||
),
|
||||
onClick = {
|
||||
orderViewModel.deleteOrder(order)
|
||||
orderViewModel.deleteOrder(order)
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
|
@ -25,12 +25,16 @@ import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.vmodel.AppViewModelProvider
|
||||
import com.example.android_programming.vmodel.BasketViewModel
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
@Composable
|
||||
fun CardSneakerLike(item: Sneaker, orderViewModel: OrderViewModel) {
|
||||
fun CardSneakerLike(item: Sneaker, basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -65,7 +69,7 @@ fun CardSneakerLike(item: Sneaker, orderViewModel: OrderViewModel) {
|
||||
contentColor = Color.White
|
||||
),
|
||||
onClick = {
|
||||
orderViewModel.removeSelectedItem(item)
|
||||
basketViewModel.deleteSneakerFromBasket(GlobalUser.getInstance().getUser()?.userId!!, item.sneakerId!!)
|
||||
},
|
||||
modifier = Modifier
|
||||
.padding(end = 16.dp)
|
||||
|
@ -11,20 +11,25 @@ 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.graphics.Color
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.vmodel.AppViewModelProvider
|
||||
import com.example.android_programming.vmodel.BasketViewModel
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
|
||||
@Composable
|
||||
fun OrderScreen(navHostController: NavHostController, orderViewModel: OrderViewModel) {
|
||||
fun OrderScreen(navHostController: NavHostController, basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory), orderViewModel: OrderViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@ -33,9 +38,16 @@ fun OrderScreen(navHostController: NavHostController, orderViewModel: OrderViewM
|
||||
.verticalScroll(rememberScrollState())
|
||||
){
|
||||
DeliveryAddress(orderViewModel)
|
||||
ShoppingList(orderViewModel.selectedItems, orderViewModel)
|
||||
SubTotal(orderViewModel)
|
||||
|
||||
val userId = GlobalUser.getInstance().getUser()?.userId
|
||||
if (userId != null) {
|
||||
val list by basketViewModel.getBasketSneakers(userId!!).collectAsState(initial = null)
|
||||
val SneakerList: List<Sneaker>? = list?.sneakers
|
||||
if (SneakerList != null) {
|
||||
orderViewModel.updateSelectedItems(SneakerList)
|
||||
ShoppingList(SneakerList)
|
||||
SubTotal(orderViewModel)
|
||||
}
|
||||
}
|
||||
Button(
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
backgroundColor = colorResource(id = R.color.figma_blue),
|
||||
@ -56,4 +68,5 @@ fun OrderScreen(navHostController: NavHostController, orderViewModel: OrderViewM
|
||||
Text("Confirm order")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,11 +7,11 @@ import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.vmodel.OrderViewModel
|
||||
|
||||
@Composable
|
||||
fun ShoppingList(list : List<Sneaker>, orderViewModel: OrderViewModel) {
|
||||
fun ShoppingList(list : List<Sneaker>) {
|
||||
Row {
|
||||
Column {
|
||||
for(item in list){
|
||||
CardSneakerLike(item = item, orderViewModel)
|
||||
CardSneakerLike(item = item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,14 @@ package com.example.android_programming.composeui.Screens.ProfileScreen.Profile;
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import com.example.android_programming.GlobalUser
|
||||
import com.example.android_programming.composeui.Screens.ProfileScreen.SignIn.LoginScreen
|
||||
import com.example.android_programming.model.Basket
|
||||
import com.example.android_programming.vmodel.AppViewModelProvider
|
||||
import com.example.android_programming.vmodel.BasketViewModel
|
||||
|
||||
@Composable
|
||||
fun ProfileScreen(navController: NavHostController) {
|
||||
|
@ -37,12 +37,15 @@ 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.GlobalUser
|
||||
import com.example.android_programming.R
|
||||
import com.example.android_programming.model.Basket
|
||||
import com.example.android_programming.vmodel.AppViewModelProvider
|
||||
import com.example.android_programming.vmodel.BasketViewModel
|
||||
import com.example.android_programming.vmodel.UserViewModel
|
||||
|
||||
@Composable
|
||||
fun SignInCard(navController: NavHostController, userViewModel: UserViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
fun SignInCard(navController: NavHostController, userViewModel: UserViewModel = viewModel(factory = AppViewModelProvider.Factory), basketViewModel: BasketViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
|
@ -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 com.example.android_programming.model.Basket
|
||||
import com.example.android_programming.model.BasketSneakers
|
||||
import com.example.android_programming.model.BasketWithSneakers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface BasketDao {
|
||||
@Insert
|
||||
suspend fun createBasket(basket: Basket):Long
|
||||
|
||||
@Insert
|
||||
suspend fun insertBasketSneaker(basketSneaker: BasketSneakers)
|
||||
|
||||
@Query("SELECT * FROM 'Basket' WHERE creatorUserId = :id")
|
||||
fun getBasketWithSneakers(id: Int): Flow<BasketWithSneakers>
|
||||
|
||||
@Query("SELECT * FROM 'Basket'")
|
||||
fun getAllBasket(): Flow<List<Basket>>
|
||||
|
||||
@Delete
|
||||
suspend fun delete(basket: Basket)
|
||||
|
||||
@Query("DELETE FROM 'BasketSneakers' WHERE basketId = :basketId AND sneakerId = :sneakerId")
|
||||
suspend fun removeSneakerFromBasket(basketId: Int, sneakerId: Int)
|
||||
}
|
@ -6,9 +6,12 @@ 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.BasketDao
|
||||
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.Basket
|
||||
import com.example.android_programming.model.BasketSneakers
|
||||
import com.example.android_programming.model.Order
|
||||
import com.example.android_programming.model.OrderSneaker
|
||||
import com.example.android_programming.model.RoleEnum
|
||||
@ -18,11 +21,12 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Database(entities = [Sneaker::class, User::class, Order::class, OrderSneaker::class], version = 5)
|
||||
@Database(entities = [Sneaker::class, User::class, Order::class, OrderSneaker::class, Basket::class, BasketSneakers::class], version = 6)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
abstract fun sneakerDao(): SneakerDao
|
||||
abstract fun userDao(): UserDao
|
||||
abstract fun orderDao(): OrderDao
|
||||
abstract fun basketDao(): BasketDao
|
||||
|
||||
companion object {
|
||||
private const val DB_NAME: String = "my-db"
|
||||
@ -51,6 +55,13 @@ abstract class AppDatabase : RoomDatabase() {
|
||||
sneakerDao.insert(sneaker3)
|
||||
sneakerDao.insert(sneaker4)
|
||||
// Order
|
||||
val basketDao = database.basketDao()
|
||||
val basket1 = Basket(1,1)
|
||||
val basket2 = Basket(2,2)
|
||||
val basket3 = Basket(3,3)
|
||||
basketDao.createBasket(basket1)
|
||||
basketDao.createBasket(basket2)
|
||||
basketDao.createBasket(basket3)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,13 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity
|
||||
class Basket (
|
||||
@PrimaryKey
|
||||
val basketId: Int? = null,
|
||||
@ColumnInfo(name = "CreatorUserId")
|
||||
val creatorUserId: Int
|
||||
)
|
@ -0,0 +1,10 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(primaryKeys = ["basketId", "sneakerId"])
|
||||
class BasketSneakers (
|
||||
val basketId: Int,
|
||||
val sneakerId: Int,
|
||||
)
|
@ -0,0 +1,15 @@
|
||||
package com.example.android_programming.model
|
||||
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Junction
|
||||
import androidx.room.Relation
|
||||
|
||||
class BasketWithSneakers (
|
||||
@Embedded val basket: Basket,
|
||||
@Relation(
|
||||
parentColumn = "basketId",
|
||||
entityColumn = "sneakerId",
|
||||
associateBy = Junction(BasketSneakers::class)
|
||||
)
|
||||
val sneakers: List<Sneaker>
|
||||
)
|
@ -0,0 +1,16 @@
|
||||
package com.example.android_programming.repository
|
||||
|
||||
import com.example.android_programming.dao.BasketDao
|
||||
import com.example.android_programming.model.Basket
|
||||
import com.example.android_programming.model.BasketSneakers
|
||||
import com.example.android_programming.model.BasketWithSneakers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
class BasketRepoImpl(private val basketDao: BasketDao): BasketRepository {
|
||||
override suspend fun createBasket(basket: Basket): Long = basketDao.createBasket(basket)
|
||||
override suspend fun removeSneakerFromBasket(basketId: Int, sneakerId: Int) = basketDao.removeSneakerFromBasket(basketId, sneakerId)
|
||||
override suspend fun insertBasketSneaker(basketSneaker: BasketSneakers) = basketDao.insertBasketSneaker(basketSneaker)
|
||||
override fun getBasketWithSneakers(id: Int): Flow<BasketWithSneakers> = basketDao.getBasketWithSneakers(id)
|
||||
override fun getAllBasket(): Flow<List<Basket>> = basketDao.getAllBasket()
|
||||
override suspend fun delete(basket: Basket) = basketDao.delete(basket)
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.example.android_programming.repository
|
||||
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import com.example.android_programming.model.Basket
|
||||
import com.example.android_programming.model.BasketSneakers
|
||||
import com.example.android_programming.model.BasketWithSneakers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface BasketRepository {
|
||||
suspend fun insertBasketSneaker(basketSneaker: BasketSneakers)
|
||||
fun getBasketWithSneakers(id: Int): Flow<BasketWithSneakers>
|
||||
fun getAllBasket(): Flow<List<Basket>>
|
||||
suspend fun delete(basket: Basket)
|
||||
suspend fun createBasket(basket: Basket):Long
|
||||
suspend fun removeSneakerFromBasket(basketId: Int, sneakerId: Int)
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.example.android_programming.repository
|
||||
|
||||
import com.example.android_programming.model.Basket
|
||||
import com.example.android_programming.model.User
|
||||
import com.example.android_programming.model.UserWithOrder
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
@ -17,6 +17,9 @@ object AppViewModelProvider {
|
||||
initializer {
|
||||
OrderViewModel(app().container.orderRepo)
|
||||
}
|
||||
initializer {
|
||||
BasketViewModel(app().container.basketRepo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.example.android_programming.vmodel
|
||||
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.android_programming.model.Basket
|
||||
import com.example.android_programming.model.BasketSneakers
|
||||
import com.example.android_programming.model.BasketWithSneakers
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.repository.BasketRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class BasketViewModel(private val basketRepository: BasketRepository): ViewModel() {
|
||||
|
||||
fun addToBasket(basketSneakers: BasketSneakers) = viewModelScope.launch {
|
||||
basketRepository.insertBasketSneaker(basketSneakers)
|
||||
}
|
||||
|
||||
fun getBasketSneakers(id: Int): Flow<BasketWithSneakers> {
|
||||
return basketRepository.getBasketWithSneakers(id)
|
||||
}
|
||||
|
||||
fun deleteSneakerFromBasket(basketId: Int, sneakerId: Int) = viewModelScope.launch {
|
||||
basketRepository.removeSneakerFromBasket(basketId, sneakerId)
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ import com.example.android_programming.model.OrderSneaker
|
||||
import com.example.android_programming.model.OrderWithSneakers
|
||||
import com.example.android_programming.model.Sneaker
|
||||
import com.example.android_programming.model.UserWithOrder
|
||||
import com.example.android_programming.repository.BasketRepository
|
||||
import com.example.android_programming.repository.OrderRepository
|
||||
import com.example.android_programming.repository.SneakerRepository
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
@ -23,14 +24,15 @@ import kotlinx.coroutines.launch
|
||||
import java.util.Date
|
||||
|
||||
class OrderViewModel(private val orderRepository: OrderRepository) : ViewModel() {
|
||||
private var _selectedItems = mutableStateOf<List<Sneaker>>(emptyList())
|
||||
val selectedItems get() = _selectedItems.value
|
||||
|
||||
var city = mutableStateOf("")
|
||||
val street = mutableStateOf("")
|
||||
val house = mutableStateOf("")
|
||||
private val _selectedItems = MutableLiveData<List<Sneaker>>()
|
||||
val selectedItems: LiveData<List<Sneaker>> get() = _selectedItems
|
||||
|
||||
fun addSelectedItem(item: Sneaker) {
|
||||
_selectedItems.value = _selectedItems.value + item
|
||||
fun updateSelectedItems(items: List<Sneaker>) {
|
||||
_selectedItems.value = items
|
||||
}
|
||||
|
||||
fun deleteOrder(order: Order) = viewModelScope.launch {
|
||||
@ -41,12 +43,6 @@ class OrderViewModel(private val orderRepository: OrderRepository) : ViewModel()
|
||||
return orderRepository.getUserOrders(id)
|
||||
}
|
||||
|
||||
fun removeSelectedItem(item: Sneaker) {
|
||||
val updatedItems = _selectedItems.value.toMutableList()
|
||||
updatedItems.remove(item)
|
||||
_selectedItems.value = updatedItems
|
||||
}
|
||||
|
||||
fun getOrderWithSneakers(id: Int) : Flow<OrderWithSneakers> {
|
||||
return orderRepository.getOrderWithSneakers(id)
|
||||
}
|
||||
@ -65,18 +61,16 @@ class OrderViewModel(private val orderRepository: OrderRepository) : ViewModel()
|
||||
|
||||
val orderId = orderRepository.createOrder(order)
|
||||
|
||||
|
||||
for (sneaker in selectedItems) {
|
||||
for (sneaker in selectedItems.value.orEmpty()) {
|
||||
val orderSneaker = OrderSneaker( orderId.toInt(), sneaker.sneakerId!!)
|
||||
orderRepository.insertOrderSneaker(orderSneaker)
|
||||
}
|
||||
city.value = ""
|
||||
street.value = ""
|
||||
house.value = ""
|
||||
_selectedItems = mutableStateOf(emptyList())
|
||||
}
|
||||
|
||||
fun getSubTotal(): Double {
|
||||
return selectedItems.sumOf { it.price }
|
||||
return 0.0
|
||||
}
|
||||
}
|
@ -8,8 +8,10 @@ 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.Basket
|
||||
import com.example.android_programming.model.RoleEnum
|
||||
import com.example.android_programming.model.User
|
||||
import com.example.android_programming.repository.BasketRepository
|
||||
import com.example.android_programming.repository.SneakerRepository
|
||||
import com.example.android_programming.repository.UserRepository
|
||||
import kotlinx.coroutines.launch
|
||||
@ -35,9 +37,9 @@ class UserViewModel(private val userRepository: UserRepository): ViewModel() {
|
||||
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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user