Skip to content

Commit

Permalink
fix(google-maps): correctly typed event listeners
Browse files Browse the repository at this point in the history
* feat: added typing for event listeners

* chore: doc fixes
  • Loading branch information
ItsChaceD committed May 18, 2022
1 parent 7cb89fb commit 656f916
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 200 deletions.
194 changes: 135 additions & 59 deletions google-maps/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import com.getcapacitor.JSArray
import com.getcapacitor.JSObject
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.GoogleMap.*
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
Expand Down Expand Up @@ -67,8 +67,6 @@ class CapacitorGoogleMap(

isReadyChannel.receive()

this@CapacitorGoogleMap.mapView = mapView

render()
}

Expand Down Expand Up @@ -100,7 +98,7 @@ class CapacitorGoogleMap(
((bridge.webView.parent) as ViewGroup).addView(mapViewParent)

bridge.webView.bringToFront()
bridge.webView.setBackgroundColor(Color.TRANSPARENT);
bridge.webView.setBackgroundColor(Color.TRANSPARENT)
}
}
}
Expand Down Expand Up @@ -134,7 +132,10 @@ class CapacitorGoogleMap(

fun bringToFront() {
CoroutineScope(Dispatchers.Main).launch {
val mapViewParent = ((delegate.bridge.webView.parent) as ViewGroup).findViewWithTag<ViewGroup>(this@CapacitorGoogleMap.id)
val mapViewParent =
((delegate.bridge.webView.parent) as ViewGroup).findViewWithTag<ViewGroup>(
this@CapacitorGoogleMap.id
)
mapViewParent.bringToFront()
}
}
Expand Down Expand Up @@ -394,17 +395,17 @@ class CapacitorGoogleMap(
CoroutineScope(Dispatchers.Main).launch {
val mapTypeInt: Int =
when (mapType) {
"Normal" -> GoogleMap.MAP_TYPE_NORMAL
"Hybrid" -> GoogleMap.MAP_TYPE_HYBRID
"Satellite" -> GoogleMap.MAP_TYPE_SATELLITE
"Terrain" -> GoogleMap.MAP_TYPE_TERRAIN
"None" -> GoogleMap.MAP_TYPE_NONE
"Normal" -> MAP_TYPE_NORMAL
"Hybrid" -> MAP_TYPE_HYBRID
"Satellite" -> MAP_TYPE_SATELLITE
"Terrain" -> MAP_TYPE_TERRAIN
"None" -> MAP_TYPE_NONE
else -> {
Log.w(
"CapacitorGoogleMaps",
"unknown mapView type '$mapType' Defaulting to normal."
)
GoogleMap.MAP_TYPE_NORMAL
MAP_TYPE_NORMAL
}
}

Expand Down Expand Up @@ -467,10 +468,10 @@ class CapacitorGoogleMap(

fun getMapBounds(): Rect {
return Rect(
getScaledPixels(delegate.bridge, config.x).toInt(),
getScaledPixels(delegate.bridge, config.y).toInt(),
getScaledPixels(delegate.bridge, config.x + config.width).toInt(),
getScaledPixels(delegate.bridge, config.y + config.height).toInt()
getScaledPixels(delegate.bridge, config.x),
getScaledPixels(delegate.bridge, config.y),
getScaledPixels(delegate.bridge, config.x + config.width),
getScaledPixels(delegate.bridge, config.y + config.height)
)
}

Expand All @@ -490,10 +491,10 @@ class CapacitorGoogleMap(

private fun getScaledRect(bridge: Bridge, rectF: RectF): RectF {
return RectF(
getScaledPixelsF(bridge, rectF.left),
getScaledPixelsF(bridge, rectF.top),
getScaledPixelsF(bridge, rectF.right),
getScaledPixelsF(bridge, rectF.bottom)
getScaledPixelsF(bridge, rectF.left),
getScaledPixelsF(bridge, rectF.top),
getScaledPixelsF(bridge, rectF.right),
getScaledPixelsF(bridge, rectF.bottom)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.capacitorjs.plugins.googlemaps

import android.Manifest
import android.annotation.SuppressLint
import android.graphics.Rect
import android.graphics.RectF
import android.util.Log
import android.view.MotionEvent
Expand All @@ -15,20 +14,19 @@ import org.json.JSONArray
import org.json.JSONObject

@CapacitorPlugin(
name = "CapacitorGoogleMaps",
permissions = [
Permission(
strings = [Manifest.permission.ACCESS_FINE_LOCATION],
alias = CapacitorGoogleMapsPlugin.LOCATION
),
],
name = "CapacitorGoogleMaps",
permissions =
[
Permission(
strings = [Manifest.permission.ACCESS_FINE_LOCATION],
alias = CapacitorGoogleMapsPlugin.LOCATION
),
],
)
class CapacitorGoogleMapsPlugin : Plugin() {
private var maps: HashMap<String, CapacitorGoogleMap> = HashMap()
private var cachedTouchEvents: HashMap<String, MutableList<MotionEvent>> = HashMap()
private val tag: String = "CAP-GOOGLE-MAPS"
private var devicePixelRatio = 1.00f


companion object {
const val LOCATION = "location"
Expand All @@ -38,46 +36,48 @@ class CapacitorGoogleMapsPlugin : Plugin() {
override fun load() {
super.load()

this.bridge.webView.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
if (event != null) {
if (event.source == -1) {
return v?.onTouchEvent(event) ?: true
}
this.bridge.webView.setOnTouchListener(
object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
if (event != null) {
if (event.source == -1) {
return v?.onTouchEvent(event) ?: true
}

val touchX = event.x
val touchY = event.y
val touchX = event.x
val touchY = event.y

for ((id, map) in maps) {
val mapRect = map.getMapBounds()
if (mapRect.contains(touchX.toInt(), touchY.toInt())) {
if (event.action == MotionEvent.ACTION_DOWN) {
if (cachedTouchEvents[id] == null) {
cachedTouchEvents[id] = mutableListOf<MotionEvent>()
}
for ((id, map) in maps) {
val mapRect = map.getMapBounds()
if (mapRect.contains(touchX.toInt(), touchY.toInt())) {
if (event.action == MotionEvent.ACTION_DOWN) {
if (cachedTouchEvents[id] == null) {
cachedTouchEvents[id] = mutableListOf<MotionEvent>()
}

cachedTouchEvents[id]?.clear()
}
cachedTouchEvents[id]?.clear()
}

val motionEvent = MotionEvent.obtain(event)
cachedTouchEvents[id]?.add(motionEvent)
val motionEvent = MotionEvent.obtain(event)
cachedTouchEvents[id]?.add(motionEvent)

val payload = JSObject()
payload.put("x", touchX / devicePixelRatio)
payload.put("y", touchY / devicePixelRatio)
payload.put("mapId", map.id)
val payload = JSObject()
payload.put("x", touchX / map.config.devicePixelRatio)
payload.put("y", touchY / map.config.devicePixelRatio)
payload.put("mapId", map.id)

notifyListeners("isMapInFocus", payload)
return true
notifyListeners("isMapInFocus", payload)
return true
}
}
}

return v?.onTouchEvent(event) ?: true
}
}

return v?.onTouchEvent(event) ?: true
}
})
)
}

override fun handleOnStart() {
super.handleOnStart()
maps.forEach { it.value.onStart() }
Expand Down Expand Up @@ -108,14 +108,13 @@ class CapacitorGoogleMapsPlugin : Plugin() {
try {
val id = call.getString("id")

this.devicePixelRatio = call.getFloat("devicePixelRatio", 1.00f)!!

if (null == id || id.isEmpty()) {
throw InvalidMapIdError()
}

val configObject = call.getObject("config")
?: throw InvalidArgumentsError("config object is missing")
val configObject =
call.getObject("config")
?: throw InvalidArgumentsError("config object is missing")

val forceCreate = call.getBoolean("forceCreate", false)!!

Expand Down Expand Up @@ -240,7 +239,7 @@ class CapacitorGoogleMapsPlugin : Plugin() {
val map = maps[id]
map ?: throw MapNotFoundError()

map.enableClustering() { err ->
map.enableClustering { err ->
if (err != null) {
throw err
}
Expand All @@ -263,7 +262,7 @@ class CapacitorGoogleMapsPlugin : Plugin() {
val map = maps[id]
map ?: throw MapNotFoundError()

map.disableClustering() { err ->
map.disableClustering { err ->
if (err != null) {
throw err
}
Expand Down Expand Up @@ -349,8 +348,9 @@ class CapacitorGoogleMapsPlugin : Plugin() {
val map = maps[id]
map ?: throw MapNotFoundError()

val cameraConfigObject = call.getObject("config")
?: throw InvalidArgumentsError("config object is missing")
val cameraConfigObject =
call.getObject("config")
?: throw InvalidArgumentsError("config object is missing")

val config = GoogleMapCameraConfig(cameraConfigObject)

Expand All @@ -377,8 +377,8 @@ class CapacitorGoogleMapsPlugin : Plugin() {
val map = maps[id]
map ?: throw MapNotFoundError()

val mapType = call.getString("mapType")
?: throw InvalidArgumentsError("mapType is missing")
val mapType =
call.getString("mapType") ?: throw InvalidArgumentsError("mapType is missing")

map.setMapType(mapType) { err ->
if (err != null) {
Expand All @@ -403,8 +403,8 @@ class CapacitorGoogleMapsPlugin : Plugin() {
val map = maps[id]
map ?: throw MapNotFoundError()

val enabled = call.getBoolean("enabled")
?: throw InvalidArgumentsError("enabled is missing")
val enabled =
call.getBoolean("enabled") ?: throw InvalidArgumentsError("enabled is missing")

map.enableIndoorMaps(enabled) { err ->
if (err != null) {
Expand All @@ -429,8 +429,8 @@ class CapacitorGoogleMapsPlugin : Plugin() {
val map = maps[id]
map ?: throw MapNotFoundError()

val enabled = call.getBoolean("enabled")
?: throw InvalidArgumentsError("enabled is missing")
val enabled =
call.getBoolean("enabled") ?: throw InvalidArgumentsError("enabled is missing")

map.enableTrafficLayer(enabled) { err ->
if (err != null) {
Expand All @@ -448,7 +448,7 @@ class CapacitorGoogleMapsPlugin : Plugin() {

@PluginMethod
fun enableCurrentLocation(call: PluginCall) {
if (getPermissionState(CapacitorGoogleMapsPlugin.LOCATION) != PermissionState.GRANTED) {
if (getPermissionState(LOCATION) != PermissionState.GRANTED) {
requestAllPermissions(call, "enableCurrentLocationCallback")
} else {
internalEnableCurrentLocation(call)
Expand All @@ -457,7 +457,7 @@ class CapacitorGoogleMapsPlugin : Plugin() {

@PermissionCallback
fun enableCurrentLocationCallback(call: PluginCall) {
if (getPermissionState(CapacitorGoogleMapsPlugin.LOCATION) == PermissionState.GRANTED) {
if (getPermissionState(LOCATION) == PermissionState.GRANTED) {
internalEnableCurrentLocation(call)
} else {
call.reject("location permission was denied")
Expand All @@ -473,8 +473,8 @@ class CapacitorGoogleMapsPlugin : Plugin() {
val map = maps[id]
map ?: throw MapNotFoundError()

val paddingObj = call.getObject("padding")
?: throw InvalidArgumentsError("padding is missing")
val paddingObj =
call.getObject("padding") ?: throw InvalidArgumentsError("padding is missing")

val padding = GoogleMapPadding(paddingObj)

Expand Down Expand Up @@ -506,7 +506,9 @@ class CapacitorGoogleMapsPlugin : Plugin() {
val map = maps[id]
map ?: throw MapNotFoundError()

val boundsObj = call.getObject("mapBounds") ?: throw InvalidArgumentsError("mapBounds object is missing")
val boundsObj =
call.getObject("mapBounds")
?: throw InvalidArgumentsError("mapBounds object is missing")

val bounds = boundsObjectToRect(boundsObj)

Expand All @@ -533,7 +535,7 @@ class CapacitorGoogleMapsPlugin : Plugin() {

val events = cachedTouchEvents[id]
if (events != null) {
for(event in events) {
for (event in events) {
if (focus) {
map.dispatchTouchEvent(event)
} else {
Expand Down Expand Up @@ -561,8 +563,8 @@ class CapacitorGoogleMapsPlugin : Plugin() {
val map = maps[id]
map ?: throw MapNotFoundError()

val enabled = call.getBoolean("enabled")
?: throw InvalidArgumentsError("enabled is missing")
val enabled =
call.getBoolean("enabled") ?: throw InvalidArgumentsError("enabled is missing")

map.enableCurrentLocation(enabled) { err ->
if (err != null) {
Expand Down

0 comments on commit 656f916

Please sign in to comment.