Skip to content

Commit

Permalink
chore: convert RNSelectableTextInput to Kotlin (#18320)
Browse files Browse the repository at this point in the history
### Summary

This commit adds `Kotlin` plugin to native module `react-native-status` and converts these files to `Kotlin` :
- `modules/react-native-status/android/src/main/java/im/status/ethereum/module/RNSelectableTextInputViewManager.java`
- `modules/react-native-status/android/src/main/java/im/status/ethereum/module/RNSelectableTextInputModule.java`

### Platforms
- Android
  • Loading branch information
siddarthkay authored Jan 4, 2024
1 parent 85c928f commit d64508a
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 180 deletions.
1 change: 1 addition & 0 deletions modules/react-native-status/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: "org.jetbrains.kotlin.android"

def getStatusGoSHA1 = { ->
def statusgoOverridePath = System.getenv("STATUS_GO_SRC_OVERRIDE")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package im.status.ethereum.module

import android.view.ActionMode
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.uimanager.NativeViewHierarchyManager
import com.facebook.react.uimanager.UIBlock
import com.facebook.react.uimanager.UIManagerModule
import com.facebook.react.views.textinput.ReactEditText

class RNSelectableTextInputModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

private var lastActionMode: ActionMode? = null

override fun getName(): String {
return "RNSelectableTextInputManager"
}

@ReactMethod
fun setupMenuItems(selectableTextViewReactTag: Int?, textInputReactTag: Int?) {
val reactContext = reactApplicationContext
val uiManager = reactContext.getNativeModule(UIManagerModule::class.java)
selectableTextViewReactTag?.let { selectableTag ->
textInputReactTag?.let { inputTag ->
uiManager?.addUIBlock(UIBlock { nvhm: NativeViewHierarchyManager ->
val rnSelectableTextManager = nvhm.resolveViewManager(selectableTag) as RNSelectableTextInputViewManager
val reactTextView = nvhm.resolveView(inputTag) as ReactEditText
rnSelectableTextManager.registerSelectionListener(reactTextView)
})
}
}
}

@ReactMethod
fun startActionMode(textInputReactTag: Int?) {
val reactContext = reactApplicationContext
val uiManager = reactContext.getNativeModule(UIManagerModule::class.java)
textInputReactTag?.let { inputTag ->
uiManager?.addUIBlock(UIBlock { nvhm: NativeViewHierarchyManager ->
val reactTextView = nvhm.resolveView(inputTag) as ReactEditText
lastActionMode = reactTextView.startActionMode(reactTextView.customSelectionActionModeCallback, ActionMode.TYPE_FLOATING)
})
}
}

@ReactMethod
fun hideLastActionMode() {
val reactContext = reactApplicationContext
val uiManager = reactContext.getNativeModule(UIManagerModule::class.java)
uiManager?.addUIBlock(UIBlock { _ ->
lastActionMode?.finish()
lastActionMode = null
})
}

@ReactMethod
fun setSelection(textInputReactTag: Int?, start: Int?, end: Int?) {
val reactContext = reactApplicationContext
val uiManager = reactContext.getNativeModule(UIManagerModule::class.java)
textInputReactTag?.let { inputTag ->
start?.let { s ->
end?.let { e ->
uiManager?.addUIBlock(UIBlock { nvhm: NativeViewHierarchyManager ->
val reactTextView = nvhm.resolveView(inputTag) as ReactEditText
reactTextView.setSelection(s, e)
})
}
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package im.status.ethereum.module

import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.WritableMap
import com.facebook.react.common.MapBuilder
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.uimanager.events.RCTEventEmitter
import com.facebook.react.views.textinput.ReactEditText
import com.facebook.react.views.view.ReactViewGroup
import com.facebook.react.views.view.ReactViewManager

class RNSelectableTextInputViewManager : ReactViewManager() {
companion object {
const val REACT_CLASS = "RNSelectableTextInput"
}

private var _menuItems = arrayOf<String>()

override fun getName(): String {
return REACT_CLASS
}

override fun createViewInstance(context: ThemedReactContext): ReactViewGroup {
return ReactViewGroup(context)
}

@ReactProp(name = "menuItems")
fun setMenuItems(reactViewGroup: ReactViewGroup, items: ReadableArray?) {
_menuItems = items?.let {
Array(items.size()) { i -> items.getString(i) }
} ?: arrayOf()
}

fun registerSelectionListener(view: ReactEditText) {
view.customSelectionActionModeCallback = object : ActionMode.Callback {
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
menu.clear()
_menuItems.forEachIndexed { i, item ->
menu.add(0, i, 0, item)
}
return true
}

override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
return true
}

override fun onDestroyActionMode(mode: ActionMode) {}

override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
val selectionStart = view.selectionStart
val selectionEnd = view.selectionEnd
val selectedText = view.text.toString().substring(selectionStart, selectionEnd)

onSelectNativeEvent(view, item.itemId, selectedText, selectionStart, selectionEnd)
mode.finish()
return true
}
}
}

private fun onSelectNativeEvent(view: ReactEditText, eventType: Int, content: String, selectionStart: Int, selectionEnd: Int) {
val event: WritableMap = Arguments.createMap().apply {
putInt("eventType", eventType)
putString("content", content)
putInt("selectionStart", selectionStart)
putInt("selectionEnd", selectionEnd)
}

val reactContext = view.context as ReactContext
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(view.id, "topSelection", event)
}

override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any>? {
return MapBuilder.builder<String, Any>()
.put("topSelection", MapBuilder.of("registrationName", "onSelection"))
.build()
}
}

0 comments on commit d64508a

Please sign in to comment.