Skip to content

Commit

Permalink
feat: change SubsItemCard style
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Dec 15, 2024
1 parent 04abd23 commit 7c8191b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 73 deletions.
19 changes: 0 additions & 19 deletions app/src/main/kotlin/li/songe/gkd/data/SubsItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.Serializable
import li.songe.gkd.appScope
import li.songe.gkd.db.DbSet
import li.songe.gkd.util.isSafeUrl
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.subsFolder
import li.songe.gkd.util.subsIdToRawFlow
Expand All @@ -36,24 +35,6 @@ data class SubsItem(

) {

private val isSafeRemote by lazy {
if (updateUrl != null) {
isSafeUrl(updateUrl)
} else {
false
}
}

val sourceText by lazy {
if (id < 0) {
"本地来源"
} else if (isSafeRemote) {
"可信来源"
} else {
"未知来源"
}
}

@Dao
interface SubsItemDao {
@Update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import li.songe.gkd.MainActivity
import li.songe.gkd.MainViewModel
import li.songe.gkd.util.isSafeUrl
import li.songe.gkd.util.launchAsFn
import li.songe.gkd.util.subsItemsFlow
import li.songe.gkd.util.throttle
import li.songe.gkd.util.toast
Expand All @@ -39,7 +33,7 @@ class InputSubsLinkOption {
continuation = null
}

private suspend fun submit(mainVm: MainViewModel) {
private fun submit() {
val value = valueFlow.value
if (!URLUtil.isNetworkUrl(value)) {
toast("非法链接")
Expand All @@ -55,12 +49,6 @@ class InputSubsLinkOption {
toast("已有相同链接订阅")
return
}
if (!isSafeUrl(value)) {
mainVm.dialogFlow.waitResult(
title = "未知来源",
text = "你正在添加一个未验证的远程订阅\n\n这可能含有恶意的规则\n\n是否仍然确认添加?"
)
}
resume(value)
}

Expand All @@ -79,7 +67,6 @@ class InputSubsLinkOption {
fun ContentDialog() {
val show by showFlow.collectAsState()
if (show) {
val context = LocalContext.current as MainActivity
val value by valueFlow.collectAsState()
val initValue by initValueFlow.collectAsState()
AlertDialog(
Expand Down Expand Up @@ -108,8 +95,8 @@ class InputSubsLinkOption {
confirmButton = {
TextButton(
enabled = value.isNotEmpty(),
onClick = throttle(fn = context.mainVm.viewModelScope.launchAsFn {
submit(context.mainVm)
onClick = throttle(fn = {
submit()
}),
) {
Text(text = "确定")
Expand Down
53 changes: 32 additions & 21 deletions app/src/main/kotlin/li/songe/gkd/ui/component/SubsItemCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.ui.input.pointer.pointerInteropFilter
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpOffset
Expand All @@ -49,6 +50,7 @@ import li.songe.gkd.data.deleteSubscription
import li.songe.gkd.ui.home.HomeVm
import li.songe.gkd.util.LOCAL_SUBS_ID
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.SafeR
import li.songe.gkd.util.formatTimeAgo
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.map
Expand Down Expand Up @@ -124,6 +126,7 @@ fun SubsItemCard(
offsetX = clickPositionX,
vm = vm
)

Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.padding(8.dp),
Expand All @@ -134,30 +137,12 @@ fun SubsItemCard(
) {
if (subscription != null) {
Text(
text = index.toString() + ". " + (subscription.name),
text = "$index.${subscription.name}",
maxLines = 1,
softWrap = false,
overflow = TextOverflow.Ellipsis,
style = MaterialTheme.typography.bodyLarge,
)
Row(
horizontalArrangement = Arrangement.spacedBy(12.dp),
) {
Text(
text = subsItem.sourceText,
style = MaterialTheme.typography.bodyMedium
)
Text(
text = formatTimeAgo(subsItem.mtime),
style = MaterialTheme.typography.bodyMedium,
)
if (subsItem.id >= 0) {
Text(
text = "v" + (subscription.version.toString()),
style = MaterialTheme.typography.bodyMedium,
)
}
}
Text(
text = subscription.numText,
style = MaterialTheme.typography.bodyMedium,
Expand All @@ -167,9 +152,35 @@ fun SubsItemCard(
LocalContentColor.current
}
)
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
if (subsItem.id >= 0) {
if (subscription.author != null) {
Text(
text = subscription.author,
style = MaterialTheme.typography.labelSmall,
)
}
Text(
text = "v" + (subscription.version.toString()),
style = MaterialTheme.typography.labelSmall,
)
} else {
Text(
text = stringResource(SafeR.app_name),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.secondary,
)
}
Text(
text = formatTimeAgo(subsItem.mtime),
style = MaterialTheme.typography.labelSmall,
)
}
} else {
Text(
text = "${index}. id:${subsItem.id}",
text = "id=${subsItem.id}",
maxLines = 1,
softWrap = false,
overflow = TextOverflow.Ellipsis,
Expand All @@ -195,7 +206,7 @@ fun SubsItemCard(
)
}
}
Spacer(modifier = Modifier.width(10.dp))
Spacer(modifier = Modifier.width(4.dp))
Switch(
checked = subsItem.enable,
enabled = !isSelectedMode,
Expand Down
17 changes: 0 additions & 17 deletions app/src/main/kotlin/li/songe/gkd/util/Constants.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package li.songe.gkd.util

import android.webkit.URLUtil

const val FILE_SHORT_URL = "https://f.gkd.li/"
const val IMPORT_SHORT_URL = "https://i.gkd.li/i/"

Expand All @@ -13,21 +11,6 @@ const val ISSUES_URL = "${REPOSITORY_URL}/issues"

const val HOME_PAGE_URL = "https://gkd.li"

private val safeRemoteBaseUrls = arrayOf(
"https://registry.npmmirror.com/@gkd-kit/",
"https://raw.githubusercontent.com/gkd-kit/",

"https://cdn.jsdelivr.net/gh/gkd-kit/",
"https://cdn.jsdelivr.net/npm/@gkd-kit/",
"https://fastly.jsdelivr.net/gh/gkd-kit/",
"https://fastly.jsdelivr.net/npm/@gkd-kit/",
)

fun isSafeUrl(url: String): Boolean {
if (!URLUtil.isHttpsUrl(url)) return false
return safeRemoteBaseUrls.any { u -> url.startsWith(u) }
}

const val LOCAL_SUBS_ID = -2L
const val LOCAL_HTTP_SUBS_ID = -1L
val LOCAL_SUBS_IDS = arrayOf(LOCAL_SUBS_ID, LOCAL_HTTP_SUBS_ID)

0 comments on commit 7c8191b

Please sign in to comment.