Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Web] Fix webview breaking preview #1190

Merged
merged 1 commit into from Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Expand Up @@ -19,6 +19,7 @@ androidxnavigation = "2.5.0-rc01"
compose-ui-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
compose-ui-util = { module = "androidx.compose.ui:ui-util", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
compose-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "compose" }
compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version.ref = "compose" }
compose-foundation-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
Expand Down
3 changes: 2 additions & 1 deletion sample/build.gradle
Expand Up @@ -70,7 +70,8 @@ dependencies {
implementation libs.compose.material.material
implementation libs.compose.material.iconsext
implementation libs.compose.foundation.layout
implementation libs.compose.ui.tooling
debugImplementation libs.compose.ui.tooling
implementation libs.compose.ui.tooling.preview
implementation libs.compose.ui.util

implementation libs.androidx.lifecycle.viewmodel.compose
Expand Down
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Icon
Expand All @@ -39,6 +40,7 @@ import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Error
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -47,6 +49,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.google.accompanist.sample.AccompanistSampleTheme
import com.google.accompanist.web.AccompanistWebViewClient
Expand Down Expand Up @@ -149,3 +152,17 @@ class BasicWebViewSample : ComponentActivity() {
}
}
}

@Preview
@Composable
private fun WebViewPreview() {
AccompanistSampleTheme {
Column {
Text("Preview should still load but WebView will be grey box.")
WebView(
state = rememberWebViewState(url = "localhost"),
modifier = Modifier.height(100.dp)
)
}
}
}
6 changes: 6 additions & 0 deletions web/src/main/java/com/google/accompanist/web/WebView.kt
Expand Up @@ -35,6 +35,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.viewinterop.AndroidView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -87,6 +88,8 @@ fun WebView(
client.navigator = navigator
chromeClient.state = state

val runningInPreview = LocalInspectionMode.current

AndroidView(
factory = { context ->
WebView(context).apply {
Expand All @@ -103,6 +106,9 @@ fun WebView(
},
modifier = modifier
) { view ->
// AndroidViews are not supported by preview, bail early
if (runningInPreview) return@AndroidView

when (val content = state.content) {
is WebContent.Url -> {
val url = content.url
Expand Down