mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 16:05:20 +00:00
simplify, doesnt subscribe right away
This commit is contained in:
@@ -1,81 +1,58 @@
|
|||||||
package com.bitchat.android.ui
|
package com.bitchat.android.ui
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.util.Log
|
|
||||||
import androidx.lifecycle.viewModelScope
|
|
||||||
import com.bitchat.android.geohash.ChannelID
|
import com.bitchat.android.geohash.ChannelID
|
||||||
import com.bitchat.android.geohash.GeohashChannelLevel
|
import com.bitchat.android.geohash.GeohashChannelLevel
|
||||||
import com.bitchat.android.geohash.LocationChannelManager
|
import com.bitchat.android.geohash.LocationChannelManager
|
||||||
import com.bitchat.android.nostr.LocationNotesManager
|
import com.bitchat.android.nostr.LocationNotesManager
|
||||||
import kotlinx.coroutines.launch
|
import com.bitchat.android.geohash.LocationChannelManager.PermissionState
|
||||||
|
|
||||||
|
private val BUILDING_LEVEL = GeohashChannelLevel.BUILDING
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension functions for ChatViewModel to manage LocationNotesManager subscription
|
* Automatically keep LocationNotesManager in sync with mesh chat and location availability.
|
||||||
* Extracts location notes subscription logic for better separation of concerns
|
* Subscribes to building-level notes when the user is in mesh chat and location access is ready.
|
||||||
*/
|
|
||||||
|
|
||||||
private const val TAG = "LocationNotesVM"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize location notes manager subscription (no separate counter)
|
|
||||||
* Subscribes/unsubscribes based on channel selection and location permission
|
|
||||||
*/
|
*/
|
||||||
fun ChatViewModel.initializeLocationNotesManagerSubscription() {
|
fun ChatViewModel.initializeLocationNotesManagerSubscription() {
|
||||||
viewModelScope.launch {
|
val app = getApplication<Application>()
|
||||||
// Observe channel changes
|
val locationManager = LocationChannelManager.getInstance(app)
|
||||||
selectedLocationChannel.observeForever { channel ->
|
val notesManager = LocationNotesManager.getInstance()
|
||||||
updateLocationNotesSubscription(channel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
fun refreshNotesSubscription() {
|
||||||
* Update location notes subscription based on current channel and location permission
|
val permissionGranted = locationManager.permissionState.value == PermissionState.AUTHORIZED
|
||||||
* iOS pattern: Subscribe when in mesh mode and location is authorized
|
val servicesEnabled = locationManager.isLocationServicesEnabled()
|
||||||
*/
|
val viewingMesh = selectedLocationChannel.value is ChannelID.Mesh
|
||||||
private fun ChatViewModel.updateLocationNotesSubscription(channel: ChannelID?) {
|
|
||||||
try {
|
if (permissionGranted && servicesEnabled && viewingMesh) {
|
||||||
val locationManager = LocationChannelManager.getInstance(getApplication<Application>())
|
val buildingChannel = locationManager.availableChannels.value
|
||||||
val permissionState = locationManager.permissionState.value
|
?.firstOrNull { it.level == BUILDING_LEVEL }
|
||||||
val locationPermissionGranted = permissionState == LocationChannelManager.PermissionState.AUTHORIZED
|
|
||||||
|
if (buildingChannel != null) {
|
||||||
when (channel) {
|
notesManager.setGeohash(buildingChannel.geohash)
|
||||||
is ChannelID.Mesh -> {
|
} else {
|
||||||
// Mesh mode: subscribe to building-level geohash notes if location authorized
|
// Trigger a refresh; when channels arrive we'll get called again
|
||||||
if (locationPermissionGranted) {
|
locationManager.refreshChannels()
|
||||||
// Refresh location to get current building geohash
|
|
||||||
locationManager.refreshChannels()
|
|
||||||
|
|
||||||
// Get building-level geohash (precision 8, same as iOS)
|
|
||||||
val buildingGeohash = locationManager.availableChannels.value
|
|
||||||
?.firstOrNull { it.level == GeohashChannelLevel.BUILDING }
|
|
||||||
?.geohash
|
|
||||||
|
|
||||||
if (buildingGeohash != null) {
|
|
||||||
Log.d(TAG, "📍 Subscribing to location notes for building geohash: $buildingGeohash")
|
|
||||||
LocationNotesManager.getInstance().setGeohash(buildingGeohash)
|
|
||||||
} else {
|
|
||||||
// Cancel if no building geohash available
|
|
||||||
if (LocationNotesManager.getInstance().geohash.value == null) {
|
|
||||||
LocationNotesManager.getInstance().cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
LocationNotesManager.getInstance().cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
is ChannelID.Location -> {
|
|
||||||
// Location channel mode: cancel (only show in mesh mode)
|
|
||||||
LocationNotesManager.getInstance().cancel()
|
|
||||||
}
|
|
||||||
null -> {
|
|
||||||
// Default to mesh behavior
|
|
||||||
if (locationPermissionGranted) {
|
|
||||||
locationManager.refreshChannels()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
notesManager.cancel()
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.w(TAG, "updateLocationNotesSubscription failed: ${e.message}")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
selectedLocationChannel.observeForever { refreshNotesSubscription() }
|
||||||
|
locationManager.permissionState.observeForever {
|
||||||
|
if (it == PermissionState.AUTHORIZED) {
|
||||||
|
locationManager.refreshChannels()
|
||||||
|
}
|
||||||
|
refreshNotesSubscription()
|
||||||
|
}
|
||||||
|
locationManager.availableChannels.observeForever { refreshNotesSubscription() }
|
||||||
|
locationManager.locationServicesEnabled.observeForever { enabled ->
|
||||||
|
if (enabled) {
|
||||||
|
locationManager.refreshChannels()
|
||||||
|
}
|
||||||
|
refreshNotesSubscription()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluate immediately with current state
|
||||||
|
refreshNotesSubscription()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user