我可以使用一个工厂将视图模型/存储库调用与 kodein 绑定吗
Can I use one Factory to bind viewmodel / repository calls with kodein
在这个 Factory 中,我需要使用 Retrofit 从 api 中获取我的数据,并用空间存储缓存,我的存储库控制着这个应用程序!
我有存储库暂停功能,负责获取我的数据和一些 save/update 数据获取和 saveing/updating 需要不同的值才能运行,我不知道(还)如何在 Kodein 中配置它
我缺乏解决这个问题的经验,而且我在 Whosebug 中找不到任何可以帮助我的东西。
我尝试将变量 ID:String 和编辑后的实体 (CampaignEntry) 添加到定义中,它符合要求但在 运行
时崩溃
No binding found for bind<CampaignEditViewModelFactory>() with ? { String -> ? }
我的主应用程序 bind() 使应用程序崩溃
class MarketingApplication : Application(), KodeinAware {
override val kodein = Kodein.lazy {
import(androidXModule(this@MarketingApplication))
...
bind() from factory { id: String, campaignEntry: CampaignEntry ->
CampaignEditViewModelFactory(id, campaignEntry, instance()) }
...
我的 ViewModel - 必须在一个 ViewModel 中传递由不同调用使用的变量 id 和 campaignEntry 可能是个问题 - 但我无法找到正确的解决方案。
class CampaignEditViewModel(
private val id: String,
private val campaignEntry: CampaignEntry,
private val marketingRepository: MarketingRepository
) : ViewModel() {
val campaignToSave by lazyDeferred { marketingRepository.updateCampaign(campaignEntry) }
val campaignToEdit by lazyDeferred { marketingRepository.getCampaignById(id) }
}
为清楚起见,我的 lazyDeferred
fun <T> lazyDeferred(block: suspend CoroutineScope.() -> T): Lazy<Deferred<T>> {
return lazy {
GlobalScope.async(start = CoroutineStart.LAZY) {
block.invoke(this)
}
}
}
存储库快照
interface MarketingRepository {
...
suspend fun getCampaignById(campaignId: String): LiveData<CampaignEntry>
suspend fun updateCampaign(campaignEntry: CampaignEntry): LiveData<CampaignEntry>
...
我像这样从我的片段中调用 Viewmodel
class CampaignEditFragment : ScopedFragment(), KodeinAware {
override val kodein by closestKodein()
private val viewModelFactoryInstanceFactory: ((String) -> CampaignEditViewModelFactory) by factory()
...
private fun bindUI() = launch {
val campaignVM = campaignEditViewModel.campaignToEdit.await()
...
btn_edit_save.setOnClickListener {it: View
saveCampaign(it)
...
private fun saveCampaign(it: View) = launch {
campaignEditViewModel.campaignToSave.await()
}
最后是 ScopedFragment
abstract class ScopedFragment : Fragment(), CoroutineScope {
private lateinit var job: Job
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
job = Job()
}
override fun onDestroy() {
super.onDestroy()
job.cancel()
}
}
如果您需要更多代码 - 请询问
由于您绑定了 2 个参数,因此您需要使用 factory2
:
private val viewModelFactoryInstanceFactory: ((String, campaignEntry) -> CampaignEditViewModelFactory) by factory2()
在这个 Factory 中,我需要使用 Retrofit 从 api 中获取我的数据,并用空间存储缓存,我的存储库控制着这个应用程序! 我有存储库暂停功能,负责获取我的数据和一些 save/update 数据获取和 saveing/updating 需要不同的值才能运行,我不知道(还)如何在 Kodein 中配置它 我缺乏解决这个问题的经验,而且我在 Whosebug 中找不到任何可以帮助我的东西。
我尝试将变量 ID:String 和编辑后的实体 (CampaignEntry) 添加到定义中,它符合要求但在 运行
时崩溃No binding found for bind<CampaignEditViewModelFactory>() with ? { String -> ? }
我的主应用程序 bind() 使应用程序崩溃
class MarketingApplication : Application(), KodeinAware {
override val kodein = Kodein.lazy {
import(androidXModule(this@MarketingApplication))
...
bind() from factory { id: String, campaignEntry: CampaignEntry ->
CampaignEditViewModelFactory(id, campaignEntry, instance()) }
...
我的 ViewModel - 必须在一个 ViewModel 中传递由不同调用使用的变量 id 和 campaignEntry 可能是个问题 - 但我无法找到正确的解决方案。
class CampaignEditViewModel(
private val id: String,
private val campaignEntry: CampaignEntry,
private val marketingRepository: MarketingRepository
) : ViewModel() {
val campaignToSave by lazyDeferred { marketingRepository.updateCampaign(campaignEntry) }
val campaignToEdit by lazyDeferred { marketingRepository.getCampaignById(id) }
}
为清楚起见,我的 lazyDeferred
fun <T> lazyDeferred(block: suspend CoroutineScope.() -> T): Lazy<Deferred<T>> {
return lazy {
GlobalScope.async(start = CoroutineStart.LAZY) {
block.invoke(this)
}
}
}
存储库快照
interface MarketingRepository {
...
suspend fun getCampaignById(campaignId: String): LiveData<CampaignEntry>
suspend fun updateCampaign(campaignEntry: CampaignEntry): LiveData<CampaignEntry>
...
我像这样从我的片段中调用 Viewmodel
class CampaignEditFragment : ScopedFragment(), KodeinAware {
override val kodein by closestKodein()
private val viewModelFactoryInstanceFactory: ((String) -> CampaignEditViewModelFactory) by factory()
...
private fun bindUI() = launch {
val campaignVM = campaignEditViewModel.campaignToEdit.await()
...
btn_edit_save.setOnClickListener {it: View
saveCampaign(it)
...
private fun saveCampaign(it: View) = launch {
campaignEditViewModel.campaignToSave.await()
}
最后是 ScopedFragment
abstract class ScopedFragment : Fragment(), CoroutineScope {
private lateinit var job: Job
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
job = Job()
}
override fun onDestroy() {
super.onDestroy()
job.cancel()
}
}
如果您需要更多代码 - 请询问
由于您绑定了 2 个参数,因此您需要使用 factory2
:
private val viewModelFactoryInstanceFactory: ((String, campaignEntry) -> CampaignEditViewModelFactory) by factory2()