-
Notifications
You must be signed in to change notification settings - Fork 5
[PLUGINS] add konfeature plugin #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,5 @@ | ||
[versions] | ||
konfeature = "0.1.0" | ||
|
||
[libraries] | ||
konfeature = { module = "com.redmadrobot.konfeature:konfeature", version.ref = "konfeature" } |
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
...in/kotlin/com/redmadrobot/debug/noop/plugin/konfeature/KonfeatureDebugPanelInterceptor.kt
This file contains hidden or 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,13 @@ | ||
package com.redmadrobot.debug.plugin.konfeature | ||
|
||
import android.content.Context | ||
import com.redmadrobot.konfeature.source.FeatureValueSource | ||
import com.redmadrobot.konfeature.source.Interceptor | ||
|
||
|
||
public class KonfeatureDebugPanelInterceptor(context: Context) : Interceptor { | ||
|
||
override val name: String = "NoopDebugPanelInterceptor" | ||
|
||
override fun intercept(valueSource: FeatureValueSource, key: String, value: Any): Any? = null | ||
} |
8 changes: 8 additions & 0 deletions
8
no-op/src/main/kotlin/com/redmadrobot/debug/noop/plugin/konfeature/KonfeaturePlugin.kt
This file contains hidden or 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,8 @@ | ||
package com.redmadrobot.debug.plugin.konfeature | ||
|
||
import com.redmadrobot.konfeature.Konfeature | ||
|
||
public class KonfeaturePlugin( | ||
private val debugPanelInterceptor: KonfeatureDebugPanelInterceptor, | ||
private val konfeature: Konfeature, | ||
) |
This file contains hidden or 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 @@ | ||
/build |
This file contains hidden or 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,54 @@ | ||
plugins { | ||
id(Plugins.Android.libraryPlagin) | ||
kotlin(Plugins.Kotlin.androidPlugin) | ||
kotlin(Plugins.Kotlin.kapt) | ||
id("convention-publish") | ||
} | ||
|
||
description = "Plugin for konfeature library integration" | ||
|
||
android { | ||
compileSdk = Project.COMPILE_SDK | ||
lint.targetSdk = Project.TARGET_SDK | ||
|
||
defaultConfig { | ||
minSdk = Project.MIN_SDK | ||
|
||
consumerProguardFile("consumer-rules.pro") | ||
} | ||
|
||
buildTypes { | ||
getByName(Project.BuildTypes.release) { | ||
isMinifyEnabled = false | ||
proguardFiles( | ||
getDefaultProguardFile(Project.Proguard.androidOptimizedRules), | ||
Project.Proguard.projectRules | ||
) | ||
} | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
freeCompilerArgs += "-Xexplicit-api=strict" | ||
} | ||
|
||
buildFeatures { | ||
compose = true | ||
} | ||
|
||
composeOptions { | ||
kotlinCompilerExtensionVersion = androidx.versions.compose.compiler.get() | ||
} | ||
namespace = "com.redmadrobot.debug.plugin.konfeature" | ||
} | ||
|
||
dependencies { | ||
implementation(project(":core")) | ||
implementation(project(":common")) | ||
implementation(androidx.lifecycle.runtime) | ||
} |
Empty file.
This file contains hidden or 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,3 @@ | ||
lib_name = plugin-flipper | ||
lib_vcs=https://github.com/RedMadRobot/debug-panel-android.git | ||
lib_issue_tracker=https://github.com/RedMadRobot/debug-panel-android/issues |
This file contains hidden or 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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle.kts.kts.kts.kts.kts.kts.kts.kts. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
115 changes: 115 additions & 0 deletions
115
...rc/main/kotlin/com/redmadrobot/debug/plugin/konfeature/KonfeatureDebugPanelInterceptor.kt
This file contains hidden or 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,115 @@ | ||
package com.redmadrobot.debug.plugin.konfeature | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import com.redmadrobot.debug.plugin.konfeature.util.JsonConverter | ||
import com.redmadrobot.konfeature.source.FeatureValueSource | ||
import com.redmadrobot.konfeature.source.Interceptor | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlinx.coroutines.withContext | ||
import org.json.JSONObject | ||
import timber.log.Timber | ||
|
||
public class KonfeatureDebugPanelInterceptor(context: Context) : Interceptor { | ||
|
||
private val preferences by lazy { | ||
context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE) | ||
} | ||
|
||
private val _valuesFlow = MutableStateFlow(emptyMap<String, Any>()) | ||
|
||
internal val valuesFlow = _valuesFlow.asStateFlow() | ||
|
||
private val mutex = Mutex() | ||
|
||
override val name: String = "DebugPanelInterceptor" | ||
|
||
init { | ||
CoroutineScope(Dispatchers.IO).launch { fetchValues() } | ||
} | ||
|
||
override fun intercept(valueSource: FeatureValueSource, key: String, value: Any): Any? { | ||
return _valuesFlow.value[key] | ||
?.let { convertTypeIfNeeded(it, value) } | ||
?.takeIf { it != value } | ||
} | ||
|
||
private suspend fun fetchValues() { | ||
_valuesFlow.value = withContext(Dispatchers.IO) { | ||
Sx1a9z4f marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mutex.withLock { preferences.fetchValues() } | ||
} | ||
} | ||
|
||
private fun convertTypeIfNeeded(debugValue: Any, value: Any): Any { | ||
TopHlop marked this conversation as resolved.
Show resolved
Hide resolved
Sx1a9z4f marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var result = when { | ||
debugValue is Int -> debugValue.toLong() | ||
debugValue is Float -> debugValue.toDouble() | ||
else -> debugValue | ||
} | ||
if (result is Long && value is Double) { | ||
result = result.toDouble() | ||
} | ||
return result | ||
} | ||
|
||
internal suspend fun setValue(key: String, value: Any) { | ||
_valuesFlow.update { it + (key to value) } | ||
updateValues(_valuesFlow.value) | ||
} | ||
|
||
internal suspend fun resetValue(key: String) { | ||
_valuesFlow.update { it - key } | ||
updateValues(_valuesFlow.value) | ||
} | ||
|
||
internal suspend fun resetAll() { | ||
Sx1a9z4f marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_valuesFlow.value = emptyMap<String, Any>() | ||
updateValues(_valuesFlow.value) | ||
} | ||
|
||
private suspend fun updateValues(map: Map<String, Any>) { | ||
coroutineScope { | ||
launch(Dispatchers.IO) { | ||
mutex.withLock { preferences.updateValues(map) } | ||
} | ||
} | ||
} | ||
|
||
private fun SharedPreferences.fetchValues(): Map<String, Any> { | ||
Sx1a9z4f marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return try { | ||
val jsonValues = preferences.getString(KEY, EMPTY_MAP) ?: EMPTY_MAP | ||
JsonConverter.toMap(JSONObject(jsonValues)) | ||
} catch (error: Exception) { | ||
Timber.tag(TAG).e(error, "cant fetch debug values") | ||
preferences.edit(commit = true) { remove(KEY) } | ||
emptyMap<String, Any>() | ||
} | ||
} | ||
|
||
private fun SharedPreferences.updateValues(map: Map<String, Any>) { | ||
try { | ||
val jsonValues = JSONObject(map).toString() | ||
preferences.edit(commit = true) { | ||
putString(KEY, jsonValues) | ||
} | ||
} catch (error: Exception) { | ||
Timber.tag(TAG).e(error, "cant update debug values") | ||
} | ||
} | ||
|
||
private companion object { | ||
private const val EMPTY_MAP = "{}" | ||
private const val FILE_NAME = "debug_panel_interceptor_values" | ||
private const val KEY = "values" | ||
private const val TAG = "DebugPanelInterceptor" | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ns/konfeature/src/main/kotlin/com/redmadrobot/debug/plugin/konfeature/KonfeaturePlugin.kt
This file contains hidden or 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,29 @@ | ||
package com.redmadrobot.debug.plugin.konfeature | ||
|
||
import androidx.compose.runtime.Composable | ||
import com.redmadrobot.debug.core.internal.CommonContainer | ||
import com.redmadrobot.debug.core.internal.PluginDependencyContainer | ||
import com.redmadrobot.debug.core.plugin.Plugin | ||
import com.redmadrobot.debug.plugin.konfeaure.ui.KonfeatureScreen | ||
import com.redmadrobot.konfeature.Konfeature | ||
|
||
public class KonfeaturePlugin( | ||
private val debugPanelInterceptor: KonfeatureDebugPanelInterceptor, | ||
private val konfeature: Konfeature, | ||
) : Plugin() { | ||
|
||
public companion object { | ||
Sx1a9z4f marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private const val NAME = "KONFEATURE" | ||
} | ||
|
||
override fun getName(): String = NAME | ||
|
||
override fun getPluginContainer(commonContainer: CommonContainer): PluginDependencyContainer { | ||
return KonfeaturePluginContainer(konfeature, debugPanelInterceptor) | ||
} | ||
|
||
@Composable | ||
override fun content() { | ||
KonfeatureScreen() | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ture/src/main/kotlin/com/redmadrobot/debug/plugin/konfeature/KonfeaturePluginContainer.kt
This file contains hidden or 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,15 @@ | ||
package com.redmadrobot.debug.plugin.konfeature | ||
|
||
import com.redmadrobot.debug.core.internal.PluginDependencyContainer | ||
import com.redmadrobot.debug.plugin.konfeature.ui.KonfeatureViewModel | ||
import com.redmadrobot.konfeature.Konfeature | ||
|
||
internal class KonfeaturePluginContainer( | ||
Sx1a9z4f marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private val konfeature: Konfeature, | ||
private val debugPanelInterceptor: KonfeatureDebugPanelInterceptor, | ||
) : PluginDependencyContainer { | ||
|
||
fun createKonfeatureViewModel(): KonfeatureViewModel { | ||
return KonfeatureViewModel(konfeature, debugPanelInterceptor) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.