-
Notifications
You must be signed in to change notification settings - Fork 984
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: convert
RNSelectableTextInput
to Kotlin (#18320)
### 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
1 parent
85c928f
commit d64508a
Showing
5 changed files
with
158 additions
and
180 deletions.
There are no files selected for viewing
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
78 changes: 0 additions & 78 deletions
78
...e-status/android/src/main/java/im/status/ethereum/module/RNSelectableTextInputModule.java
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
...ive-status/android/src/main/java/im/status/ethereum/module/RNSelectableTextInputModule.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,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) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
102 changes: 0 additions & 102 deletions
102
...tus/android/src/main/java/im/status/ethereum/module/RNSelectableTextInputViewManager.java
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
...tatus/android/src/main/java/im/status/ethereum/module/RNSelectableTextInputViewManager.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,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() | ||
} | ||
} |