-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Pull Request resolved: #43778 ## Changelog: [Internal] - As in the title. Differential Revision: D55640524
- Loading branch information
1 parent
0c02b26
commit 51b8127
Showing
2 changed files
with
143 additions
and
176 deletions.
There are no files selected for viewing
176 changes: 0 additions & 176 deletions
176
...eact-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ImageLoadEvent.java
This file was deleted.
Oops, something went wrong.
143 changes: 143 additions & 0 deletions
143
.../react-native/ReactAndroid/src/main/java/com/facebook/react/views/image/ImageLoadEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.views.image | ||
|
||
import androidx.annotation.IntDef | ||
import com.facebook.react.bridge.Arguments | ||
import com.facebook.react.bridge.WritableMap | ||
import com.facebook.react.uimanager.common.ViewUtil | ||
import com.facebook.react.uimanager.events.Event | ||
|
||
public class ImageLoadEvent | ||
private constructor( | ||
surfaceId: Int, | ||
viewId: Int, | ||
@param:ImageEventType private val eventType: Int, | ||
private val errorMessage: String? = null, | ||
private val sourceUri: String? = null, | ||
private val width: Int = 0, | ||
private val height: Int = 0, | ||
private val loaded: Int = 0, | ||
private val total: Int = 0 | ||
) : Event<ImageLoadEvent>(surfaceId, viewId) { | ||
@IntDef(ON_ERROR, ON_LOAD, ON_LOAD_END, ON_LOAD_START, ON_PROGRESS) | ||
@Retention(AnnotationRetention.SOURCE) | ||
internal annotation class ImageEventType | ||
|
||
override fun getEventName(): String = eventNameForType(eventType) | ||
|
||
// Intentionally casting mEventType because it is guaranteed to be small | ||
// enough to fit into short. | ||
override fun getCoalescingKey(): Short = eventType.toShort() | ||
|
||
override fun getEventData(): WritableMap? { | ||
val eventData = Arguments.createMap() | ||
when (eventType) { | ||
ON_PROGRESS -> { | ||
eventData.putInt("loaded", loaded) | ||
eventData.putInt("total", total) | ||
} | ||
ON_LOAD -> eventData.putMap("source", createEventDataSource()) | ||
ON_ERROR -> eventData.putString("error", errorMessage) | ||
} | ||
return eventData | ||
} | ||
|
||
private fun createEventDataSource(): WritableMap { | ||
val source = Arguments.createMap() | ||
source.putString("uri", sourceUri) | ||
source.putDouble("width", width.toDouble()) | ||
source.putDouble("height", height.toDouble()) | ||
return source | ||
} | ||
|
||
public companion object { | ||
public const val ON_ERROR: Int = 1 | ||
public const val ON_LOAD: Int = 2 | ||
public const val ON_LOAD_END: Int = 3 | ||
public const val ON_LOAD_START: Int = 4 | ||
public const val ON_PROGRESS: Int = 5 | ||
|
||
@Deprecated("") | ||
public fun createLoadStartEvent(viewId: Int): ImageLoadEvent = | ||
createLoadStartEvent(ViewUtil.NO_SURFACE_ID, viewId) | ||
|
||
@Deprecated("") | ||
public fun createProgressEvent( | ||
viewId: Int, | ||
imageUri: String?, | ||
loaded: Int, | ||
total: Int | ||
): ImageLoadEvent = createProgressEvent(ViewUtil.NO_SURFACE_ID, viewId, imageUri, loaded, total) | ||
|
||
@Deprecated("") | ||
public fun createLoadEvent( | ||
viewId: Int, | ||
imageUri: String?, | ||
width: Int, | ||
height: Int | ||
): ImageLoadEvent = createLoadEvent(ViewUtil.NO_SURFACE_ID, viewId, imageUri, width, height) | ||
|
||
@Deprecated("") | ||
public fun createErrorEvent(viewId: Int, throwable: Throwable): ImageLoadEvent = | ||
createErrorEvent(ViewUtil.NO_SURFACE_ID, viewId, throwable) | ||
|
||
@Deprecated("") | ||
public fun createLoadEndEvent(viewId: Int): ImageLoadEvent = | ||
createLoadEndEvent(ViewUtil.NO_SURFACE_ID, viewId) | ||
|
||
@JvmStatic | ||
public fun createLoadStartEvent(surfaceId: Int, viewId: Int): ImageLoadEvent = | ||
ImageLoadEvent(surfaceId, viewId, ON_LOAD_START) | ||
|
||
/** | ||
* @param loaded Amount of the image that has been loaded. It should be number of bytes, but | ||
* Fresco does not currently provides that information. | ||
* @param total Amount that `loaded` will be when the image is fully loaded. | ||
*/ | ||
@JvmStatic | ||
public fun createProgressEvent( | ||
surfaceId: Int, | ||
viewId: Int, | ||
imageUri: String?, | ||
loaded: Int, | ||
total: Int | ||
): ImageLoadEvent = | ||
ImageLoadEvent(surfaceId, viewId, ON_PROGRESS, null, imageUri, 0, 0, loaded, total) | ||
|
||
@JvmStatic | ||
public fun createLoadEvent( | ||
surfaceId: Int, | ||
viewId: Int, | ||
imageUri: String?, | ||
width: Int, | ||
height: Int | ||
): ImageLoadEvent = | ||
ImageLoadEvent(surfaceId, viewId, ON_LOAD, null, imageUri, width, height, 0, 0) | ||
|
||
@JvmStatic | ||
public fun createErrorEvent(surfaceId: Int, viewId: Int, throwable: Throwable): ImageLoadEvent = | ||
ImageLoadEvent(surfaceId, viewId, ON_ERROR, throwable.message, null, 0, 0, 0, 0) | ||
|
||
@JvmStatic | ||
public fun createLoadEndEvent(surfaceId: Int, viewId: Int): ImageLoadEvent = | ||
ImageLoadEvent(surfaceId, viewId, ON_LOAD_END) | ||
|
||
@JvmStatic | ||
public fun eventNameForType(@ImageEventType eventType: Int): String { | ||
return when (eventType) { | ||
ON_ERROR -> "topError" | ||
ON_LOAD -> "topLoad" | ||
ON_LOAD_END -> "topLoadEnd" | ||
ON_LOAD_START -> "topLoadStart" | ||
ON_PROGRESS -> "topProgress" | ||
else -> throw IllegalStateException("Invalid image event: " + Integer.toString(eventType)) | ||
} | ||
} | ||
} | ||
} |