From 77f97310b51eb5038fd06726b173d54ba960d220 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Wed, 14 Jan 2026 17:54:43 +0700 Subject: [PATCH] fix: resolve compilation errors and improve location state handling - Fix missing imports and stray braces in LocationChannelManager - Implement checkSystemLocationEnabled and BroadcastReceiver for location state - Order class members for correct initialization - Consolidate UI refresh logic in LocationChannelsSheet --- .../android/geohash/LocationChannelManager.kt | 30 +++++++++++++++++-- .../android/ui/LocationChannelsSheet.kt | 28 ----------------- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/bitchat/android/geohash/LocationChannelManager.kt b/app/src/main/java/com/bitchat/android/geohash/LocationChannelManager.kt index e80a7966..ab484ae6 100644 --- a/app/src/main/java/com/bitchat/android/geohash/LocationChannelManager.kt +++ b/app/src/main/java/com/bitchat/android/geohash/LocationChannelManager.kt @@ -1,6 +1,8 @@ package com.bitchat.android.geohash import android.Manifest +import android.content.Context +import android.content.IntentFilter import android.content.pm.PackageManager import android.location.Geocoder import android.location.Location @@ -51,6 +53,25 @@ class LocationChannelManager private constructor(private val context: Context) { private val gson = Gson() private var dataManager: com.bitchat.android.ui.DataManager? = null + private fun checkSystemLocationEnabled(): Boolean { + return try { + locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || + locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) + } catch (_: Exception) { + false + } + } + + private val locationStateReceiver = object : android.content.BroadcastReceiver() { + override fun onReceive(context: Context?, intent: android.content.Intent?) { + if (intent?.action == LocationManager.PROVIDERS_CHANGED_ACTION) { + val isEnabled = checkSystemLocationEnabled() + Log.d(TAG, "System location state changed: $isEnabled") + _systemLocationEnabled.value = isEnabled + } + } + } + // Published state for UI bindings (matching iOS @Published properties) private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) @@ -96,6 +117,9 @@ class LocationChannelManager private constructor(private val context: Context) { dataManager = com.bitchat.android.ui.DataManager(context) loadPersistedChannelSelection() loadLocationServicesState() + + // Register for system location changes + context.registerReceiver(locationStateReceiver, IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)) } // MARK: - Public API (matching iOS interface) @@ -679,9 +703,6 @@ class LocationChannelManager private constructor(private val context: Context) { } } - - } - data class PersistedChannel( val mesh: Boolean, val level: String? = null, @@ -745,6 +766,9 @@ class LocationChannelManager private constructor(private val context: Context) { geocodingJob?.cancel() geocodingJob = null + // Unregister receiver + try { context.unregisterReceiver(locationStateReceiver) } catch (_: Exception) {} + // Remove listeners to prevent memory leaks try { locationManager.removeUpdates(oneShotLocationListener) } catch (_: Exception) {} try { locationManager.removeUpdates(continuousLocationListener) } catch (_: Exception) {} diff --git a/app/src/main/java/com/bitchat/android/ui/LocationChannelsSheet.kt b/app/src/main/java/com/bitchat/android/ui/LocationChannelsSheet.kt index 4442354c..c15e1788 100644 --- a/app/src/main/java/com/bitchat/android/ui/LocationChannelsSheet.kt +++ b/app/src/main/java/com/bitchat/android/ui/LocationChannelsSheet.kt @@ -196,20 +196,6 @@ fun LocationChannelsSheet( color = standardGreen ) } - null -> { - Row( - horizontalArrangement = Arrangement.spacedBy(4.dp), - verticalAlignment = Alignment.CenterVertically - ) { - CircularProgressIndicator(modifier = Modifier.size(12.dp)) - Text( - text = stringResource(R.string.checking_permissions), - fontSize = 11.sp, - fontFamily = FontFamily.Monospace, - color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f) - ) - } - } } } } @@ -568,20 +554,6 @@ fun LocationChannelsSheet( viewModel.endGeohashSampling() } } - - // React to permission changes - LaunchedEffect(permissionState) { - if (permissionState == LocationChannelManager.PermissionState.AUTHORIZED && locationServicesEnabled) { - locationManager.refreshChannels() - } - } - - // React to location services enable/disable - LaunchedEffect(locationServicesEnabled) { - if (locationServicesEnabled && permissionState == LocationChannelManager.PermissionState.AUTHORIZED) { - locationManager.refreshChannels() - } - } } @Composable