diff --git a/app/src/main/java/com/example/android_programming/AppContainer.kt b/app/src/main/java/com/example/android_programming/AppContainer.kt index e7372a6..1e14623 100644 --- a/app/src/main/java/com/example/android_programming/AppContainer.kt +++ b/app/src/main/java/com/example/android_programming/AppContainer.kt @@ -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 } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/AppDataContainer.kt b/app/src/main/java/com/example/android_programming/AppDataContainer.kt index 8db3a7c..39c708d 100644 --- a/app/src/main/java/com/example/android_programming/AppDataContainer.kt +++ b/app/src/main/java/com/example/android_programming/AppDataContainer.kt @@ -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()) + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/MainActivity.kt b/app/src/main/java/com/example/android_programming/MainActivity.kt index 63b35fb..b4a7940 100644 --- a/app/src/main/java/com/example/android_programming/MainActivity.kt +++ b/app/src/main/java/com/example/android_programming/MainActivity.kt @@ -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() } diff --git a/app/src/main/java/com/example/android_programming/composeui/Navigation/NavController.kt b/app/src/main/java/com/example/android_programming/composeui/Navigation/NavController.kt index a2b597e..f9397af 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Navigation/NavController.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Navigation/NavController.kt @@ -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) diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt index 80197cc..e2b210f 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/HomeScreen.kt @@ -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) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/CardSneaker.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/CardSneaker.kt index f85f7de..8943a69 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/CardSneaker.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/CardSneaker.kt @@ -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, 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) diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt index 00f5a22..645ced3 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/HomeScreen/SneakerRecyclerView/RecyclerView.kt @@ -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) } } } diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/MyOrderScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/MyOrderScreen.kt index 2b65a1a..24f59e8 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/MyOrderScreen.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/MyOrderScreen.kt @@ -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) } } } diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/OrderCard.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/OrderCard.kt index a609204..1a8d24f 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/OrderCard.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/MyOrderScreen/OrderCard.kt @@ -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? = 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() diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/CardSneaker.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/CardSneaker.kt index 72f4f60..394034a 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/CardSneaker.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/CardSneaker.kt @@ -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) diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/OrderScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/OrderScreen.kt index eea7d0f..a955c09 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/OrderScreen.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/OrderScreen.kt @@ -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? = 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") } } -} \ No newline at end of file +} + diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/ShoppingList.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/ShoppingList.kt index c5d69eb..e94d218 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/ShoppingList.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/OrderScreen/ShoppingList.kt @@ -7,11 +7,11 @@ import com.example.android_programming.model.Sneaker import com.example.android_programming.vmodel.OrderViewModel @Composable -fun ShoppingList(list : List, orderViewModel: OrderViewModel) { +fun ShoppingList(list : List) { Row { Column { for(item in list){ - CardSneakerLike(item = item, orderViewModel) + CardSneakerLike(item = item) } } } diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileScreen.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileScreen.kt index e9f44cd..fbfe741 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileScreen.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/Profile/ProfileScreen.kt @@ -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) { diff --git a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInCard.kt b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInCard.kt index a656dd8..db9b4c8 100644 --- a/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInCard.kt +++ b/app/src/main/java/com/example/android_programming/composeui/Screens/ProfileScreen/SignIn/SignInCard.kt @@ -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() diff --git a/app/src/main/java/com/example/android_programming/dao/BasketDao.kt b/app/src/main/java/com/example/android_programming/dao/BasketDao.kt new file mode 100644 index 0000000..2c47b7f --- /dev/null +++ b/app/src/main/java/com/example/android_programming/dao/BasketDao.kt @@ -0,0 +1,31 @@ +package com.example.android_programming.dao + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import 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 + + @Query("SELECT * FROM 'Basket'") + fun getAllBasket(): Flow> + + @Delete + suspend fun delete(basket: Basket) + + @Query("DELETE FROM 'BasketSneakers' WHERE basketId = :basketId AND sneakerId = :sneakerId") + suspend fun removeSneakerFromBasket(basketId: Int, sneakerId: Int) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/database/AppDatabase.kt b/app/src/main/java/com/example/android_programming/database/AppDatabase.kt index 0d2a619..d15307b 100644 --- a/app/src/main/java/com/example/android_programming/database/AppDatabase.kt +++ b/app/src/main/java/com/example/android_programming/database/AppDatabase.kt @@ -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) } } diff --git a/app/src/main/java/com/example/android_programming/model/Basket.kt b/app/src/main/java/com/example/android_programming/model/Basket.kt new file mode 100644 index 0000000..f878a9c --- /dev/null +++ b/app/src/main/java/com/example/android_programming/model/Basket.kt @@ -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 +) \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/model/BasketSneakers.kt b/app/src/main/java/com/example/android_programming/model/BasketSneakers.kt new file mode 100644 index 0000000..e69706d --- /dev/null +++ b/app/src/main/java/com/example/android_programming/model/BasketSneakers.kt @@ -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, +) \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/model/BasketWithSneakers.kt b/app/src/main/java/com/example/android_programming/model/BasketWithSneakers.kt new file mode 100644 index 0000000..88f5b8a --- /dev/null +++ b/app/src/main/java/com/example/android_programming/model/BasketWithSneakers.kt @@ -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 +) \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/repository/BasketRepoImpl.kt b/app/src/main/java/com/example/android_programming/repository/BasketRepoImpl.kt new file mode 100644 index 0000000..c37ce54 --- /dev/null +++ b/app/src/main/java/com/example/android_programming/repository/BasketRepoImpl.kt @@ -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 = basketDao.getBasketWithSneakers(id) + override fun getAllBasket(): Flow> = basketDao.getAllBasket() + override suspend fun delete(basket: Basket) = basketDao.delete(basket) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/repository/BasketRepository.kt b/app/src/main/java/com/example/android_programming/repository/BasketRepository.kt new file mode 100644 index 0000000..6c80fee --- /dev/null +++ b/app/src/main/java/com/example/android_programming/repository/BasketRepository.kt @@ -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 + fun getAllBasket(): Flow> + suspend fun delete(basket: Basket) + suspend fun createBasket(basket: Basket):Long + suspend fun removeSneakerFromBasket(basketId: Int, sneakerId: Int) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/repository/UserRepository.kt b/app/src/main/java/com/example/android_programming/repository/UserRepository.kt index 9a3cfaf..06d1fd9 100644 --- a/app/src/main/java/com/example/android_programming/repository/UserRepository.kt +++ b/app/src/main/java/com/example/android_programming/repository/UserRepository.kt @@ -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 diff --git a/app/src/main/java/com/example/android_programming/vmodel/AppViewModelProvider.kt b/app/src/main/java/com/example/android_programming/vmodel/AppViewModelProvider.kt index 2db2622..15aef32 100644 --- a/app/src/main/java/com/example/android_programming/vmodel/AppViewModelProvider.kt +++ b/app/src/main/java/com/example/android_programming/vmodel/AppViewModelProvider.kt @@ -17,6 +17,9 @@ object AppViewModelProvider { initializer { OrderViewModel(app().container.orderRepo) } + initializer { + BasketViewModel(app().container.basketRepo) + } } } diff --git a/app/src/main/java/com/example/android_programming/vmodel/BasketViewModel.kt b/app/src/main/java/com/example/android_programming/vmodel/BasketViewModel.kt new file mode 100644 index 0000000..53e7ddf --- /dev/null +++ b/app/src/main/java/com/example/android_programming/vmodel/BasketViewModel.kt @@ -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 { + return basketRepository.getBasketWithSneakers(id) + } + + fun deleteSneakerFromBasket(basketId: Int, sneakerId: Int) = viewModelScope.launch { + basketRepository.removeSneakerFromBasket(basketId, sneakerId) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/vmodel/OrderViewModel.kt b/app/src/main/java/com/example/android_programming/vmodel/OrderViewModel.kt index 6fdd4f5..4b4675d 100644 --- a/app/src/main/java/com/example/android_programming/vmodel/OrderViewModel.kt +++ b/app/src/main/java/com/example/android_programming/vmodel/OrderViewModel.kt @@ -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>(emptyList()) - val selectedItems get() = _selectedItems.value + var city = mutableStateOf("") val street = mutableStateOf("") val house = mutableStateOf("") + private val _selectedItems = MutableLiveData>() + val selectedItems: LiveData> get() = _selectedItems - fun addSelectedItem(item: Sneaker) { - _selectedItems.value = _selectedItems.value + item + fun updateSelectedItems(items: List) { + _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 { 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 } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_programming/vmodel/UserViewModel.kt b/app/src/main/java/com/example/android_programming/vmodel/UserViewModel.kt index 077807b..0fa440e 100644 --- a/app/src/main/java/com/example/android_programming/vmodel/UserViewModel.kt +++ b/app/src/main/java/com/example/android_programming/vmodel/UserViewModel.kt @@ -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() }