Common types
Summary: EnumMapping and InspectableValue taken from the original 'inspector' plugin but translated to Kotlin. Accumulator is just a Map<String, Any> Reviewed By: LukeDefeo Differential Revision: D38823678 fbshipit-source-id: 6af395cab2e3e6930575eb63d22d660dab962fde
This commit is contained in:
committed by
Facebook GitHub Bot
parent
9c8f80c70c
commit
769a1844b7
@@ -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<T>(private val defaultKey: String) {
|
||||
private val map = ArrayMap<String, T>()
|
||||
|
||||
fun put(key: String, value: T) {
|
||||
map.put(key, value)
|
||||
}
|
||||
|
||||
fun get(value: T): InspectableValue<String> {
|
||||
return get(value, true)
|
||||
}
|
||||
|
||||
fun get(value: T, mutable: Boolean = true): InspectableValue<String> {
|
||||
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 <T> findKeyForValue(
|
||||
mapping: SimpleArrayMap<String, T>,
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<T>
|
||||
private constructor(val type: Type<T>, 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<T> internal constructor(private val name: String) {
|
||||
override fun toString(): String {
|
||||
return name
|
||||
}
|
||||
|
||||
companion object {
|
||||
val Auto: Type<Any> = Type<Any>("auto")
|
||||
val Text = Type<String>("text")
|
||||
val Number = Type<Number>("number")
|
||||
val Boolean = Type<Boolean>("boolean")
|
||||
val Enum = Type<String>("enum")
|
||||
val Color = Type<Int>("color")
|
||||
val Picker = Type<Picker>("picker")
|
||||
}
|
||||
}
|
||||
|
||||
class Picker(val values: Set<String>, val selected: String) {}
|
||||
|
||||
companion object {
|
||||
fun <T> mutable(type: Type<T>, value: T): InspectableValue<T> {
|
||||
return InspectableValue(type, value, true)
|
||||
}
|
||||
|
||||
fun <T> immutable(type: Type<T>, value: T): InspectableValue<T> {
|
||||
return InspectableValue(type, value, false)
|
||||
}
|
||||
|
||||
fun mutable(value: Any): InspectableValue<*> {
|
||||
return InspectableValue<Any>(Type.Auto, value, true)
|
||||
}
|
||||
|
||||
fun immutable(value: Any): InspectableValue<*> {
|
||||
return InspectableValue<Any>(Type.Auto, value, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Int> = object : EnumMapping<Int>("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<Int> =
|
||||
object : EnumMapping<Int>("VISIBLE") {
|
||||
init {
|
||||
put("VISIBLE", View.VISIBLE)
|
||||
put("INVISIBLE", View.INVISIBLE)
|
||||
put("GONE", View.GONE)
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(visibility.get("VISIBLE"), equalTo(View.VISIBLE))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user