Skip to content

Commit

Permalink
feat(android): typegen for old arch
Browse files Browse the repository at this point in the history
  • Loading branch information
mym0404 committed Apr 2, 2024
1 parent 9e0fefe commit 8e8eb49
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mjstudio.reactnativenavermap

import androidx.core.math.MathUtils.clamp
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
Expand All @@ -14,6 +13,7 @@ import com.mjstudio.reactnativenavermap.mapview.RNCNaverMapViewWrapper
import com.mjstudio.reactnativenavermap.util.RectUtil
import com.mjstudio.reactnativenavermap.util.getDoubleOrNull
import com.mjstudio.reactnativenavermap.util.getLatLng
import com.naver.maps.geometry.MathUtils.clamp
import com.naver.maps.map.CameraPosition
import com.naver.maps.map.NaverMap
import com.naver.maps.map.NaverMap.MapType.Basic
Expand All @@ -24,6 +24,7 @@ import com.naver.maps.map.NaverMap.MapType.None
import com.naver.maps.map.NaverMap.MapType.Satellite
import com.naver.maps.map.NaverMap.MapType.Terrain
import com.naver.maps.map.NaverMapOptions
import debugE

@ReactModule(name = RNCNaverMapViewManager.NAME)
class RNCNaverMapViewManager : RNCNaverMapViewManagerSpec<RNCNaverMapViewWrapper>() {
Expand All @@ -45,8 +46,14 @@ class RNCNaverMapViewManager : RNCNaverMapViewManagerSpec<RNCNaverMapViewWrapper

override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any> =
(super.getExportedCustomDirectEventTypeConstants() ?: mutableMapOf()).apply {
put(NaverMapInitializeEvent.EVENT_NAME, mapOf("registrationName" to NaverMapInitializeEvent.EVENT_NAME))
put(NaverMapOptionChangeEvent.EVENT_NAME, mapOf("registrationName" to NaverMapOptionChangeEvent.EVENT_NAME))
put(
NaverMapInitializeEvent.EVENT_NAME,
mapOf("registrationName" to NaverMapInitializeEvent.EVENT_NAME)
)
put(
NaverMapOptionChangeEvent.EVENT_NAME,
mapOf("registrationName" to NaverMapOptionChangeEvent.EVENT_NAME)
)
}

private fun RNCNaverMapViewWrapper?.withMapView(callback: (mapView: RNCNaverMapView) -> Unit) {
Expand Down Expand Up @@ -76,87 +83,110 @@ class RNCNaverMapViewManager : RNCNaverMapViewManagerSpec<RNCNaverMapViewWrapper
}

@ReactProp(name = "layerGroups")
override fun setLayerGroups(view: RNCNaverMapViewWrapper?, value: ReadableArray?) = view.withMap { map ->
(value ?: Arguments.createArray().apply { pushString("BUILDING") }).also { arr ->
arrayOf(
"BUILDING", "TRAFFIC", "TRANSIT", "BICYCLE", "MOUNTAIN", "CADASTRAL",
).forEach {
var isEnabled = false
for (i in 0 until arr.size()) {
if (arr.getString(i) == it) {
isEnabled = true
override fun setLayerGroups(view: RNCNaverMapViewWrapper?, value: ReadableArray?) =
view.withMap { map ->
(value ?: Arguments.createArray().apply { pushString("BUILDING") }).also { arr ->
arrayOf(
"BUILDING", "TRAFFIC", "TRANSIT", "BICYCLE", "MOUNTAIN", "CADASTRAL",
).forEach {
var isEnabled = false
for (i in 0 until arr.size()) {
if (arr.getString(i) == it) {
isEnabled = true
}
}
map.setLayerGroupEnabled("LAYER_GROUP_$it", isEnabled)
}
map.setLayerGroupEnabled("LAYER_GROUP_$it", isEnabled)
}
}
}

@ReactProp(name = "isIndoorEnabled", defaultBoolean = true)
override fun setIsIndoorEnabled(view: RNCNaverMapViewWrapper?, value: Boolean) = view.withMap {
it.isIndoorEnabled = value
}

@ReactProp(name = "isNightModeEnabled", defaultBoolean = false)
override fun setIsNightModeEnabled(view: RNCNaverMapViewWrapper?, value: Boolean) = view.withMap {
it.isNightModeEnabled = value
}
override fun setIsNightModeEnabled(view: RNCNaverMapViewWrapper?, value: Boolean) =
view.withMap {
it.isNightModeEnabled = value
}

@ReactProp(name = "isLiteModeEnabled", defaultBoolean = false)
override fun setIsLiteModeEnabled(view: RNCNaverMapViewWrapper?, value: Boolean) = view.withMap {
it.isLiteModeEnabled = value
}
override fun setIsLiteModeEnabled(view: RNCNaverMapViewWrapper?, value: Boolean) =
view.withMap {
it.isLiteModeEnabled = value
}

@ReactProp(name = "lightness", defaultFloat = 0f)
override fun setLightness(view: RNCNaverMapViewWrapper?, value: Float) = view.withMap {
it.lightness = clamp(value, -1f, 1f)
@ReactProp(name = "lightness", defaultDouble = 0.0)
override fun setLightness(view: RNCNaverMapViewWrapper?, value: Double) = view.withMap {
it.lightness = clamp(value, -1.0, 1.0).toFloat()
}

@ReactProp(name = "buildingHeight", defaultFloat = 1f)
override fun setBuildingHeight(view: RNCNaverMapViewWrapper?, value: Float) = view.withMap {
it.buildingHeight = clamp(value, 0f, 1f)
@ReactProp(name = "buildingHeight", defaultDouble = 1.0)
override fun setBuildingHeight(view: RNCNaverMapViewWrapper?, value: Double) = view.withMap {
it.buildingHeight = clamp(value, 0.0, 1.0).toFloat()
}

@ReactProp(name = "symbolScale", defaultFloat = 1f)
override fun setSymbolScale(view: RNCNaverMapViewWrapper?, value: Float) = view.withMap {
it.symbolScale = clamp(value, 0f, 2f)
@ReactProp(name = "symbolScale", defaultDouble = 1.0)
override fun setSymbolScale(view: RNCNaverMapViewWrapper?, value: Double) = view.withMap {
it.symbolScale = clamp(value, 0.0, 2.0).toFloat()
}

@ReactProp(name = "symbolPerspectiveRatio", defaultFloat = 1f)
override fun setSymbolPerspectiveRatio(view: RNCNaverMapViewWrapper?, value: Float) = view.withMap {
it.symbolPerspectiveRatio = clamp(value, 0f, 1f)
}
@ReactProp(name = "symbolPerspectiveRatio", defaultDouble = 1.0)
override fun setSymbolPerspectiveRatio(view: RNCNaverMapViewWrapper?, value: Double) =
view.withMap {
it.symbolPerspectiveRatio = clamp(value, 0.0, 1.0).toFloat()
}

@ReactProp(name = "center")
override fun setCenter(view: RNCNaverMapViewWrapper?, value: ReadableMap?) = view.withMap {
value.getLatLng()?.also { latlng ->
val zoom = value.getDoubleOrNull("zoom") ?: 16.0
val tilt = value.getDoubleOrNull("tilt")
val bearing = value.getDoubleOrNull("bearing")
val zoom = value.getDoubleOrNull("zoom") ?: it.cameraPosition.zoom
val tilt = value.getDoubleOrNull("tilt") ?: it.cameraPosition.tilt
val bearing = value.getDoubleOrNull("bearing") ?: it.cameraPosition.bearing

it.cameraPosition = if (tilt != null && bearing != null) CameraPosition(
it.cameraPosition = CameraPosition(
latlng,
zoom,
tilt,
bearing,
) else if ((tilt == null) != (bearing == null)) CameraPosition(
latlng,
zoom,
tilt ?: 20.0,
bearing ?: 180.0
) else CameraPosition(latlng, zoom)
)
}
}

@ReactProp(name = "mapPadding")
override fun setMapPadding(view: RNCNaverMapViewWrapper?, value: ReadableMap?) = view.withMapView {
RectUtil.getRect(value, it.resources.displayMetrics.density)?.run {
it.withMap { map ->
map.setContentPadding(left, top, right, bottom)
override fun setMapPadding(view: RNCNaverMapViewWrapper?, value: ReadableMap?) =
view.withMapView {
RectUtil.getRect(value, it.resources.displayMetrics.density, defaultValue = 0.0)?.run {
it.withMap { map ->
debugE(this)
map.setContentPadding(left, top, right, bottom)
}
}
}

// endregion

// region COMMANDS
// override fun receiveCommand(
// root: RNCNaverMapViewWrapper,
// commandId: String?,
// args: ReadableArray?
// ) {
// super.receiveCommand(root, commandId, args)
// }

override fun animateToCoordinate(
view: RNCNaverMapViewWrapper?,
latitude: Double,
longitude: Double
) {
TODO("Not yet implemented")
}

override fun animateToBound(view: RNCNaverMapViewWrapper?, encodedJsonString: String?) {
TODO("Not yet implemented")
}
// endregion

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mjstudio.reactnativenavermap.event

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

class NaverMapCameraChangeEvent(
surfaceId: Int, viewId: Int, private val latitude: Double, private val longitude: Double, private val zoom: Double
) : Event<NaverMapCameraChangeEvent>(surfaceId, viewId) {
override fun getEventName(): String = EVENT_NAME
override fun canCoalesce(): Boolean = false
override fun getCoalescingKey(): Short = 0
override fun getEventData(): WritableMap = Arguments.createMap().apply {
putDouble("latitude", latitude)
putDouble("longitude", longitude)
putDouble("zoom", zoom)
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@ package com.mjstudio.reactnativenavermap.util

import android.graphics.Rect
import com.facebook.react.bridge.ReadableMap
import kotlin.math.roundToInt

object RectUtil {
fun getRect(padding: ReadableMap?, density: Float): Rect? {
fun getRect(padding: ReadableMap?, density: Float, defaultValue: Double = 0.0): Rect? {
var left = 0
var top = 0
var right = 0
var bottom = 0
if (padding != null) {
if (padding.hasKey("left")) {
left = Math.round(padding.getDouble("left").toFloat() * density)
}
if (padding.hasKey("top")) {
top = Math.round(padding.getDouble("top").toFloat() * density)
}
if (padding.hasKey("right")) {
right = Math.round(padding.getDouble("right").toFloat() * density)
}
if (padding.hasKey("bottom")) {
bottom = Math.round(padding.getDouble("bottom").toFloat() * density)
}
top = ((padding.getDoubleOrNull("top") ?: defaultValue) * density).roundToInt()
right = ((padding.getDoubleOrNull("right") ?: defaultValue) * density).roundToInt()
bottom = ((padding.getDoubleOrNull("bottom") ?: defaultValue) * density).roundToInt()
left = ((padding.getDoubleOrNull("left") ?: defaultValue) * density).roundToInt()
return Rect(left, top, right, bottom)
}
return null
Expand Down
77 changes: 77 additions & 0 deletions android/src/oldarch/RNCNaverMapViewManagerDelegate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
* <p>
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GeneratePropsJavaDelegate.js
*/


import android.view.View;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.BaseViewManagerDelegate;
import com.facebook.react.uimanager.BaseViewManagerInterface;

import androidx.annotation.Nullable;

public class RNCNaverMapViewManagerDelegate<T extends View, U extends BaseViewManagerInterface<T> & RNCNaverMapViewManagerInterface<T>> extends BaseViewManagerDelegate<T, U> {
public RNCNaverMapViewManagerDelegate(U viewManager) {
super(viewManager);
}

@Override
public void setProperty(T view, String propName, @Nullable Object value) {
switch (propName) {
case "mapType":
mViewManager.setMapType(view, (String) value);
break;
case "layerGroups":
mViewManager.setLayerGroups(view, (ReadableArray) value);
break;
case "isIndoorEnabled":
mViewManager.setIsIndoorEnabled(view, value == null ? false : (boolean) value);
break;
case "isNightModeEnabled":
mViewManager.setIsNightModeEnabled(view, value == null ? false : (boolean) value);
break;
case "isLiteModeEnabled":
mViewManager.setIsLiteModeEnabled(view, value == null ? false : (boolean) value);
break;
case "lightness":
mViewManager.setLightness(view, value == null ? 0f : ((Double) value).doubleValue());
break;
case "buildingHeight":
mViewManager.setBuildingHeight(view, value == null ? 1f : ((Double) value).doubleValue());
break;
case "symbolScale":
mViewManager.setSymbolScale(view, value == null ? 1f : ((Double) value).doubleValue());
break;
case "symbolPerspectiveRatio":
mViewManager.setSymbolPerspectiveRatio(view, value == null ? 1f : ((Double) value).doubleValue());
break;
case "center":
mViewManager.setCenter(view, (ReadableMap) value);
break;
case "mapPadding":
mViewManager.setMapPadding(view, (ReadableMap) value);
break;
default:
super.setProperty(view, propName, value);
}
}

@Override
public void receiveCommand(T view, String commandName, ReadableArray args) {
switch (commandName) {
case "animateToCoordinate":
mViewManager.animateToCoordinate(view, args.getDouble(0), args.getDouble(1));
break;
case "animateToBound":
mViewManager.animateToBound(view, args.getString(0));
break;
}
}
}
43 changes: 43 additions & 0 deletions android/src/oldarch/RNCNaverMapViewManagerInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
* <p>
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GeneratePropsJavaInterface.js
*/

import android.view.View;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;

import androidx.annotation.Nullable;

public interface RNCNaverMapViewManagerInterface<T extends View> {
void setMapType(T view, @Nullable String value);

void setLayerGroups(T view, @Nullable ReadableArray value);

void setIsIndoorEnabled(T view, boolean value);

void setIsNightModeEnabled(T view, boolean value);

void setIsLiteModeEnabled(T view, boolean value);

void setLightness(T view, double value);

void setBuildingHeight(T view, double value);

void setSymbolScale(T view, double value);

void setSymbolPerspectiveRatio(T view, double value);

void setCenter(T view, @Nullable ReadableMap value);

void setMapPadding(T view, @Nullable ReadableMap value);

void animateToCoordinate(T view, double latitude, double longitude);

void animateToBound(T view, String encodedJsonString);
}
18 changes: 15 additions & 3 deletions android/src/oldarch/RNCNaverMapViewManagerSpec.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package com.mjstudio.reactnativenavermap

import android.view.View
import RNCNaverMapViewManagerDelegate
import RNCNaverMapViewManagerInterface
import android.view.ViewGroup
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.ViewManagerDelegate

abstract class RNCNaverMapViewManagerSpec<T : ViewGroup> : ViewGroupManager<T>() {}
abstract class RNCNaverMapViewManagerSpec<T : ViewGroup> : ViewGroupManager<T>(),
RNCNaverMapViewManagerInterface<T> {
private val mDelegate: ViewManagerDelegate<T>

init {
mDelegate = RNCNaverMapViewManagerDelegate(this)
}

override fun getDelegate(): ViewManagerDelegate<T>? {
return mDelegate
}
}
Loading

0 comments on commit 8e8eb49

Please sign in to comment.