fix crash on not initialized viewControllerClass field #18

Merged
daniil.bakherov merged 2 commits from fix_ubrir_crash into ubrir/develop 2024-10-31 13:05:48 +03:00
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>
}