From 247be5742eca98109a1dd94ea2f137386e18beae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gonz=C3=A1lez?= Date: Tue, 20 Dec 2022 16:44:03 +0100 Subject: [PATCH] Tweaks.init() use Context class instead Application (#23) * Using Context instead Application * Minor format --- README.md | 3 +- .../tweaks/demo/TweakDemoApplication.kt | 108 +++++++++--------- .../java/com/telefonica/tweaks/Tweaks.kt | 11 +- 3 files changed, 60 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 5f17eb1..862a40a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -

@@ -34,7 +33,7 @@ Then initialize the library in your app's `onCreate`: ```kotlin override fun onCreate() { super.onCreate() - Tweaks.init(this@TweakDemoApplication, demoTweakGraph()) + Tweaks.init(context, demoTweakGraph()) } ``` diff --git a/app/src/main/java/com/telefonica/tweaks/demo/TweakDemoApplication.kt b/app/src/main/java/com/telefonica/tweaks/demo/TweakDemoApplication.kt index 8d6ecaf..7fa9302 100644 --- a/app/src/main/java/com/telefonica/tweaks/demo/TweakDemoApplication.kt +++ b/app/src/main/java/com/telefonica/tweaks/demo/TweakDemoApplication.kt @@ -3,7 +3,7 @@ package com.telefonica.tweaks.demo import android.app.Application import android.widget.Toast import com.telefonica.tweaks.Tweaks -import com.telefonica.tweaks.domain.* +import com.telefonica.tweaks.domain.tweaksGraph import kotlinx.coroutines.delay import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOf @@ -11,7 +11,7 @@ import kotlinx.coroutines.flow.flowOf class TweakDemoApplication : Application() { override fun onCreate() { super.onCreate() - Tweaks.init(this@TweakDemoApplication, demoTweakGraph()) + Tweaks.init(this, demoTweakGraph()) } var timestampState = flow { @@ -21,61 +21,61 @@ class TweakDemoApplication : Application() { } } -private fun demoTweakGraph() = tweaksGraph { - cover("Tweaks") { - label("Current user ID:") { flowOf("80057182") } - label("Current IP:") { flowOf("192.168.1.127") } - label("Current IP (public):") { flowOf("80.68.1.92") } - label("Timestamp:") { timestampState } - dropDownMenu( - key = "spinner1", - name = "Spinner example", - values = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"), - defaultValue = flowOf("Monday") - ) - } - category("Statistics") { - group("Group 1") { - label( - name = "Current timestamp", - ) { - timestampState - } - editableString( - key = "value1", - name = "Value 1", - ) - editableBoolean( - key = "value2", - name = "Value 2", - defaultValue = true, - ) - editableLong( - key = "value4", - name = "Value 4", - defaultValue = 42L, + private fun demoTweakGraph() = tweaksGraph { + cover("Tweaks") { + label("Current user ID:") { flowOf("80057182") } + label("Current IP:") { flowOf("192.168.1.127") } + label("Current IP (public):") { flowOf("80.68.1.92") } + label("Timestamp:") { timestampState } + dropDownMenu( + key = "spinner1", + name = "Spinner example", + values = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"), + defaultValue = flowOf("Monday") ) + } + category("Statistics") { + group("Group 1") { + label( + name = "Current timestamp", + ) { + timestampState + } + editableString( + key = "value1", + name = "Value 1", + ) + editableBoolean( + key = "value2", + name = "Value 2", + defaultValue = true, + ) + editableLong( + key = "value4", + name = "Value 4", + defaultValue = 42L, + ) - button( - name = "Demo button" - ) { - Toast.makeText(this@TweakDemoApplication, "Demo button", Toast.LENGTH_LONG) - .show() - } - routeButton( - name = "Custom screen button", - route = "custom-screen" - ) - customNavigationButton( - name = "Another custom screen button", - navigation = { navController -> - navController.navigate("custom-screen") + button( + name = "Demo button" + ) { + Toast.makeText(this@TweakDemoApplication, "Demo button", Toast.LENGTH_LONG) + .show() } - ) + routeButton( + name = "Custom screen button", + route = "custom-screen" + ) + customNavigationButton( + name = "Another custom screen button", + navigation = { navController -> + navController.navigate("custom-screen") + } + ) + } } + category("API") {} + category("Chat") {} + category("Crash reporting") {} } - category("API") {} - category("Chat") {} - category("Crash reporting") {} -} } \ No newline at end of file diff --git a/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt b/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt index f5b1c1f..0d5c044 100644 --- a/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt +++ b/library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt @@ -2,7 +2,6 @@ package com.telefonica.tweaks import android.Manifest import android.annotation.SuppressLint -import android.app.Application import android.content.Context import android.content.pm.PackageManager import android.hardware.SensorManager @@ -50,10 +49,10 @@ open class Tweaks { private lateinit var component: TweaksComponent fun init( - application: Application, + context: Context, tweaksGraph: TweaksGraph, ) { - inject(application) + inject(context) reference.initializeGraph(tweaksGraph) } @@ -61,10 +60,10 @@ open class Tweaks { @JvmStatic fun getReference(): Tweaks = reference - private fun inject(application: Application) { + private fun inject(context: Context) { component = DaggerTweaksComponent .builder() - .tweaksModule(TweaksModule(application)) + .tweaksModule(TweaksModule(context)) .build() component.inject(reference) @@ -109,7 +108,7 @@ private fun vibrateIfAble(context: Context) { fun NavGraphBuilder.addTweakGraph( navController: NavController, tweaksCustomTheme: @Composable (block: @Composable () -> Unit) -> Unit = { - DefaultTweaksTheme(content = it) + DefaultTweaksTheme(content = it) }, customComposableScreens: NavGraphBuilder.() -> Unit = {}, ) {