Manually disable location (#347)

* manually disable location, design needs to be fixed

* fix ui
This commit is contained in:
callebtc
2025-08-29 22:09:44 +02:00
committed by GitHub
parent b1234ff548
commit 0aecaf50f8
3 changed files with 210 additions and 79 deletions
@@ -72,11 +72,16 @@ class LocationChannelManager private constructor(private val context: Context) {
private val _isLoadingLocation = MutableLiveData(false)
val isLoadingLocation: LiveData<Boolean> = _isLoadingLocation
// Add a new LiveData property to track if location services are enabled by user
private val _locationServicesEnabled = MutableLiveData(false)
val locationServicesEnabled: LiveData<Boolean> = _locationServicesEnabled
init {
updatePermissionState()
// Initialize DataManager and load persisted channel selection
// Initialize DataManager and load persisted settings
dataManager = com.bitchat.android.ui.DataManager(context)
loadPersistedChannelSelection()
loadLocationServicesState()
}
// MARK: - Public API (matching iOS interface)
@@ -108,7 +113,7 @@ class LocationChannelManager private constructor(private val context: Context) {
* Refresh available channels from current location
*/
fun refreshChannels() {
if (_permissionState.value == PermissionState.AUTHORIZED) {
if (_permissionState.value == PermissionState.AUTHORIZED && isLocationServicesEnabled()) {
requestOneShotLocation()
}
}
@@ -124,13 +129,20 @@ class LocationChannelManager private constructor(private val context: Context) {
return
}
if (!isLocationServicesEnabled()) {
Log.w(TAG, "Cannot start live refresh - location services disabled by user")
return
}
// Cancel existing timer
refreshTimer?.cancel()
// Start new timer with coroutines
refreshTimer = CoroutineScope(Dispatchers.IO).launch {
while (isActive) {
if (isLocationServicesEnabled()) {
requestOneShotLocation()
}
delay(interval)
}
}
@@ -185,6 +197,48 @@ class LocationChannelManager private constructor(private val context: Context) {
_teleported.postValue(teleported)
}
/**
* Enable location services (user-controlled toggle)
*/
fun enableLocationServices() {
Log.d(TAG, "enableLocationServices() called by user")
_locationServicesEnabled.postValue(true)
saveLocationServicesState(true)
// If we have permission, start location operations
if (_permissionState.value == PermissionState.AUTHORIZED) {
requestOneShotLocation()
}
}
/**
* Disable location services (user-controlled toggle)
*/
fun disableLocationServices() {
Log.d(TAG, "disableLocationServices() called by user")
_locationServicesEnabled.postValue(false)
saveLocationServicesState(false)
// Stop any ongoing location operations
endLiveRefresh()
// Clear available channels when location is disabled
_availableChannels.postValue(emptyList())
_locationNames.postValue(emptyMap())
// If user had a location channel selected, switch back to mesh
if (_selectedChannel.value is ChannelID.Location) {
select(ChannelID.Mesh)
}
}
/**
* Check if location services are enabled by the user
*/
fun isLocationServicesEnabled(): Boolean {
return _locationServicesEnabled.value ?: false
}
// MARK: - Location Operations
private fun requestOneShotLocation() {
@@ -572,6 +626,34 @@ class LocationChannelManager private constructor(private val context: Context) {
Log.d(TAG, "Cleared persisted channel selection")
}
// MARK: - Location Services State Persistence
/**
* Save location services enabled state to persistent storage
*/
private fun saveLocationServicesState(enabled: Boolean) {
try {
dataManager?.saveLocationServicesEnabled(enabled)
Log.d(TAG, "Saved location services state: $enabled")
} catch (e: Exception) {
Log.e(TAG, "Failed to save location services state: ${e.message}")
}
}
/**
* Load persisted location services state from storage
*/
private fun loadLocationServicesState() {
try {
val enabled = dataManager?.isLocationServicesEnabled() ?: false
_locationServicesEnabled.postValue(enabled)
Log.d(TAG, "Loaded location services state: $enabled")
} catch (e: Exception) {
Log.e(TAG, "Failed to load location services state: ${e.message}")
_locationServicesEnabled.postValue(false)
}
}
/**
* Cleanup resources
*/
@@ -62,6 +62,17 @@ class DataManager(private val context: Context) {
Log.d(TAG, "Cleared last geohash channel")
}
// MARK: - Location Services State
fun saveLocationServicesEnabled(enabled: Boolean) {
prefs.edit().putBoolean("location_services_enabled", enabled).apply()
Log.d(TAG, "Saved location services enabled state: $enabled")
}
fun isLocationServicesEnabled(): Boolean {
return prefs.getBoolean("location_services_enabled", false)
}
// MARK: - Channel Data Management
fun loadChannelData(): Pair<Set<String>, Set<String>> {
@@ -12,6 +12,7 @@ import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PinDrop
import androidx.compose.material3.*
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
@@ -20,7 +21,6 @@ import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bitchat.android.ui.theme.BASE_FONT_SIZE
@@ -52,6 +52,7 @@ fun LocationChannelsSheet(
val selectedChannel by locationManager.selectedChannel.observeAsState()
val teleported by locationManager.teleported.observeAsState(false)
val locationNames by locationManager.locationNames.observeAsState(emptyMap())
val locationServicesEnabled by locationManager.locationServicesEnabled.observeAsState(false)
// CRITICAL FIX: Observe reactive participant counts for real-time updates
val geohashParticipantCounts by viewModel.geohashParticipantCounts.observeAsState(emptyMap())
@@ -109,7 +110,8 @@ fun LocationChannelsSheet(
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f)
)
// Permission handling
// Location Services Control - Show permission handling if enabled
if (locationServicesEnabled) {
when (permissionState) {
LocationChannelManager.PermissionState.NOT_DETERMINED -> {
Button(
@@ -121,7 +123,7 @@ fun LocationChannelsSheet(
modifier = Modifier.fillMaxWidth()
) {
Text(
text = "get location and my geohashes",
text = "grant location permission",
fontSize = 12.sp,
fontFamily = FontFamily.Monospace
)
@@ -133,9 +135,9 @@ fun LocationChannelsSheet(
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text(
text = "location permission denied. enable in settings to use location channels.",
fontSize = 12.sp,
fontSize = 11.sp,
fontFamily = FontFamily.Monospace,
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f)
color = Color.Red.copy(alpha = 0.8f)
)
TextButton(
@@ -148,7 +150,7 @@ fun LocationChannelsSheet(
) {
Text(
text = "open settings",
fontSize = 12.sp,
fontSize = 11.sp,
fontFamily = FontFamily.Monospace
)
}
@@ -156,12 +158,28 @@ fun LocationChannelsSheet(
}
LocationChannelManager.PermissionState.AUTHORIZED -> {
// Authorized - show channels below
Text(
text = "✓ location permission granted",
fontSize = 11.sp,
fontFamily = FontFamily.Monospace,
color = standardGreen
)
}
null -> {
// Loading state
CircularProgressIndicator()
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
CircularProgressIndicator(modifier = Modifier.size(12.dp))
Text(
text = "checking permissions...",
fontSize = 11.sp,
fontFamily = FontFamily.Monospace,
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f)
)
}
}
}
}
@@ -185,8 +203,8 @@ fun LocationChannelsSheet(
)
}
// Nearby options
if (availableChannels.isNotEmpty()) {
// Nearby options (only show if location services are enabled)
if (availableChannels.isNotEmpty() && locationServicesEnabled) {
items(availableChannels) { channel ->
val coverage = coverageString(channel.geohash.length)
val nameBase = locationNames[channel.level]
@@ -210,7 +228,7 @@ fun LocationChannelsSheet(
}
)
}
} else if (permissionState == LocationChannelManager.PermissionState.AUTHORIZED) {
} else if (permissionState == LocationChannelManager.PermissionState.AUTHORIZED && locationServicesEnabled) {
item {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
@@ -348,24 +366,36 @@ fun LocationChannelsSheet(
}
}
// Footer action - remove location access
if (permissionState == LocationChannelManager.PermissionState.AUTHORIZED) {
// Location services toggle button
item {
Button(
onClick = {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.fromParts("package", context.packageName, null)
if (locationServicesEnabled) {
locationManager.disableLocationServices()
} else {
locationManager.enableLocationServices()
}
context.startActivity(intent)
},
colors = ButtonDefaults.buttonColors(
containerColor = Color.Red.copy(alpha = 0.08f),
contentColor = Color(0xFFBF1A1A)
containerColor = if (locationServicesEnabled) {
Color.Red.copy(alpha = 0.08f)
} else {
standardGreen.copy(alpha = 0.12f)
},
contentColor = if (locationServicesEnabled) {
Color(0xFFBF1A1A)
} else {
standardGreen
}
),
modifier = Modifier.fillMaxWidth()
) {
Text(
text = "remove location access",
text = if (locationServicesEnabled) {
"disable location services"
} else {
"enable location services"
},
fontSize = 12.sp,
fontFamily = FontFamily.Monospace
)
@@ -375,17 +405,18 @@ fun LocationChannelsSheet(
}
}
}
}
// Lifecycle management
LaunchedEffect(isPresented) {
if (isPresented) {
// Refresh channels when opening
if (permissionState == LocationChannelManager.PermissionState.AUTHORIZED) {
// Refresh channels when opening (only if location services are enabled)
if (permissionState == LocationChannelManager.PermissionState.AUTHORIZED && locationServicesEnabled) {
locationManager.refreshChannels()
}
// Begin periodic refresh while sheet is open
// Begin periodic refresh while sheet is open (only if location services are enabled)
if (locationServicesEnabled) {
locationManager.beginLiveRefresh()
}
// Begin multi-channel sampling for counts
val geohashes = availableChannels.map { it.geohash }
@@ -398,7 +429,14 @@ fun LocationChannelsSheet(
// React to permission changes
LaunchedEffect(permissionState) {
if (permissionState == LocationChannelManager.PermissionState.AUTHORIZED) {
if (permissionState == LocationChannelManager.PermissionState.AUTHORIZED && locationServicesEnabled) {
locationManager.refreshChannels()
}
}
// React to location services enable/disable
LaunchedEffect(locationServicesEnabled) {
if (locationServicesEnabled && permissionState == LocationChannelManager.PermissionState.AUTHORIZED) {
locationManager.refreshChannels()
}
}