-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper methods for context and improved readme.
- Loading branch information
1 parent
08bb21b
commit fcd205e
Showing
3 changed files
with
48 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
kinject-android/src/main/kotlin/kinject/android/ContextExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} |
27 changes: 27 additions & 0 deletions
27
kinject-android/src/main/kotlin/kinject/android/KinjectContextWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |