Skip to content

Commit

Permalink
Draft: moving forward to Kotlin 1.9.0 and WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
romainbsl committed Aug 4, 2023
1 parent 4cc8f73 commit 3c90ba6
Show file tree
Hide file tree
Showing 27 changed files with 372 additions and 585 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ dependencies {

android {
namespace = "org.kodein.di.android"

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}

kodeinUpload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,61 +30,61 @@ private fun closestDI(thisRef: Any?, rootContext: Context): DI {
*
* @param T The receiver type.
*/
interface DIPropertyDelegateProvider<in T> {
public interface DIPropertyDelegateProvider<in T> {
/** @suppress */
operator fun provideDelegate(thisRef: T, property: KProperty<*>?): Lazy<DI>
public operator fun provideDelegate(thisRef: T, property: KProperty<*>?): Lazy<DI>
}

private class ContextDIPropertyDelegateProvider : DIPropertyDelegateProvider<Context> {
override operator fun provideDelegate(thisRef: Context, property: KProperty<*>?) = lazy { closestDI(thisRef, thisRef) }
}

class LazyContextDIPropertyDelegateProvider(private val getContext: () -> Context) : DIPropertyDelegateProvider<Any?> {
override operator fun provideDelegate(thisRef: Any?, property: KProperty<*>?) = lazy { closestDI(thisRef, getContext()) }
public class LazyContextDIPropertyDelegateProvider(private val getContext: () -> Context) : DIPropertyDelegateProvider<Any?> {
override operator fun provideDelegate(thisRef: Any?, property: KProperty<*>?): Lazy<DI> = lazy { closestDI(thisRef, getContext()) }
}

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*
* To be used on Android's `Context` classes, such as `Activity` or `Service`.
*/
fun closestDI(): DIPropertyDelegateProvider<Context> = ContextDIPropertyDelegateProvider()
public fun closestDI(): DIPropertyDelegateProvider<Context> = ContextDIPropertyDelegateProvider()

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*
* @param context The Android context to use to walk up the context hierarchy.
*/
fun closestDI(context: Context): LazyContextDIPropertyDelegateProvider = LazyContextDIPropertyDelegateProvider { context }
public fun closestDI(context: Context): LazyContextDIPropertyDelegateProvider = LazyContextDIPropertyDelegateProvider { context }

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*
* @param getContext A function that returns the Android context to use to walk up the context hierarchy.
*/
fun closestDI(getContext: () -> Context): DIPropertyDelegateProvider<Any?> = LazyContextDIPropertyDelegateProvider(getContext)
public fun closestDI(getContext: () -> Context): DIPropertyDelegateProvider<Any?> = LazyContextDIPropertyDelegateProvider(getContext)

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun Fragment.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { activity }
public fun Fragment.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { activity }

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun Dialog.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { context }
public fun Dialog.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { context }

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun View.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { context }
public fun View.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { context }

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun AbstractThreadedSyncAdapter.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { context }
public fun AbstractThreadedSyncAdapter.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { context }

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun Loader<*>.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { context }
public fun Loader<*>.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { context }
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import org.kodein.di.bindings.SimpleContextTranslator
import org.kodein.type.TypeToken
import org.kodein.type.generic

val androidCoreContextTranslators = DI.Module(name = "\u2063androidCoreContextTranslators") {
public val androidCoreContextTranslators: DI.Module = DI.Module(name = "\u2063androidCoreContextTranslators") {
RegisterContextTranslator(SimpleContextTranslator<Fragment, Activity>(generic(), generic()) { it.activity })
RegisterContextTranslator(SimpleContextTranslator<Dialog, Context>(generic(), generic()) { it.context })
RegisterContextTranslator(SimpleContextTranslator<View, Context>(generic(), generic()) { it.context })
Expand All @@ -84,7 +84,7 @@ val androidCoreContextTranslators = DI.Module(name = "\u2063androidCoreContextTr
* @return An Android `DI.Module` that defines a lot of platform bindings.
*/
@SuppressLint("NewApi")
fun androidCoreModule(app: Application) = DI.Module(name = "\u2063androidModule") {
public fun androidCoreModule(app: Application): DI.Module = DI.Module(name = "\u2063androidModule") {

importOnce(androidCoreContextTranslators)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import android.os.Bundle
import org.kodein.di.*

/** @suppress */
class RetainedDIFragment : Fragment() {
public class RetainedDIFragment : Fragment() {

private var _di: DI? = null
var di: DI?
public var di: DI?
get() = _di
set(value) {
_di = value
Expand All @@ -34,7 +34,7 @@ private const val DI_RETAINED_FRAGMENT_TAG = "org.kodein.di.android.RetainedDIFr
* @property allowSilentOverride Whether this module is allowed to non-explicit overrides.
* @property init The block of configuration for this module.
*/
fun Activity.retainedDI(allowSilentOverride: Boolean = false, init: DI.MainBuilder.() -> Unit): Lazy<DI> = lazy {
public fun Activity.retainedDI(allowSilentOverride: Boolean = false, init: DI.MainBuilder.() -> Unit): Lazy<DI> = lazy {
(fragmentManager.findFragmentByTag(DI_RETAINED_FRAGMENT_TAG) as? RetainedDIFragment)?.di?.let { return@lazy it }

val di = DI(allowSilentOverride, init)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ private const val SCOPE_FRAGMENT_TAG = "org.kodein.android.ActivityRetainedScope
/**
* A scope that allows to get an activity-scoped singleton that's independent from the activity restart.
*/
open class ActivityRetainedScope private constructor(private val registryType: RegistryType) : Scope<Activity> {
public open class ActivityRetainedScope private constructor(private val registryType: RegistryType) : Scope<Activity> {

private enum class RegistryType {
Standard { override fun new() = StandardScopeRegistry() },
SingleItem { override fun new() = SingleItemScopeRegistry() };
abstract fun new(): ScopeRegistry
}

object Keys {
const val registryTypeOrdinal = "org.kodein.di.android.registryTypeOrdinal"
public object Keys {
public const val registryTypeOrdinal: String = "org.kodein.di.android.registryTypeOrdinal"
}

companion object MultiItem: ActivityRetainedScope(RegistryType.Standard)
public companion object MultiItem: ActivityRetainedScope(RegistryType.Standard)

object SingleItem: ActivityRetainedScope(RegistryType.SingleItem)
public object SingleItem: ActivityRetainedScope(RegistryType.SingleItem)

/** @suppress */
class RetainedScopeFragment: Fragment() {
val registry by lazy {
public class RetainedScopeFragment: Fragment() {
public val registry: ScopeRegistry by lazy {
val ordinal = arguments.getInt(Keys.registryTypeOrdinal)
RegistryType.values()[ordinal].new()
RegistryType.entries[ordinal].new()
}

var transactionPendingFragmentCache: MutableMap<Activity, WeakReference<RetainedScopeFragment>>? = null
public var transactionPendingFragmentCache: MutableMap<Activity, WeakReference<RetainedScopeFragment>>? = null

@Deprecated("Deprecated in Java")
override fun onAttach(context: Context?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package org.kodein.di.android
import android.app.Activity
import org.kodein.di.*

inline fun subDI(noinline parentDI: () -> DI, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit) = DI.lazy(allowSilentOverride) {
public inline fun subDI(noinline parentDI: () -> DI, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit): LazyDI = DI.lazy(allowSilentOverride) {
extend(parentDI(), copy = copy)
init()
}

inline fun subDI(parentDI: Lazy<DI>, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit) = subDI({ parentDI.value }, allowSilentOverride, copy, init)
public inline fun subDI(parentDI: Lazy<DI>, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit): LazyDI = subDI({ parentDI.value }, allowSilentOverride, copy, init)

inline fun <T> T.subDI(parentDI: DIPropertyDelegateProvider<T>, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit) = subDI(parentDI.provideDelegate(this, null), allowSilentOverride, copy, init)
public inline fun <T> T.subDI(parentDI: DIPropertyDelegateProvider<T>, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit): LazyDI = subDI(parentDI.provideDelegate(this, null), allowSilentOverride, copy, init)

inline fun Activity.retainedSubDI(noinline parentDI: () -> DI, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit) = retainedDI(allowSilentOverride) {
public inline fun Activity.retainedSubDI(noinline parentDI: () -> DI, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit): Lazy<DI> = retainedDI(allowSilentOverride) {
extend(parentDI(), copy = copy)
init()
}

inline fun Activity.retainedSubDI(parentDI: Lazy<DI>, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit) = retainedSubDI({ parentDI.value }, allowSilentOverride, copy, init)
public inline fun Activity.retainedSubDI(parentDI: Lazy<DI>, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit): Lazy<DI> = retainedSubDI({ parentDI.value }, allowSilentOverride, copy, init)

inline fun Activity.retainedSubDI(parentDI: DIPropertyDelegateProvider<Activity>, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit) = retainedSubDI(parentDI.provideDelegate(this, null), allowSilentOverride, copy, init)
public inline fun Activity.retainedSubDI(parentDI: DIPropertyDelegateProvider<Activity>, allowSilentOverride: Boolean = false, copy: Copy = Copy.NonCached, crossinline init: DI.MainBuilder.() -> Unit): Lazy<DI> = retainedSubDI(parentDI.provideDelegate(this, null), allowSilentOverride, copy, init)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ dependencies {

android {
namespace = "org.kodein.di.android.support"

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}

kodeinUpload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import android.app.Application
import android.arch.lifecycle.AndroidViewModel
import android.support.v4.app.Fragment
import android.support.v4.content.Loader
import org.kodein.di.android.DIPropertyDelegateProvider
import org.kodein.di.android.LazyContextDIPropertyDelegateProvider
import org.kodein.di.android.closestDI


/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun Fragment.closestDI() = closestDI { requireActivity() }
public fun Fragment.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { requireActivity() }

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun Loader<*>.closestDI() = closestDI { context }
public fun Loader<*>.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { context }

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun AndroidViewModel.closestDI() = closestDI(getApplication<Application>())
public fun AndroidViewModel.closestDI(): LazyContextDIPropertyDelegateProvider = closestDI(getApplication<Application>())
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import org.kodein.di.android.androidCoreModule
import org.kodein.di.bindings.SimpleContextTranslator
import org.kodein.type.generic

val androidSupportContextTranslators = DI.Module("\u2063androidSupportContextTranslators") {
public val androidSupportContextTranslators: DI.Module = DI.Module("\u2063androidSupportContextTranslators") {
importOnce(androidCoreContextTranslators)

RegisterContextTranslator(SimpleContextTranslator<Fragment, Activity>(generic(), generic()) { it.requireActivity() })
RegisterContextTranslator(SimpleContextTranslator<Loader<*>, Context>(generic(), generic()) { it.context })
RegisterContextTranslator(SimpleContextTranslator<AndroidViewModel, Application>(generic(), generic()) { it.getApplication() })
}

fun androidSupportModule(app: Application) = DI.Module("\u2063androidSupportModule") {
public fun androidSupportModule(app: Application): DI.Module = DI.Module("\u2063androidSupportModule") {
importOnce(androidSupportContextTranslators)
importOnce(androidCoreModule(app))
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import org.kodein.di.bindings.*
import org.kodein.di.internal.synchronizedIfNull
import java.util.HashMap

open class AndroidLifecycleScope private constructor(private val newRegistry: () -> ScopeRegistry) : Scope<LifecycleOwner> {
public open class AndroidLifecycleScope private constructor(private val newRegistry: () -> ScopeRegistry) : Scope<LifecycleOwner> {

companion object multiItem: AndroidLifecycleScope(::StandardScopeRegistry)
public companion object multiItem: AndroidLifecycleScope(::StandardScopeRegistry)

object singleItem: AndroidLifecycleScope(::SingleItemScopeRegistry)
public object singleItem: AndroidLifecycleScope(::SingleItemScopeRegistry)

private val map = HashMap<LifecycleOwner, ScopeRegistry>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import org.kodein.type.generic
* @throws DI.DependencyLoopException When calling the factory, if the value construction triggered a dependency loop.
*/
@MainThread
inline fun <A, reified VM> A.viewModelWithSavedStateHandle(
public inline fun <A, reified VM> A.viewModelWithSavedStateHandle(
tag: Any? = null,
): Lazy<VM> where A : AppCompatActivity, A : DIAware, VM : ViewModel {
val factoryProducer = { object : AbstractSavedStateViewModelFactory(this@viewModelWithSavedStateHandle, null) {
Expand Down Expand Up @@ -74,7 +74,7 @@ inline fun <A, reified VM> A.viewModelWithSavedStateHandle(
* @throws DI.DependencyLoopException When calling the factory, if the value construction triggered a dependency loop.
*/
@MainThread
inline fun <F, reified VM> F.viewModelWithSavedStateHandle(
public inline fun <F, reified VM> F.viewModelWithSavedStateHandle(
noinline ownerProducer: () -> ViewModelStoreOwner = { this },
tag: Any? = null,
): Lazy<VM> where F : Fragment, F : DIAware, VM : ViewModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import org.kodein.di.provider
* @throws DI.DependencyLoopException When calling the provider, if the value construction triggered a dependency loop.
*/
@MainThread
inline fun <A, reified VM> A.viewModel(
public inline fun <A, reified VM> A.viewModel(
tag: Any? = null,
): Lazy<VM> where A : AppCompatActivity, A : DIAware, VM : ViewModel {
val factoryProducer = { object : ViewModelProvider.Factory {
Expand Down Expand Up @@ -69,7 +69,7 @@ inline fun <A, reified VM> A.viewModel(
* @throws DI.DependencyLoopException When calling the provider, if the value construction triggered a dependency loop.
*/
@MainThread
inline fun <F, reified VM> F.viewModel(
public inline fun <F, reified VM> F.viewModel(
noinline ownerProducer: () -> ViewModelStoreOwner = { this },
tag: Any? = null,
): Lazy<VM> where F : Fragment, F : DIAware, VM : ViewModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ dependencies {

android {
namespace = "org.kodein.di.android.x"

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}

kodeinUpload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import android.app.Application
import androidx.fragment.app.Fragment
import androidx.lifecycle.AndroidViewModel
import androidx.loader.content.Loader
import org.kodein.di.android.DIPropertyDelegateProvider
import org.kodein.di.android.LazyContextDIPropertyDelegateProvider
import org.kodein.di.android.closestDI


/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun Fragment.closestDI() = closestDI { requireActivity() }
public fun Fragment.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { requireActivity() }

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun Loader<*>.closestDI() = closestDI { context }
public fun Loader<*>.closestDI(): DIPropertyDelegateProvider<Any?> = closestDI { context }

/**
* Returns the closest DI (or the app DI, if no closest DI could be found).
*/
fun AndroidViewModel.closestDI() = closestDI(getApplication<Application>())
public fun AndroidViewModel.closestDI(): LazyContextDIPropertyDelegateProvider = closestDI(getApplication<Application>())
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import org.kodein.di.android.androidCoreModule
import org.kodein.di.bindings.SimpleContextTranslator
import org.kodein.type.generic

val androidXContextTranslators = DI.Module("\u2063androidXContextTranslators") {
public val androidXContextTranslators: DI.Module = DI.Module("\u2063androidXContextTranslators") {
importOnce(androidCoreContextTranslators)

RegisterContextTranslator(SimpleContextTranslator<Fragment, Activity>(generic(), generic()) { it.requireActivity() })
RegisterContextTranslator(SimpleContextTranslator<Loader<*>, Context>(generic(), generic()) { it.context })
RegisterContextTranslator(SimpleContextTranslator<AndroidViewModel, Application>(generic(), generic()) { it.getApplication() })
}

fun androidXModule(app: Application) = DI.Module("\u2063androidXModule") {
public fun androidXModule(app: Application): DI.Module = DI.Module("\u2063androidXModule") {
importOnce(androidXContextTranslators)
importOnce(androidCoreModule(app))
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import org.kodein.di.bindings.*
import org.kodein.di.internal.synchronizedIfNull
import java.util.*

open class AndroidLifecycleScope private constructor(
public open class AndroidLifecycleScope private constructor(
private val newRegistry: () -> ScopeRegistry
) : Scope<LifecycleOwner>, DefaultLifecycleObserver {

companion object multiItem : AndroidLifecycleScope(::StandardScopeRegistry)
public companion object multiItem : AndroidLifecycleScope(::StandardScopeRegistry)

object singleItem : AndroidLifecycleScope(::SingleItemScopeRegistry)
public object singleItem : AndroidLifecycleScope(::SingleItemScopeRegistry)

private val map = HashMap<LifecycleOwner, ScopeRegistry>()

Expand Down
Loading

0 comments on commit 3c90ba6

Please sign in to comment.