mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 07:05:21 +00:00
Fix location update in notes sheet and Nostr subscription leaks (#636)
- Fixes #634: Trigger location refresh when LocationNotesSheet opens - Fixes #635: Implement proper subscription diffing and cleanup in GeohashViewModel
This commit is contained in:
@@ -60,6 +60,7 @@ class GeohashViewModel(
|
|||||||
private var geoTimer: Job? = null
|
private var geoTimer: Job? = null
|
||||||
private var globalPresenceJob: Job? = null
|
private var globalPresenceJob: Job? = null
|
||||||
private var locationChannelManager: com.bitchat.android.geohash.LocationChannelManager? = null
|
private var locationChannelManager: com.bitchat.android.geohash.LocationChannelManager? = null
|
||||||
|
private val activeSamplingGeohashes = mutableSetOf<String>()
|
||||||
|
|
||||||
val geohashPeople: StateFlow<List<GeoPerson>> = state.geohashPeople
|
val geohashPeople: StateFlow<List<GeoPerson>> = state.geohashPeople
|
||||||
val geohashParticipantCounts: StateFlow<Map<String, Int>> = state.geohashParticipantCounts
|
val geohashParticipantCounts: StateFlow<Map<String, Int>> = state.geohashParticipantCounts
|
||||||
@@ -211,25 +212,49 @@ class GeohashViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun beginGeohashSampling(geohashes: List<String>) {
|
fun beginGeohashSampling(geohashes: List<String>) {
|
||||||
if (geohashes.isEmpty()) return
|
if (geohashes.isEmpty()) {
|
||||||
Log.d(TAG, "🌍 Beginning geohash sampling for ${geohashes.size} geohashes")
|
endGeohashSampling()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Subscribe to events
|
// Diffing logic to avoid redundant REQ and leaks
|
||||||
viewModelScope.launch {
|
val currentSet = activeSamplingGeohashes.toSet()
|
||||||
geohashes.forEach { geohash ->
|
val newSet = geohashes.toSet()
|
||||||
subscriptionManager.subscribeGeohash(
|
|
||||||
geohash = geohash,
|
val toRemove = currentSet - newSet
|
||||||
sinceMs = System.currentTimeMillis() - 86400000L,
|
val toAdd = newSet - currentSet
|
||||||
limit = 200,
|
|
||||||
id = "sampling-$geohash",
|
if (toAdd.isEmpty() && toRemove.isEmpty()) return
|
||||||
handler = { event -> geohashMessageHandler.onEvent(event, geohash) }
|
|
||||||
)
|
Log.d(TAG, "🌍 Updating sampling: +${toAdd.size} new, -${toRemove.size} removed")
|
||||||
}
|
|
||||||
|
// Remove old subscriptions
|
||||||
|
toRemove.forEach { geohash ->
|
||||||
|
subscriptionManager.unsubscribe("sampling-$geohash")
|
||||||
|
activeSamplingGeohashes.remove(geohash)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new subscriptions
|
||||||
|
toAdd.forEach { geohash ->
|
||||||
|
subscriptionManager.subscribeGeohash(
|
||||||
|
geohash = geohash,
|
||||||
|
sinceMs = System.currentTimeMillis() - 86400000L,
|
||||||
|
limit = 200,
|
||||||
|
id = "sampling-$geohash",
|
||||||
|
handler = { event -> geohashMessageHandler.onEvent(event, geohash) }
|
||||||
|
)
|
||||||
|
activeSamplingGeohashes.add(geohash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun endGeohashSampling() {
|
fun endGeohashSampling() {
|
||||||
Log.d(TAG, "🌍 Ending geohash sampling")
|
if (activeSamplingGeohashes.isEmpty()) return
|
||||||
|
Log.d(TAG, "🌍 Ending geohash sampling (cleaning up ${activeSamplingGeohashes.size} subs)")
|
||||||
|
|
||||||
|
activeSamplingGeohashes.toList().forEach { geohash ->
|
||||||
|
subscriptionManager.unsubscribe("sampling-$geohash")
|
||||||
|
}
|
||||||
|
activeSamplingGeohashes.clear()
|
||||||
}
|
}
|
||||||
fun geohashParticipantCount(geohash: String): Int = repo.geohashParticipantCount(geohash)
|
fun geohashParticipantCount(geohash: String): Int = repo.geohashParticipantCount(geohash)
|
||||||
fun isPersonTeleported(pubkeyHex: String): Boolean = repo.isPersonTeleported(pubkeyHex)
|
fun isPersonTeleported(pubkeyHex: String): Boolean = repo.isPersonTeleported(pubkeyHex)
|
||||||
|
|||||||
@@ -89,6 +89,11 @@ fun LocationNotesSheet(
|
|||||||
label = "topBarAlpha"
|
label = "topBarAlpha"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Refresh location when sheet opens
|
||||||
|
LaunchedEffect(Unit) {
|
||||||
|
locationManager.refreshChannels()
|
||||||
|
}
|
||||||
|
|
||||||
// Effect to set geohash when sheet opens
|
// Effect to set geohash when sheet opens
|
||||||
LaunchedEffect(geohash) {
|
LaunchedEffect(geohash) {
|
||||||
notesManager.setGeohash(geohash)
|
notesManager.setGeohash(geohash)
|
||||||
|
|||||||
Reference in New Issue
Block a user