fix crash on not initialized viewControllerClass field

This commit is contained in:
baheroff 2024-10-25 13:11:16 +03:00
parent 57486cab51
commit f4c81cf2a2
1 changed files with 6 additions and 5 deletions

View File

@ -80,7 +80,8 @@ open class ViewControllerFragment<TActivity : FragmentActivity, TState : Parcela
lateinit var state: TState private set
lateinit var viewControllerClass: Class<ViewController<TActivity, TState>> private set
var viewControllerClass: Class<ViewController<TActivity, TState>>? = null
private set
private var viewController: ViewController<out TActivity, out TState>? = null
@ -241,14 +242,14 @@ open class ViewControllerFragment<TActivity : FragmentActivity, TState : Parcela
creationContext: ViewController.CreationContext,
savedInstanceState: Bundle?
): ViewController<out TActivity, out TState> {
if (viewControllerClass.constructors.size != 1) {
if (viewControllerClass?.constructors?.size != 1) {
throw IllegalStateException("There should be single constructor for $viewControllerClass")
}
val constructor = viewControllerClass.constructors[0]
return when (constructor.parameterTypes.size) {
val constructor = viewControllerClass?.constructors?.get(0)
return when (constructor?.parameterTypes?.size) {
1 -> constructor.newInstance(creationContext)
2 -> constructor.newInstance(creationContext, savedInstanceState)
else -> throw IllegalArgumentException("Wrong constructor parameters count: ${constructor.parameterTypes.size}")
else -> throw IllegalArgumentException("Wrong constructor parameters count: ${constructor?.parameterTypes?.size}")
} as ViewController<out TActivity, out TState>
}