@@ -47,6 +47,7 @@ import androidx.compose.runtime.mutableFloatStateOf
47
47
import androidx.compose.runtime.remember
48
48
import androidx.compose.runtime.rememberCoroutineScope
49
49
import androidx.compose.runtime.setValue
50
+ import androidx.compose.runtime.snapshotFlow
50
51
import androidx.compose.ui.Alignment
51
52
import androidx.compose.ui.Modifier
52
53
import androidx.compose.ui.geometry.Offset
@@ -70,6 +71,7 @@ import com.google.accompanist.permissions.ExperimentalPermissionsApi
70
71
import com.google.accompanist.permissions.isGranted
71
72
import com.google.accompanist.permissions.rememberPermissionState
72
73
import com.google.accompanist.permissions.shouldShowRationale
74
+ import kotlinx.coroutines.flow.takeWhile
73
75
import kotlinx.coroutines.launch
74
76
75
77
@OptIn(
@@ -129,6 +131,21 @@ fun CameraPreviewScreen(
129
131
}
130
132
131
133
uiState.surfaceRequest?.let { surface ->
134
+ // Workaround for https://issuetracker.google.com/275157240
135
+ // When switching to/from tabletop posture, the underlying SurfaceView
136
+ // destroys its Surface. Invalidate the SurfaceRequest when this happens
137
+ // so CameraX can retrieve the new Surface.
138
+ LaunchedEffect (surface) {
139
+ val oldIsTableTop = isTableTopPosture(foldingFeature)
140
+ snapshotFlow { foldingFeature }
141
+ .takeWhile {
142
+ val newIsTableTop = isTableTopPosture(it)
143
+ val shouldInvalidate = oldIsTableTop != newIsTableTop
144
+ if (shouldInvalidate) surface.invalidate()
145
+ ! shouldInvalidate
146
+ }.collect {}
147
+ }
148
+
132
149
CameraPreviewContent (
133
150
surfaceRequest = surface,
134
151
autofocusUiState = uiState.autofocusUiState,
0 commit comments