|
| 1 | +package app.myfaq.android.screens |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.webkit.WebView |
| 5 | +import androidx.compose.foundation.isSystemInDarkTheme |
| 6 | +import androidx.compose.foundation.layout.Column |
| 7 | +import androidx.compose.foundation.layout.Spacer |
| 8 | +import androidx.compose.foundation.layout.fillMaxSize |
| 9 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 10 | +import androidx.compose.foundation.layout.height |
| 11 | +import androidx.compose.foundation.layout.padding |
| 12 | +import androidx.compose.foundation.rememberScrollState |
| 13 | +import androidx.compose.foundation.verticalScroll |
| 14 | +import androidx.compose.material.icons.Icons |
| 15 | +import androidx.compose.material.icons.automirrored.filled.ArrowBack |
| 16 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 17 | +import androidx.compose.material3.Icon |
| 18 | +import androidx.compose.material3.IconButton |
| 19 | +import androidx.compose.material3.MaterialTheme |
| 20 | +import androidx.compose.material3.Scaffold |
| 21 | +import androidx.compose.material3.Text |
| 22 | +import androidx.compose.material3.TopAppBar |
| 23 | +import androidx.compose.runtime.Composable |
| 24 | +import androidx.compose.runtime.LaunchedEffect |
| 25 | +import androidx.compose.runtime.collectAsState |
| 26 | +import androidx.compose.runtime.getValue |
| 27 | +import androidx.compose.runtime.mutableStateOf |
| 28 | +import androidx.compose.runtime.remember |
| 29 | +import androidx.compose.runtime.setValue |
| 30 | +import androidx.compose.ui.Modifier |
| 31 | +import androidx.compose.ui.graphics.toArgb |
| 32 | +import androidx.compose.ui.platform.LocalContext |
| 33 | +import androidx.compose.ui.unit.dp |
| 34 | +import androidx.compose.ui.viewinterop.AndroidView |
| 35 | +import app.myfaq.android.screens.components.ErrorRetry |
| 36 | +import app.myfaq.android.screens.components.LoadingIndicator |
| 37 | +import app.myfaq.shared.api.dto.NewsItem |
| 38 | +import app.myfaq.shared.data.ActiveInstanceManager |
| 39 | +import app.myfaq.shared.ui.NewsDetailViewModel |
| 40 | +import app.myfaq.shared.ui.UiState |
| 41 | +import org.koin.compose.koinInject |
| 42 | + |
| 43 | +@OptIn(ExperimentalMaterial3Api::class) |
| 44 | +@Composable |
| 45 | +fun NewsDetailScreen( |
| 46 | + newsId: Int, |
| 47 | + onBack: () -> Unit, |
| 48 | + aim: ActiveInstanceManager = koinInject(), |
| 49 | +) { |
| 50 | + val vm = remember { NewsDetailViewModel(aim, newsId) } |
| 51 | + val state by vm.news.collectAsState() |
| 52 | + |
| 53 | + LaunchedEffect(newsId) { vm.load() } |
| 54 | + |
| 55 | + Scaffold( |
| 56 | + topBar = { |
| 57 | + TopAppBar( |
| 58 | + title = { Text("News") }, |
| 59 | + navigationIcon = { |
| 60 | + IconButton(onClick = onBack) { |
| 61 | + Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back") |
| 62 | + } |
| 63 | + }, |
| 64 | + ) |
| 65 | + }, |
| 66 | + ) { padding -> |
| 67 | + when (val s = state) { |
| 68 | + is UiState.Loading -> LoadingIndicator(modifier = Modifier.padding(padding)) |
| 69 | + is UiState.Error -> ErrorRetry( |
| 70 | + message = s.message, |
| 71 | + onRetry = { vm.load() }, |
| 72 | + modifier = Modifier.padding(padding), |
| 73 | + ) |
| 74 | + is UiState.Success -> NewsDetailContent( |
| 75 | + news = s.data, |
| 76 | + modifier = Modifier.padding(padding), |
| 77 | + ) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +@Composable |
| 83 | +private fun NewsDetailContent( |
| 84 | + news: NewsItem, |
| 85 | + modifier: Modifier = Modifier, |
| 86 | +) { |
| 87 | + val isDark = isSystemInDarkTheme() |
| 88 | + val bgColor = MaterialTheme.colorScheme.surface.toArgb() |
| 89 | + val textColor = MaterialTheme.colorScheme.onSurface.toArgb() |
| 90 | + |
| 91 | + Column( |
| 92 | + modifier = modifier |
| 93 | + .fillMaxSize() |
| 94 | + .verticalScroll(rememberScrollState()) |
| 95 | + .padding(16.dp), |
| 96 | + ) { |
| 97 | + Text( |
| 98 | + text = news.header, |
| 99 | + style = MaterialTheme.typography.headlineSmall, |
| 100 | + ) |
| 101 | + |
| 102 | + if (!news.date.isNullOrBlank()) { |
| 103 | + Spacer(Modifier.height(4.dp)) |
| 104 | + Text( |
| 105 | + text = news.date!!, |
| 106 | + style = MaterialTheme.typography.labelSmall, |
| 107 | + color = MaterialTheme.colorScheme.onSurfaceVariant, |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + if (!news.authorName.isNullOrBlank()) { |
| 112 | + Spacer(Modifier.height(2.dp)) |
| 113 | + Text( |
| 114 | + text = news.authorName!!, |
| 115 | + style = MaterialTheme.typography.labelSmall, |
| 116 | + color = MaterialTheme.colorScheme.onSurfaceVariant, |
| 117 | + ) |
| 118 | + } |
| 119 | + |
| 120 | + if (news.content.isNotBlank()) { |
| 121 | + Spacer(Modifier.height(16.dp)) |
| 122 | + val bgHex = String.format("#%06X", bgColor and 0xFFFFFF) |
| 123 | + val fgHex = String.format("#%06X", textColor and 0xFFFFFF) |
| 124 | + val htmlContent = buildNewsHtml(news.content, bgHex, fgHex) |
| 125 | + val density = LocalContext.current.resources.displayMetrics.density |
| 126 | + var webViewHeight by remember { mutableStateOf(200.dp) } |
| 127 | + |
| 128 | + AndroidView( |
| 129 | + factory = { ctx -> |
| 130 | + WebView(ctx).apply { |
| 131 | + @SuppressLint("SetJavaScriptEnabled") |
| 132 | + settings.javaScriptEnabled = true |
| 133 | + setBackgroundColor(bgColor) |
| 134 | + webViewClient = object : android.webkit.WebViewClient() { |
| 135 | + override fun onPageFinished(view: WebView?, url: String?) { |
| 136 | + view?.evaluateJavascript("document.body.scrollHeight") { heightStr -> |
| 137 | + val heightPx = heightStr.toFloatOrNull() ?: return@evaluateJavascript |
| 138 | + webViewHeight = (heightPx / density).dp + 16.dp |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + loadDataWithBaseURL(null, htmlContent, "text/html", "UTF-8", null) |
| 143 | + } |
| 144 | + }, |
| 145 | + update = { webView -> |
| 146 | + webView.setBackgroundColor(bgColor) |
| 147 | + webView.loadDataWithBaseURL(null, buildNewsHtml(news.content, bgHex, fgHex), "text/html", "UTF-8", null) |
| 148 | + }, |
| 149 | + modifier = Modifier.fillMaxWidth().height(webViewHeight), |
| 150 | + ) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +private fun buildNewsHtml(body: String, bgColor: String, fgColor: String): String = |
| 156 | + """ |
| 157 | + <!DOCTYPE html><html><head> |
| 158 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 159 | + <style> |
| 160 | + body { background-color: $bgColor; color: $fgColor; |
| 161 | + font-family: -apple-system, sans-serif; font-size: 16px; |
| 162 | + padding: 0; margin: 0; word-wrap: break-word; } |
| 163 | + img { max-width: 100%; height: auto; } |
| 164 | + a { color: #1a73e8; } |
| 165 | + </style> |
| 166 | + </head><body>$body</body></html> |
| 167 | + """.trimIndent() |
0 commit comments