@@ -20,6 +20,7 @@ import androidx.compose.foundation.layout.height
2020import androidx.compose.foundation.layout.padding
2121import androidx.compose.foundation.layout.systemBarsPadding
2222import androidx.compose.foundation.lazy.LazyColumn
23+ import androidx.compose.foundation.lazy.LazyListState
2324import androidx.compose.foundation.lazy.items
2425import androidx.compose.foundation.lazy.rememberLazyListState
2526import androidx.compose.foundation.rememberScrollState
@@ -40,7 +41,7 @@ import androidx.compose.runtime.setValue
4041import androidx.compose.ui.Alignment
4142import androidx.compose.ui.Modifier
4243import androidx.compose.ui.graphics.Color
43- import androidx.compose.ui.text.TextStyle
44+ import androidx.compose.ui.platform.testTag
4445import androidx.compose.ui.text.font.FontWeight
4546import androidx.compose.ui.unit.dp
4647import androidx.compose.ui.unit.sp
@@ -51,12 +52,16 @@ import com.google.android.gms.maps.model.LatLng
5152
5253private data class CountryLocation (val name : String , val latLng : LatLng , val zoom : Float )
5354
54- private typealias MapItemId = String
55+ typealias MapItemId = String
5556
5657// From https://developers.google.com/public-data/docs/canonical/countries_csv
5758private val countries = listOf (
5859 CountryLocation (" Hong Kong" , LatLng (22.396428 , 114.109497 ), 5f ),
59- CountryLocation (" Madison Square Garden (has indoor mode)" , LatLng (40.7504656 , - 73.9937246 ), 19.33f ),
60+ CountryLocation (
61+ " Madison Square Garden (has indoor mode)" ,
62+ LatLng (40.7504656 , - 73.9937246 ),
63+ 19.33f
64+ ),
6065 CountryLocation (" Bolivia" , LatLng (- 16.290154 , - 63.588653 ), 5f ),
6166 CountryLocation (" Ecuador" , LatLng (- 1.831239 , - 78.183406 ), 5f ),
6267 CountryLocation (" Sweden" , LatLng (60.128161 , 18.643501 ), 5f ),
@@ -74,7 +79,7 @@ private val countries = listOf(
7479 CountryLocation (" Burundi" , LatLng (- 3.373056 , 29.918886 ), 5f )
7580)
7681
77- private data class MapListItem (
82+ data class MapListItem (
7883 val title : String ,
7984 val location : LatLng ,
8085 val zoom : Float ,
@@ -98,7 +103,8 @@ class MapsInLazyColumnActivity : ComponentActivity() {
98103 }
99104
100105 Column (
101- Modifier .fillMaxSize()
106+ Modifier
107+ .fillMaxSize()
102108 .systemBarsPadding(),
103109 ) {
104110 Row (
@@ -125,7 +131,7 @@ class MapsInLazyColumnActivity : ComponentActivity() {
125131 }
126132 if (showLazyColumn) {
127133 Box (Modifier .border(1 .dp, Color .LightGray .copy(0.5f ))) {
128- MapsInLazyColumn (visibleItems)
134+ MapsInLazyColumn (visibleItems, onMapLoaded = { } )
129135 }
130136 }
131137 }
@@ -134,8 +140,15 @@ class MapsInLazyColumnActivity : ComponentActivity() {
134140}
135141
136142@Composable
137- private fun MapsInLazyColumn (mapItems : List <MapListItem >) {
138- val lazyListState = rememberLazyListState()
143+ fun MapsInLazyColumn (
144+ mapItems : List <MapListItem >,
145+ lazyListState : LazyListState = rememberLazyListState(),
146+ onMapLoaded : () -> Unit
147+ ) {
148+
149+ var isMapLoaded by remember { mutableStateOf(false ) }
150+
151+ val lazyListState = lazyListState
139152
140153 val cameraPositionStates = mapItems.associate { item ->
141154 item.id to rememberCameraPositionState(
@@ -168,7 +181,10 @@ private fun MapsInLazyColumn(mapItems: List<MapListItem>) {
168181 .height(300 .dp),
169182 contentAlignment = Alignment .Center
170183 ) {
171- MapCard (item, cameraPositionState)
184+ MapCard (item, cameraPositionState, onMapLoaded = {
185+ isMapLoaded = true
186+ onMapLoaded()
187+ })
172188 }
173189 }
174190 }
@@ -177,7 +193,11 @@ private fun MapsInLazyColumn(mapItems: List<MapListItem>) {
177193
178194@OptIn(MapsComposeExperimentalApi ::class )
179195@Composable
180- private fun MapCard (item : MapListItem , cameraPositionState : CameraPositionState ) {
196+ private fun MapCard (
197+ item : MapListItem ,
198+ cameraPositionState : CameraPositionState ,
199+ onMapLoaded : () -> Unit ,
200+ ) {
181201 Card (
182202 Modifier .padding(16 .dp),
183203 elevation = 4 .dp
@@ -192,11 +212,13 @@ private fun MapCard(item: MapListItem, cameraPositionState: CameraPositionState)
192212 var map: GoogleMap ? by remember { mutableStateOf(null ) }
193213
194214 fun updateIndoorLevel () {
195- activatedIndoorLevel = map!! .focusedBuilding?.run { levels.getOrNull(activeLevelIndex)?.name }
215+ activatedIndoorLevel =
216+ map!! .focusedBuilding?.run { levels.getOrNull(activeLevelIndex)?.name }
196217 }
197218
198219 Box {
199220 GoogleMap (
221+ modifier = Modifier .testTag(" Map" ),
200222 onMapClick = {
201223 onMapClickCount++
202224 },
@@ -207,7 +229,10 @@ private fun MapCard(item: MapListItem, cameraPositionState: CameraPositionState)
207229 )
208230 },
209231 cameraPositionState = cameraPositionState,
210- onMapLoaded = { mapLoaded = true },
232+ onMapLoaded = {
233+ onMapLoaded.invoke()
234+ mapLoaded = true
235+ },
211236 indoorStateChangeListener = object : IndoorStateChangeListener {
212237 override fun onIndoorBuildingFocused () {
213238 super .onIndoorBuildingFocused()
@@ -242,7 +267,7 @@ private fun MapCard(item: MapListItem, cameraPositionState: CameraPositionState)
242267 @Composable
243268 fun TextWithBackground (text : String , fontWeight : FontWeight = FontWeight .Medium ) {
244269 Text (
245- modifier = Modifier .background(Color .White .copy(0.7f )),
270+ modifier = Modifier .background(Color .White .copy(0.7f )).testTag(text) ,
246271 text = text,
247272 fontWeight = fontWeight,
248273 fontSize = 10 .sp
0 commit comments