Skip to content

Commit

Permalink
Convert MountUtils to Kotlin
Browse files Browse the repository at this point in the history
Summary: as title

Reviewed By: astreet

Differential Revision: D52796982

fbshipit-source-id: bf83f8f85695f8d2b3efac58c81d80a55756c106
  • Loading branch information
Peng Jiang authored and facebook-github-bot committed Jan 22, 2024
1 parent 8cf81bf commit 2a4826d
Showing 1 changed file with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,70 @@
* 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 <T> void moveItem(int oldIndex, int newIndex, T[] items, T[] scrapItems) {
T itemToMove;

if (existsScrapItemAt(oldIndex, scrapItems)) {
@JvmStatic
fun <T> moveItem(oldIndex: Int, newIndex: Int, items: Array<T?>, scrapItems: Array<T?>?) {
val itemToMove: T?
if (existsScrapItemAt<T?>(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 <T> 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 <T> scrapItemAt(index: Int, items: Array<T?>, scrapItems: Array<T?>?) {
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 <T> boolean existsScrapItemAt(int index, T[] scrapItems) {
return scrapItems != null && scrapItems[index] != null;
}
@JvmStatic
fun <T> existsScrapItemAt(index: Int, scrapItems: Array<T?>?): 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
}
}

/**
* 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 <T> void removeItem(int index, T[] items, T[] scrapItems) {
if (existsScrapItemAt(index, scrapItems)) {
scrapItems[index] = null;
@JvmStatic
fun <T> removeItem(index: Int, items: Array<T?>, scrapItems: Array<T?>?) {
if (existsScrapItemAt<T?>(index, scrapItems)) {
scrapItems?.set(index, null)
} else {
items[index] = null;
items[index] = null
}
}

Expand All @@ -89,9 +87,10 @@ static <T> 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)
}
}

0 comments on commit 2a4826d

Please sign in to comment.