|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.reactnative.osslibraryexample |
| 9 | + |
| 10 | +import android.graphics.Color |
| 11 | +import android.graphics.drawable.GradientDrawable |
| 12 | +import android.view.View |
| 13 | +import com.facebook.react.bridge.Arguments |
| 14 | +import com.facebook.react.bridge.ReactContext |
| 15 | +import com.facebook.react.bridge.WritableArray |
| 16 | +import com.facebook.react.bridge.WritableMap |
| 17 | +import com.facebook.react.uimanager.ThemedReactContext |
| 18 | +import com.facebook.react.uimanager.UIManagerHelper |
| 19 | +import com.facebook.react.uimanager.events.Event |
| 20 | +import com.facebook.react.uimanager.events.RCTEventEmitter |
| 21 | + |
| 22 | +class SampleNativeView(context: ThemedReactContext) : View(context) { |
| 23 | + private var currentColor = 0 |
| 24 | + private var background: GradientDrawable = GradientDrawable() |
| 25 | + private val reactContext: ReactContext = context.reactApplicationContext |
| 26 | + |
| 27 | + override fun setBackgroundColor(color: Int) { |
| 28 | + if (color != currentColor) { |
| 29 | + background.setColor(color) |
| 30 | + currentColor = color |
| 31 | + emitNativeEvent(color) |
| 32 | + setBackground(background) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + fun setCornerRadius(cornerRadius: Float) { |
| 37 | + background.cornerRadius = cornerRadius |
| 38 | + setBackground(background) |
| 39 | + } |
| 40 | + |
| 41 | + private fun emitNativeEvent(color: Int) { |
| 42 | + val event = Arguments.createMap() |
| 43 | + val hsv = FloatArray(3) |
| 44 | + Color.colorToHSV(color, hsv) |
| 45 | + val backgroundColor = |
| 46 | + Arguments.createMap().apply { |
| 47 | + putDouble("hue", hsv[0].toDouble()) |
| 48 | + putDouble("saturation", hsv[1].toDouble()) |
| 49 | + putDouble("brightness", hsv[2].toDouble()) |
| 50 | + putDouble("alpha", Color.alpha(color).toDouble()) |
| 51 | + } |
| 52 | + |
| 53 | + event.putMap("backgroundColor", backgroundColor) |
| 54 | + |
| 55 | + reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "onColorChanged", event) |
| 56 | + } |
| 57 | + |
| 58 | + fun emitOnArrayChangedEvent(ints: List<Int>) { |
| 59 | + val newIntArray = Arguments.createArray() |
| 60 | + val newBoolArray = Arguments.createArray() |
| 61 | + val newFloatArray = Arguments.createArray() |
| 62 | + val newDoubleArray = Arguments.createArray() |
| 63 | + val newYesNoArray = Arguments.createArray() |
| 64 | + val newStringArray = Arguments.createArray() |
| 65 | + val newObjectArray = Arguments.createArray() |
| 66 | + val newArrayArray = Arguments.createArray() |
| 67 | + |
| 68 | + for (i in ints) { |
| 69 | + newIntArray.pushInt(i * 2) |
| 70 | + newBoolArray.pushBoolean(i % 2 == 1) |
| 71 | + newFloatArray.pushDouble(i * 3.14) |
| 72 | + newDoubleArray.pushDouble(i / 3.14) |
| 73 | + newYesNoArray.pushString(if (i % 2 == 1) "yep" else "nope") |
| 74 | + newStringArray.pushString(i.toString()) |
| 75 | + |
| 76 | + val latLon = Arguments.createMap() |
| 77 | + latLon.putDouble("lat", -1.0 * i) |
| 78 | + latLon.putDouble("lon", 2.0 * i) |
| 79 | + newObjectArray.pushMap(latLon) |
| 80 | + |
| 81 | + val innerArray: WritableArray = Arguments.createArray() |
| 82 | + innerArray.pushInt(i) |
| 83 | + innerArray.pushInt(i) |
| 84 | + innerArray.pushInt(i) |
| 85 | + newArrayArray.pushArray(innerArray) |
| 86 | + } |
| 87 | + |
| 88 | + val payload = |
| 89 | + Arguments.createMap().apply { |
| 90 | + putArray("values", newIntArray) |
| 91 | + putArray("boolValues", newBoolArray) |
| 92 | + putArray("floats", newFloatArray) |
| 93 | + putArray("doubles", newDoubleArray) |
| 94 | + putArray("yesNos", newYesNoArray) |
| 95 | + putArray("strings", newStringArray) |
| 96 | + putArray("latLons", newObjectArray) |
| 97 | + putArray("multiArrays", newArrayArray) |
| 98 | + } |
| 99 | + |
| 100 | + val reactContext = context as ReactContext |
| 101 | + val surfaceId = UIManagerHelper.getSurfaceId(reactContext) |
| 102 | + val eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactContext, id) |
| 103 | + val event = OnIntArrayChangedEvent(surfaceId, id, payload) |
| 104 | + |
| 105 | + eventDispatcher?.dispatchEvent(event) |
| 106 | + } |
| 107 | + |
| 108 | + inner class OnIntArrayChangedEvent( |
| 109 | + surfaceId: Int, |
| 110 | + viewId: Int, |
| 111 | + private val payload: WritableMap |
| 112 | + ) : Event<OnIntArrayChangedEvent>(surfaceId, viewId) { |
| 113 | + override fun getEventName() = "topIntArrayChanged" |
| 114 | + |
| 115 | + override fun getEventData() = payload |
| 116 | + } |
| 117 | +} |
0 commit comments