|
| 1 | +/* Copyright 2023 Esri |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +package com.esri.arcgismaps.sample.showcallout.components |
| 18 | + |
| 19 | +import androidx.compose.runtime.Composable |
| 20 | +import androidx.compose.runtime.DisposableEffect |
| 21 | +import androidx.compose.runtime.LaunchedEffect |
| 22 | +import androidx.compose.runtime.collectAsState |
| 23 | +import androidx.compose.runtime.getValue |
| 24 | +import androidx.compose.ui.Modifier |
| 25 | +import androidx.compose.ui.platform.LocalContext |
| 26 | +import androidx.compose.ui.platform.LocalLifecycleOwner |
| 27 | +import androidx.compose.ui.viewinterop.AndroidView |
| 28 | +import androidx.lifecycle.LifecycleOwner |
| 29 | +import androidx.lifecycle.lifecycleScope |
| 30 | +import com.arcgismaps.mapping.view.MapView |
| 31 | +import com.arcgismaps.mapping.view.SingleTapConfirmedEvent |
| 32 | +import kotlinx.coroutines.launch |
| 33 | + |
| 34 | +/** |
| 35 | + * Wraps the MapView in a Composable function. |
| 36 | + */ |
| 37 | +@Composable |
| 38 | +fun ComposeMapView( |
| 39 | + modifier: Modifier = Modifier, |
| 40 | + mapViewModel: MapViewModel, |
| 41 | + onSingleTap: (SingleTapConfirmedEvent) -> Unit = {} |
| 42 | +) { |
| 43 | + // get an instance of the current lifecycle owner |
| 44 | + val lifecycleOwner = LocalLifecycleOwner.current |
| 45 | + // collect the latest state of the MapViewState |
| 46 | + val mapViewState by mapViewModel.mapViewState.collectAsState() |
| 47 | + // create and add MapView to the activity lifecycle |
| 48 | + val mapView = createMapViewInstance(lifecycleOwner).apply { |
| 49 | + map = mapViewState.arcGISMap |
| 50 | + setViewpoint(mapViewState.viewpoint) |
| 51 | + // enable animated callout |
| 52 | + callout.isAnimationEnabled = true |
| 53 | + } |
| 54 | + |
| 55 | + // wrap the MapView as an AndroidView |
| 56 | + AndroidView( |
| 57 | + modifier = modifier, |
| 58 | + factory = { mapView }, |
| 59 | + // recomposes the MapView on changes in the MapViewState |
| 60 | + update = { mapView -> |
| 61 | + mapView.apply { |
| 62 | + val latlonPoint = mapViewModel.latLonPoint |
| 63 | + latlonPoint?.let { |
| 64 | + // show callout at the tapped location using the set View |
| 65 | + callout.show( |
| 66 | + mapViewModel.calloutContent, |
| 67 | + latlonPoint |
| 68 | + ) |
| 69 | + lifecycleOwner.lifecycleScope.launch { |
| 70 | + // center the map on the tapped location |
| 71 | + setViewpointCenter(latlonPoint) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + ) |
| 77 | + |
| 78 | + // launch coroutine functions in the composition's CoroutineContext |
| 79 | + LaunchedEffect(Unit) { |
| 80 | + mapView.onSingleTapConfirmed.collect { |
| 81 | + onSingleTap(it) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Create the MapView instance and add it to the Activity lifecycle |
| 88 | + */ |
| 89 | +@Composable |
| 90 | +fun createMapViewInstance(lifecycleOwner: LifecycleOwner): MapView { |
| 91 | + // create the MapView |
| 92 | + val mapView = MapView(LocalContext.current) |
| 93 | + // add the side effects for MapView composition |
| 94 | + DisposableEffect(lifecycleOwner) { |
| 95 | + lifecycleOwner.lifecycle.addObserver(mapView) |
| 96 | + onDispose { |
| 97 | + lifecycleOwner.lifecycle.removeObserver(mapView) |
| 98 | + } |
| 99 | + } |
| 100 | + return mapView |
| 101 | +} |
0 commit comments