|
| 1 | +@file:OptIn(ExperimentalLayoutApi::class) |
| 2 | + |
| 3 | +package github.tornaco.thanos.module.component.manager.redesign; |
| 4 | + |
| 5 | +import androidx.compose.animation.AnimatedContent |
| 6 | +import androidx.compose.foundation.layout.Arrangement |
| 7 | +import androidx.compose.foundation.layout.Column |
| 8 | +import androidx.compose.foundation.layout.ExperimentalLayoutApi |
| 9 | +import androidx.compose.foundation.layout.FlowRow |
| 10 | +import androidx.compose.foundation.layout.Row |
| 11 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 12 | +import androidx.compose.foundation.layout.heightIn |
| 13 | +import androidx.compose.foundation.layout.padding |
| 14 | +import androidx.compose.foundation.layout.size |
| 15 | +import androidx.compose.foundation.layout.wrapContentHeight |
| 16 | +import androidx.compose.foundation.layout.wrapContentWidth |
| 17 | +import androidx.compose.material3.AlertDialogDefaults |
| 18 | +import androidx.compose.material3.Badge |
| 19 | +import androidx.compose.material3.CircularProgressIndicator |
| 20 | +import androidx.compose.material3.Icon |
| 21 | +import androidx.compose.material3.MaterialTheme |
| 22 | +import androidx.compose.material3.Surface |
| 23 | +import androidx.compose.material3.Text |
| 24 | +import androidx.compose.runtime.Composable |
| 25 | +import androidx.compose.runtime.LaunchedEffect |
| 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.Alignment |
| 31 | +import androidx.compose.ui.Modifier |
| 32 | +import androidx.compose.ui.res.painterResource |
| 33 | +import androidx.compose.ui.text.font.FontWeight |
| 34 | +import androidx.compose.ui.unit.dp |
| 35 | +import github.tornaco.android.thanos.core.annotation.DoNotStrip |
| 36 | +import github.tornaco.android.thanos.core.util.GsonUtils |
| 37 | +import github.tornaco.android.thanos.module.compose.common.widget.LargeSpacer |
| 38 | +import github.tornaco.android.thanos.module.compose.common.widget.SmallSpacer |
| 39 | +import github.tornaco.android.thanos.module.compose.common.widget.StandardSpacer |
| 40 | +import github.tornaco.thanos.module.component.manager.ComponentRule |
| 41 | +import kotlinx.coroutines.Dispatchers |
| 42 | +import kotlinx.coroutines.withContext |
| 43 | +import java.net.URL |
| 44 | + |
| 45 | +@Composable |
| 46 | +internal fun RuleInfoDialog(rule: ComponentRule, dismiss: () -> Unit) { |
| 47 | + Surface( |
| 48 | + modifier = Modifier |
| 49 | + .wrapContentWidth() |
| 50 | + .wrapContentHeight(), |
| 51 | + shape = MaterialTheme.shapes.large, |
| 52 | + tonalElevation = AlertDialogDefaults.TonalElevation |
| 53 | + ) { |
| 54 | + var description by remember { mutableStateOf<RuleDescription?>(null) } |
| 55 | + LaunchedEffect(rule) { |
| 56 | + withContext(Dispatchers.IO) { |
| 57 | + kotlin.runCatching { |
| 58 | + val json = |
| 59 | + URL(rule.descriptionUrl.orEmpty()).readText(charset = Charsets.UTF_8) |
| 60 | + val model = GsonUtils.GSON.fromJson(json, RuleDescription::class.java) |
| 61 | + description = model |
| 62 | + }.onFailure { |
| 63 | + description = RuleDescription( |
| 64 | + description = "${it.message}", |
| 65 | + label = "ERROR" |
| 66 | + ) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + Column( |
| 71 | + Modifier |
| 72 | + .fillMaxWidth() |
| 73 | + .heightIn(min = 200.dp) |
| 74 | + .padding(16.dp) |
| 75 | + .padding(vertical = 20.dp), |
| 76 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 77 | + verticalArrangement = Arrangement.Center |
| 78 | + ) { |
| 79 | + Row(verticalAlignment = Alignment.CenterVertically) { |
| 80 | + Text( |
| 81 | + rule.label, |
| 82 | + style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.W500) |
| 83 | + ) |
| 84 | + SmallSpacer() |
| 85 | + RuleIcon(rule) |
| 86 | + } |
| 87 | + LargeSpacer() |
| 88 | + AnimatedContent(description) { rd -> |
| 89 | + if (rd == null) { |
| 90 | + CircularProgressIndicator() |
| 91 | + } else { |
| 92 | + Column { |
| 93 | + Text("Team: ${rd.team.orEmpty()}") |
| 94 | + StandardSpacer() |
| 95 | + FlowRow { |
| 96 | + rd.contributors?.forEach { |
| 97 | + Badge(containerColor = MaterialTheme.colorScheme.primaryContainer) { |
| 98 | + Icon( |
| 99 | + painter = painterResource(github.tornaco.android.thanos.icon.remix.R.drawable.ic_remix_user_line), |
| 100 | + contentDescription = "Filter", |
| 101 | + modifier = Modifier.size(9.dp) |
| 102 | + ) |
| 103 | + SmallSpacer() |
| 104 | + Text(it) |
| 105 | + } |
| 106 | + SmallSpacer() |
| 107 | + } |
| 108 | + } |
| 109 | + StandardSpacer() |
| 110 | + Text( |
| 111 | + rd.description.orEmpty(), |
| 112 | + style = MaterialTheme.typography.bodySmall |
| 113 | + ) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// { |
| 122 | +// "label": "Jetpack Camera", |
| 123 | +// "team": "Google", |
| 124 | +// "iconUrl": "", |
| 125 | +// "contributors": ["Absinthe"], |
| 126 | +// "description": "CameraX 是 Jetpack 的新增库。利用该库,可以更轻松地向应用添加相机功能。该库提供了很多兼容性修复程序和解决方法,有助于在众多设备上打造一致的开发者体验。", |
| 127 | +// "relativeUrl": "https://developer.android.com/jetpack/androidx/releases/camera" |
| 128 | +//} |
| 129 | +@DoNotStrip |
| 130 | +data class RuleDescription( |
| 131 | + val label: String? = null, |
| 132 | + val team: String? = null, |
| 133 | + val iconUrl: String? = null, |
| 134 | + val contributors: List<String>? = null, |
| 135 | + val description: String? = null, |
| 136 | + val relativeUrl: String? = null |
| 137 | +) |
0 commit comments