Skip to content

Commit cdaa948

Browse files
andrewpmsmithfacebook-github-bot
authored andcommitted
Convert DataUtils to Kotlin
Summary: Steps: 1. rootfoo > Kotlin > Convert This Java file to Kotlin 2. Minimal manual tweaking Differential Revision: D38032911 fbshipit-source-id: 7a21cb144bad141e33cab7516f570900e7441d05
1 parent 80346a6 commit cdaa948

File tree

3 files changed

+142
-149
lines changed

3 files changed

+142
-149
lines changed

litho-editor-flipper/build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616

1717
apply plugin: 'com.android.library'
18+
apply plugin: 'kotlin-android'
19+
apply plugin: 'kotlin-kapt'
1820

1921
android {
2022
compileSdkVersion rootProject.compileSdkVersion
@@ -31,6 +33,12 @@ android {
3133
}
3234

3335
dependencies {
36+
api project(':litho-core-kotlin')
37+
38+
kapt project(':litho-processor')
39+
kapt project(':litho-sections-processor')
40+
41+
implementation deps.kotlinStandardLib
3442
implementation project(':litho-core')
3543
implementation project(':litho-widget')
3644
implementation project(':litho-editor-core')
@@ -44,6 +52,17 @@ android {
4452

4553
testImplementation deps.junit
4654
testImplementation deps.robolectric
55+
testImplementation project(':litho-rendercore-testing')
56+
testImplementation project(':litho-testing')
57+
testImplementation project(':litho-widget-kotlin')
58+
testAnnotationProcessor project(':litho-processor')
59+
testImplementation deps.assertjCore
60+
testImplementation deps.supportRecyclerView
61+
testImplementation deps.supportTestJunit
62+
testImplementation deps.mockitokotlin
63+
testImplementation deps.supportTestCore
64+
65+
kaptTest project(':litho-processor')
4766
}
4867
}
4968

litho-editor-flipper/src/main/java/com/facebook/litho/editor/flipper/DataUtils.java

Lines changed: 0 additions & 149 deletions
This file was deleted.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.facebook.litho.editor.flipper
18+
19+
import android.graphics.drawable.ColorDrawable
20+
import android.graphics.drawable.Drawable
21+
import com.facebook.flipper.core.FlipperObject
22+
import com.facebook.flipper.plugins.inspector.InspectorValue
23+
import com.facebook.flipper.plugins.inspector.InspectorValue.Type.Color
24+
import com.facebook.flipper.plugins.inspector.Named
25+
import com.facebook.litho.SpecGeneratedComponent
26+
import com.facebook.litho.StateContainer
27+
import com.facebook.litho.annotations.Prop
28+
import com.facebook.litho.annotations.ResType
29+
import com.facebook.litho.annotations.State
30+
import com.facebook.litho.drawable.ComparableColorDrawable
31+
import java.util.ArrayList
32+
33+
object DataUtils {
34+
35+
@JvmStatic
36+
fun getPropData(node: Any): List<Named<FlipperObject>> {
37+
val props = FlipperObject.Builder()
38+
val data: MutableList<Named<FlipperObject>> = ArrayList()
39+
var hasProps = false
40+
val isSpecComponent = node is SpecGeneratedComponent
41+
for (f in node.javaClass.declaredFields) {
42+
f.isAccessible = true
43+
val prop =
44+
try {
45+
f[node]
46+
} catch (e: IllegalAccessException) {
47+
continue
48+
}
49+
val propName = f.name
50+
val annotation = f.getAnnotation(Prop::class.java)
51+
if (isSpecComponent && annotation == null) {
52+
// Only expose `@Prop` annotated fields for Spec components
53+
continue
54+
}
55+
hasProps = true
56+
if (prop != null && PropWithInspectorSection::class.java.isAssignableFrom(prop.javaClass)) {
57+
val datum = (prop as PropWithInspectorSection).flipperLayoutInspectorSection
58+
if (datum != null) {
59+
data.add(Named(datum.key, FlipperObject(datum.value)))
60+
}
61+
}
62+
if (annotation != null) {
63+
val resType = annotation.resType
64+
if (resType == ResType.COLOR) {
65+
props.put(propName, if (prop == null) "null" else fromColor(prop as Int))
66+
continue
67+
} else if (resType == ResType.DRAWABLE) {
68+
props.put(propName, if (prop == null) "null" else fromDrawable(prop as Drawable?))
69+
continue
70+
}
71+
}
72+
if (prop != null && PropWithDescription::class.java.isAssignableFrom(prop.javaClass)) {
73+
val description = (prop as PropWithDescription).flipperLayoutInspectorPropDescription
74+
// Treat the description as immutable for now, because it's a "translation" of the
75+
// actual prop,
76+
// mutating them is not going to change the original prop.
77+
if (description is Map<*, *>) {
78+
for ((key, value) in description) {
79+
props.put(key.toString(), InspectorValue.immutable(value))
80+
}
81+
} else {
82+
props.put(propName, InspectorValue.immutable(description))
83+
}
84+
continue
85+
}
86+
props.put(propName, FlipperEditor.makeFlipperField(node, f))
87+
}
88+
if (hasProps) {
89+
data.add(Named("Props", props.build()))
90+
}
91+
return data
92+
}
93+
94+
@JvmStatic
95+
fun getStateData(stateContainer: StateContainer?): FlipperObject? {
96+
stateContainer ?: return null
97+
98+
val state = FlipperObject.Builder()
99+
var hasState = false
100+
for (f in stateContainer.javaClass.declaredFields) {
101+
f.isAccessible = true
102+
val annotation = f.getAnnotation(State::class.java)
103+
if (annotation != null) {
104+
state.put(f.name, FlipperEditor.makeFlipperField(stateContainer, f))
105+
hasState = true
106+
}
107+
}
108+
return if (hasState) state.build() else null
109+
}
110+
111+
@JvmStatic
112+
fun fromDrawable(d: Drawable?): InspectorValue<*> {
113+
var color = 0
114+
if (d is ColorDrawable) {
115+
color = d.color
116+
} else if (d is ComparableColorDrawable) {
117+
color = d.color
118+
}
119+
return InspectorValue.mutable(Color, color)
120+
}
121+
122+
@JvmStatic fun fromColor(color: Int): InspectorValue<*> = InspectorValue.mutable(Color, color)
123+
}

0 commit comments

Comments
 (0)