From e96adb6250e4ab2ae7bc59bf47ed73b0d5dec95d Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Thu, 15 Jan 2026 12:50:31 +0700 Subject: [PATCH] Refactor: Sync permission state on check Replaces manual updatePermissionState calls with a unified checkAndSyncPermission method. This ensures that _permissionState flow always reflects the actual system permission status whenever it is checked (e.g. in requestOneShotLocation), preventing desync issues when permissions are revoked at runtime. --- .../android/geohash/LocationChannelManager.kt | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 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 3e1b1c90..51ec6545 100644 --- a/app/src/main/java/com/bitchat/android/geohash/LocationChannelManager.kt +++ b/app/src/main/java/com/bitchat/android/geohash/LocationChannelManager.kt @@ -128,7 +128,7 @@ class LocationChannelManager private constructor(private val context: Context) { SystemLocationProvider(context) } - updatePermissionState() + checkAndSyncPermission() // Initialize DataManager and load persisted settings dataManager = com.bitchat.android.ui.DataManager(context) loadPersistedChannelSelection() @@ -293,7 +293,7 @@ class LocationChannelManager private constructor(private val context: Context) { // MARK: - Location Operations private fun requestOneShotLocation() { - if (!hasLocationPermission()) { + if (!checkAndSyncPermission()) { Log.w(TAG, "No location permission for one-shot request") return } @@ -334,22 +334,25 @@ class LocationChannelManager private constructor(private val context: Context) { // MARK: - Helpers private fun getCurrentPermissionStatus(): PermissionState { - return if (hasLocationPermission()) { + return if (checkAndSyncPermission()) { PermissionState.AUTHORIZED } else { PermissionState.DENIED } } - private fun updatePermissionState() { - val newState = getCurrentPermissionStatus() - Log.d(TAG, "Permission state updated to: $newState") - _permissionState.value = newState - } - - private fun hasLocationPermission(): Boolean { - return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || + private fun checkAndSyncPermission(): Boolean { + val hasPermission = ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED + + val newState = if (hasPermission) PermissionState.AUTHORIZED else PermissionState.DENIED + + if (_permissionState.value != newState) { + Log.d(TAG, "Permission state updated to: $newState") + _permissionState.value = newState + } + + return hasPermission } private fun computeChannels(location: Location) {