Skip to content

Commit

Permalink
Allowing users to mock & inject Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
gmerinojimenez committed Nov 24, 2021
1 parent a583349 commit ccecd6c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ task clean(type: Delete) {
allprojects {

group = 'com.gmerinojimenez.tweaks'
version = '0.0.11'
version = '0.0.12'

}
18 changes: 9 additions & 9 deletions library/src/enabled/java/com/gmerinojimenez/tweaks/Tweaks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject


class Tweaks {
open class Tweaks {

@Inject
internal lateinit var tweaksBusinessLogic: TweaksBusinessLogic

fun <T>getTweakValue(key: String): StateFlow<T?> = tweaksBusinessLogic.getValue(key)
fun <T> getTweakValue(entry: TweakEntry<T>): StateFlow<T?> = tweaksBusinessLogic.getValue(entry)
open fun <T>getTweakValue(key: String): StateFlow<T?> = tweaksBusinessLogic.getValue(key)

open fun <T> getTweakValue(entry: TweakEntry<T>): StateFlow<T?> = tweaksBusinessLogic.getValue(entry)

private fun initializeGraph(tweaksGraph: TweaksGraph) {
tweaksBusinessLogic.initialize(tweaksGraph)
}

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,
Expand All @@ -59,19 +59,19 @@ 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
.builder()
.tweaksModule(TweaksModule(application))
.build()

component.inject(reference!!)
component.inject(reference)
}
}

Expand Down
15 changes: 6 additions & 9 deletions library/src/noop/java/com/gmerinojimenez/tweaks/Tweaks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, TweakEntry<*>> = mutableMapOf()

@Suppress("UNCHECKED_CAST")
fun <T> getTweakValue(key: String): StateFlow<T?> {
open fun <T> getTweakValue(key: String): StateFlow<T?> {
val entry= keyToEntryValueMap[key] as TweakEntry<T>
return getTweakValue(entry)
}

@Suppress("UNCHECKED_CAST")
fun <T> getTweakValue(entry: TweakEntry<T>): StateFlow<T?> = when (entry as Modifiable) {
open fun <T> getTweakValue(entry: TweakEntry<T>): StateFlow<T?> = when (entry as Modifiable) {
is ReadOnly<*> -> (entry as ReadOnly<T>).value
is Editable<*> -> (entry as Editable<T>).defaultValue ?: MutableStateFlow(null)
}
Expand All @@ -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
}


Expand Down

0 comments on commit ccecd6c

Please sign in to comment.