|
| 1 | +package com.threegap.bitnagil.presentation.webview |
| 2 | + |
| 3 | +import android.view.ViewGroup |
| 4 | +import android.webkit.WebResourceRequest |
| 5 | +import android.webkit.WebView |
| 6 | +import android.webkit.WebViewClient |
| 7 | +import androidx.compose.foundation.background |
| 8 | +import androidx.compose.foundation.clickable |
| 9 | +import androidx.compose.foundation.layout.Box |
| 10 | +import androidx.compose.foundation.layout.Column |
| 11 | +import androidx.compose.foundation.layout.fillMaxSize |
| 12 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 13 | +import androidx.compose.foundation.layout.height |
| 14 | +import androidx.compose.foundation.layout.padding |
| 15 | +import androidx.compose.foundation.layout.size |
| 16 | +import androidx.compose.material.icons.Icons |
| 17 | +import androidx.compose.material.icons.automirrored.filled.ArrowBack |
| 18 | +import androidx.compose.material3.Icon |
| 19 | +import androidx.compose.material3.Text |
| 20 | +import androidx.compose.runtime.Composable |
| 21 | +import androidx.compose.runtime.DisposableEffect |
| 22 | +import androidx.compose.runtime.remember |
| 23 | +import androidx.compose.ui.Alignment |
| 24 | +import androidx.compose.ui.Modifier |
| 25 | +import androidx.compose.ui.platform.LocalContext |
| 26 | +import androidx.compose.ui.tooling.preview.Preview |
| 27 | +import androidx.compose.ui.unit.dp |
| 28 | +import androidx.compose.ui.viewinterop.AndroidView |
| 29 | +import com.threegap.bitnagil.designsystem.BitnagilTheme |
| 30 | + |
| 31 | +@Composable |
| 32 | +fun BitnagilWebViewScreen( |
| 33 | + title: String, |
| 34 | + url: String, |
| 35 | + onBackClick: () -> Unit, |
| 36 | + modifier: Modifier = Modifier, |
| 37 | +) { |
| 38 | + val context = LocalContext.current |
| 39 | + val webView = remember { |
| 40 | + WebView(context).apply { |
| 41 | + webViewClient = object : WebViewClient() { |
| 42 | + override fun shouldOverrideUrlLoading( |
| 43 | + view: WebView?, |
| 44 | + request: WebResourceRequest?, |
| 45 | + ): Boolean { |
| 46 | + val requestUrl = request?.url?.toString() |
| 47 | + return requestUrl?.contains("notion.site") != true |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + settings.apply { |
| 52 | + javaScriptEnabled = true |
| 53 | + domStorageEnabled = true |
| 54 | + allowFileAccess = false |
| 55 | + allowContentAccess = false |
| 56 | + } |
| 57 | + |
| 58 | + layoutParams = ViewGroup.LayoutParams( |
| 59 | + ViewGroup.LayoutParams.MATCH_PARENT, |
| 60 | + ViewGroup.LayoutParams.MATCH_PARENT, |
| 61 | + ) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + DisposableEffect(webView) { |
| 66 | + onDispose { |
| 67 | + webView.destroy() |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + Column( |
| 72 | + modifier = modifier |
| 73 | + .fillMaxSize() |
| 74 | + .background(BitnagilTheme.colors.white), |
| 75 | + ) { |
| 76 | + Box( |
| 77 | + modifier = Modifier |
| 78 | + .height(54.dp) |
| 79 | + .fillMaxWidth(), |
| 80 | + ) { |
| 81 | + // todo: 아이콘 수정하기 |
| 82 | + Icon( |
| 83 | + imageVector = Icons.AutoMirrored.Filled.ArrowBack, |
| 84 | + contentDescription = null, |
| 85 | + modifier = Modifier |
| 86 | + .align(Alignment.CenterStart) |
| 87 | + .padding(start = 4.dp) |
| 88 | + // todo: 리플효과 제거하기 |
| 89 | + .clickable( |
| 90 | + onClick = onBackClick, |
| 91 | + ) |
| 92 | + .size(36.dp), |
| 93 | + ) |
| 94 | + |
| 95 | + Text( |
| 96 | + text = title, |
| 97 | + color = BitnagilTheme.colors.coolGray10, |
| 98 | + style = BitnagilTheme.typography.title3SemiBold, |
| 99 | + modifier = Modifier |
| 100 | + .align(Alignment.Center), |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + AndroidView( |
| 105 | + modifier = Modifier.fillMaxSize(), |
| 106 | + factory = { webView }, |
| 107 | + update = { view -> |
| 108 | + if (view.url != url) { |
| 109 | + view.loadUrl(url) |
| 110 | + } |
| 111 | + }, |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +@Preview(showBackground = true) |
| 117 | +@Composable |
| 118 | +private fun BitnagilWebViewScreenPreview() { |
| 119 | + BitnagilWebViewScreen( |
| 120 | + title = "약관 동의", |
| 121 | + url = "", |
| 122 | + onBackClick = {}, |
| 123 | + ) |
| 124 | +} |
0 commit comments