14
14
* limitations under the License.
15
15
*/
16
16
17
- package com .facebook .rendercore ;
17
+ package com.facebook.rendercore
18
18
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
23
21
22
+ internal object MountUtils {
24
23
/* *
25
24
* Moves an item from oldIndex to newIndex. The item is taken from scrapitems if an item exists in
26
25
* 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.
29
28
*/
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)) {
34
33
// Before moving the item from items we need to check whether an old item has been put in
35
34
// the scrapItems array. If there is an item at oldIndex there, it means that in
36
35
// items at position oldIndex there's now something else and the correct item to move to
37
36
// 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 )
40
39
} else {
41
- itemToMove = items [ oldIndex ];
42
- items [oldIndex ] = null ;
40
+ itemToMove = items.get( oldIndex)
41
+ items[oldIndex]= null
43
42
}
44
-
45
- items [newIndex ] = itemToMove ;
43
+ items[newIndex] = itemToMove
46
44
}
47
45
48
46
/* *
49
47
* Takes the item at position index from items and puts it into scrapItems. If no such item exists
50
48
* the invocation of this method will have no effect.
51
49
*/
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
59
54
}
55
+ items[index]?.let { scrapItems[index] = it }
60
56
}
61
57
62
58
/* * 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
66
62
67
63
/* * 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
71
68
}
72
69
}
73
70
74
71
/* *
75
72
* Remove the item at given {@param index}. The item is removed from {@param scrapItems} if the
76
73
* item exists there at given index, otherwise it is removed from {@param items}.
77
74
*/
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 )
81
79
} else {
82
- items [index ] = null ;
80
+ items[index] = null
83
81
}
84
82
}
85
83
@@ -89,9 +87,10 @@ static <T> void removeItem(int index, T[] items, T[] scrapItems) {
89
87
* @param view view into which the drawable should be mounted
90
88
* @param drawable drawable to be mounted
91
89
*/
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)
96
95
}
97
96
}
0 commit comments