Skip to content

Commit

Permalink
feat: compose factories implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed Feb 15, 2024
1 parent f81da14 commit 6191a49
Show file tree
Hide file tree
Showing 14 changed files with 543 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package app.meetacy.di.android.compose.factory

import androidx.compose.runtime.Composable
import app.meetacy.di.DI
import app.meetacy.di.android.compose.internal.LazyFactory
import app.meetacy.di.builder.DIBuilder
import app.meetacy.di.builder.DIBuilderProviderDelegate
import kotlin.reflect.KProperty
import kotlin.reflect.KType
import kotlin.reflect.typeOf

public fun interface ComposableFactory0<out R> {
@Composable
public fun create(): R
}

// DI Extensions

@Composable
public fun <R> DI.createComposable(
factoryType: KType,
name: String? = null
): R = get<ComposableFactory0<R>>(factoryType, name).create()

@Composable
public inline fun <reified R> DI.createComposable(
name: String? = null
): R = createComposable(typeOf<ComposableFactory0<R>>(), name)

// DI Delegates

public inline fun <reified R> DI.creatingComposable(): ComposableFactory0Dependency<R> =
ComposableFactory0Dependency(
di = this,
factoryType = typeOf<ComposableFactory0<R>>()
)

public class ComposableFactory0Dependency<R>(
private val di: DI,
private val factoryType: KType
) {
private val lazy = LazyFactory<R>()

@Composable
public operator fun getValue(
thisRef: Any?,
property: KProperty<*>
): R = lazy.get { di.createComposable(factoryType, property.name) }
}

// DIBuilder Extensions

public inline fun <reified R> DIBuilder.composableFactory0(
name: String? = null,
crossinline factory: @Composable DI.() -> R
) {
provider(name) {
ComposableFactory0 { factory() }
}
}

