Skip to content

Commit

Permalink
feat: added warnings about usage of GlobalDI
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed Aug 31, 2023
1 parent 4b873e8 commit 42b0035
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
@file:Suppress("UNUSED_VARIABLE", "NAME_SHADOWING")
@file:Suppress("NAME_SHADOWING")

package app.meetacy.di.android

import android.app.Application
import app.meetacy.di.DI
import app.meetacy.di.android.annotation.AndroidGlobalApi
import app.meetacy.di.builder.di
import app.meetacy.di.dependency.Dependency
import app.meetacy.di.global.GlobalDI
import app.meetacy.di.global.annotation.GlobalApi
import app.meetacy.di.global.di

val di: DI get() {
Expand All @@ -19,7 +21,9 @@ val di: DI get() {

val DI.application: Application by Dependency

@AndroidGlobalApi
object AndroidDI {
@OptIn(GlobalApi::class)
fun init(
application: Application,
di: DI
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package app.meetacy.di.android.annotation

@RequiresOptIn(
message = "Be careful when using such API. " +
"First of all it's immutable (and it will be), so you initialize it's value only once. " +
"That already means, that you can't use global DI if you plan testing things. " +
"It was made primarily meant for integrations between modules where there is no " +
"another option, but to use a Global singleton",
level = RequiresOptIn.Level.WARNING
)
annotation class AndroidGlobalApi()
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package app.meetacy.di.global

import app.meetacy.di.DI
import app.meetacy.di.global.annotation.GlobalApi

public val di: DI
get() = diSource ?: error("DI is not initialized, call GlobalDI.init(di)")

private var diSource: DI? = null

@GlobalApi
public object GlobalDI {
public fun init(di: DI) {
if (diSource != null) error("DI can only be initialized once")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package app.meetacy.di.global.annotation

@RequiresOptIn(
message = "Be careful when using such API. " +
"First of all it's immutable (and it will be), so you initialize it's value only once. " +
"That already means, that you can't use global DI if you plan testing things. " +
"It was made primarily for Android where global singleton is the only way to transfer dependencies " +
"between modules",
level = RequiresOptIn.Level.WARNING
)
public annotation class GlobalApi
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ composeRuntime = "1.3.3"
composeCompiler = "1.4.4"
lifecycle = "2.6.1"

mdi = "0.0.24"
mdi = "0.0.25"

[libraries]

Expand Down

0 comments on commit 42b0035

Please sign in to comment.