Commit c181df86 authored by Qiu Xiang's avatar Qiu Xiang
Browse files

使用 setter 重构

parent 50335b35
Loading
Loading
Loading
Loading
+71 −73
Original line number Diff line number Diff line
@@ -4,23 +4,79 @@ import android.graphics.Bitmap
import android.graphics.Canvas
import com.amap.api.maps.AMap
import com.amap.api.maps.model.*
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.events.RCTEventEmitter
import com.facebook.react.views.view.ReactViewGroup

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
    private var marker: Marker? = null
    private var position: LatLng? = null
    private var title = ""
    private var snippet = ""
    private var flat: Boolean = false
    private var opacity: Float = 1f
    private var draggable: Boolean = false
    private var active: Boolean = false
    private var infoWindowEnabled: Boolean = true
    var infoWindowEnabled: Boolean = true

    var marker: Marker? = null
        private set

    var position: LatLng? = null
        set(value) {
            field = value
            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 val eventEmitter: RCTEventEmitter = context.getJSModule(RCTEventEmitter::class.java)

@@ -47,36 +103,6 @@ class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) {
                .infoWindowEnable(infoWindowEnabled)
                .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) {
        bitmapDescriptor = COLORS[icon.toUpperCase()]?.let {
            BitmapDescriptorFactory.defaultMarker(it)
@@ -84,29 +110,16 @@ class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) {
        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) {
        overlay.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updateIcon(overlay) }
        overlay.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> updateIconView(overlay) }
        overlay.setOnUpdateListener(object : AMapOverlay.OnUpdateListener {
            override fun onUpdate() {
                updateIcon(overlay)
                updateIconView(overlay)
            }
        })
    }

    private fun updateIcon(overlay: AMapOverlay) {
    private fun updateIconView(overlay: AMapOverlay) {
        val bitmap = Bitmap.createBitmap(
                overlay.width, overlay.height, Bitmap.Config.ARGB_8888)
        overlay.draw(Canvas(bitmap))
@@ -114,22 +127,7 @@ class AMapMarker(context: ThemedReactContext) : ReactViewGroup(context) {
        marker?.setIcon(bitmapDescriptor)
    }

    fun setEnabledInfoWindow(enabled: Boolean) {
        infoWindowEnabled = enabled
    }

    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
        )
    fun sendEvent(name: String, data: WritableMap) {
        eventEmitter.receiveEvent(id, name, data)
    }
}
+18 −18
Original line number Diff line number Diff line
package cn.qiuxiang.react.amap3d

import android.view.View
import com.amap.api.maps.model.LatLng
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.common.MapBuilder
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.views.view.ReactViewGroup

internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
    override fun getName(): String {
@@ -25,48 +24,49 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {
    }

    override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any>? {
        val map = HashMap<String, Any>()
        map.put("onMarkerClick", MapBuilder.of("registrationName", "onMarkerClick"))
        map.put("onMarkerDragStart", MapBuilder.of("registrationName", "onMarkerDragStart"))
        map.put("onMarkerDrag", MapBuilder.of("registrationName", "onMarkerDrag"))
        map.put("onMarkerDragEnd", MapBuilder.of("registrationName", "onMarkerDragEnd"))
        map.put("onInfoWindowClick", MapBuilder.of("registrationName", "onInfoWindowClick"))
        return map
        return mapOf(
                "onMarkerClick" to mapOf("registrationName" to "onMarkerClick"),
                "onMarkerDragStart" to mapOf("registrationName" to "onMarkerDragStart"),
                "onMarkerDrag" to mapOf("registrationName" to "onMarkerDrag"),
                "onMarkerDragEnd" to mapOf("registrationName" to "onMarkerDragEnd"),
                "onInfoWindowClick" to mapOf("registrationName" to "onInfoWindowClick"))
    }

    @ReactProp(name = "title")
    fun setTitle(marker: AMapMarker, title: String) {
        marker.setTitle(title)
        marker.title = title
    }

    @ReactProp(name = "description")
    fun setSnippet(marker: AMapMarker, description: String) {
        marker.setSnippet(description)
        marker.snippet = description
    }

    @ReactProp(name = "coordinate")
    fun setCoordinate(view: AMapMarker, coordinate: ReadableMap) {
        view.setCoordinate(coordinate)
        view.position = LatLng(
                coordinate.getDouble("latitude"),
                coordinate.getDouble("longitude"))
    }

    @ReactProp(name = "flat")
    fun setFlat(marker: AMapMarker, flat: Boolean) {
        marker.setFlat(flat)
        marker.flat = flat
    }

    @ReactProp(name = "opacity")
    override fun setOpacity(marker: AMapMarker, opacity: Float) {
        marker.setOpacity(opacity)
        marker.opacity = opacity
    }

    @ReactProp(name = "draggable")
    fun setDraggable(marker: AMapMarker, draggable: Boolean) {
        marker.setDraggable(draggable)
        marker.draggable = draggable
    }

    @ReactProp(name = "selected")
    fun setSelected(marker: AMapMarker, selected: Boolean) {
        marker.setActive(selected)
    fun setSelected(marker: AMapMarker, active: Boolean) {
        marker.active = active
    }

    @ReactProp(name = "icon")
@@ -76,6 +76,6 @@ internal class AMapMarkerManager : ViewGroupManager<AMapMarker>() {

    @ReactProp(name = "showsInfoWindow")
    fun setEnabledInfoWindow(marker: AMapMarker, enabled: Boolean) {
        marker.setEnabledInfoWindow(enabled)
        marker.infoWindowEnabled = enabled
    }
}
+41 −45
Original line number Diff line number Diff line
@@ -12,61 +12,57 @@ import com.facebook.react.uimanager.events.RCTEventEmitter
import com.facebook.react.views.view.ReactViewGroup

class AMapPolyline(context: ThemedReactContext) : ReactViewGroup(context) {
    private var polyline: Polyline? = null
    private var coordinates: ArrayList<LatLng> = ArrayList()
    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)
    var polyline: Polyline? = null
        private set

    val polylineId: String?
        get() = polyline?.id

    fun setCoordinates(coordinates: ReadableArray) {
        this.coordinates = ArrayList((0..coordinates.size() - 1)
                .map { coordinates.getMap(it) }
                .map { LatLng(it.getDouble("latitude"), it.getDouble("longitude")) })

        polyline?.points = this.coordinates
    var width: Float = 1f
        set(value) {
            field = value
            polyline?.width = value
        }

    fun setColor(color: Int) {
        this.color = color
        polyline?.color = color
    var color: Int = Color.BLACK
        set(value) {
            field = value
            polyline?.color = value
        }

    fun setWidth(width: Float) {
        this.width = width
        polyline?.width = width
    var opacity: Float = 1f
        set(value) {
            field = value
            polyline?.setTransparency(value)
        }

    fun setZIndex(zIndex: Float) {
        this.zIndex = zIndex
        polyline?.zIndex = zIndex
    var zIndex: Float = 0f
        set(value) {
            field = value
            polyline?.zIndex = value
        }

    fun setGeodesic(geodesic: Boolean) {
        this.geodesic = geodesic
        polyline?.isGeodesic = geodesic
    var geodesic: Boolean = false
        set(value) {
            field = value
            polyline?.isGeodesic = value
        }

    fun setDottedLine(dottedLine: Boolean) {
        this.dottedLine = dottedLine
        polyline?.isDottedLine = dottedLine
    var dottedLine: Boolean = false
        set(value) {
            field = value
            polyline?.isDottedLine = value
        }

    fun setGradient(gradient: Boolean) {
        this.gradient = gradient
    }
    var gradient: Boolean = false

    fun setOpacity(opacity: Float) {
        this.opacity = opacity
        polyline?.setTransparency(opacity)
    private var coordinates: ArrayList<LatLng> = ArrayList()
    private var colors: ArrayList<Int> = ArrayList()
    private val eventEmitter: RCTEventEmitter = context.getJSModule(RCTEventEmitter::class.java)

    fun setCoordinates(coordinates: ReadableArray) {
        this.coordinates = ArrayList((0..coordinates.size() - 1)
                .map { coordinates.getMap(it) }
                .map { LatLng(it.getDouble("latitude"), it.getDouble("longitude")) })

        polyline?.points = this.coordinates
    }

    fun setColors(colors: ReadableArray) {
+9 −9
Original line number Diff line number Diff line
@@ -31,36 +31,36 @@ internal class AMapPolylineManager : ViewGroupManager<AMapPolyline>() {

    @ReactProp(name = "color", customType = "Color")
    fun setColor(polyline: AMapPolyline, color: Int) {
        polyline.setColor(color)
        polyline.color = color
    }

    @ReactProp(name = "width")
    fun setWidth(polyline: AMapPolyline, width: Int) {
        polyline.setWidth(width.toFloat())
    fun setWidth(polyline: AMapPolyline, width: Float) {
        polyline.width = width
    }

    @ReactProp(name = "zIndex")
    fun setZIndex(polyline: AMapPolyline, zIndex: Int) {
        polyline.setZIndex(zIndex.toFloat())
    fun setZIndex_(polyline: AMapPolyline, zIndex: Float) {
        polyline.zIndex = zIndex
    }

    @ReactProp(name = "opacity")
    override fun setOpacity(polyline: AMapPolyline, opacity: Float) {
        polyline.setOpacity(opacity)
        polyline.opacity = opacity
    }

    @ReactProp(name = "geodesic")
    fun setGeodesic(polyline: AMapPolyline, geodesic: Boolean) {
        polyline.setGeodesic(geodesic)
        polyline.geodesic = geodesic
    }

    @ReactProp(name = "dottedLine")
    fun setDottedLine(polyline: AMapPolyline, dottedLine: Boolean) {
        polyline.setDottedLine(dottedLine)
        polyline.dottedLine = dottedLine
    }

    @ReactProp(name = "gradient")
    fun setGradient(polyline: AMapPolyline, gradient: Boolean) {
        polyline.setGradient(gradient)
        polyline.gradient = gradient
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -89,7 +89,7 @@ class AMapView(context: ThemedReactContext) : MapView(context) {

    fun addPolyline(polyline: AMapPolyline) {
        polyline.addToMap(map)
        polylines.put(polyline.polylineId!!, polyline)
        polylines.put(polyline.polyline?.id!!, polyline)
    }

    fun sendEvent(name: String, data: WritableMap) {