SchildiChat-android/tools/templates/ElementFeature/root/src/app_package/ViewModel.kt.ftl

47 lines
1.6 KiB
Text
Raw Normal View History

2020-04-29 15:38:01 +03:00
package ${escapeKotlinIdentifiers(packageName)}
import com.airbnb.mvrx.ActivityViewModelContext
2020-04-29 15:38:01 +03:00
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MavericksViewModelFactory
2020-04-29 15:38:01 +03:00
import com.airbnb.mvrx.ViewModelContext
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import dagger.assisted.AssistedFactory
import im.vector.app.core.platform.VectorViewModel
2020-04-29 15:38:01 +03:00
<#if createViewEvents>
<#else>
import im.vector.app.core.platform.EmptyViewEvents
2020-04-29 15:38:01 +03:00
</#if>
class ${viewModelClass} @AssistedInject constructor(@Assisted initialState: ${viewStateClass})
<#if createViewEvents>
: VectorViewModel<${viewStateClass}, ${actionClass}, ${viewEventsClass}>(initialState) {
<#else>
: VectorViewModel<${viewStateClass}, ${actionClass}, EmptyViewEvents>(initialState) {
</#if>
@AssistedFactory
2020-04-29 15:38:01 +03:00
interface Factory {
fun create(initialState: ${viewStateClass}): ${viewModelClass}
}
companion object : MavericksViewModelFactory<${viewModelClass}, ${viewStateClass}> {
2020-04-29 15:38:01 +03:00
@JvmStatic
override fun create(viewModelContext: ViewModelContext, state: ${viewStateClass}): ${viewModelClass}? {
val factory = when (viewModelContext) {
is FragmentViewModelContext -> viewModelContext.fragment as? Factory
is ActivityViewModelContext -> viewModelContext.activity as? Factory
}
return factory?.create(state) ?: error("You should let your activity/fragment implements Factory interface")
2020-04-29 15:38:01 +03:00
}
}
override fun handle(action: ${actionClass}) {
2020-05-14 02:31:15 +03:00
when (action) {
2020-04-29 15:38:01 +03:00
}
2020-05-14 02:31:15 +03:00
}
2020-04-29 15:38:01 +03:00
}