Skip to content

Commit 750638f

Browse files
authored
Add SideEffect to Callout (#519)
1 parent 780ba20 commit 750638f

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

toolkit/geoview-compose/src/main/java/com/arcgismaps/toolkit/geoviewcompose/GeoViewScope.kt

+20-17
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import androidx.compose.foundation.layout.padding
3131
import androidx.compose.foundation.layout.sizeIn
3232
import androidx.compose.runtime.Composable
3333
import androidx.compose.runtime.LaunchedEffect
34+
import androidx.compose.runtime.SideEffect
3435
import androidx.compose.runtime.State
3536
import androidx.compose.runtime.getValue
3637
import androidx.compose.runtime.mutableStateOf
@@ -124,15 +125,16 @@ public sealed class GeoViewScope protected constructor(private val geoView: GeoV
124125
shapes: CalloutShapes = CalloutDefaults.shapes(),
125126
content: @Composable BoxScope.() -> Unit
126127
) {
127-
// Enables the recomposition of the first Callout in the content lambda that is displayed
128-
// on the MapView/SceneView
129-
var allowCalloutRecomposition by remember { mutableStateOf(false) }
130-
131-
if (this.isCalloutBeingDisplayed.compareAndSet(false, true)
132-
|| allowCalloutRecomposition
133-
) {
134-
allowCalloutRecomposition = true
128+
if (this.isCalloutBeingDisplayed.compareAndSet(false, true)) {
135129
this.CalloutInternal(location, modifier, offset, rotateOffsetWithGeoView, colorScheme, shapes, content)
130+
131+
SideEffect {
132+
// The SideEffect is executed after every successful (re)composition. This means that it runs at the
133+
// end of the GeoView's content lambda from which this Callout function was called. Resetting at this point
134+
// allows us to run the callout code at subsequent recomposition but also to prevent multiple callouts from
135+
// being rendered within a single (re)composition pass.
136+
reset()
137+
}
136138
}
137139
}
138140

@@ -165,15 +167,16 @@ public sealed class GeoViewScope protected constructor(private val geoView: GeoV
165167
shapes: CalloutShapes = CalloutDefaults.shapes(),
166168
content: @Composable BoxScope.() -> Unit
167169
) {
168-
// Enables the recomposition of the first Callout in the content lambda that is displayed
169-
// on the MapView/SceneView
170-
var allowCalloutRecomposition by remember { mutableStateOf(false) }
171-
172-
if (this.isCalloutBeingDisplayed.compareAndSet(false, true)
173-
|| allowCalloutRecomposition
174-
) {
175-
allowCalloutRecomposition = true
170+
if (this.isCalloutBeingDisplayed.compareAndSet(false, true)) {
176171
this.CalloutInternal(geoElement, modifier, tapLocation, colorScheme, shapes, content)
172+
173+
SideEffect {
174+
// The SideEffect is executed after every successful (re)composition. This means that it runs at the
175+
// end of the GeoView's content lambda from which this Callout function was called. Resetting at this point
176+
// allows us to run the callout code at subsequent recomposition but also to prevent multiple callouts from
177+
// being rendered within a single (re)composition pass.
178+
reset()
179+
}
177180
}
178181
}
179182

@@ -189,7 +192,7 @@ public sealed class GeoViewScope protected constructor(private val geoView: GeoV
189192
*
190193
* @since 200.5.0
191194
*/
192-
internal fun reset() {
195+
private fun reset() {
193196
isCalloutBeingDisplayed.set(false)
194197
}
195198

toolkit/geoview-compose/src/main/java/com/arcgismaps/toolkit/geoviewcompose/MapView.kt

-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ public fun MapView(
206206

207207
if (isMapViewReady.value) {
208208
content?.let {
209-
mapViewScope.reset()
210209
mapViewScope.it()
211210
}
212211
}

toolkit/geoview-compose/src/main/java/com/arcgismaps/toolkit/geoviewcompose/SceneView.kt

-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ public fun SceneView(
211211

212212
if (isSceneViewReady.value) {
213213
content?.let {
214-
sceneViewScope.reset()
215214
sceneViewScope.it()
216215
}
217216
}

0 commit comments

Comments
 (0)