@@ -17,7 +17,6 @@ import androidx.compose.foundation.layout.Box
17
17
import androidx.compose.foundation.layout.wrapContentSize
18
18
import androidx.compose.foundation.text.BasicText
19
19
import androidx.compose.runtime.Composable
20
- import androidx.compose.runtime.mutableStateOf
21
20
import androidx.compose.runtime.remember
22
21
import androidx.compose.ui.Alignment
23
22
import androidx.compose.ui.Modifier
@@ -163,7 +162,7 @@ private fun EmbeddedViewContent(
163
162
){ layout ->
164
163
EmbeddedViewWrapper (
165
164
embeddedId = state.embeddedId,
166
- layout = layout,
165
+ embeddedLayout = layout,
167
166
embeddedSize = state.placementSize
168
167
?.toEmbeddedSize(parentWidthProvider, parentHeightProvider),
169
168
placeholder = placeholder,
@@ -174,64 +173,71 @@ private fun EmbeddedViewContent(
174
173
}
175
174
}
176
175
177
- /* * Shows the embedded view if content is available , otherwise shows the placeholder. */
176
+ /* * Shows the [embeddedLayout] if not null , otherwise shows the [ placeholder] . */
178
177
@Composable
179
- private fun EmbeddedViewWrapper (
178
+ internal fun EmbeddedViewWrapper (
180
179
embeddedId : String ,
181
- layout : EmbeddedLayout ? ,
180
+ embeddedLayout : EmbeddedLayout ? ,
182
181
modifier : Modifier = Modifier ,
183
182
embeddedSize : EmbeddedSize ? ,
184
183
placeholder : (@Composable () -> Unit )?
185
184
) {
186
- if (layout != null ) {
187
- // Only nullable because we're safe-casting the placement type farther down.
188
- // Placement should never be null here, in practice.
189
- val (width, height) = embeddedSize ? : return
190
-
191
- // Remember the view, updating it if the layout instance ID changes.
192
- val view = remember(layout.viewInstanceId) {
193
- mutableStateOf(
194
- layout.makeView(width.fill, height.fill)?.apply {
195
- layoutParams = LayoutParams (width.spec, height.spec).apply {
185
+ val layout = embeddedLayout ? : run {
186
+ // Show the placeholder if we have one
187
+ placeholder?.invoke()
188
+
189
+ // Bail out if we don't have a layout
190
+ return
191
+ }
192
+
193
+ // Only nullable because we're safe-casting the placement type farther down.
194
+ // Placement should never be null here, in practice.
195
+ val (width, height) = embeddedSize ? : run {
196
+ UALog .w(" Embedded size is null for embedded ID \" $embeddedId \" !" )
197
+ return
198
+ }
199
+
200
+ // Remember the view, updating it if the layout instance ID changes.
201
+ val view = remember(embeddedId, layout.viewInstanceId) {
202
+ embeddedLayout.makeView(width.fill, height.fill)!! .apply {
203
+ layoutParams = LayoutParams (width.spec, height.spec).apply {
204
+ gravity = Gravity .CENTER
205
+ }
206
+ }
207
+ }
208
+
209
+ AndroidView (
210
+ factory = { viewContext ->
211
+ FrameLayout (viewContext).apply {
212
+ layoutParams = LayoutParams (width.spec, height.spec)
213
+ }.also {
214
+ UALog .v { " Create embedded layout for id: \" $embeddedId \" , instance: \" ${embeddedLayout.viewInstanceId} \" " }
215
+ }
216
+ },
217
+ update = { frame ->
218
+ view.apply {
219
+ // Update the layout params to pass along size changes to the
220
+ // child embedded view.
221
+ updateLayoutParams {
222
+ LayoutParams (width.spec, height.spec).apply {
196
223
gravity = Gravity .CENTER
197
224
}
198
225
}
199
- )
200
- }
201
226
202
- AndroidView (
203
- factory = { viewContext ->
204
- FrameLayout (viewContext).apply {
205
- layoutParams = LayoutParams (width.spec, height.spec)
206
- }.also {
207
- UALog .v { " Create embedded layout for id: \" $embeddedId \" " }
227
+ // If the frame has children, remove them before adding the new view.
228
+ if (frame.childCount > 0 ) {
229
+ frame.removeAllViews()
208
230
}
209
- },
210
- update = { frame ->
211
- view.value?.apply {
212
- // Update the layout params to pass along size changes to the
213
- // child embedded view.
214
- updateLayoutParams {
215
- LayoutParams (width.spec, height.spec).apply {
216
- gravity = Gravity .CENTER
217
- }
218
- }
219
- // If the frame is empty, add the view.
220
- // The frame will be empty on the first update after the view is
221
- // created, and when the frame is reset and then updated again.
222
- if (frame.childCount == 0 ) {
223
- frame.addView(this )
224
- }
225
- UALog .v { " Update embedded layout for id: \" $embeddedId \" " }
226
- }
227
- },
228
- onReset = { frame ->
229
- frame.removeAllViews()
230
- UALog .v { " Reset embedded layout for id: \" $embeddedId \" " }
231
- },
232
- modifier = modifier
233
- )
234
- } else if (placeholder != null ) {
235
- placeholder()
236
- }
231
+
232
+ frame.addView(this )
233
+
234
+ UALog .v { " Update embedded layout for id: \" $embeddedId \" , instance: \" ${embeddedLayout.viewInstanceId} \" }" }
235
+ }
236
+ },
237
+ onReset = { frame ->
238
+ frame.removeAllViews()
239
+ UALog .v { " Reset embedded layout for id: \" $embeddedId \" , instance: \" ${embeddedLayout.viewInstanceId} \" " }
240
+ },
241
+ modifier = modifier
242
+ )
237
243
}
0 commit comments