Skip to content

Commit

Permalink
Tweaks.init() use Context class instead Application (#23)
Browse files Browse the repository at this point in the history
* Using Context instead Application

* Minor format
  • Loading branch information
dagonco authored Dec 20, 2022
1 parent cc0c7da commit 247be57
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 62 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<p align="center">
<img src="https://img.shields.io/badge/Platform-Android-brightgreen" />
<img src="https://maven-badges.herokuapp.com/maven-central/com.telefonica/tweaks/badge.png" />
Expand Down Expand Up @@ -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())
}
```

Expand Down
108 changes: 54 additions & 54 deletions app/src/main/java/com/telefonica/tweaks/demo/TweakDemoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ 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

class TweakDemoApplication : Application() {
override fun onCreate() {
super.onCreate()
Tweaks.init(this@TweakDemoApplication, demoTweakGraph())
Tweaks.init(this, demoTweakGraph())
}

var timestampState = flow {
Expand All @@ -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") {}
}
}
11 changes: 5 additions & 6 deletions library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,21 +49,21 @@ open class Tweaks {
private lateinit var component: TweaksComponent

fun init(
application: Application,
context: Context,
tweaksGraph: TweaksGraph,
) {
inject(application)
inject(context)

reference.initializeGraph(tweaksGraph)
}

@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)
Expand Down Expand Up @@ -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 = {},
) {
Expand Down

0 comments on commit 247be57

Please sign in to comment.