fix: correct the default value handling issue when the train display is empty

This commit is contained in:
Nedifinita
2025-08-19 17:03:09 +08:00
parent 39effddfc1
commit cd4b58e16b
2 changed files with 34 additions and 29 deletions

View File

@@ -149,7 +149,7 @@ class TrainRecord(jsonData: JSONObject? = null) {
lbjClass.trim() lbjClass.trim()
} else if (isValidValue(train)) { } else if (isValidValue(train)) {
train.trim() train.trim()
} else "" } else null
val map = mutableMapOf<String, String>() val map = mutableMapOf<String, String>()
@@ -158,7 +158,8 @@ class TrainRecord(jsonData: JSONObject? = null) {
map["receivedTimestamp"] = dateFormat.format(receivedTimestamp) map["receivedTimestamp"] = dateFormat.format(receivedTimestamp)
if (trainDisplay.isNotEmpty()) map["train"] = trainDisplay trainDisplay?.takeIf { it.isNotEmpty() }?.let { map["train"] = it }
if (directionText != "未知") map["direction"] = directionText if (directionText != "未知") map["direction"] = directionText
if (isValidValue(speed)) map["speed"] = "速度: ${speed.trim()} km/h" if (isValidValue(speed)) map["speed"] = "速度: ${speed.trim()} km/h"
if (isValidValue(position)) map["position"] = "位置: ${position.trim()} km" if (isValidValue(position)) map["position"] = "位置: ${position.trim()} km"

View File

@@ -162,18 +162,20 @@ fun TrainRecordItem(
horizontalArrangement = Arrangement.SpaceBetween, horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
val trainDisplay = recordMap["train"]?.toString() ?: "未知列车" val trainDisplay = recordMap["train"]?.toString() ?: ""
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(6.dp) horizontalArrangement = Arrangement.spacedBy(6.dp)
) { ) {
Text( if (trainDisplay.isNotEmpty()) {
text = trainDisplay, Text(
fontWeight = FontWeight.Bold, text = trainDisplay,
fontSize = 20.sp, fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.primary fontSize = 20.sp,
) color = MaterialTheme.colorScheme.primary
)
}
val directionText = when (record.direction) { val directionText = when (record.direction) {
1 -> "" 1 -> ""
@@ -206,13 +208,13 @@ fun TrainRecordItem(
val formattedInfo = when { val formattedInfo = when {
record.locoType.isNotEmpty() && record.loco.isNotEmpty() -> { record.locoType.isNotEmpty() && record.loco.isNotEmpty() -> {
val shortLoco = if (record.loco.length > 5) { val shortLoco = if (record.loco.length > 5) {
record.loco.takeLast(5) record.loco.takeLast(5)
} else { } else {
record.loco record.loco
}
"${record.locoType}-${shortLoco}"
} }
"${record.locoType}-${shortLoco}"
}
record.locoType.isNotEmpty() -> record.locoType record.locoType.isNotEmpty() -> record.locoType
record.loco.isNotEmpty() -> record.loco record.loco.isNotEmpty() -> record.loco
else -> "" else -> ""
@@ -528,18 +530,20 @@ fun MergedTrainRecordItem(
horizontalArrangement = Arrangement.SpaceBetween, horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
val trainDisplay = recordMap["train"]?.toString() ?: "未知列车" val trainDisplay = recordMap["train"]?.toString() ?: ""
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(6.dp) horizontalArrangement = Arrangement.spacedBy(6.dp)
) { ) {
Text( if (trainDisplay.isNotEmpty()) {
text = trainDisplay, Text(
fontWeight = FontWeight.Bold, text = trainDisplay,
fontSize = 20.sp, fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.primary fontSize = 20.sp,
) color = MaterialTheme.colorScheme.primary
)
}
val directionText = when (latestRecord.direction) { val directionText = when (latestRecord.direction) {
1 -> "" 1 -> ""
@@ -572,13 +576,13 @@ fun MergedTrainRecordItem(
val formattedInfo = when { val formattedInfo = when {
latestRecord.locoType.isNotEmpty() && latestRecord.loco.isNotEmpty() -> { latestRecord.locoType.isNotEmpty() && latestRecord.loco.isNotEmpty() -> {
val shortLoco = if (latestRecord.loco.length > 5) { val shortLoco = if (latestRecord.loco.length > 5) {
latestRecord.loco.takeLast(5) latestRecord.loco.takeLast(5)
} else { } else {
latestRecord.loco latestRecord.loco
}
"${latestRecord.locoType}-${shortLoco}"
} }
"${latestRecord.locoType}-${shortLoco}"
}
latestRecord.locoType.isNotEmpty() -> latestRecord.locoType latestRecord.locoType.isNotEmpty() -> latestRecord.locoType
latestRecord.loco.isNotEmpty() -> latestRecord.loco latestRecord.loco.isNotEmpty() -> latestRecord.loco
else -> "" else -> ""