|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 New Vector Ltd |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * Copied and adapted from android-maps-compose (https://github.com/googlemaps/android-maps-compose) |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.element.android.libraries.maplibre.compose |
| 20 | + |
| 21 | +import android.location.Location |
| 22 | +import android.os.Parcelable |
| 23 | +import androidx.compose.runtime.Composable |
| 24 | +import androidx.compose.runtime.ReadOnlyComposable |
| 25 | +import androidx.compose.runtime.getValue |
| 26 | +import androidx.compose.runtime.mutableStateOf |
| 27 | +import androidx.compose.runtime.saveable.Saver |
| 28 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 29 | +import androidx.compose.runtime.setValue |
| 30 | +import androidx.compose.runtime.staticCompositionLocalOf |
| 31 | +import com.mapbox.mapboxsdk.camera.CameraPosition |
| 32 | +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory |
| 33 | +import com.mapbox.mapboxsdk.maps.MapboxMap |
| 34 | +import com.mapbox.mapboxsdk.maps.Projection |
| 35 | +import kotlinx.parcelize.Parcelize |
| 36 | + |
| 37 | +/** |
| 38 | + * Create and [rememberSaveable] a [CameraPositionState] using [CameraPositionState.Saver]. |
| 39 | + * [init] will be called when the [CameraPositionState] is first created to configure its |
| 40 | + * initial state. |
| 41 | + */ |
| 42 | +@Composable |
| 43 | +public inline fun rememberCameraPositionState( |
| 44 | + key: String? = null, |
| 45 | + crossinline init: CameraPositionState.() -> Unit = {} |
| 46 | +): CameraPositionState = rememberSaveable(key = key, saver = CameraPositionState.Saver) { |
| 47 | + CameraPositionState().apply(init) |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * A state object that can be hoisted to control and observe the map's camera state. |
| 52 | + * A [CameraPositionState] may only be used by a single [MapboxMap] composable at a time |
| 53 | + * as it reflects instance state for a single view of a map. |
| 54 | + * |
| 55 | + * @param position the initial camera position |
| 56 | + * @param cameraMode the initial camera mode |
| 57 | + */ |
| 58 | +public class CameraPositionState( |
| 59 | + position: CameraPosition = CameraPosition.Builder().build(), |
| 60 | + cameraMode: CameraMode = CameraMode.NONE, |
| 61 | +) { |
| 62 | + /** |
| 63 | + * Whether the camera is currently moving or not. This includes any kind of movement: |
| 64 | + * panning, zooming, or rotation. |
| 65 | + */ |
| 66 | + public var isMoving: Boolean by mutableStateOf(false) |
| 67 | + internal set |
| 68 | + |
| 69 | + /** |
| 70 | + * The reason for the start of the most recent camera moment, or |
| 71 | + * [CameraMoveStartedReason.NO_MOVEMENT_YET] if the camera hasn't moved yet or |
| 72 | + * [CameraMoveStartedReason.UNKNOWN] if an unknown constant is received from the Maps SDK. |
| 73 | + */ |
| 74 | + public var cameraMoveStartedReason: CameraMoveStartedReason by mutableStateOf( |
| 75 | + CameraMoveStartedReason.NO_MOVEMENT_YET |
| 76 | + ) |
| 77 | + internal set |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the current [Projection] to be used for converting between screen |
| 81 | + * coordinates and lat/lng. |
| 82 | + */ |
| 83 | + public val projection: Projection? |
| 84 | + get() = map?.projection |
| 85 | + |
| 86 | + /** |
| 87 | + * Local source of truth for the current camera position. |
| 88 | + * While [map] is non-null this reflects the current position of [map] as it changes. |
| 89 | + * While [map] is null it reflects the last known map position, or the last value set by |
| 90 | + * explicitly setting [position]. |
| 91 | + */ |
| 92 | + internal var rawPosition by mutableStateOf(position) |
| 93 | + |
| 94 | + /** |
| 95 | + * Current position of the camera on the map. |
| 96 | + */ |
| 97 | + public var position: CameraPosition |
| 98 | + get() = rawPosition |
| 99 | + set(value) { |
| 100 | + synchronized(lock) { |
| 101 | + val map = map |
| 102 | + if (map == null) { |
| 103 | + rawPosition = value |
| 104 | + } else { |
| 105 | + map.moveCamera(CameraUpdateFactory.newCameraPosition(value)) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Local source of truth for the current camera mode. |
| 112 | + * While [map] is non-null this reflects the current camera mode as it changes. |
| 113 | + * While [map] is null it reflects the last known camera mode, or the last value set by |
| 114 | + * explicitly setting [cameraMode]. |
| 115 | + */ |
| 116 | + internal var rawCameraMode by mutableStateOf(cameraMode) |
| 117 | + |
| 118 | + /** |
| 119 | + * Current tracking mode of the camera. |
| 120 | + */ |
| 121 | + public var cameraMode: CameraMode |
| 122 | + get() = rawCameraMode |
| 123 | + set(value) { |
| 124 | + synchronized(lock) { |
| 125 | + val map = map |
| 126 | + if (map == null) { |
| 127 | + rawCameraMode = value |
| 128 | + } else { |
| 129 | + map.locationComponent.cameraMode = value.toInternal() |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * The user's last available location. |
| 136 | + */ |
| 137 | + public var location: Location? by mutableStateOf(null) |
| 138 | + internal set |
| 139 | + |
| 140 | + // Used to perform side effects thread-safely. |
| 141 | + // Guards all mutable properties that are not `by mutableStateOf`. |
| 142 | + private val lock = Unit |
| 143 | + |
| 144 | + // The map currently associated with this CameraPositionState. |
| 145 | + // Guarded by `lock`. |
| 146 | + private var map: MapboxMap? by mutableStateOf(null) |
| 147 | + |
| 148 | + // The current map is set and cleared by side effect. |
| 149 | + // There can be only one associated at a time. |
| 150 | + internal fun setMap(map: MapboxMap?) { |
| 151 | + synchronized(lock) { |
| 152 | + if (this.map == null && map == null) return |
| 153 | + if (this.map != null && map != null) { |
| 154 | + error("CameraPositionState may only be associated with one MapboxMap at a time") |
| 155 | + } |
| 156 | + this.map = map |
| 157 | + if (map == null) { |
| 158 | + isMoving = false |
| 159 | + } else { |
| 160 | + map.moveCamera(CameraUpdateFactory.newCameraPosition(position)) |
| 161 | + map.locationComponent.cameraMode = cameraMode.toInternal() |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + public companion object { |
| 167 | + /** |
| 168 | + * The default saver implementation for [CameraPositionState]. |
| 169 | + */ |
| 170 | + public val Saver: Saver<CameraPositionState, SaveableCameraPositionState> = Saver( |
| 171 | + save = { SaveableCameraPositionState(it.position, it.cameraMode.toInternal()) }, |
| 172 | + restore = { CameraPositionState(it.position, CameraMode.fromInternal(it.cameraMode)) } |
| 173 | + ) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +/** Provides the [CameraPositionState] used by the map. */ |
| 178 | +internal val LocalCameraPositionState = staticCompositionLocalOf { CameraPositionState() } |
| 179 | + |
| 180 | +/** The current [CameraPositionState] used by the map. */ |
| 181 | +public val currentCameraPositionState: CameraPositionState |
| 182 | + @[MapboxMapComposable ReadOnlyComposable Composable] |
| 183 | + get() = LocalCameraPositionState.current |
| 184 | + |
| 185 | +@Parcelize |
| 186 | +public data class SaveableCameraPositionState( |
| 187 | + val position: CameraPosition, |
| 188 | + val cameraMode: Int |
| 189 | +) : Parcelable |
0 commit comments