From 2a4826da16d71c47b76ce5ccff46386ab12de458 Mon Sep 17 00:00:00 2001 From: Peng Jiang Date: Mon, 22 Jan 2024 09:12:27 -0800 Subject: [PATCH] Convert MountUtils to Kotlin Summary: as title Reviewed By: astreet Differential Revision: D52796982 fbshipit-source-id: bf83f8f85695f8d2b3efac58c81d80a55756c106 --- .../{MountUtils.java => MountUtils.kt} | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) rename litho-rendercore/src/main/java/com/facebook/rendercore/{MountUtils.java => MountUtils.kt} (58%) diff --git a/litho-rendercore/src/main/java/com/facebook/rendercore/MountUtils.java b/litho-rendercore/src/main/java/com/facebook/rendercore/MountUtils.kt similarity index 58% rename from litho-rendercore/src/main/java/com/facebook/rendercore/MountUtils.java rename to litho-rendercore/src/main/java/com/facebook/rendercore/MountUtils.kt index 3ce835fecf7..1221429b94f 100644 --- a/litho-rendercore/src/main/java/com/facebook/rendercore/MountUtils.java +++ b/litho-rendercore/src/main/java/com/facebook/rendercore/MountUtils.kt @@ -14,60 +14,57 @@ * limitations under the License. */ -package com.facebook.rendercore; +package com.facebook.rendercore -import android.graphics.drawable.Drawable; -import android.view.View; - -class MountUtils { +import android.graphics.drawable.Drawable +import android.view.View +internal object MountUtils { /** * Moves an item from oldIndex to newIndex. The item is taken from scrapitems if an item exists in * scrapItems at oldPosition. Otherwise the item is taken from items. This assumes that there is - * no item at newIndex for the items array. If that's the case {@link MountUtils#scrapItemAt(int, - * T[], T[])} has to be called before invoking this. + * no item at newIndex for the items array. If that's the case [MountUtils#scrapItemAt(int, T[], + * T[])] has to be called before invoking this. */ - static void moveItem(int oldIndex, int newIndex, T[] items, T[] scrapItems) { - T itemToMove; - - if (existsScrapItemAt(oldIndex, scrapItems)) { + @JvmStatic + fun moveItem(oldIndex: Int, newIndex: Int, items: Array, scrapItems: Array?) { + val itemToMove: T? + if (existsScrapItemAt(oldIndex, scrapItems)) { // Before moving the item from items we need to check whether an old item has been put in // the scrapItems array. If there is an item at oldIndex there, it means that in // items at position oldIndex there's now something else and the correct item to move to // newIndex is instead in the scrapItems SparseArray. - itemToMove = scrapItems[oldIndex]; - scrapItems[oldIndex] = null; + itemToMove = scrapItems?.get(oldIndex) + scrapItems?.set(oldIndex, null) } else { - itemToMove = items[oldIndex]; - items[oldIndex] = null; + itemToMove = items.get(oldIndex) + items[oldIndex]= null } - - items[newIndex] = itemToMove; + items[newIndex] = itemToMove } /** * Takes the item at position index from items and puts it into scrapItems. If no such item exists * the invocation of this method will have no effect. */ - static void scrapItemAt(int index, T[] items, T[] scrapItems) { - if (items == null || scrapItems == null) { - return; - } - final T value = items[index]; - if (value != null) { - scrapItems[index] = value; + @JvmStatic + fun scrapItemAt(index: Int, items: Array, scrapItems: Array?) { + if (scrapItems == null) { + return } + items[index]?.let { scrapItems[index] = it } } /** Returns true if scrapItems is not null and contains an item with key index. */ - static boolean existsScrapItemAt(int index, T[] scrapItems) { - return scrapItems != null && scrapItems[index] != null; - } + @JvmStatic + fun existsScrapItemAt(index: Int, scrapItems: Array?): Boolean = + scrapItems != null && scrapItems[index] != null /** Sets the state on a drawable if it is clickable or should duplicate its parent's state. */ - static void maybeSetDrawableState(View view, Drawable drawable) { - if (drawable.isStateful()) { - drawable.setState(view.getDrawableState()); + @JvmStatic + fun maybeSetDrawableState(view: View, drawable: Drawable) { + if (drawable.isStateful) { + drawable.state = view.drawableState } } @@ -75,11 +72,12 @@ static void maybeSetDrawableState(View view, Drawable drawable) { * Remove the item at given {@param index}. The item is removed from {@param scrapItems} if the * item exists there at given index, otherwise it is removed from {@param items}. */ - static void removeItem(int index, T[] items, T[] scrapItems) { - if (existsScrapItemAt(index, scrapItems)) { - scrapItems[index] = null; + @JvmStatic + fun removeItem(index: Int, items: Array, scrapItems: Array?) { + if (existsScrapItemAt(index, scrapItems)) { + scrapItems?.set(index, null) } else { - items[index] = null; + items[index] = null } } @@ -89,9 +87,10 @@ static void removeItem(int index, T[] items, T[] scrapItems) { * @param view view into which the drawable should be mounted * @param drawable drawable to be mounted */ - static void mountDrawable(View view, Drawable drawable) { - drawable.setVisible(view.getVisibility() == View.VISIBLE, false); - drawable.setCallback(view); - maybeSetDrawableState(view, drawable); + @JvmStatic + fun mountDrawable(view: View, drawable: Drawable) { + drawable.setVisible(view.visibility == View.VISIBLE, false) + drawable.callback = view + maybeSetDrawableState(view, drawable) } }