-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathMapUpdater.kt
More file actions
164 lines (150 loc) · 6.69 KB
/
MapUpdater.kt
File metadata and controls
164 lines (150 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.maps.android.compose
import android.annotation.SuppressLint
import android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReusableComposeNode
import androidx.compose.runtime.currentComposer
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.LocationSource
internal class MapPropertiesNode(
val map: GoogleMap,
cameraPositionState: CameraPositionState,
contentDescription: String?,
var density: Density,
var layoutDirection: LayoutDirection,
) : MapNode {
init {
cameraPositionState.setMap(map)
if (contentDescription != null) {
map.setContentDescription(contentDescription)
}
}
var contentDescription = contentDescription
set(value) {
field = value
map.setContentDescription(contentDescription)
}
var cameraPositionState = cameraPositionState
set(value) {
if (value == field) return
field.setMap(null)
field = value
value.setMap(map)
}
override fun onAttached() {
map.setOnCameraIdleListener {
cameraPositionState.isMoving = false
// setOnCameraMoveListener is only invoked when the camera position
// is changed via .animate(). To handle updating state when .move()
// is used, it's necessary to set the camera's position here as well
cameraPositionState.rawPosition = map.cameraPosition
}
map.setOnCameraMoveCanceledListener {
cameraPositionState.isMoving = false
}
map.setOnCameraMoveStartedListener {
cameraPositionState.cameraMoveStartedReason = CameraMoveStartedReason.fromInt(it)
cameraPositionState.isMoving = true
}
map.setOnCameraMoveListener {
cameraPositionState.rawPosition = map.cameraPosition
}
}
override fun onRemoved() {
cameraPositionState.setMap(null)
}
override fun onCleared() {
cameraPositionState.setMap(null)
}
}
internal val NoPadding = PaddingValues()
/**
* Used to keep the primary map properties up to date. This should never leave the map composition.
*/
@SuppressLint("MissingPermission")
@Suppress("NOTHING_TO_INLINE")
@Composable
internal inline fun MapUpdater(
mergeDescendants: Boolean = false,
contentDescription: String?,
cameraPositionState: CameraPositionState,
contentPadding: PaddingValues = NoPadding,
locationSource: LocationSource?,
mapProperties: MapProperties,
mapUiSettings: MapUiSettings,
) {
val map = (currentComposer.applier as MapApplier).map
val mapView = (currentComposer.applier as MapApplier).mapView
if (mergeDescendants) {
mapView.importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
}
val density = LocalDensity.current
val layoutDirection = LocalLayoutDirection.current
ReusableComposeNode<MapPropertiesNode, MapApplier>(
factory = {
MapPropertiesNode(
map = map,
contentDescription = contentDescription,
cameraPositionState = cameraPositionState,
density = density,
layoutDirection = layoutDirection,
)
}
) {
// The node holds density and layoutDirection so that the updater blocks can be
// non-capturing, allowing the compiler to turn them into singletons
update(density) { this.density = it }
update(layoutDirection) { this.layoutDirection = it }
update(contentDescription) { this.contentDescription = it }
set(locationSource) { map.setLocationSource(it) }
set(mapProperties.isBuildingEnabled) { map.isBuildingsEnabled = it }
set(mapProperties.isIndoorEnabled) { map.isIndoorEnabled = it }
set(mapProperties.isMyLocationEnabled) { map.isMyLocationEnabled = it }
set(mapProperties.isTrafficEnabled) { map.isTrafficEnabled = it }
set(mapProperties.latLngBoundsForCameraTarget) { map.setLatLngBoundsForCameraTarget(it) }
set(mapProperties.mapStyleOptions) { map.setMapStyle(it) }
set(mapProperties.mapType) { map.mapType = it.value }
set(mapProperties.maxZoomPreference) { map.setMaxZoomPreference(it) }
set(mapProperties.minZoomPreference) { map.setMinZoomPreference(it) }
set(contentPadding) {
val node = this
with(this.density) {
map.setPadding(
it.calculateLeftPadding(node.layoutDirection).roundToPx(),
it.calculateTopPadding().roundToPx(),
it.calculateRightPadding(node.layoutDirection).roundToPx(),
it.calculateBottomPadding().roundToPx()
)
}
}
set(mapUiSettings.compassEnabled) { map.uiSettings.isCompassEnabled = it }
set(mapUiSettings.indoorLevelPickerEnabled) { map.uiSettings.isIndoorLevelPickerEnabled = it }
set(mapUiSettings.mapToolbarEnabled) { map.uiSettings.isMapToolbarEnabled = it }
set(mapUiSettings.myLocationButtonEnabled) { map.uiSettings.isMyLocationButtonEnabled = it }
set(mapUiSettings.rotationGesturesEnabled) { map.uiSettings.isRotateGesturesEnabled = it }
set(mapUiSettings.scrollGesturesEnabled) { map.uiSettings.isScrollGesturesEnabled = it }
set(mapUiSettings.scrollGesturesEnabledDuringRotateOrZoom) { map.uiSettings.isScrollGesturesEnabledDuringRotateOrZoom = it }
set(mapUiSettings.tiltGesturesEnabled) { map.uiSettings.isTiltGesturesEnabled = it }
set(mapUiSettings.zoomControlsEnabled) { map.uiSettings.isZoomControlsEnabled = it }
set(mapUiSettings.zoomGesturesEnabled) { map.uiSettings.isZoomGesturesEnabled = it }
update(cameraPositionState) { this.cameraPositionState = it }
}
}