-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
android/src/main/java/com/turboimage/TurboImageListener.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,45 @@ | ||
package com.turboimage | ||
|
||
import android.widget.ImageView | ||
import coil.request.ErrorResult | ||
import coil.request.ImageRequest | ||
import coil.request.SuccessResult | ||
import com.facebook.react.bridge.WritableNativeMap | ||
import com.facebook.react.common.MapBuilder | ||
import com.facebook.react.uimanager.ThemedReactContext | ||
import com.facebook.react.uimanager.events.RCTEventEmitter | ||
|
||
open class TurboImageListener<T>(private val view: T) : ImageRequest.Listener where T : ImageView { | ||
private val currentContext = view.context as ThemedReactContext | ||
private var eventEmitter: RCTEventEmitter = (view.context as ThemedReactContext).getJSModule( | ||
RCTEventEmitter::class.java | ||
) | ||
|
||
fun getExportedCustomDirectEventTypeConstants(): Map<String, Any> { | ||
return MapBuilder.of( | ||
ON_ERROR, MapBuilder.of("registrationName", ON_ERROR), | ||
ON_SUCCESS, MapBuilder.of("registrationName", ON_SUCCESS) | ||
) | ||
} | ||
|
||
override fun onError(request: ImageRequest, result: ErrorResult) { | ||
|
||
val payload = WritableNativeMap().apply { | ||
putString("error", result.toString()) | ||
} | ||
eventEmitter.receiveEvent(view.id, ON_ERROR, payload) | ||
} | ||
|
||
override fun onSuccess(request: ImageRequest, result: SuccessResult) { | ||
val payload = WritableNativeMap().apply { | ||
putBoolean("isSampled", result.isSampled) | ||
putString("dataSource", result.dataSource.toString()) | ||
} | ||
eventEmitter.receiveEvent(view.id, ON_SUCCESS, payload) | ||
} | ||
|
||
companion object { | ||
const val ON_ERROR = "onError" | ||
const val ON_SUCCESS = "onSuccess" | ||
} | ||
} |
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