Вроде робит, хз насколько правильно только я это сделал
This commit is contained in:
parent
a11e81ab27
commit
25cdd9b083
@ -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")
|
||||
|
@ -22,7 +22,7 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
setContent {
|
||||
LabWorkTheme {
|
||||
MainScreen(bicycleDao,userDao)
|
||||
MainScreen(bicycleDao, userDao)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
)
|
||||
}
|
||||
}
|
@ -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<Bicycle>() }
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
@ -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 <T : ViewModel> create(modelClass: Class<T>): T {
|
||||
if (modelClass.isAssignableFrom(BicycleViewModel::class.java)) {
|
||||
return BicycleViewModel(bicycleRepository) as T
|
||||
}
|
||||
throw IllegalArgumentException("Unknown ViewModel class")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<User> {
|
||||
@ -31,4 +34,13 @@ class UserRepository(private val userDao: UserDao) {
|
||||
suspend fun getUserByEmailAndPassword(email: String, password: String): User? {
|
||||
return userDao.getUserByEmailAndPassword(email, password)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class UserViewModelFactory(private val userRepository: UserRepository): ViewModelProvider.Factory {
|
||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||
if (modelClass.isAssignableFrom(UserViewModel::class.java)) {
|
||||
return UserViewModel(userRepository) as T
|
||||
}
|
||||
throw IllegalArgumentException("Unknown ViewModel class")
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Bicycle>>()
|
||||
val bicycles: LiveData<List<Bicycle>> get() = _bicycles
|
||||
val bicycles: StateFlow<List<Bicycle>> = MutableStateFlow(emptyList())
|
||||
|
||||
fun fetchBicycles() {
|
||||
viewModelScope.launch {
|
||||
val fetchedBicycles = bicycleRepository.getAllBicycles()
|
||||
(bicycles as MutableStateFlow).value = fetchedBicycles
|
||||
}
|
||||
}
|
||||
fun getAllBicycles() {
|
||||
viewModelScope.launch {
|
||||
_bicycles.value = bicycleRepository.getAllBicycles()
|
||||
|
Loading…
Reference in New Issue
Block a user