public inline fun <reified R> DIBuilder.composableFactory0(
crossinline factory: @Composable DI.() -> R
): DIBuilderProviderDelegate<ComposableFactory0<R>> = provider {
ComposableFactory0 { factory() }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package app.meetacy.di.android.compose.factory

import androidx.compose.runtime.Composable
import app.meetacy.di.DI
import app.meetacy.di.android.compose.internal.LazyFactory
import app.meetacy.di.builder.DIBuilder
import app.meetacy.di.builder.DIBuilderProviderDelegate
import kotlin.reflect.KProperty
import kotlin.reflect.KType
import kotlin.reflect.typeOf

public fun interface ComposableFactory1<in T1, out R> {
@Composable
public fun create(arg1: T1): R
}

// DI Extensions

@Composable
public fun <T1, R> DI.createComposable(
factoryType: KType,
arg1: T1,
name: String? = null
): R = get<ComposableFactory1<T1, R>>(
type = factoryType,
name = name
).create(arg1)

@Composable
public inline fun <T1, reified R> DI.createComposable(
arg1: T1,
name: String? = null
): R = createComposable(
factoryType = typeOf<ComposableFactory1<T1, R>>(),
arg1 = arg1,
name = name
)

// DI Delegates

public inline fun <T1, reified R> DI.creatingComposable(
arg1: T1
): ComposableFactory1Dependency<T1, R> = ComposableFactory1Dependency(
di = this,
arg1 = arg1,
factoryType = typeOf<ComposableFactory1<T1, R>>()
)

public class ComposableFactory1Dependency<T1, R>(
private val di: DI,
private val arg1: T1,
private val factoryType: KType
) {
private val lazy = LazyFactory<R>()

@Composable
public operator fun getValue(
thisRef: Any?,
property: KProperty<*>
): R = lazy.get { di.createComposable(factoryType, arg1, property.name) }
}

// DIBuilder Extensions

public inline fun <T1, reified R> DIBuilder.composableFactory1(
name: String? = null,
crossinline factory: @Composable DI.(T1) -> R
) {
provider(name) {
ComposableFactory1 { arg1: T1 -> factory(arg1) }
}
}

public inline fun <T1, reified R> DIBuilder.composableFactory1(
crossinline factory: @Composable DI.(T1) -> R
): DIBuilderProviderDelegate<ComposableFactory1<T1, R>> = provider {
ComposableFactory1 { arg1: T1 -> factory(arg1) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package app.meetacy.di.android.compose.factory

import androidx.compose.runtime.Composable
import app.meetacy.di.DI
import app.meetacy.di.android.compose.internal.LazyFactory
import app.meetacy.di.builder.DIBuilder
import app.meetacy.di.builder.DIBuilderProviderDelegate
import kotlin.reflect.KProperty
import kotlin.reflect.KType
import kotlin.reflect.typeOf

public fun interface ComposableFactory2<in T1, in T2, out R> {
@Composable
public fun create(arg1: T1, arg2: T2): R
}

// DI Extensions

@Composable
public fun <T1, T2, R> DI.createComposable(
factoryType: KType,
arg1: T1,
arg2: T2,
name: String? = null
): R = get<ComposableFactory2<T1, T2, R>>(
type = factoryType,
name = name
).create(arg1, arg2)

@Composable
public inline fun <T1, T2, reified R> DI.createComposable(
arg1: T1,
arg2: T2,
name: String? = null
): R = createComposable(
factoryType = typeOf<ComposableFactory2<T1, T2, R>>(),
arg1 = arg1,
arg2 = arg2,
name = name
)

// DI Delegates

public inline fun <T1, T2, reified R> DI.creatingComposable(
arg1: T1,
arg2: T2
): ComposableFactory2Dependency<T1, T2, R> = ComposableFactory2Dependency(
di = this,
arg1 = arg1,
arg2 = arg2,
factoryType = typeOf<ComposableFactory2<T1, T2, R>>()
)

public class ComposableFactory2Dependency<T1, T2, R>(
private val di: DI,
private val arg1: T1,
private val arg2: T2,
private val factoryType: KType
) {
private val lazy = LazyFactory<R>()

@Composable
public operator fun getValue(
thisRef: Any?,
property: KProperty<*>
): R = lazy.get { di.createComposable(factoryType, arg1, arg2, property.name) }
}

// DIBuilder Extensions

public inline fun <T1, T2, reified R> DIBuilder.composableFactory2(
name: String? = null,
crossinline factory: @Composable DI.(T1, T2) -> R
) {
provider(name) {
ComposableFactory2 { arg1: T1, arg2: T2 -> factory(arg1, arg2) }
}
}

public inline fun <T1, T2, reified R> DIBuilder.composableFactory2(
crossinline factory: @Composable DI.(T1, T2) -> R
): DIBuilderProviderDelegate<ComposableFactory2<T1, T2, R>> = provider {
ComposableFactory2 { arg1: T1, arg2: T2 -> factory(arg1, arg2) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package app.meetacy.di.android.compose.factory

import androidx.compose.runtime.Composable
import app.meetacy.di.DI
import app.meetacy.di.android.compose.internal.LazyFactory
import app.meetacy.di.builder.DIBuilder
import app.meetacy.di.builder.DIBuilderProviderDelegate
import kotlin.reflect.KProperty
import kotlin.reflect.KType
import kotlin.reflect.typeOf

public fun interface ComposableFactory3<in T1, in T2, in T3, out R> {
@Composable
public fun create(arg1: T1, arg2: T2, arg3: T3): R
}

// DI Extensions

@Composable
public fun <T1, T2, T3, R> DI.createComposable(
factoryType: KType,
arg1: T1,
arg2: T2,
arg3: T3,
name: String? = null
): R = get<ComposableFactory3<T1, T2, T3, R>>(
type = factoryType,
name = name
).create(arg1, arg2, arg3)

@Composable
public inline fun <T1, T2, T3, reified R> DI.createComposable(
arg1: T1,
arg2: T2,
arg3: T3,
name: String? = null
): R = createComposable(
factoryType = typeOf<ComposableFactory3<T1, T2, T3, R>>(),
arg1 = arg1,
arg2 = arg2,
arg3 = arg3,
name = name
)

// DI Delegates

public inline fun <T1, T2, T3, reified R> DI.creatingComposable(
arg1: T1,
arg2: T2,
arg3: T3
): ComposableFactory3Dependency<T1, T2, T3, R> = ComposableFactory3Dependency(
di = this,
arg1 = arg1,
arg2 = arg2,
arg3 = arg3,
factoryType = typeOf<ComposableFactory3<T1, T2, T3, R>>()
)

public class ComposableFactory3Dependency<T1, T2, T3, R>(
private val di: DI,
private val arg1: T1,
private val arg2: T2,
private val arg3: T3,
private val factoryType: KType
) {
private val lazy = LazyFactory<R>()

@Composable
public operator fun getValue(
thisRef: Any?,
property: KProperty<*>
): R = lazy.get { di.createComposable(factoryType, arg1, arg2, arg3, property.name) }
}

// DIBuilder Extensions

public inline fun <T1, T2, T3, reified R> DIBuilder.composableFactory3(
name: String? = null,
crossinline factory: @Composable DI.(T1, T2, T3) -> R
) {
provider(name) {
ComposableFactory3 { arg1: T1, arg2: T2, arg3: T3 -> factory(arg1, arg2, arg3) }
}
}

public inline fun <T1, T2, T3, reified R> DIBuilder.composableFactory3(
crossinline factory: @Composable DI.(T1, T2, T3) -> R
): DIBuilderProviderDelegate<ComposableFactory3<T1, T2, T3, R>> = provider {
ComposableFactory3 { arg1: T1, arg2: T2, arg3: T3 -> factory(arg1, arg2, arg3) }
}
Loading

0 comments on commit 6191a49

Please sign in to comment.