From ccecd6cfd448dd91e5298a3ddc495f1f5838757d Mon Sep 17 00:00:00 2001 From: gmerino Date: Wed, 24 Nov 2021 18:45:48 +0100 Subject: [PATCH] Allowing users to mock & inject Tweaks --- build.gradle | 2 +- .../java/com/gmerinojimenez/tweaks/Tweaks.kt | 18 +++++++++--------- .../java/com/gmerinojimenez/tweaks/Tweaks.kt | 15 ++++++--------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/build.gradle b/build.gradle index f14b27f..9c5bed2 100644 --- a/build.gradle +++ b/build.gradle @@ -44,6 +44,6 @@ task clean(type: Delete) { allprojects { group = 'com.gmerinojimenez.tweaks' - version = '0.0.11' + version = '0.0.12' } diff --git a/library/src/enabled/java/com/gmerinojimenez/tweaks/Tweaks.kt b/library/src/enabled/java/com/gmerinojimenez/tweaks/Tweaks.kt index f43fddc..bc77412 100644 --- a/library/src/enabled/java/com/gmerinojimenez/tweaks/Tweaks.kt +++ b/library/src/enabled/java/com/gmerinojimenez/tweaks/Tweaks.kt @@ -34,14 +34,14 @@ import kotlinx.coroutines.flow.StateFlow import javax.inject.Inject -class Tweaks { +open class Tweaks { @Inject internal lateinit var tweaksBusinessLogic: TweaksBusinessLogic - fun getTweakValue(key: String): StateFlow = tweaksBusinessLogic.getValue(key) - - fun getTweakValue(entry: TweakEntry): StateFlow = tweaksBusinessLogic.getValue(entry) + open fun getTweakValue(key: String): StateFlow = tweaksBusinessLogic.getValue(key) + + open fun getTweakValue(entry: TweakEntry): StateFlow = tweaksBusinessLogic.getValue(entry) private fun initializeGraph(tweaksGraph: TweaksGraph) { tweaksBusinessLogic.initialize(tweaksGraph) @@ -49,8 +49,8 @@ class Tweaks { companion object { const val TWEAKS_NAVIGATION_ENTRYPOINT = "tweaks" - private var reference: Tweaks? = null - internal lateinit var component: TweaksComponent + private var reference: Tweaks = Tweaks() + private lateinit var component: TweaksComponent fun init( application: Application, @@ -59,11 +59,11 @@ class Tweaks { reference = Tweaks() inject(application) - reference!!.initializeGraph(tweaksGraph) + reference.initializeGraph(tweaksGraph) } @JvmStatic - fun getReference() = reference!! + fun getReference(): Tweaks = reference private fun inject(application: Application) { component = DaggerTweaksComponent @@ -71,7 +71,7 @@ class Tweaks { .tweaksModule(TweaksModule(application)) .build() - component.inject(reference!!) + component.inject(reference) } } diff --git a/library/src/noop/java/com/gmerinojimenez/tweaks/Tweaks.kt b/library/src/noop/java/com/gmerinojimenez/tweaks/Tweaks.kt index c3e506a..40b86a9 100644 --- a/library/src/noop/java/com/gmerinojimenez/tweaks/Tweaks.kt +++ b/library/src/noop/java/com/gmerinojimenez/tweaks/Tweaks.kt @@ -8,19 +8,18 @@ import com.gmerinojimenez.tweaks.domain.* import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow -class Tweaks { +open class Tweaks { - internal lateinit var tweaksGraph: TweaksGraph private val keyToEntryValueMap: MutableMap> = mutableMapOf() @Suppress("UNCHECKED_CAST") - fun getTweakValue(key: String): StateFlow { + open fun getTweakValue(key: String): StateFlow { val entry= keyToEntryValueMap[key] as TweakEntry return getTweakValue(entry) } @Suppress("UNCHECKED_CAST") - fun getTweakValue(entry: TweakEntry): StateFlow = when (entry as Modifiable) { + open fun getTweakValue(entry: TweakEntry): StateFlow = when (entry as Modifiable) { is ReadOnly<*> -> (entry as ReadOnly).value is Editable<*> -> (entry as Editable).defaultValue ?: MutableStateFlow(null) } @@ -39,19 +38,17 @@ class Tweaks { companion object { const val TWEAKS_NAVIGATION_ENTRYPOINT = "tweaks" - private var reference: Tweaks? = null + private var reference: Tweaks = Tweaks() @JvmStatic fun init( - application: Application, tweaksGraph: TweaksGraph, ) { - reference = Tweaks() - reference!!.initialize(tweaksGraph) + reference.initialize(tweaksGraph) } @JvmStatic - fun getReference() = reference!! + fun getReference(): Tweaks = reference }