From 25cdd9b0830ef9b1ba0c1d28aded276fb1476696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC?= Date: Thu, 23 Nov 2023 15:18:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D1=80=D0=BE?= =?UTF-8?q?=D0=B1=D0=B8=D1=82,=20=D1=85=D0=B7=20=D0=BD=D0=B0=D1=81=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D0=BE=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE?= =?UTF-8?q?=20=D1=8F=20=D1=8D=D1=82=D0=BE=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle.kts | 4 +++ .../java/com/example/labwork/MainActivity.kt | 2 +- .../labwork/button_navigation/MainScreen.kt | 22 +++++++++--- .../labwork/button_navigation/Screens.kt | 34 ++++++++++++------- .../labwork/button_navigation/SlideGraph.kt | 31 ++++++++++++----- .../labwork/pages/product/ListProduct.kt | 13 ++++--- .../labwork/repository/BicycleRepository.kt | 13 +++++++ .../labwork/repository/UserRepository.kt | 14 +++++++- .../labwork/viewmodel/BicycleViewModel.kt | 10 +++++- 9 files changed, 107 insertions(+), 36 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 2e5c962..faa25de 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,3 +1,5 @@ +import org.jetbrains.kotlin.kapt3.base.Kapt.kapt + plugins { id("com.android.application") id("org.jetbrains.kotlin.android") @@ -58,6 +60,8 @@ dependencies { implementation("androidx.room:room-ktx:2.6.0") kapt("androidx.room:room-compiler:2.6.0") implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2") + implementation ("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0") + implementation("androidx.core:core-ktx:1.9.0") implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1") diff --git a/app/src/main/java/com/example/labwork/MainActivity.kt b/app/src/main/java/com/example/labwork/MainActivity.kt index c2d46ea..ed74f53 100644 --- a/app/src/main/java/com/example/labwork/MainActivity.kt +++ b/app/src/main/java/com/example/labwork/MainActivity.kt @@ -22,7 +22,7 @@ class MainActivity : ComponentActivity() { setContent { LabWorkTheme { - MainScreen(bicycleDao,userDao) + MainScreen(bicycleDao, userDao) } } } diff --git a/app/src/main/java/com/example/labwork/button_navigation/MainScreen.kt b/app/src/main/java/com/example/labwork/button_navigation/MainScreen.kt index 72128f7..0bd4eb5 100644 --- a/app/src/main/java/com/example/labwork/button_navigation/MainScreen.kt +++ b/app/src/main/java/com/example/labwork/button_navigation/MainScreen.kt @@ -4,20 +4,34 @@ import android.annotation.SuppressLint import androidx.compose.material.Scaffold import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember import androidx.navigation.compose.rememberNavController import com.example.labwork.database.DAO.BicycleDao import com.example.labwork.database.DAO.UserDao +import com.example.labwork.repository.BicycleRepository +import com.example.labwork.repository.UserRepository @SuppressLint("UnusedMaterialScaffoldPaddingParameter") @Composable fun MainScreen(bicycleDao: BicycleDao, userDao: UserDao) { val navController = rememberNavController() - Scaffold( - bottomBar = { - SlideNavigation(navController = navController) + val bicycleRepository = remember { + BicycleRepository(bicycleDao) + } + val userRepository = remember { + UserRepository(userDao) + } + + Scaffold( + bottomBar = { + SlideNavigation(navController = navController) } ) { - SlideGraph(navHostController = navController, bicycleDao = bicycleDao, userDao = userDao) + SlideGraph( + navHostController = navController, + bicycleRepository = bicycleRepository, + userRepository = userRepository + ) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/labwork/button_navigation/Screens.kt b/app/src/main/java/com/example/labwork/button_navigation/Screens.kt index fb697fa..8e1c7f3 100644 --- a/app/src/main/java/com/example/labwork/button_navigation/Screens.kt +++ b/app/src/main/java/com/example/labwork/button_navigation/Screens.kt @@ -4,21 +4,25 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn -import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import androidx.navigation.NavHostController -import com.example.labwork.database.DAO.BicycleDao import com.example.labwork.database.DAO.UserDao import com.example.labwork.models.Bicycle import com.example.labwork.pages.ListInfo import com.example.labwork.pages.product.FormNewProduct import com.example.labwork.pages.product.ListProduct import com.example.labwork.pages.user.RegisteryOrLogin +import com.example.labwork.repository.BicycleRepository +import com.example.labwork.viewmodel.BicycleViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -32,27 +36,31 @@ fun ScreenProfile(userDao: UserDao, navHostController: NavHostController) { RegisteryOrLogin(userDao, navHostController) } - @Composable -fun ScreenListProduct(bicycleDao: BicycleDao, navHostController: NavHostController) { - val bicycles = remember { mutableStateListOf() } +fun ScreenListProduct( + bicycleViewModel: BicycleViewModel, + navHostController: NavHostController +) { + val bicycles by bicycleViewModel.bicycles.collectAsState() LaunchedEffect(Unit) { - val fetchedBicycles = withContext(Dispatchers.IO) { - bicycleDao.getAllBicycles() - } - bicycles.addAll(fetchedBicycles) + bicycleViewModel.fetchBicycles() } Column( modifier = Modifier.fillMaxHeight().padding(bottom = 65.dp) ) { - FormNewProduct(bicycleDao, navHostController) + //FormNewProduct(bicycleViewModel, navHostController) LazyColumn { - items(bicycles) { item -> - ListProduct(item = item, bicycleDao = bicycleDao, navHostController = navHostController) + itemsIndexed(bicycles) { index, item -> + ListProduct( + item = item, + bicycleViewModel = bicycleViewModel, + navHostController = navHostController + ) } } } } + diff --git a/app/src/main/java/com/example/labwork/button_navigation/SlideGraph.kt b/app/src/main/java/com/example/labwork/button_navigation/SlideGraph.kt index 6605732..73e7d08 100644 --- a/app/src/main/java/com/example/labwork/button_navigation/SlideGraph.kt +++ b/app/src/main/java/com/example/labwork/button_navigation/SlideGraph.kt @@ -2,30 +2,43 @@ package com.example.labwork.button_navigation import androidx.compose.runtime.Composable +import androidx.lifecycle.viewmodel.compose.viewModel import androidx.navigation.NavHostController import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import com.example.labwork.database.DAO.BicycleDao import com.example.labwork.database.DAO.UserDao +import com.example.labwork.repository.BicycleRepository +import com.example.labwork.repository.BicycleViewModelFactory +import com.example.labwork.repository.UserRepository +import com.example.labwork.repository.UserViewModelFactory +import com.example.labwork.viewmodel.BicycleViewModel +import com.example.labwork.viewmodel.UserViewModel @Composable fun SlideGraph( navHostController: NavHostController, - bicycleDao: BicycleDao, - userDao: UserDao + bicycleRepository: BicycleRepository, + userRepository: UserRepository ) { - NavHost(navController = navHostController, startDestination = "Profile"){ + val bicycleViewModel: BicycleViewModel = viewModel( + factory = BicycleViewModelFactory(bicycleRepository) + ) + val userViewModel: UserViewModel = viewModel( + factory = UserViewModelFactory(userRepository) + ) - composable("Profile"){ - ScreenProfile(userDao, navHostController = navHostController) + NavHost(navController = navHostController, startDestination = "Profile") { + composable("Profile") { + //ScreenProfile(userViewModel, navHostController = navHostController) + ScreenListProduct(bicycleViewModel, navHostController = navHostController) } - composable("Info"){ + composable("Info") { ScreenInfo() } - composable("ListProduct"){ - ScreenListProduct(bicycleDao, navHostController = navHostController) + composable("ListProduct") { + ScreenListProduct(bicycleViewModel, navHostController = navHostController) } - } } \ No newline at end of file diff --git a/app/src/main/java/com/example/labwork/pages/product/ListProduct.kt b/app/src/main/java/com/example/labwork/pages/product/ListProduct.kt index 30c03ee..58d6172 100644 --- a/app/src/main/java/com/example/labwork/pages/product/ListProduct.kt +++ b/app/src/main/java/com/example/labwork/pages/product/ListProduct.kt @@ -39,13 +39,15 @@ import androidx.navigation.NavHostController import com.example.labwork.R import com.example.labwork.database.DAO.BicycleDao import com.example.labwork.models.Bicycle +import com.example.labwork.repository.BicycleRepository import com.example.labwork.ui.theme.LightBluePolitech +import com.example.labwork.viewmodel.BicycleViewModel import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @Composable -fun ListProduct(item: Bicycle, bicycleDao: BicycleDao, navHostController: NavHostController) { +fun ListProduct(item: Bicycle, bicycleViewModel : BicycleViewModel, navHostController: NavHostController) { var isFullAbout by remember { mutableStateOf(false) } val scale by animateFloatAsState(if (isFullAbout) 1f else 0f) val textSize by animateDpAsState(if (isFullAbout) 18.dp else 24.dp) @@ -87,21 +89,18 @@ fun ListProduct(item: Bicycle, bicycleDao: BicycleDao, navHostController: NavHos ) } Column { - FormUpdateProduct(item,bicycleDao, navHostController) + //FormUpdateProduct(item,bicycleDao, navHostController) Button( colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech), modifier = Modifier .padding(start = 9.dp, bottom = 9.dp) .size(height = 32.dp, width = 128.dp), onClick = { - // СУПЕР ЖОСКИЙ КОСТЫЛЬ ЭТО ВООБЩЕ ТРЕШ - GlobalScope.launch { - bicycleDao.deleteBicycle(item) - } + bicycleViewModel.deleteBicycle(item) navHostController.navigate("ListProduct") }, shape = RoundedCornerShape(15.dp) - ) { + ) { Text( text = "Удалить", color = Color.White, diff --git a/app/src/main/java/com/example/labwork/repository/BicycleRepository.kt b/app/src/main/java/com/example/labwork/repository/BicycleRepository.kt index 4e37581..c3a2307 100644 --- a/app/src/main/java/com/example/labwork/repository/BicycleRepository.kt +++ b/app/src/main/java/com/example/labwork/repository/BicycleRepository.kt @@ -1,7 +1,10 @@ package com.example.labwork.repository +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider import com.example.labwork.database.DAO.BicycleDao import com.example.labwork.models.Bicycle +import com.example.labwork.viewmodel.BicycleViewModel class BicycleRepository(private val bicycleDao: BicycleDao) { suspend fun insertBicycle(bicycle: Bicycle) { @@ -27,3 +30,13 @@ class BicycleRepository(private val bicycleDao: BicycleDao) { return bicycleDao.getBicycleById(bicycleId) } } + +class BicycleViewModelFactory(private val bicycleRepository: BicycleRepository): ViewModelProvider.Factory { + override fun create(modelClass: Class): T { + if (modelClass.isAssignableFrom(BicycleViewModel::class.java)) { + return BicycleViewModel(bicycleRepository) as T + } + throw IllegalArgumentException("Unknown ViewModel class") + } +} + diff --git a/app/src/main/java/com/example/labwork/repository/UserRepository.kt b/app/src/main/java/com/example/labwork/repository/UserRepository.kt index 6c7f054..94d3c69 100644 --- a/app/src/main/java/com/example/labwork/repository/UserRepository.kt +++ b/app/src/main/java/com/example/labwork/repository/UserRepository.kt @@ -1,7 +1,10 @@ package com.example.labwork.repository +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider import com.example.labwork.database.DAO.UserDao import com.example.labwork.models.User +import com.example.labwork.viewmodel.UserViewModel class UserRepository(private val userDao: UserDao) { suspend fun getAllUsers(): List { @@ -31,4 +34,13 @@ class UserRepository(private val userDao: UserDao) { suspend fun getUserByEmailAndPassword(email: String, password: String): User? { return userDao.getUserByEmailAndPassword(email, password) } -} \ No newline at end of file +} + +class UserViewModelFactory(private val userRepository: UserRepository): ViewModelProvider.Factory { + override fun create(modelClass: Class): T { + if (modelClass.isAssignableFrom(UserViewModel::class.java)) { + return UserViewModel(userRepository) as T + } + throw IllegalArgumentException("Unknown ViewModel class") + } +} diff --git a/app/src/main/java/com/example/labwork/viewmodel/BicycleViewModel.kt b/app/src/main/java/com/example/labwork/viewmodel/BicycleViewModel.kt index a9db0db..d648639 100644 --- a/app/src/main/java/com/example/labwork/viewmodel/BicycleViewModel.kt +++ b/app/src/main/java/com/example/labwork/viewmodel/BicycleViewModel.kt @@ -6,12 +6,20 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.example.labwork.models.Bicycle import com.example.labwork.repository.BicycleRepository +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch class BicycleViewModel(private val bicycleRepository: BicycleRepository) : ViewModel() { private val _bicycles = MutableLiveData>() - val bicycles: LiveData> get() = _bicycles + val bicycles: StateFlow> = MutableStateFlow(emptyList()) + fun fetchBicycles() { + viewModelScope.launch { + val fetchedBicycles = bicycleRepository.getAllBicycles() + (bicycles as MutableStateFlow).value = fetchedBicycles + } + } fun getAllBicycles() { viewModelScope.launch { _bicycles.value = bicycleRepository.getAllBicycles()