Skip to content

Commit

Permalink
move package + dsl to allow viewModel definition from common source
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed May 17, 2024
1 parent 1f416bc commit 854d9d7
Show file tree
Hide file tree
Showing 8 changed files with 540 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@
*/
@file:Suppress("DeprecatedCallableAddReplaceWith")

package org.koin.compose
package org.koin.compose.viewmodel

import androidx.compose.runtime.Composable
import androidx.lifecycle.*
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import org.koin.compose.currentKoinScope
import org.koin.core.annotation.KoinExperimentalAPI
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.qualifier.Qualifier
import org.koin.core.scope.Scope

/*
Ported directly from Android side. Waiting more feedback
*/

/**
* Resolve ViewModel instance with Navigation NavBackStackEntry as extras parameters
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
@file:Suppress("DeprecatedCallableAddReplaceWith")

package org.koin.compose
package org.koin.compose.viewmodel

import androidx.compose.runtime.Composable
import androidx.core.bundle.Bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@
*/
@file:Suppress("DeprecatedCallableAddReplaceWith")

package org.koin.compose
package org.koin.compose.viewmodel

import androidx.compose.runtime.Composable
import androidx.lifecycle.*
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import org.koin.compose.currentKoinScope
import org.koin.core.annotation.KoinExperimentalAPI
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.qualifier.Qualifier
import org.koin.core.scope.Scope

/*
Ported directly from Android side. Waiting more feedback
*/

/**
* Resolve ViewModel instance
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.koin.compose
package org.koin.compose.viewmodel

import androidx.compose.runtime.Composable
import androidx.lifecycle.HasDefaultViewModelProviderFactory
Expand Down Expand Up @@ -66,8 +66,7 @@ class KoinViewModelFactory(
private val params: ParametersDefinition? = null
) : ViewModelProvider.Factory {

//TODO Handle Extras/Bundle

//TODO Should handle Extras/Bundle injection here (see Android side)
override fun <T : ViewModel> create(modelClass: KClass<T>, extras: CreationExtras): T {
return scope.get(kClass, qualifier,params)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.koin.compose.viewmodel.dsl

import androidx.lifecycle.ViewModel
import org.koin.core.definition.Definition
import org.koin.core.definition.KoinDefinition
import org.koin.core.module.Module
import org.koin.core.qualifier.Qualifier

//TODO Ported from Android side

/**
* ViewModel DSL Extension
* Allow to declare a ViewModel - be later inject into Activity/Fragment with dedicated injector
*
* @author Arnaud Giuliani
*
* @param qualifier - definition qualifier
* @param definition - allow definition override
*/
inline fun <reified T : ViewModel> Module.viewModel(
qualifier: Qualifier? = null,
noinline definition: Definition<T>
): KoinDefinition<T> {
return factory(qualifier, definition)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.koin.compose.viewmodel.dsl

import androidx.lifecycle.ViewModel
import org.koin.core.definition.Definition
import org.koin.core.definition.KoinDefinition
import org.koin.core.qualifier.Qualifier
import org.koin.dsl.ScopeDSL

/**
* ViewModel DSL Extension
* Allow to declare a ViewModel - be later inject into Activity/Fragment with dedicated injector
*
* @author Arnaud Giuliani
*
* @param qualifier - definition qualifier
* @param definition - allow definition override
*/
inline fun <reified T : ViewModel> ScopeDSL.viewModel(
qualifier: Qualifier? = null,
noinline definition: Definition<T>
): KoinDefinition<T> {
return factory(qualifier, definition)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.koin.compose.viewmodel.dsl

import androidx.lifecycle.ViewModel
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.definition.KoinDefinition
import org.koin.core.module.dsl.DefinitionOptions
import org.koin.core.module.dsl.new
import org.koin.core.module.dsl.onOptions
import org.koin.dsl.ScopeDSL

/**
* Declare a [ScopeDSL.viewModel] definition by resolving a constructor reference for the dependency.
* The resolution is done at compile time by leveraging inline functions, no reflection is required.
*
* Example:
* ```
* class MyViewModel : ViewModel()
*
* val myModule = module {
* viewModelOf(::MyViewModel)
* }
* ```
*
* @author Arnaud Giuliani
*
* @see new - function operator
*/

inline fun <reified R : ViewModel> ScopeDSL.viewModelOf(
crossinline constructor: () -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1> ScopeDSL.viewModelOf(
crossinline constructor: (T1) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12, reified T13> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12, reified T13, reified T14> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12, reified T13, reified T14, reified T15> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12, reified T13, reified T14, reified T15, reified T16> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12, reified T13, reified T14, reified T15, reified T16, reified T17> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12, reified T13, reified T14, reified T15, reified T16, reified T17, reified T18> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12, reified T13, reified T14, reified T15, reified T16, reified T17, reified T18, reified T19> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12, reified T13, reified T14, reified T15, reified T16, reified T17, reified T18, reified T19, reified T20> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12, reified T13, reified T14, reified T15, reified T16, reified T17, reified T18, reified T19, reified T20, reified T21> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)

/**
* @see viewModelOf
*/
inline fun <reified R : ViewModel, reified T1, reified T2, reified T3, reified T4, reified T5, reified T6, reified T7, reified T8, reified T9, reified T10, reified T11, reified T12, reified T13, reified T14, reified T15, reified T16, reified T17, reified T18, reified T19, reified T20, reified T21, reified T22> ScopeDSL.viewModelOf(
crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22) -> R,
noinline options: DefinitionOptions<R>? = null,
): KoinDefinition<R> = viewModel { new(constructor) }.onOptions(options)
Loading

0 comments on commit 854d9d7

Please sign in to comment.