Skip to content

Commit

Permalink
helper methods for context and improved readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPierce committed Jul 3, 2024
1 parent 08bb21b commit fcd205e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ A multiplatform Dependency Injection library.

## Artifacts

| Artifact | Description |
|---------------------------|--------------------------------------------------------------------------------------|
| kinject | Provides core DI functionality. |
| kinject-android | |
| kinject-compose | Provides the ability to provide an ObjectGraph in a Compose hierarchy. |
| kinject-compose-viewmodel | Provides the ability to get a ViewModel in a compose hierarchy. |
| kinject-viewmodel | Provides the ability to declare a view model dependency, and a factory to create it. |
| Artifact | Description |
|---------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
| kinject | Provides core DI functionality. |
| kinject-android | Provides helper methods for working with `ObjectGraph`s embedded in `Context`s, and helper classes for embedding `ObjectGraph`s in `Contexts`. |
| kinject-android-appcompat | Provides `KinjectAppCompatActivity`. |
| kinject-android-compose | Provides `KinjectComponentActivity`. |
| kinject-compose | Provides the ability to provide an ObjectGraph in a Compose hierarchy. |
| kinject-compose-viewmodel | Provides the ability to get a ViewModel in a compose hierarchy. |
| kinject-viewmodel | Provides the ability to declare a view model dependency, and a factory to create it. |


## Usage
Expand Down
12 changes: 12 additions & 0 deletions kinject-android/src/main/kotlin/kinject/android/ContextExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kinject.android

import android.content.Context
import kinject.ObjectGraph

fun Context.objectGraphOrNull(): ObjectGraph? {
return getSystemService(KinjectApplication.KINJECT_SERVICE) as ObjectGraph?
}

fun Context.objectGraph(): ObjectGraph {
return objectGraphOrNull() ?: error("ObjectGraph not found in Context.")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kinject.android

import android.content.Context
import android.content.ContextWrapper
import kinject.HasObjectGraph
import kinject.ObjectGraph
import kinject.android.KinjectApplication.Companion.KINJECT_SERVICE

class KinjectContextWrapper(
base: Context,
override val objectGraph: ObjectGraph,
) : ContextWrapper(base), HasObjectGraph {
override fun getSystemService(name: String): Any {
return when (name) {
KINJECT_SERVICE -> objectGraph
else -> super.getSystemService(name)
}
}

override fun getSystemServiceName(serviceClass: Class<*>): String? {
return if (serviceClass === ObjectGraph::class.java) {
KINJECT_SERVICE
} else {
super.getSystemServiceName(serviceClass)
}
}
}

0 comments on commit fcd205e

Please sign in to comment.