Skip to content

Commit

Permalink
Make ComponentActivity.tangleViewModel() initialize lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
WhosNickDoglio committed Feb 27, 2025
1 parent a3a2dd4 commit ee823f3
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelLazy
import tangle.inject.InternalTangleApi
import tangle.viewmodel.internal.TangleViewModelFactory
import kotlin.LazyThreadSafetyMode.NONE

/**
* Equivalent to the Androidx ktx `by viewModels()` delegate.
Expand All @@ -29,13 +30,14 @@ import tangle.viewmodel.internal.TangleViewModelFactory
* @since 0.11.0
*/
@OptIn(InternalTangleApi::class)
public inline fun <reified VM : ViewModel> ComponentActivity.tangleViewModel(): Lazy<VM> {
public inline fun <reified VM : ViewModel> ComponentActivity.tangleViewModel(): Lazy<VM> =
lazy(NONE) {

val viewModelFactory = TangleViewModelFactory(
owner = this,
defaultArgs = intent.extras,
defaultFactory = defaultViewModelProviderFactory
)
val viewModelFactory = TangleViewModelFactory(
owner = this,
defaultArgs = intent.extras,
defaultFactory = defaultViewModelProviderFactory
)

return ViewModelLazy(VM::class, { viewModelStore }, { viewModelFactory })
}
ViewModelLazy(VM::class, { viewModelStore }, { viewModelFactory }).value
}

0 comments on commit ee823f3

Please sign in to comment.