From 769a1844b7f4790488d77bc8df3ff1c15c980900 Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Fri, 19 Aug 2022 03:39:06 -0700 Subject: [PATCH] Common types Summary: EnumMapping and InspectableValue taken from the original 'inspector' plugin but translated to Kotlin. Accumulator is just a Map Reviewed By: LukeDefeo Differential Revision: D38823678 fbshipit-source-id: 6af395cab2e3e6930575eb63d22d660dab962fde --- .../plugins/uidebugger/common/EnumMapping.kt | 66 ++++++++++++++++++ .../uidebugger/common/InspectableValue.kt | 54 +++++++++++++++ .../plugins/uidebugger/EnumMappingTest.kt | 68 +++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/EnumMapping.kt create mode 100644 android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/InspectableValue.kt create mode 100644 android/src/test/java/com/facebook/flipper/plugins/uidebugger/EnumMappingTest.kt diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/EnumMapping.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/EnumMapping.kt new file mode 100644 index 000000000..e26a996ed --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/EnumMapping.kt @@ -0,0 +1,66 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.flipper.plugins.uidebugger.common + +import androidx.collection.ArrayMap +import androidx.collection.SimpleArrayMap + +open class EnumMapping(private val defaultKey: String) { + private val map = ArrayMap() + + fun put(key: String, value: T) { + map.put(key, value) + } + + fun get(value: T): InspectableValue { + return get(value, true) + } + + fun get(value: T, mutable: Boolean = true): InspectableValue { + val key = findKeyForValue(map, defaultKey, value) + return if (mutable) InspectableValue.mutable(InspectableValue.Type.Enum, key) + else InspectableValue.immutable(InspectableValue.Type.Enum, key) + } + + fun get(s: String): T? { + return if (map.containsKey(s)) { + map[s] + } else map[defaultKey] + } + + fun toPicker(mutable: Boolean = true): InspectableValue<*> { + return if (mutable) + InspectableValue.mutable( + InspectableValue.Type.Picker, InspectableValue.Picker(map.keys, defaultKey)) + else InspectableValue.immutable(InspectableValue.Type.Enum, defaultKey) + } + + fun toPicker(currentValue: T, mutable: Boolean = true): InspectableValue<*> { + val value = findKeyForValue(map, defaultKey, currentValue) + return if (mutable) + InspectableValue.mutable( + InspectableValue.Type.Picker, InspectableValue.Picker(map.keys, value)) + else InspectableValue.immutable(InspectableValue.Type.Enum, value) + } + + companion object { + fun findKeyForValue( + mapping: SimpleArrayMap, + defaultKey: String, + currentValue: T + ): String { + val count = mapping.size() - 1 + for (i in 0..count) { + if (mapping.valueAt(i) == currentValue) { + return mapping.keyAt(i) + } + } + return defaultKey + } + } +} diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/InspectableValue.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/InspectableValue.kt new file mode 100644 index 000000000..ccd16432a --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/common/InspectableValue.kt @@ -0,0 +1,54 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.flipper.plugins.uidebugger.common + +class InspectableValue +private constructor(val type: Type, val value: T, val mutable: Boolean) { + /** + * Describe the type of data this value contains. This will influence how values are parsed and + * displayed by the Flipper desktop app. For example colors will be parse as integers and + * displayed using hex values and be editable using a color picker. + * + * Do not extends this list of types without adding support for the type in the desktop Inspector. + */ + class Type internal constructor(private val name: String) { + override fun toString(): String { + return name + } + + companion object { + val Auto: Type = Type("auto") + val Text = Type("text") + val Number = Type("number") + val Boolean = Type("boolean") + val Enum = Type("enum") + val Color = Type("color") + val Picker = Type("picker") + } + } + + class Picker(val values: Set, val selected: String) {} + + companion object { + fun mutable(type: Type, value: T): InspectableValue { + return InspectableValue(type, value, true) + } + + fun immutable(type: Type, value: T): InspectableValue { + return InspectableValue(type, value, false) + } + + fun mutable(value: Any): InspectableValue<*> { + return InspectableValue(Type.Auto, value, true) + } + + fun immutable(value: Any): InspectableValue<*> { + return InspectableValue(Type.Auto, value, false) + } + } +} diff --git a/android/src/test/java/com/facebook/flipper/plugins/uidebugger/EnumMappingTest.kt b/android/src/test/java/com/facebook/flipper/plugins/uidebugger/EnumMappingTest.kt new file mode 100644 index 000000000..6b15948b0 --- /dev/null +++ b/android/src/test/java/com/facebook/flipper/plugins/uidebugger/EnumMappingTest.kt @@ -0,0 +1,68 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.flipper.plugins.uidebugger + +import android.view.View +import com.facebook.flipper.plugins.uidebugger.common.EnumMapping +import com.facebook.flipper.plugins.uidebugger.common.InspectableValue +import org.hamcrest.CoreMatchers +import org.hamcrest.CoreMatchers.* +import org.hamcrest.MatcherAssert.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class EnumMappingTest { + @Throws(Exception::class) + @Test + fun emptyMapping() { + val e: EnumMapping = object : EnumMapping("k") {} + + assertThat(e.get("j"), CoreMatchers.`is`(nullValue())) + + var inspectable = e.get(0) + assertThat(inspectable.mutable, equalTo(true)) + assertThat(inspectable.type, equalTo(InspectableValue.Type.Enum)) + assertThat(inspectable.value, equalTo("k")) + + inspectable = e.get(0, true) + assertThat(inspectable.mutable, equalTo(true)) + assertThat(inspectable.type, equalTo(InspectableValue.Type.Enum)) + assertThat(inspectable.value, equalTo("k")) + + inspectable = e.get(0, false) + assertThat(inspectable.mutable, equalTo(false)) + assertThat(inspectable.type, equalTo(InspectableValue.Type.Enum)) + assertThat(inspectable.value, equalTo("k")) + + var picker = e.toPicker() + assertThat(picker.mutable, equalTo(true)) + assertThat(picker.type, equalTo(InspectableValue.Type.Picker)) + assertThat(picker.value, CoreMatchers.`is`(notNullValue())) + + val value: InspectableValue.Picker = picker.value as InspectableValue.Picker + assertThat(value.selected, equalTo("k")) + assertThat(value.values.size, equalTo(0)) + } + + @Throws(Exception::class) + @Test + fun putGet() { + val visibility: EnumMapping = + object : EnumMapping("VISIBLE") { + init { + put("VISIBLE", View.VISIBLE) + put("INVISIBLE", View.INVISIBLE) + put("GONE", View.GONE) + } + } + + assertThat(visibility.get("VISIBLE"), equalTo(View.VISIBLE)) + } +}