Skip to content

Commit

Permalink
fix: 点击记录名称显示错误 (gkd-kit#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Dec 18, 2023
1 parent 2ea4242 commit 5c430e0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
32 changes: 13 additions & 19 deletions app/src/main/kotlin/li/songe/gkd/ui/ClickLogPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fun ClickLogPage() {
val navController = LocalNavController.current

val vm = hiltViewModel<ClickLogVm>()
val clickLogs by vm.clickLogsFlow.collectAsState()
val clickDataList by vm.clickDataListFlow.collectAsState()
val clickLogCount by vm.clickLogCountFlow.collectAsState()
val appInfoCache by appInfoCacheFlow.collectAsState()

Expand All @@ -87,7 +87,7 @@ fun ClickLogPage() {
},
title = { Text(text = "点击记录" + if (clickLogCount <= 0) "" else ("-$clickLogCount")) },
actions = {
if (clickLogs.isNotEmpty()) {
if (clickDataList.isNotEmpty()) {
IconButton(onClick = { showDeleteDlg = true }) {
Icon(
imageVector = Icons.Outlined.Delete,
Expand All @@ -97,37 +97,36 @@ fun ClickLogPage() {
}
})
}, content = { contentPadding ->
if (clickLogs.isNotEmpty()) {
if (clickDataList.isNotEmpty()) {
LazyColumn(
modifier = Modifier.padding(contentPadding),
) {
items(clickLogs, { triggerLog -> triggerLog.id }) { triggerLog ->
items(clickDataList, { it.t0.id }) { (clickLog, group, rule) ->
Column(modifier = Modifier
.clickable {
previewClickLog = triggerLog
previewClickLog = clickLog
}
.fillMaxWidth()
.padding(10.dp)) {
Row {
Text(
text = triggerLog.id.format("MM-dd HH:mm:ss"),
text = clickLog.id.format("MM-dd HH:mm:ss"),
fontFamily = FontFamily.Monospace
)
Spacer(modifier = Modifier.width(10.dp))
Text(
text = appInfoCache[triggerLog.appId]?.name ?: triggerLog.appId
?: "null"
text = appInfoCache[clickLog.appId]?.name ?: clickLog.appId ?: ""
)
}
Spacer(modifier = Modifier.width(10.dp))
val showActivityId = if (triggerLog.activityId != null) {
if (triggerLog.appId != null && triggerLog.activityId.startsWith(
triggerLog.appId
val showActivityId = if (clickLog.activityId != null) {
if (clickLog.appId != null && clickLog.activityId.startsWith(
clickLog.appId
)
) {
triggerLog.activityId.substring(triggerLog.appId.length)
clickLog.activityId.substring(clickLog.appId.length)
} else {
triggerLog.activityId
clickLog.activityId
}
} else {
null
Expand All @@ -137,21 +136,16 @@ fun ClickLogPage() {
overflow = TextOverflow.Ellipsis,
maxLines = 1,
)
val group = vm.getGroup(triggerLog)
if (group?.name != null) {
Spacer(modifier = Modifier.width(10.dp))
Text(text = group.name)
}
val rule = group?.rules?.run {
find { r -> r.key == triggerLog.ruleKey }
?: getOrNull(triggerLog.ruleIndex)
}
if (rule?.name != null) {
Spacer(modifier = Modifier.width(10.dp))
Text(text = rule.name)
} else if ((group?.rules?.size ?: 0) > 1) {
Spacer(modifier = Modifier.width(10.dp))
Text(text = (if (triggerLog.ruleKey != null) "key=${triggerLog.ruleKey}, " else "") + "index=${triggerLog.ruleIndex}")
Text(text = (if (clickLog.ruleKey != null) "key=${clickLog.ruleKey}, " else "") + "index=${clickLog.ruleIndex}")
}
}
Divider()
Expand Down
27 changes: 18 additions & 9 deletions app/src/main/kotlin/li/songe/gkd/ui/ClickLogVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import li.songe.gkd.data.SubscriptionRaw
import li.songe.gkd.data.ClickLog
import li.songe.gkd.data.Tuple3
import li.songe.gkd.db.DbSet
import li.songe.gkd.util.subsIdToRawFlow
import li.songe.gkd.util.subsItemsFlow
import javax.inject.Inject

@HiltViewModel
class ClickLogVm @Inject constructor() : ViewModel() {
val clickLogsFlow =
DbSet.clickLogDao.query().stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

val clickDataListFlow =
combine(DbSet.clickLogDao.query(), subsIdToRawFlow) { clickLogs, subsIdToRaw ->
clickLogs.map { c ->
val app = subsIdToRaw[c.subsId]?.apps?.find { a -> a.id == c.appId }
val group = app?.groups?.find { g -> g.key == c.groupKey }
val rule = group?.rules?.run {
if (c.ruleKey != null) {
find { r -> r.key == c.ruleKey }
} else {
getOrNull(c.ruleIndex)
}
}
Tuple3(c, group, rule)
}
}.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

val clickLogCountFlow =
DbSet.clickLogDao.count().stateIn(viewModelScope, SharingStarted.Eagerly, 0)

fun getGroup(clickLog: ClickLog): SubscriptionRaw.GroupRaw? {
val subsItem = subsItemsFlow.value.find { s -> s.id == clickLog.subsId } ?: return null
return subsIdToRawFlow.value[subsItem.id]?.apps?.find { a -> a.id == clickLog.appId }?.groups?.find { g -> g.key == clickLog.groupKey }
}
}

0 comments on commit 5c430e0

Please sign in to comment.