Вынес factory

This commit is contained in:
Кашин Максим 2023-11-24 17:38:41 +04:00
parent b46391b649
commit e96160ad7d
5 changed files with 32 additions and 18 deletions

View File

@ -9,11 +9,11 @@ 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
import com.example.labwork.viewmodel.factory.BicycleViewModelFactory
import com.example.labwork.viewmodel.factory.UserViewModelFactory
@Composable

View File

@ -35,12 +35,4 @@ class BicycleRepository(private val bicycleDao: BicycleDao) {
}
}
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")
}
}

View File

@ -36,11 +36,3 @@ class UserRepository(private val userDao: UserDao) {
}
}
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")
}
}

View File

@ -0,0 +1,15 @@
package com.example.labwork.viewmodel.factory
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.labwork.repository.BicycleRepository
import com.example.labwork.viewmodel.BicycleViewModel
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")
}
}

View File

@ -0,0 +1,15 @@
package com.example.labwork.viewmodel.factory
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.labwork.repository.UserRepository
import com.example.labwork.viewmodel.UserViewModel
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")
}
}