Commit c181df86 authored by Qiu Xiang's avatar Qiu Xiang

使用 setter 重构

parent 50335b35
...@@ -4,23 +4,79 @@ import android.graphics.Bitmap ...@@ -4,23 +4,79 @@ import android.graphics.Bitmap
import android.graphics.Canvas import android.graphics.Canvas
import com.amap.api.maps.AMap import com.amap.api.maps.AMap
import com.amap.api.maps.model.* import com.amap.api.maps.model.*
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.ThemedReactContext import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.events.RCTEventEmitter import com.facebook.react.uimanager.events.RCTEventEmitter
import com.facebook.react.views.view.ReactViewGroup import com.facebook.react.views.view.ReactViewGroup
class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) { class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) {
companion object {
private val COLORS = mapOf(
"AZURE" to BitmapDescriptorFactory.HUE_AZURE,
"BLUE" to BitmapDescriptorFactory.HUE_BLUE,
"CYAN" to BitmapDescriptorFactory.HUE_CYAN,
"GREEN" to BitmapDescriptorFactory.HUE_GREEN,
"MAGENTA" to BitmapDescriptorFactory.HUE_MAGENTA,
"ORANGE" to BitmapDescriptorFactory.HUE_ORANGE,
"RED" to BitmapDescriptorFactory.HUE_RED,
"ROSE" to BitmapDescriptorFactory.HUE_ROSE,
"VIOLET" to BitmapDescriptorFactory.HUE_VIOLET,
"YELLOW" to BitmapDescriptorFactory.HUE_YELLOW
)
}
var infoWindow: ReactViewGroup? = null var infoWindow: ReactViewGroup? = null
private var marker: Marker? = null var infoWindowEnabled: Boolean = true
private var position: LatLng? = null
private var title = "" var marker: Marker? = null
private var snippet = "" private set
private var flat: Boolean = false
private var opacity: Float = 1f var position: LatLng? = null
private var draggable: Boolean = false set(value) {
private var active: Boolean = false field = value
private var infoWindowEnabled: Boolean = true marker?.position = value
}
var title = ""
set(value) {
field = value
marker?.title = value
}
var snippet = ""
set(value) {
field = value
marker?.snippet = value
}
var flat: Boolean = false
set(value) {
field = value
marker?.isFlat = value
}
var opacity: Float = 1f
set(value) {
field = value
marker?.alpha = value
}
var draggable: Boolean = false
set(value) {
field = value
marker?.isDraggable = value
}
var active: Boolean = false
set(value) {
field = value
if (value) {
marker?.showInfoWindow()
} else {
marker?.hideInfoWindow()
}
}
private var bitmapDescriptor: BitmapDescriptor? = null private var bitmapDescriptor: BitmapDescriptor? = null
private val eventEmitter: RCTEventEmitter = context.getJSModule(RCTEventEmitter::class.java) private val eventEmitter: RCTEventEmitter = context.getJSModule(RCTEventEmitter::class.java)
...@@ -47,36 +103,6 @@ class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) { ...@@ -47,36 +103,6 @@ class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) {
.infoWindowEnable(infoWindowEnabled) .infoWindowEnable(infoWindowEnabled)
.snippet(snippet) .snippet(snippet)
fun setTitle(title: String) {
this.title = title
marker?.title = title
}
fun setSnippet(snippet: String) {
this.snippet = snippet
marker?.snippet = snippet
}
fun setCoordinate(coordinate: ReadableMap) {
position = LatLng(coordinate.getDouble("latitude"), coordinate.getDouble("longitude"))
marker?.position = position
}
fun setFlat(flat: Boolean) {
this.flat = flat
marker?.isFlat = flat
}
fun setOpacity(opacity: Float) {
this.opacity = opacity
marker?.alpha = opacity
}
fun setDraggable(draggable: Boolean) {
this.draggable = draggable
marker?.isDraggable = draggable
}
fun setIcon(icon: String) { fun setIcon(icon: String) {
bitmapDescriptor = COLORS[icon.toUpperCase()]?.let { bitmapDescriptor = COLORS[icon.toUpperCase()]?.let {
BitmapDescriptorFactory.defaultMarker(it) BitmapDescriptorFactory.defaultMarker(it)
...@@ -84,29 +110,16 @@ class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) { ...@@ -84,29 +110,16 @@ class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) {
marker?.setIcon(bitmapDescriptor) marker?.setIcon(bitmapDescriptor)
} }
fun sendEvent(name: String, data: WritableMap) {
eventEmitter.receiveEvent(id, name, data)
}
fun setActive(selected: Boolean) {
this.active = selected
if (selected) {
marker?.showInfoWindow()
} else {
marker?.hideInfoWindow()
}
}
fun setIconView(overlay: AMapOverlay) { fun setIconView(overlay: AMapOverlay) {
overlay.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updateIcon(overlay) } overlay.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updateIconView(overlay) }
overlay.setOnUpdateListener(object : AMapOverlay.OnUpdateListener { overlay.setOnUpdateListener(object : AMapOverlay.OnUpdateListener {
override fun onUpdate() { override fun onUpdate() {
updateIcon(overlay) updateIconView(overlay)
} }
}) })
} }
private fun updateIcon(overlay: AMapOverlay) { private fun updateIconView(overlay: AMapOverlay) {
val bitmap = Bitmap.createBitmap( val bitmap = Bitmap.createBitmap(
overlay.width, overlay.height, Bitmap.Config.ARGB_8888) overlay.width, overlay.height, Bitmap.Config.ARGB_8888)
overlay.draw(Canvas(bitmap)) overlay.draw(Canvas(bitmap))
...@@ -114,22 +127,7 @@ class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) { ...@@ -114,22 +127,7 @@ class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) {
marker?.setIcon(bitmapDescriptor) marker?.setIcon(bitmapDescriptor)
} }
fun setEnabledInfoWindow(enabled: Boolean) { fun sendEvent(name: String, data: WritableMap) {
infoWindowEnabled = enabled eventEmitter.receiveEvent(id, name, data)
}
companion object {
private val COLORS = mapOf(
"AZURE" to BitmapDescriptorFactory.HUE_AZURE,
"BLUE" to BitmapDescriptorFactory.HUE_BLUE,
"CYAN" to BitmapDescriptorFactory.HUE_CYAN,
"GREEN" to BitmapDescriptorFactory.HUE_GREEN,
"MAGENTA" to BitmapDescriptorFactory.HUE_MAGENTA,
"ORANGE" to BitmapDescriptorFactory.HUE_ORANGE,
"RED" to BitmapDescriptorFactory.HUE_RED,
"ROSE" to BitmapDescriptorFactory.HUE_ROSE,
"VIOLET" to BitmapDescriptorFactory.HUE_VIOLET,
"YELLOW" to BitmapDescriptorFactory.HUE_YELLOW
)
} }
} }
package cn.qiuxiang.react.amap3d package cn.qiuxiang.react.amap3d
import android.view.View import android.view.View
import com.amap.api.maps.model.LatLng
import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.ReadableMap
import com.facebook.react.common.MapBuilder
import com.facebook.react.uimanager.ThemedReactContext import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.annotations.ReactProp import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.views.view.ReactViewGroup
internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() { internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
override fun getName(): String { override fun getName(): String {
...@@ -25,48 +24,49 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() { ...@@ -25,48 +24,49 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
} }
override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any>? { override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any>? {
val map = HashMap<String, Any>() return mapOf(
map.put("onMarkerClick", MapBuilder.of("registrationName", "onMarkerClick")) "onMarkerClick" to mapOf("registrationName" to "onMarkerClick"),
map.put("onMarkerDragStart", MapBuilder.of("registrationName", "onMarkerDragStart")) "onMarkerDragStart" to mapOf("registrationName" to "onMarkerDragStart"),
map.put("onMarkerDrag", MapBuilder.of("registrationName", "onMarkerDrag")) "onMarkerDrag" to mapOf("registrationName" to "onMarkerDrag"),
map.put("onMarkerDragEnd", MapBuilder.of("registrationName", "onMarkerDragEnd")) "onMarkerDragEnd" to mapOf("registrationName" to "onMarkerDragEnd"),
map.put("onInfoWindowClick", MapBuilder.of("registrationName", "onInfoWindowClick")) "onInfoWindowClick" to mapOf("registrationName" to "onInfoWindowClick"))
return map
} }
@ReactProp(name = "title") @ReactProp(name = "title")
fun setTitle(marker: AMapMarker, title: String) { fun setTitle(marker: AMapMarker, title: String) {
marker.setTitle(title) marker.title = title
} }
@ReactProp(name = "description") @ReactProp(name = "description")
fun setSnippet(marker: AMapMarker, description: String) { fun setSnippet(marker: AMapMarker, description: String) {
marker.setSnippet(description) marker.snippet = description
} }
@ReactProp(name = "coordinate") @ReactProp(name = "coordinate")
fun setCoordinate(view: AMapMarker, coordinate: ReadableMap) { fun setCoordinate(view: AMapMarker, coordinate: ReadableMap) {
view.setCoordinate(coordinate) view.position = LatLng(
coordinate.getDouble("latitude"),
coordinate.getDouble("longitude"))
} }
@ReactProp(name = "flat") @ReactProp(name = "flat")
fun setFlat(marker: AMapMarker, flat: Boolean) { fun setFlat(marker: AMapMarker, flat: Boolean) {
marker.setFlat(flat) marker.flat = flat
} }
@ReactProp(name = "opacity") @ReactProp(name = "opacity")
override fun setOpacity(marker: AMapMarker, opacity: Float) { override fun setOpacity(marker: AMapMarker, opacity: Float) {
marker.setOpacity(opacity) marker.opacity = opacity
} }
@ReactProp(name = "draggable") @ReactProp(name = "draggable")
fun setDraggable(marker: AMapMarker, draggable: Boolean) { fun setDraggable(marker: AMapMarker, draggable: Boolean) {
marker.setDraggable(draggable) marker.draggable = draggable
} }
@ReactProp(name = "selected") @ReactProp(name = "selected")
fun setSelected(marker: AMapMarker, selected: Boolean) { fun setSelected(marker: AMapMarker, active: Boolean) {
marker.setActive(selected) marker.active = active
} }
@ReactProp(name = "icon") @ReactProp(name = "icon")
...@@ -76,6 +76,6 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() { ...@@ -76,6 +76,6 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
@ReactProp(name = "showsInfoWindow") @ReactProp(name = "showsInfoWindow")
fun setEnabledInfoWindow(marker: AMapMarker, enabled: Boolean) { fun setEnabledInfoWindow(marker: AMapMarker, enabled: Boolean) {
marker.setEnabledInfoWindow(enabled) marker.infoWindowEnabled = enabled
} }
} }
...@@ -12,61 +12,57 @@ import com.facebook.react.uimanager.events.RCTEventEmitter ...@@ -12,61 +12,57 @@ import com.facebook.react.uimanager.events.RCTEventEmitter
import com.facebook.react.views.view.ReactViewGroup import com.facebook.react.views.view.ReactViewGroup
class AMapPolyline(context: ThemedReactContext) : ReactViewGroup(context) { class AMapPolyline(context: ThemedReactContext) : ReactViewGroup(context) {
private var polyline: Polyline? = null var polyline: Polyline? = null
private var coordinates: ArrayList<LatLng> = ArrayList() private set
private var width: Float = 1f
private var color: Int = Color.BLACK
private var colors: ArrayList<Int> = ArrayList()
private var opacity: Float = 1f
private var zIndex: Float = 0f
private var geodesic: Boolean = false
private var dottedLine: Boolean = false
private var gradient: Boolean = false
private val eventEmitter: RCTEventEmitter = context.getJSModule(RCTEventEmitter::class.java)
val polylineId: String? var width: Float = 1f
get() = polyline?.id set(value) {
field = value
polyline?.width = value
}
fun setCoordinates(coordinates: ReadableArray) { var color: Int = Color.BLACK
this.coordinates = ArrayList((0..coordinates.size() - 1) set(value) {
.map { coordinates.getMap(it) } field = value
.map { LatLng(it.getDouble("latitude"), it.getDouble("longitude")) }) polyline?.color = value
}
polyline?.points = this.coordinates var opacity: Float = 1f
} set(value) {
field = value
polyline?.setTransparency(value)
}
fun setColor(color: Int) { var zIndex: Float = 0f
this.color = color set(value) {
polyline?.color = color field = value
} polyline?.zIndex = value
}
fun setWidth(width: Float) { var geodesic: Boolean = false
this.width = width set(value) {
polyline?.width = width field = value
} polyline?.isGeodesic = value
}
fun setZIndex(zIndex: Float) { var dottedLine: Boolean = false
this.zIndex = zIndex set(value) {
polyline?.zIndex = zIndex field = value
} polyline?.isDottedLine = value
}
fun setGeodesic(geodesic: Boolean) { var gradient: Boolean = false
this.geodesic = geodesic
polyline?.isGeodesic = geodesic
}
fun setDottedLine(dottedLine: Boolean) { private var coordinates: ArrayList<LatLng> = ArrayList()
this.dottedLine = dottedLine private var colors: ArrayList<Int> = ArrayList()
polyline?.isDottedLine = dottedLine private val eventEmitter: RCTEventEmitter = context.getJSModule(RCTEventEmitter::class.java)
}
fun setGradient(gradient: Boolean) { fun setCoordinates(coordinates: ReadableArray) {
this.gradient = gradient this.coordinates = ArrayList((0..coordinates.size() - 1)
} .map { coordinates.getMap(it) }
.map { LatLng(it.getDouble("latitude"), it.getDouble("longitude")) })
fun setOpacity(opacity: Float) { polyline?.points = this.coordinates
this.opacity = opacity
polyline?.setTransparency(opacity)
} }
fun setColors(colors: ReadableArray) { fun setColors(colors: ReadableArray) {
......
...@@ -31,36 +31,36 @@ internal class AMapPolylineManager : ViewGroupManager<AMapPolyline>() { ...@@ -31,36 +31,36 @@ internal class AMapPolylineManager : ViewGroupManager<AMapPolyline>() {
@ReactProp(name = "color", customType = "Color") @ReactProp(name = "color", customType = "Color")
fun setColor(polyline: AMapPolyline, color: Int) { fun setColor(polyline: AMapPolyline, color: Int) {
polyline.setColor(color) polyline.color = color
} }
@ReactProp(name = "width") @ReactProp(name = "width")
fun setWidth(polyline: AMapPolyline, width: Int) { fun setWidth(polyline: AMapPolyline, width: Float) {
polyline.setWidth(width.toFloat()) polyline.width = width
} }
@ReactProp(name = "zIndex") @ReactProp(name = "zIndex")
fun setZIndex(polyline: AMapPolyline, zIndex: Int) { fun setZIndex_(polyline: AMapPolyline, zIndex: Float) {
polyline.setZIndex(zIndex.toFloat()) polyline.zIndex = zIndex
} }
@ReactProp(name = "opacity") @ReactProp(name = "opacity")
override fun setOpacity(polyline: AMapPolyline, opacity: Float) { override fun setOpacity(polyline: AMapPolyline, opacity: Float) {
polyline.setOpacity(opacity) polyline.opacity = opacity
} }
@ReactProp(name = "geodesic") @ReactProp(name = "geodesic")
fun setGeodesic(polyline: AMapPolyline, geodesic: Boolean) { fun setGeodesic(polyline: AMapPolyline, geodesic: Boolean) {
polyline.setGeodesic(geodesic) polyline.geodesic = geodesic
} }
@ReactProp(name = "dottedLine") @ReactProp(name = "dottedLine")
fun setDottedLine(polyline: AMapPolyline, dottedLine: Boolean) { fun setDottedLine(polyline: AMapPolyline, dottedLine: Boolean) {
polyline.setDottedLine(dottedLine) polyline.dottedLine = dottedLine
} }
@ReactProp(name = "gradient") @ReactProp(name = "gradient")
fun setGradient(polyline: AMapPolyline, gradient: Boolean) { fun setGradient(polyline: AMapPolyline, gradient: Boolean) {
polyline.setGradient(gradient) polyline.gradient = gradient
} }
} }
...@@ -89,7 +89,7 @@ class AMapView(context: ThemedReactContext) : MapView(context) { ...@@ -89,7 +89,7 @@ class AMapView(context: ThemedReactContext) : MapView(context) {
fun addPolyline(polyline: AMapPolyline) { fun addPolyline(polyline: AMapPolyline) {
polyline.addToMap(map) polyline.addToMap(map)
polylines.put(polyline.polylineId!!, polyline) polylines.put(polyline.polyline?.id!!, polyline)
} }
fun sendEvent(name: String, data: WritableMap) { fun sendEvent(name: String, data: WritableMap) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment