Skip to content

Commit 2a4826d

Browse files
Peng Jiangfacebook-github-bot
authored andcommitted
Convert MountUtils to Kotlin
Summary: as title Reviewed By: astreet Differential Revision: D52796982 fbshipit-source-id: bf83f8f85695f8d2b3efac58c81d80a55756c106
1 parent 8cf81bf commit 2a4826d

File tree

1 file changed

+37
-38
lines changed
  • litho-rendercore/src/main/java/com/facebook/rendercore

1 file changed

+37
-38
lines changed

litho-rendercore/src/main/java/com/facebook/rendercore/MountUtils.java renamed to litho-rendercore/src/main/java/com/facebook/rendercore/MountUtils.kt

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,72 +14,70 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.facebook.rendercore;
17+
package com.facebook.rendercore
1818

19-
import android.graphics.drawable.Drawable;
20-
import android.view.View;
21-
22-
class MountUtils {
19+
import android.graphics.drawable.Drawable
20+
import android.view.View
2321

22+
internal object MountUtils {
2423
/**
2524
* Moves an item from oldIndex to newIndex. The item is taken from scrapitems if an item exists in
2625
* scrapItems at oldPosition. Otherwise the item is taken from items. This assumes that there is
27-
* no item at newIndex for the items array. If that's the case {@link MountUtils#scrapItemAt(int,
28-
* T[], T[])} has to be called before invoking this.
26+
* no item at newIndex for the items array. If that's the case [MountUtils#scrapItemAt(int, T[],
27+
* T[])] has to be called before invoking this.
2928
*/
30-
static <T> void moveItem(int oldIndex, int newIndex, T[] items, T[] scrapItems) {
31-
T itemToMove;
32-
33-
if (existsScrapItemAt(oldIndex, scrapItems)) {
29+
@JvmStatic
30+
fun <T> moveItem(oldIndex: Int, newIndex: Int, items: Array<T?>, scrapItems: Array<T?>?) {
31+
val itemToMove: T?
32+
if (existsScrapItemAt<T?>(oldIndex, scrapItems)) {
3433
// Before moving the item from items we need to check whether an old item has been put in
3534
// the scrapItems array. If there is an item at oldIndex there, it means that in
3635
// items at position oldIndex there's now something else and the correct item to move to
3736
// newIndex is instead in the scrapItems SparseArray.
38-
itemToMove = scrapItems[oldIndex];
39-
scrapItems[oldIndex] = null;
37+
itemToMove = scrapItems?.get(oldIndex)
38+
scrapItems?.set(oldIndex, null)
4039
} else {
41-
itemToMove = items[oldIndex];
42-
items[oldIndex] = null;
40+
itemToMove = items.get(oldIndex)
41+
items[oldIndex]= null
4342
}
44-
45-
items[newIndex] = itemToMove;
43+
items[newIndex] = itemToMove
4644
}
4745

4846
/**
4947
* Takes the item at position index from items and puts it into scrapItems. If no such item exists
5048
* the invocation of this method will have no effect.
5149
*/
52-
static <T> void scrapItemAt(int index, T[] items, T[] scrapItems) {
53-
if (items == null || scrapItems == null) {
54-
return;
55-
}
56-
final T value = items[index];
57-
if (value != null) {
58-
scrapItems[index] = value;
50+
@JvmStatic
51+
fun <T> scrapItemAt(index: Int, items: Array<T?>, scrapItems: Array<T?>?) {
52+
if (scrapItems == null) {
53+
return
5954
}
55+
items[index]?.let { scrapItems[index] = it }
6056
}
6157

6258
/** Returns true if scrapItems is not null and contains an item with key index. */
63-
static <T> boolean existsScrapItemAt(int index, T[] scrapItems) {
64-
return scrapItems != null && scrapItems[index] != null;
65-
}
59+
@JvmStatic
60+
fun <T> existsScrapItemAt(index: Int, scrapItems: Array<T?>?): Boolean =
61+
scrapItems != null && scrapItems[index] != null
6662

6763
/** Sets the state on a drawable if it is clickable or should duplicate its parent's state. */
68-
static void maybeSetDrawableState(View view, Drawable drawable) {
69-
if (drawable.isStateful()) {
70-
drawable.setState(view.getDrawableState());
64+
@JvmStatic
65+
fun maybeSetDrawableState(view: View, drawable: Drawable) {
66+
if (drawable.isStateful) {
67+
drawable.state = view.drawableState
7168
}
7269
}
7370

7471
/**
7572
* Remove the item at given {@param index}. The item is removed from {@param scrapItems} if the
7673
* item exists there at given index, otherwise it is removed from {@param items}.
7774
*/
78-
static <T> void removeItem(int index, T[] items, T[] scrapItems) {
79-
if (existsScrapItemAt(index, scrapItems)) {
80-
scrapItems[index] = null;
75+
@JvmStatic
76+
fun <T> removeItem(index: Int, items: Array<T?>, scrapItems: Array<T?>?) {
77+
if (existsScrapItemAt<T?>(index, scrapItems)) {
78+
scrapItems?.set(index, null)
8179
} else {
82-
items[index] = null;
80+
items[index] = null
8381
}
8482
}
8583

@@ -89,9 +87,10 @@ static <T> void removeItem(int index, T[] items, T[] scrapItems) {
8987
* @param view view into which the drawable should be mounted
9088
* @param drawable drawable to be mounted
9189
*/
92-
static void mountDrawable(View view, Drawable drawable) {
93-
drawable.setVisible(view.getVisibility() == View.VISIBLE, false);
94-
drawable.setCallback(view);
95-
maybeSetDrawableState(view, drawable);
90+
@JvmStatic
91+
fun mountDrawable(view: View, drawable: Drawable) {
92+
drawable.setVisible(view.visibility == View.VISIBLE, false)
93+
drawable.callback = view
94+
maybeSetDrawableState(view, drawable)
9695
}
9796
}

0 commit comments

Comments
 (0)