From 1db39b8171f7c0c584fd35898f3fd9dfa2c69eca Mon Sep 17 00:00:00 2001 From: Omer Strulovich Date: Thu, 19 Aug 2021 07:27:32 -0700 Subject: [PATCH] Enable and apply Ktfmt to xplat/simplesql, xplat/sonar, and xplat/spectrum Summary: As title. Reviewed By: zertosh Differential Revision: D30425160 fbshipit-source-id: c72d270d7cd3f30990aac55e33e8f72d60ed5fe2 --- .../leakcanary2/FlipperLeakListener.kt | 46 ++--- .../leakcanary2/LeakCanary2FlipperPlugin.kt | 60 +++--- .../plugins/leakcanary2/LeakCanary2Report.kt | 186 +++++++++--------- .../SendProtobufToFlipperFromRetrofit.kt | 47 +++-- ...finitionsToMessageDefinitionsIfProtobuf.kt | 33 ++-- ...RetrofitServiceToGenericCallDefinitions.kt | 95 +++++---- .../model/CallNestedMessagesPayload.kt | 39 ++-- .../flipper/sample/tutorial/MainActivity.kt | 14 +- .../flipper/sample/tutorial/MarineMammal.kt | 2 +- .../flipper/sample/tutorial/MarineMammals.kt | 30 +-- .../sample/tutorial/TutorialApplication.kt | 30 +-- .../tutorial/plugin/SeaMammalFlipperPlugin.kt | 39 ++-- .../sample/tutorial/ui/FeedItemCardSpec.kt | 14 +- .../sample/tutorial/ui/FeedSectionSpec.kt | 28 ++- .../tutorial/ui/MarineMammelComponentSpec.kt | 39 ++-- .../sample/tutorial/ui/RootComponentSpec.kt | 16 +- .../tutorial/ui/SingleImageComponentSpec.kt | 17 +- .../sample/tutorial/ExampleUnitTest.kt | 11 +- 18 files changed, 359 insertions(+), 387 deletions(-) diff --git a/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/FlipperLeakListener.kt b/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/FlipperLeakListener.kt index 2cba5cfb4..a8db767e1 100644 --- a/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/FlipperLeakListener.kt +++ b/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/FlipperLeakListener.kt @@ -14,32 +14,34 @@ import shark.HeapAnalysis import shark.HeapAnalysisSuccess class FlipperLeakListener : OnHeapAnalyzedListener { - private val leaks: MutableList = mutableListOf() + private val leaks: MutableList = mutableListOf() - private val defaultListener = DefaultOnHeapAnalyzedListener.create() + private val defaultListener = DefaultOnHeapAnalyzedListener.create() - override fun onHeapAnalyzed(heapAnalysis: HeapAnalysis) { - leaks.addAll(heapAnalysis.toLeakList()) + override fun onHeapAnalyzed(heapAnalysis: HeapAnalysis) { + leaks.addAll(heapAnalysis.toLeakList()) - AndroidFlipperClient.getInstanceIfInitialized()?.let { client -> - (client.getPlugin(LeakCanary2FlipperPlugin.ID) as? LeakCanary2FlipperPlugin) - ?.reportLeaks(leaks) - } - - defaultListener.onHeapAnalyzed(heapAnalysis) + AndroidFlipperClient.getInstanceIfInitialized()?.let { client -> + (client.getPlugin(LeakCanary2FlipperPlugin.ID) as? LeakCanary2FlipperPlugin)?.reportLeaks( + leaks) } - private fun HeapAnalysis.toLeakList(): List { - return if (this is HeapAnalysisSuccess) { - allLeaks.mapNotNull { - if (it.leakTraces.isNotEmpty()) { - it.leakTraces[0].toLeak(it.shortDescription) - } else { - null - } - }.toList() - } else { - emptyList() - } + defaultListener.onHeapAnalyzed(heapAnalysis) + } + + private fun HeapAnalysis.toLeakList(): List { + return if (this is HeapAnalysisSuccess) { + allLeaks + .mapNotNull { + if (it.leakTraces.isNotEmpty()) { + it.leakTraces[0].toLeak(it.shortDescription) + } else { + null + } + } + .toList() + } else { + emptyList() } + } } diff --git a/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/LeakCanary2FlipperPlugin.kt b/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/LeakCanary2FlipperPlugin.kt index 9d0388139..dc1021795 100644 --- a/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/LeakCanary2FlipperPlugin.kt +++ b/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/LeakCanary2FlipperPlugin.kt @@ -14,40 +14,40 @@ private const val REPORT_LEAK_EVENT = "reportLeak2" private const val CLEAR_EVENT = "clear" class LeakCanary2FlipperPlugin : FlipperPlugin { - private val leaks: MutableList = mutableListOf() - private val alreadySeenLeakSignatures: MutableSet = mutableSetOf() - private var connection: FlipperConnection? = null + private val leaks: MutableList = mutableListOf() + private val alreadySeenLeakSignatures: MutableSet = mutableSetOf() + private var connection: FlipperConnection? = null - override fun getId() = ID + override fun getId() = ID - override fun onConnect(connection: FlipperConnection?) { - this.connection = connection - connection?.receive(CLEAR_EVENT) { _, _ -> leaks.clear() } - sendLeakList() + override fun onConnect(connection: FlipperConnection?) { + this.connection = connection + connection?.receive(CLEAR_EVENT) { _, _ -> leaks.clear() } + sendLeakList() + } + + override fun onDisconnect() { + connection = null + } + + override fun runInBackground() = false + + internal fun reportLeaks(leaks: List) { + for (leak in leaks) { + if (leak.signature !in alreadySeenLeakSignatures) { + this.leaks.add(leak) + alreadySeenLeakSignatures.add(leak.signature) + } } - override fun onDisconnect() { - connection = null - } + sendLeakList() + } - override fun runInBackground() = false + private fun sendLeakList() { + connection?.send(REPORT_LEAK_EVENT, LeakCanary2Report(leaks).toFlipperObject()) + } - internal fun reportLeaks(leaks: List) { - for (leak in leaks) { - if (leak.signature !in alreadySeenLeakSignatures) { - this.leaks.add(leak) - alreadySeenLeakSignatures.add(leak.signature) - } - } - - sendLeakList() - } - - private fun sendLeakList() { - connection?.send(REPORT_LEAK_EVENT, LeakCanary2Report(leaks).toFlipperObject()) - } - - companion object { - const val ID = "LeakCanary" - } + companion object { + const val ID = "LeakCanary" + } } diff --git a/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/LeakCanary2Report.kt b/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/LeakCanary2Report.kt index 410db39f8..e255d3864 100644 --- a/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/LeakCanary2Report.kt +++ b/android/plugins/leakcanary2/src/main/java/com/facebook/flipper/plugins/leakcanary2/LeakCanary2Report.kt @@ -10,135 +10,133 @@ package com.facebook.flipper.plugins.leakcanary2 import com.facebook.flipper.core.FlipperArray import com.facebook.flipper.core.FlipperObject import com.facebook.flipper.core.FlipperValue +import java.util.UUID import shark.LeakTrace import shark.LeakTraceObject -import java.util.UUID internal data class LeakCanary2Report(val leaks: List) : FlipperValue { - override fun toFlipperObject(): FlipperObject = FlipperObject.Builder() - .put("leaks", leaks.map { it.toFlipperObject() }.toFlipperArray()) - .build() + override fun toFlipperObject(): FlipperObject = + FlipperObject.Builder() + .put("leaks", leaks.map { it.toFlipperObject() }.toFlipperArray()) + .build() } internal data class Leak( - val title: String, - val root: String, - val elements: Map, - val retainedSize: String, - val signature: String, - val details: String + val title: String, + val root: String, + val elements: Map, + val retainedSize: String, + val signature: String, + val details: String ) : FlipperValue { - override fun toFlipperObject(): FlipperObject { - return FlipperObject.Builder() - .put("title", title) - .put("root", root) - .put("elements", elements.toFlipperObject()) - .put("retainedSize", retainedSize) - .put("details", details) - .build() - } + override fun toFlipperObject(): FlipperObject { + return FlipperObject.Builder() + .put("title", title) + .put("root", root) + .put("elements", elements.toFlipperObject()) + .put("retainedSize", retainedSize) + .put("details", details) + .build() + } - private fun Map.toFlipperObject(): FlipperObject = - mapValues { it.value.toFlipperObject() }.toFlipperObject() + private fun Map.toFlipperObject(): FlipperObject = + mapValues { it.value.toFlipperObject() }.toFlipperObject() - @JvmName("toFlipperObjectStringFlipperObject") - private fun Map.toFlipperObject(): FlipperObject = - asIterable() - .fold(FlipperObject.Builder()) { builder, entry -> - builder.put(entry.key, entry.value) - } - .build() + @JvmName("toFlipperObjectStringFlipperObject") + private fun Map.toFlipperObject(): FlipperObject = + asIterable() + .fold(FlipperObject.Builder()) { builder, entry -> builder.put(entry.key, entry.value) } + .build() } internal fun LeakTrace.toLeak(title: String): Leak { - val elements = getElements() - return Leak( - title = title, - elements = elements.toMap(), - retainedSize = retainedHeapByteSize?.let { "$it bytes" } ?: "unknown size", - signature = signature, - root = elements.first().first, - details = "$this" - ) + val elements = getElements() + return Leak( + title = title, + elements = elements.toMap(), + retainedSize = retainedHeapByteSize?.let { "$it bytes" } ?: "unknown size", + signature = signature, + root = elements.first().first, + details = "$this") } private fun LeakTrace.getElements(): List> { - val referenceElements = referencePath.map { reference -> - val id = UUID.randomUUID().toString() - id to Element(id, reference.originObject) - }.toMutableList() + val referenceElements = + referencePath + .map { reference -> + val id = UUID.randomUUID().toString() + id to Element(id, reference.originObject) + } + .toMutableList() - val leakId = UUID.randomUUID().toString() - referenceElements.add(leakId to Element(leakId, leakingObject)) + val leakId = UUID.randomUUID().toString() + referenceElements.add(leakId to Element(leakId, leakingObject)) - return referenceElements.mapIndexed { index, pair -> - pair.first to if (index == referenceElements.lastIndex) pair.second else pair.second.copy( - children = listOf(referenceElements[index + 1].second.id) - ) - } + return referenceElements.mapIndexed { index, pair -> + pair.first to + if (index == referenceElements.lastIndex) pair.second + else pair.second.copy(children = listOf(referenceElements[index + 1].second.id)) + } } internal data class Element( - val id: String, - val name: String, - val expanded: Boolean = true, - val children: List = emptyList(), - val attributes: List, - val decoration: String = "" + val id: String, + val name: String, + val expanded: Boolean = true, + val children: List = emptyList(), + val attributes: List, + val decoration: String = "" ) : FlipperValue { - constructor(id: String, leakObject: LeakTraceObject) : this( - id = id, - name = "${leakObject.className} (${leakObject.typeName})", - attributes = listOf( - ElementAttribute("leaking", leakObject.leakingStatus.shortName), - ElementAttribute("retaining", leakObject.retaining) - ) - ) + constructor( + id: String, + leakObject: LeakTraceObject + ) : this( + id = id, + name = "${leakObject.className} (${leakObject.typeName})", + attributes = + listOf( + ElementAttribute("leaking", leakObject.leakingStatus.shortName), + ElementAttribute("retaining", leakObject.retaining))) - override fun toFlipperObject(): FlipperObject { - return FlipperObject.Builder() - .put("id", id) - .put("name", name) - .put("expanded", expanded) - .put("children", children.toFlipperArray()) - .put("attributes", attributes.toFlipperArray()) - .put("data", EMPTY_FLIPPER_OBJECT) - .put("decoration", decoration) - .put("extraInfo", EMPTY_FLIPPER_OBJECT) - .build() - } + override fun toFlipperObject(): FlipperObject { + return FlipperObject.Builder() + .put("id", id) + .put("name", name) + .put("expanded", expanded) + .put("children", children.toFlipperArray()) + .put("attributes", attributes.toFlipperArray()) + .put("data", EMPTY_FLIPPER_OBJECT) + .put("decoration", decoration) + .put("extraInfo", EMPTY_FLIPPER_OBJECT) + .build() + } - @JvmName("toFlipperArrayFlipperValue") - private fun Iterable.toFlipperArray(): FlipperArray = - map { it.toFlipperObject() }.toFlipperArray() + @JvmName("toFlipperArrayFlipperValue") + private fun Iterable.toFlipperArray(): FlipperArray = + map { it.toFlipperObject() }.toFlipperArray() - @JvmName("toFlipperArrayString") - private fun Iterable.toFlipperArray(): FlipperArray = - fold(FlipperArray.Builder()) { builder, row -> builder.put(row) }.build() + @JvmName("toFlipperArrayString") + private fun Iterable.toFlipperArray(): FlipperArray = + fold(FlipperArray.Builder()) { builder, row -> builder.put(row) }.build() } internal fun Iterable.toFlipperArray(): FlipperArray = - fold(FlipperArray.Builder()) { builder, row -> builder.put(row) }.build() + fold(FlipperArray.Builder()) { builder, row -> builder.put(row) }.build() private val LeakTraceObject.LeakingStatus.shortName: String - get() = when (this) { + get() = + when (this) { LeakTraceObject.LeakingStatus.NOT_LEAKING -> "N" LeakTraceObject.LeakingStatus.LEAKING -> "Y" LeakTraceObject.LeakingStatus.UNKNOWN -> "?" - } + } private val LeakTraceObject.retaining: String - get() = retainedHeapByteSize?.let { "$it bytes ($retainedObjectCount objects)" } ?: "unknown" + get() = retainedHeapByteSize?.let { "$it bytes ($retainedObjectCount objects)" } ?: "unknown" private val EMPTY_FLIPPER_OBJECT = FlipperObject.Builder().build() -data class ElementAttribute( - val name: String, - val value: String -) : FlipperValue { - override fun toFlipperObject(): FlipperObject { - return FlipperObject.Builder() - .put("name", name) - .put("value", value) - .build() - } +data class ElementAttribute(val name: String, val value: String) : FlipperValue { + override fun toFlipperObject(): FlipperObject { + return FlipperObject.Builder().put("name", name).put("value", value).build() + } } diff --git a/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/SendProtobufToFlipperFromRetrofit.kt b/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/SendProtobufToFlipperFromRetrofit.kt index 24fd4f93d..b297fb0d7 100644 --- a/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/SendProtobufToFlipperFromRetrofit.kt +++ b/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/SendProtobufToFlipperFromRetrofit.kt @@ -16,35 +16,32 @@ import com.facebook.flipper.plugins.retrofit2protobuf.adapter.RetrofitServiceToG import com.facebook.flipper.plugins.retrofit2protobuf.model.CallNestedMessagesPayload object SendProtobufToFlipperFromRetrofit { - operator fun invoke(baseUrl: String, service: Class<*>) { - getNetworkPlugin()?.addProtobufDefinitions( - baseUrl, - generateProtobufDefinitions(service).toFlipperArray() - ) - } + operator fun invoke(baseUrl: String, service: Class<*>) { + getNetworkPlugin() + ?.addProtobufDefinitions(baseUrl, generateProtobufDefinitions(service).toFlipperArray()) + } - private fun getNetworkPlugin(): NetworkFlipperPlugin? { - return AndroidFlipperClient.getInstanceIfInitialized()?.let { client -> - client.getPlugin(NetworkFlipperPlugin.ID) as? NetworkFlipperPlugin - } + private fun getNetworkPlugin(): NetworkFlipperPlugin? { + return AndroidFlipperClient.getInstanceIfInitialized()?.let { client -> + client.getPlugin(NetworkFlipperPlugin.ID) as? NetworkFlipperPlugin } + } - private fun generateProtobufDefinitions(service: Class<*>): List { - return RetrofitServiceToGenericCallDefinitions(service).let { definitions -> - GenericCallDefinitionsToMessageDefinitionsIfProtobuf(definitions) - }.let { messages -> - messages.map { - CallNestedMessagesPayload( - path = it.path, - method = it.method, - requestMessageFullName = it.requestMessageFullName, - requestDefinitions = it.requestModel, - responseMessageFullName = it.responseMessageFullName, - responseDefinitions = it.responseModel - ) - } + private fun generateProtobufDefinitions(service: Class<*>): List { + return RetrofitServiceToGenericCallDefinitions(service) + .let { definitions -> GenericCallDefinitionsToMessageDefinitionsIfProtobuf(definitions) } + .let { messages -> + messages.map { + CallNestedMessagesPayload( + path = it.path, + method = it.method, + requestMessageFullName = it.requestMessageFullName, + requestDefinitions = it.requestModel, + responseMessageFullName = it.responseMessageFullName, + responseDefinitions = it.responseModel) + } } - } + } } private fun Iterable.toFlipperArray(): FlipperArray = diff --git a/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/adapter/GenericCallDefinitionsToMessageDefinitionsIfProtobuf.kt b/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/adapter/GenericCallDefinitionsToMessageDefinitionsIfProtobuf.kt index 57e29ac26..6d2a05f56 100644 --- a/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/adapter/GenericCallDefinitionsToMessageDefinitionsIfProtobuf.kt +++ b/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/adapter/GenericCallDefinitionsToMessageDefinitionsIfProtobuf.kt @@ -12,23 +12,22 @@ import com.facebook.flipper.plugins.retrofit2protobuf.model.GenericCallDefinitio import me.haroldmartin.protobufjavatoprotobufjs.ProtobufGeneratedJavaToProtobufJs internal object GenericCallDefinitionsToMessageDefinitionsIfProtobuf { - operator fun invoke(callDefinitions: List): List { - return callDefinitions.mapNotNull { definition -> - val responseRootAndMessages = definition.responseType?.let { - ProtobufGeneratedJavaToProtobufJs(it) - } - val requestRootAndMessages = definition.requestType?.let { - ProtobufGeneratedJavaToProtobufJs(it) - } + operator fun invoke( + callDefinitions: List + ): List { + return callDefinitions.mapNotNull { definition -> + val responseRootAndMessages = + definition.responseType?.let { ProtobufGeneratedJavaToProtobufJs(it) } + val requestRootAndMessages = + definition.requestType?.let { ProtobufGeneratedJavaToProtobufJs(it) } - FullNamedMessagesCallDefinition( - path = definition.path, - method = definition.method, - responseMessageFullName = responseRootAndMessages?.rootFullName, - responseModel = responseRootAndMessages?.descriptors, - requestMessageFullName = requestRootAndMessages?.rootFullName, - requestModel = requestRootAndMessages?.descriptors - ) - } + FullNamedMessagesCallDefinition( + path = definition.path, + method = definition.method, + responseMessageFullName = responseRootAndMessages?.rootFullName, + responseModel = responseRootAndMessages?.descriptors, + requestMessageFullName = requestRootAndMessages?.rootFullName, + requestModel = requestRootAndMessages?.descriptors) } + } } diff --git a/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/adapter/RetrofitServiceToGenericCallDefinitions.kt b/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/adapter/RetrofitServiceToGenericCallDefinitions.kt index 6075704cc..573961e99 100644 --- a/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/adapter/RetrofitServiceToGenericCallDefinitions.kt +++ b/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/adapter/RetrofitServiceToGenericCallDefinitions.kt @@ -13,65 +13,64 @@ import java.lang.reflect.ParameterizedType import java.lang.reflect.Type internal object RetrofitServiceToGenericCallDefinitions { - @Suppress("LoopWithTooManyJumpStatements") - operator fun invoke(service: Class<*>): List { - val methodToProtobufDefinition = mutableListOf() - for (method in service.declaredMethods) { - val responseType = method.innerGenericReturnClass ?: continue - val (path, httpMethod) = method.annotations.urlPathAndMethod ?: continue - methodToProtobufDefinition.add( - GenericCallDefinition( - path = path, - method = httpMethod, - responseType = responseType, - requestType = method.requestBodyType - ) - ) - } - return methodToProtobufDefinition + @Suppress("LoopWithTooManyJumpStatements") + operator fun invoke(service: Class<*>): List { + val methodToProtobufDefinition = mutableListOf() + for (method in service.declaredMethods) { + val responseType = method.innerGenericReturnClass ?: continue + val (path, httpMethod) = method.annotations.urlPathAndMethod ?: continue + methodToProtobufDefinition.add( + GenericCallDefinition( + path = path, + method = httpMethod, + responseType = responseType, + requestType = method.requestBodyType)) } + return methodToProtobufDefinition + } } private val Array.urlPathAndMethod: Pair? - get() { - var path: Pair? = null - for (a in this) { - path = when (a.annotationClass) { - retrofit2.http.DELETE::class -> (a as retrofit2.http.DELETE).value to "DELETE" - retrofit2.http.GET::class -> (a as retrofit2.http.GET).value to "GET" - retrofit2.http.HEAD::class -> (a as retrofit2.http.HEAD).value to "HEAD" - retrofit2.http.OPTIONS::class -> (a as retrofit2.http.OPTIONS).value to "OPTIONS" - retrofit2.http.PATCH::class -> (a as retrofit2.http.PATCH).value to "PATCH" - retrofit2.http.POST::class -> (a as retrofit2.http.POST).value to "POST" - retrofit2.http.PUT::class -> (a as retrofit2.http.PUT).value to "PUT" - else -> null - } - if (path != null) break - } - return path + get() { + var path: Pair? = null + for (a in this) { + path = + when (a.annotationClass) { + retrofit2.http.DELETE::class -> (a as retrofit2.http.DELETE).value to "DELETE" + retrofit2.http.GET::class -> (a as retrofit2.http.GET).value to "GET" + retrofit2.http.HEAD::class -> (a as retrofit2.http.HEAD).value to "HEAD" + retrofit2.http.OPTIONS::class -> (a as retrofit2.http.OPTIONS).value to "OPTIONS" + retrofit2.http.PATCH::class -> (a as retrofit2.http.PATCH).value to "PATCH" + retrofit2.http.POST::class -> (a as retrofit2.http.POST).value to "POST" + retrofit2.http.PUT::class -> (a as retrofit2.http.PUT).value to "PUT" + else -> null + } + if (path != null) break } + return path + } private val Method.requestBodyType: Class<*>? - get() { - parameterAnnotations.forEachIndexed { index, annotations -> - annotations.forEach { annotation -> - if (annotation.annotationClass == retrofit2.http.Body::class) { - return parameterTypes[index] - } - } + get() { + parameterAnnotations.forEachIndexed { index, annotations -> + annotations.forEach { annotation -> + if (annotation.annotationClass == retrofit2.http.Body::class) { + return parameterTypes[index] } - return null + } } + return null + } private val Method.innerGenericReturnClass: Class<*>? - get() = (genericReturnType as? ParameterizedType)?.innerGenericType as? Class<*> + get() = (genericReturnType as? ParameterizedType)?.innerGenericType as? Class<*> private val ParameterizedType?.innerGenericType: Type? - get() { - val innerType = this?.actualTypeArguments?.get(0) - return if (innerType is ParameterizedType) { - innerType.innerGenericType - } else { - innerType - } + get() { + val innerType = this?.actualTypeArguments?.get(0) + return if (innerType is ParameterizedType) { + innerType.innerGenericType + } else { + innerType } + } diff --git a/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/model/CallNestedMessagesPayload.kt b/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/model/CallNestedMessagesPayload.kt index a97879c8a..dd2109bf5 100644 --- a/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/model/CallNestedMessagesPayload.kt +++ b/android/plugins/retrofit2-protobuf/src/main/java/com/facebook/flipper/plugins/retrofit2protobuf/model/CallNestedMessagesPayload.kt @@ -19,29 +19,30 @@ internal data class CallNestedMessagesPayload( val responseMessageFullName: String?, val responseDefinitions: Map? ) : FlipperValue { - override fun toFlipperObject(): FlipperObject { - return FlipperObject.Builder() - .put("path", path) - .put("method", method) - .put("requestMessageFullName", requestMessageFullName) - .put("requestDefinitions", requestDefinitions?.toFlipperObject()) - .put("responseMessageFullName", responseMessageFullName) - .put("responseDefinitions", responseDefinitions?.toFlipperObject()) - .build() - } + override fun toFlipperObject(): FlipperObject { + return FlipperObject.Builder() + .put("path", path) + .put("method", method) + .put("requestMessageFullName", requestMessageFullName) + .put("requestDefinitions", requestDefinitions?.toFlipperObject()) + .put("responseMessageFullName", responseMessageFullName) + .put("responseDefinitions", responseDefinitions?.toFlipperObject()) + .build() + } } private fun Map<*, *>.toFlipperObject(): FlipperObject { - val builder = FlipperObject.Builder() - this.forEach { (key, value) -> - val castValue = when (value) { - is Map<*, *> -> value.toFlipperObject() - is Iterable<*> -> value.toFlipperArray() - else -> value + val builder = FlipperObject.Builder() + this.forEach { (key, value) -> + val castValue = + when (value) { + is Map<*, *> -> value.toFlipperObject() + is Iterable<*> -> value.toFlipperArray() + else -> value } - builder.put(key as String, castValue) - } - return builder.build() + builder.put(key as String, castValue) + } + return builder.build() } private fun Iterable<*>.toFlipperArray(): FlipperArray = diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MainActivity.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MainActivity.kt index b443faeb4..5f29ae93b 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MainActivity.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MainActivity.kt @@ -7,21 +7,19 @@ package com.facebook.flipper.sample.tutorial -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity import com.facebook.flipper.sample.tutorial.ui.RootComponent import com.facebook.litho.LithoView import com.facebook.litho.sections.SectionContext class MainActivity : AppCompatActivity() { - private val sectionContext: SectionContext by lazy { SectionContext(this) } + private val sectionContext: SectionContext by lazy { SectionContext(this) } - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) - setContentView( - LithoView.create(this, RootComponent.create(sectionContext).build()) - ) - } + setContentView(LithoView.create(this, RootComponent.create(sectionContext).build())) + } } diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MarineMammal.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MarineMammal.kt index acb97f986..f16c14e51 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MarineMammal.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MarineMammal.kt @@ -9,4 +9,4 @@ package com.facebook.flipper.sample.tutorial import android.net.Uri -data class MarineMammal(val title: String, val picture_url: Uri) \ No newline at end of file +data class MarineMammal(val title: String, val picture_url: Uri) diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MarineMammals.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MarineMammals.kt index 4b8498d9e..4418eebcd 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MarineMammals.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/MarineMammals.kt @@ -10,18 +10,18 @@ package com.facebook.flipper.sample.tutorial import androidx.core.net.toUri object MarineMammals { - val list = listOf( - MarineMammal( - "Polar Bear", - "https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Ursus_maritimus_4_1996-08-04.jpg/190px-Ursus_maritimus_4_1996-08-04.jpg".toUri()), - MarineMammal( - "Sea Otter", - "https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Sea_otter_cropped.jpg/220px-Sea_otter_cropped.jpg".toUri()), - MarineMammal( - "West Indian Manatee", - "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/FL_fig04.jpg/230px-FL_fig04.jpg".toUri()), - MarineMammal( - "Bottlenose Dolphin", - "https://upload.wikimedia.org/wikipedia/commons/thumb/1/10/Tursiops_truncatus_01.jpg/220px-Tursiops_truncatus_01.jpg".toUri()) - ) -} \ No newline at end of file + val list = + listOf( + MarineMammal( + "Polar Bear", + "https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Ursus_maritimus_4_1996-08-04.jpg/190px-Ursus_maritimus_4_1996-08-04.jpg".toUri()), + MarineMammal( + "Sea Otter", + "https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Sea_otter_cropped.jpg/220px-Sea_otter_cropped.jpg".toUri()), + MarineMammal( + "West Indian Manatee", + "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/FL_fig04.jpg/230px-FL_fig04.jpg".toUri()), + MarineMammal( + "Bottlenose Dolphin", + "https://upload.wikimedia.org/wikipedia/commons/thumb/1/10/Tursiops_truncatus_01.jpg/220px-Tursiops_truncatus_01.jpg".toUri())) +} diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/TutorialApplication.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/TutorialApplication.kt index 471c816fc..b3a6e0f37 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/TutorialApplication.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/TutorialApplication.kt @@ -19,23 +19,23 @@ import com.facebook.litho.editor.flipper.LithoFlipperDescriptors import com.facebook.soloader.SoLoader class TutorialApplication : Application() { - override fun onCreate() { - super.onCreate() + override fun onCreate() { + super.onCreate() - SoLoader.init(this, false) - Fresco.initialize(this) + SoLoader.init(this, false) + Fresco.initialize(this) - // Normally, you would want to make these dependent on BuildConfig.DEBUG. - ComponentsConfiguration.isDebugModeEnabled = true - ComponentsConfiguration.enableRenderInfoDebugging = true + // Normally, you would want to make these dependent on BuildConfig.DEBUG. + ComponentsConfiguration.isDebugModeEnabled = true + ComponentsConfiguration.enableRenderInfoDebugging = true - val flipperClient = AndroidFlipperClient.getInstance(this) - val descriptorMapping = DescriptorMapping.withDefaults() - LithoFlipperDescriptors.addWithSections(descriptorMapping) + val flipperClient = AndroidFlipperClient.getInstance(this) + val descriptorMapping = DescriptorMapping.withDefaults() + LithoFlipperDescriptors.addWithSections(descriptorMapping) - flipperClient.addPlugin(InspectorFlipperPlugin(this, descriptorMapping)) - flipperClient.addPlugin(FrescoFlipperPlugin()) - flipperClient.addPlugin(SeaMammalFlipperPlugin()) - flipperClient.start() - } + flipperClient.addPlugin(InspectorFlipperPlugin(this, descriptorMapping)) + flipperClient.addPlugin(FrescoFlipperPlugin()) + flipperClient.addPlugin(SeaMammalFlipperPlugin()) + flipperClient.start() + } } diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/plugin/SeaMammalFlipperPlugin.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/plugin/SeaMammalFlipperPlugin.kt index 35f53bc30..9d522ddc8 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/plugin/SeaMammalFlipperPlugin.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/plugin/SeaMammalFlipperPlugin.kt @@ -13,28 +13,31 @@ import com.facebook.flipper.core.FlipperPlugin import com.facebook.flipper.sample.tutorial.MarineMammals class SeaMammalFlipperPlugin : FlipperPlugin { - private var connection: FlipperConnection? = null + private var connection: FlipperConnection? = null - override fun getId(): String = "sea-mammals" + override fun getId(): String = "sea-mammals" - override fun onConnect(connection: FlipperConnection?) { - this.connection = connection + override fun onConnect(connection: FlipperConnection?) { + this.connection = connection - MarineMammals.list.mapIndexed { index, (title, picture_url) -> - FlipperObject.Builder() - .put("id", index) - .put("title", title) - .put("url", picture_url).build() - }.forEach(this::newRow) - } + MarineMammals.list + .mapIndexed { index, (title, picture_url) -> + FlipperObject.Builder() + .put("id", index) + .put("title", title) + .put("url", picture_url) + .build() + } + .forEach(this::newRow) + } - override fun onDisconnect() { - connection = null - } + override fun onDisconnect() { + connection = null + } - override fun runInBackground(): Boolean = true + override fun runInBackground(): Boolean = true - private fun newRow(row: FlipperObject) { - connection?.send("newRow", row) - } + private fun newRow(row: FlipperObject) { + connection?.send("newRow", row) + } } diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/FeedItemCardSpec.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/FeedItemCardSpec.kt index cb431531d..23588fc94 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/FeedItemCardSpec.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/FeedItemCardSpec.kt @@ -15,7 +15,6 @@ import com.facebook.litho.annotations.LayoutSpec import com.facebook.litho.annotations.OnCreateLayout import com.facebook.litho.annotations.Prop import com.facebook.litho.widget.Card - import com.facebook.yoga.YogaEdge.HORIZONTAL import com.facebook.yoga.YogaEdge.VERTICAL @@ -23,17 +22,10 @@ import com.facebook.yoga.YogaEdge.VERTICAL object FeedItemCardSpec { @OnCreateLayout - fun onCreateLayout( - c: ComponentContext, - @Prop mammal: MarineMammal - ): Component = + fun onCreateLayout(c: ComponentContext, @Prop mammal: MarineMammal): Component = Column.create(c) .paddingDip(VERTICAL, 8f) .paddingDip(HORIZONTAL, 16f) - .child( - Card.create(c) - .content( - MarineMammelComponent.create(c) - .mammal(mammal))) + .child(Card.create(c).content(MarineMammelComponent.create(c).mammal(mammal))) .build() -} \ No newline at end of file +} diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/FeedSectionSpec.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/FeedSectionSpec.kt index 0483d25ff..a92ca8030 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/FeedSectionSpec.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/FeedSectionSpec.kt @@ -22,20 +22,16 @@ import com.facebook.litho.widget.RenderInfo @GroupSectionSpec object FeedSectionSpec { - @OnCreateChildren - fun onCreateChildren(c: SectionContext, @Prop data: List): Children = - Children.create() - .child(DataDiffSection.create(c) - .data(data) - .renderEventHandler(FeedSection.render(c))) - .build() + @OnCreateChildren + fun onCreateChildren(c: SectionContext, @Prop data: List): Children = + Children.create() + .child( + DataDiffSection.create(c) + .data(data) + .renderEventHandler(FeedSection.render(c))) + .build() - @OnEvent(RenderEvent::class) - fun render( - c: SectionContext, - @FromEvent model: MarineMammal - ): RenderInfo = - ComponentRenderInfo.create() - .component(FeedItemCard.create(c).mammal(model).build()) - .build() -} \ No newline at end of file + @OnEvent(RenderEvent::class) + fun render(c: SectionContext, @FromEvent model: MarineMammal): RenderInfo = + ComponentRenderInfo.create().component(FeedItemCard.create(c).mammal(model).build()).build() +} diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/MarineMammelComponentSpec.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/MarineMammelComponentSpec.kt index 4a34153cf..0c4411808 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/MarineMammelComponentSpec.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/MarineMammelComponentSpec.kt @@ -17,32 +17,25 @@ import com.facebook.litho.annotations.OnCreateLayout import com.facebook.litho.annotations.Prop import com.facebook.litho.widget.Text import com.facebook.yoga.YogaEdge.BOTTOM -import com.facebook.yoga.YogaEdge.LEFT import com.facebook.yoga.YogaEdge.HORIZONTAL +import com.facebook.yoga.YogaEdge.LEFT import com.facebook.yoga.YogaPositionType.ABSOLUTE @LayoutSpec object MarineMammelComponentSpec { - @OnCreateLayout - fun onCreateLayout( - c: ComponentContext, - @Prop mammal: MarineMammal - ): Component = - Column.create(c) - .child(SingleImageComponent.create(c) - .image(mammal.picture_url) - .build() - ) - .child( - Text.create(c) - .text(mammal.title) - .textStyle(Typeface.BOLD) - .textSizeDip(24f) - .backgroundColor(0xDDFFFFFF.toInt()) - .positionType(ABSOLUTE) - .positionDip(BOTTOM, 4f) - .positionDip(LEFT, 4f) - .paddingDip(HORIZONTAL, 6f)) - .build() - + @OnCreateLayout + fun onCreateLayout(c: ComponentContext, @Prop mammal: MarineMammal): Component = + Column.create(c) + .child(SingleImageComponent.create(c).image(mammal.picture_url).build()) + .child( + Text.create(c) + .text(mammal.title) + .textStyle(Typeface.BOLD) + .textSizeDip(24f) + .backgroundColor(0xDDFFFFFF.toInt()) + .positionType(ABSOLUTE) + .positionDip(BOTTOM, 4f) + .positionDip(LEFT, 4f) + .paddingDip(HORIZONTAL, 6f)) + .build() } diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/RootComponentSpec.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/RootComponentSpec.kt index e7f42ba45..b6b4c6fd3 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/RootComponentSpec.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/RootComponentSpec.kt @@ -18,11 +18,11 @@ import com.facebook.yoga.YogaEdge @LayoutSpec object RootComponentSpec { - @OnCreateLayout - fun onCreateLayout(c: ComponentContext): Component = - RecyclerCollectionComponent.create(c) - .disablePTR(true) - .section(FeedSection.create(SectionContext(c)).data(MarineMammals.list).build()) - .paddingDip(YogaEdge.TOP, 8f) - .build() -} \ No newline at end of file + @OnCreateLayout + fun onCreateLayout(c: ComponentContext): Component = + RecyclerCollectionComponent.create(c) + .disablePTR(true) + .section(FeedSection.create(SectionContext(c)).data(MarineMammals.list).build()) + .paddingDip(YogaEdge.TOP, 8f) + .build() +} diff --git a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/SingleImageComponentSpec.kt b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/SingleImageComponentSpec.kt index 1565935f1..5b828377c 100644 --- a/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/SingleImageComponentSpec.kt +++ b/android/tutorial/src/main/java/com/facebook/flipper/sample/tutorial/ui/SingleImageComponentSpec.kt @@ -20,20 +20,15 @@ import com.facebook.litho.fresco.FrescoImage @LayoutSpec object SingleImageComponentSpec { - @PropDefault - val imageAspectRatio = 1f + @PropDefault val imageAspectRatio = 1f @OnCreateLayout fun onCreateLayout( c: ComponentContext, @Prop image: Uri, - @Prop(optional = true) imageAspectRatio: Float): Component = - Fresco.newDraweeControllerBuilder() - .setUri(image) - .build().let { - FrescoImage.create(c) - .controller(it) - .imageAspectRatio(imageAspectRatio) - .build() + @Prop(optional = true) imageAspectRatio: Float + ): Component = + Fresco.newDraweeControllerBuilder().setUri(image).build().let { + FrescoImage.create(c).controller(it).imageAspectRatio(imageAspectRatio).build() } -} \ No newline at end of file +} diff --git a/android/tutorial/src/test/java/com/facebook/flipper/sample/tutorial/ExampleUnitTest.kt b/android/tutorial/src/test/java/com/facebook/flipper/sample/tutorial/ExampleUnitTest.kt index dba030daa..ac69a4de1 100644 --- a/android/tutorial/src/test/java/com/facebook/flipper/sample/tutorial/ExampleUnitTest.kt +++ b/android/tutorial/src/test/java/com/facebook/flipper/sample/tutorial/ExampleUnitTest.kt @@ -7,9 +7,8 @@ package com.facebook.flipper.sample.tutorial -import org.junit.Test - import org.junit.Assert.* +import org.junit.Test /** * Example local unit test, which will execute on the development machine (host). @@ -17,8 +16,8 @@ import org.junit.Assert.* * See [testing documentation](http://d.android.com/tools/testing). */ class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } + @Test + fun addition_isCorrect() { + assertEquals(4, 2 + 2) + } }