Skip to content

Commit

Permalink
refactor: use EventDispatcher (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
duguyihou authored Jun 24, 2024
1 parent 1ddf31b commit 433277b
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 27 deletions.
73 changes: 46 additions & 27 deletions android/src/main/java/com/turboimage/TurboImageListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,70 @@ package com.turboimage
import coil.request.ErrorResult
import coil.request.ImageRequest
import coil.request.SuccessResult
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.WritableNativeMap
import com.facebook.react.uimanager.events.RCTEventEmitter
import com.facebook.react.uimanager.UIManagerHelper
import com.turboimage.events.CompletionEvent
import com.turboimage.events.FailureEvent
import com.turboimage.events.StartEvent
import com.turboimage.events.SuccessEvent

class TurboImageListener(private val view: TurboImageView) : ImageRequest.Listener {

override fun onStart(request: ImageRequest) {
super.onStart(request)
val payload = WritableNativeMap().apply {
putString("state", "running")
}

val reactContext = view.context as ReactContext
reactContext.getJSModule(RCTEventEmitter::class.java)
.receiveEvent(view.id, "onStart", payload)
UIManagerHelper.getEventDispatcher(reactContext, view.id)?.let {
val payload = Arguments.createMap().apply {
putString("state", "running")
}
val surfaceId = UIManagerHelper.getSurfaceId(reactContext)
it.dispatchEvent(StartEvent(surfaceId, view.id, payload))
}
}

override fun onSuccess(request: ImageRequest, result: SuccessResult) {
super.onSuccess(request, result)
val successPayload = WritableNativeMap().apply {
putInt("width", result.drawable.intrinsicWidth)
putInt("height", result.drawable.intrinsicHeight)
putString("source", request.data.toString())
}

val reactContext = view.context as ReactContext
reactContext.getJSModule(RCTEventEmitter::class.java)
.receiveEvent(view.id, "onSuccess", successPayload)
val completionPayload = WritableNativeMap().apply {
putString("state", "completed")
UIManagerHelper.getEventDispatcher(reactContext, view.id)?.let {
val payload = Arguments.createMap().apply {
putInt("width", result.drawable.intrinsicWidth)
putInt("height", result.drawable.intrinsicHeight)
putString("source", request.data.toString())
}
val surfaceId = UIManagerHelper.getSurfaceId(reactContext)
it.dispatchEvent(SuccessEvent(surfaceId, view.id, payload))
}

UIManagerHelper.getEventDispatcher(reactContext, view.id)?.let {
val payload = Arguments.createMap().apply {
putString("state", "completed")
}
val surfaceId = UIManagerHelper.getSurfaceId(reactContext)
it.dispatchEvent(CompletionEvent(surfaceId, view.id, payload))
}
reactContext.getJSModule(RCTEventEmitter::class.java)
.receiveEvent(view.id, "onCompletion", completionPayload)
}

override fun onError(request: ImageRequest, result: ErrorResult) {
super.onError(request, result)
val failurePayload = WritableNativeMap().apply {
putString("error", result.throwable.message)
}

val reactContext = view.context as ReactContext
reactContext.getJSModule(RCTEventEmitter::class.java)
.receiveEvent(view.id, "onFailure", failurePayload)
val completionPayload = WritableNativeMap().apply {
putString("state", "completed")
UIManagerHelper.getEventDispatcher(reactContext, view.id)?.let {
val payload = Arguments.createMap().apply {
putString("error", result.throwable.message)
}
val surfaceId = UIManagerHelper.getSurfaceId(reactContext)
it.dispatchEvent(FailureEvent(surfaceId, view.id, payload))
}

UIManagerHelper.getEventDispatcher(reactContext, view.id)?.let {
val payload = Arguments.createMap().apply {
putString("state", "completed")
}
val surfaceId = UIManagerHelper.getSurfaceId(reactContext)
it.dispatchEvent(CompletionEvent(surfaceId, view.id, payload))
}
reactContext.getJSModule(RCTEventEmitter::class.java)
.receiveEvent(view.id, "onCompletion", completionPayload)
}
}
17 changes: 17 additions & 0 deletions android/src/main/java/com/turboimage/events/CompletionEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.turboimage.events

import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.events.Event

class CompletionEvent(surfaceId: Int, viewId: Int, private val payload: WritableMap):
Event<CompletionEvent>(surfaceId, viewId) {
override fun getEventName(): String {
return EVENT_NAME
}

override fun getEventData() = payload

companion object {
const val EVENT_NAME = "onCompletion"
}
}
17 changes: 17 additions & 0 deletions android/src/main/java/com/turboimage/events/FailureEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.turboimage.events

import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.events.Event

class FailureEvent(surfaceId: Int, viewId: Int, private val payload: WritableMap):
Event<FailureEvent>(surfaceId, viewId) {
override fun getEventName(): String {
return EVENT_NAME
}

override fun getEventData() = payload

companion object {
const val EVENT_NAME = "onFailure"
}
}
17 changes: 17 additions & 0 deletions android/src/main/java/com/turboimage/events/StartEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.turboimage.events

import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.events.Event

class StartEvent(surfaceId: Int, viewId: Int, private val payload: WritableMap):
Event<StartEvent>(surfaceId, viewId) {
override fun getEventName(): String {
return EVENT_NAME
}

override fun getEventData() = payload

companion object {
const val EVENT_NAME = "onStart"
}
}
17 changes: 17 additions & 0 deletions android/src/main/java/com/turboimage/events/SuccessEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.turboimage.events

import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.events.Event

class SuccessEvent(surfaceId: Int, viewId: Int, private val payload: WritableMap):
Event<SuccessEvent>(surfaceId, viewId) {
override fun getEventName(): String {
return EVENT_NAME
}

override fun getEventData() = payload

companion object {
const val EVENT_NAME = "onSuccess"
}
}

0 comments on commit 433277b

Please sign in to comment.