Skip to content

Commit 1f7c579

Browse files
committed
fix(discovery): ensure map icon is visible on session detail
- Load nodes in DiscoveryHistoryDetailViewModel so the UI can check for mappable data - Update DiscoveryHistoryDetailScreen to show the Map icon if either the session or any discovered nodes have coordinates - Pass loaded nodes to PresetResultCard for proper display in the history detail view
1 parent a6eae01 commit 1f7c579

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

feature/discovery/src/commonMain/kotlin/org/meshtastic/feature/discovery/DiscoveryHistoryDetailViewModel.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,45 @@ package org.meshtastic.feature.discovery
1818

1919
import androidx.lifecycle.SavedStateHandle
2020
import androidx.lifecycle.ViewModel
21+
import kotlinx.coroutines.flow.MutableStateFlow
2122
import kotlinx.coroutines.flow.StateFlow
23+
import kotlinx.coroutines.flow.asStateFlow
2224
import org.koin.core.annotation.InjectedParam
2325
import org.koin.core.annotation.KoinViewModel
2426
import org.meshtastic.core.database.dao.DiscoveryDao
27+
import org.meshtastic.core.database.entity.DiscoveredNodeEntity
2528
import org.meshtastic.core.database.entity.DiscoveryPresetResultEntity
2629
import org.meshtastic.core.database.entity.DiscoverySessionEntity
30+
import org.meshtastic.core.ui.viewmodel.safeLaunch
2731
import org.meshtastic.core.ui.viewmodel.stateInWhileSubscribed
2832

2933
@KoinViewModel
3034
class DiscoveryHistoryDetailViewModel(
3135
@InjectedParam private val sessionId: Long,
32-
discoveryDao: DiscoveryDao,
36+
private val discoveryDao: DiscoveryDao,
3337
) : ViewModel() {
3438

3539
val session: StateFlow<DiscoverySessionEntity?> =
3640
discoveryDao.getSessionFlow(sessionId).stateInWhileSubscribed(initialValue = null)
3741

3842
val presetResults: StateFlow<List<DiscoveryPresetResultEntity>> =
3943
discoveryDao.getPresetResultsFlow(sessionId).stateInWhileSubscribed(initialValue = emptyList())
44+
45+
private val _nodesByPreset = MutableStateFlow<Map<Long, List<DiscoveredNodeEntity>>>(emptyMap())
46+
val nodesByPreset: StateFlow<Map<Long, List<DiscoveredNodeEntity>>> = _nodesByPreset.asStateFlow()
47+
48+
init {
49+
loadNodes()
50+
}
51+
52+
private fun loadNodes() {
53+
safeLaunch(tag = "loadNodes") {
54+
val results = discoveryDao.getPresetResults(sessionId)
55+
val nodesMap = mutableMapOf<Long, List<DiscoveredNodeEntity>>()
56+
for (result in results) {
57+
nodesMap[result.id] = discoveryDao.getDiscoveredNodes(result.id)
58+
}
59+
_nodesByPreset.value = nodesMap
60+
}
61+
}
4062
}

feature/discovery/src/commonMain/kotlin/org/meshtastic/feature/discovery/ui/DiscoveryHistoryDetailScreen.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fun DiscoveryHistoryDetailScreen(
5858
) {
5959
val session by viewModel.session.collectAsStateWithLifecycle()
6060
val presetResults by viewModel.presetResults.collectAsStateWithLifecycle()
61+
val nodesByPreset by viewModel.nodesByPreset.collectAsStateWithLifecycle()
6162

6263
Scaffold(
6364
topBar = {
@@ -68,7 +69,8 @@ fun DiscoveryHistoryDetailScreen(
6869
},
6970
actions = {
7071
val s = session
71-
if (s != null && s.userLatitude != 0.0 && s.userLongitude != 0.0) {
72+
val hasAnyMappableNodes = nodesByPreset.values.flatten().any { it.latitude != null && it.longitude != null && it.latitude != 0.0 }
73+
if (s != null && (s.userLatitude != 0.0 || hasAnyMappableNodes)) {
7274
IconButton(onClick = { onNavigateToMap(s.id) }) {
7375
Icon(MeshtasticIcons.Map, contentDescription = "View map")
7476
}
@@ -85,7 +87,12 @@ fun DiscoveryHistoryDetailScreen(
8587

8688
if (presetResults.isNotEmpty()) {
8789
Text(text = "Preset Results", style = MaterialTheme.typography.titleMedium)
88-
presetResults.forEach { result -> PresetResultCard(result = result, nodes = emptyList()) }
90+
presetResults.forEach { result ->
91+
PresetResultCard(
92+
result = result,
93+
nodes = nodesByPreset[result.id].orEmpty(),
94+
)
95+
}
8996
}
9097
}
9198
}

0 commit comments

Comments
 (0)