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.
This commit is contained in:
callebtc
2026-01-15 12:50:31 +07:00
parent 96172c32f6
commit e96adb6250
@@ -128,7 +128,7 @@ class LocationChannelManager private constructor(private val context: Context) {
SystemLocationProvider(context) SystemLocationProvider(context)
} }
updatePermissionState() checkAndSyncPermission()
// Initialize DataManager and load persisted settings // Initialize DataManager and load persisted settings
dataManager = com.bitchat.android.ui.DataManager(context) dataManager = com.bitchat.android.ui.DataManager(context)
loadPersistedChannelSelection() loadPersistedChannelSelection()
@@ -293,7 +293,7 @@ class LocationChannelManager private constructor(private val context: Context) {
// MARK: - Location Operations // MARK: - Location Operations
private fun requestOneShotLocation() { private fun requestOneShotLocation() {
if (!hasLocationPermission()) { if (!checkAndSyncPermission()) {
Log.w(TAG, "No location permission for one-shot request") Log.w(TAG, "No location permission for one-shot request")
return return
} }
@@ -334,22 +334,25 @@ class LocationChannelManager private constructor(private val context: Context) {
// MARK: - Helpers // MARK: - Helpers
private fun getCurrentPermissionStatus(): PermissionState { private fun getCurrentPermissionStatus(): PermissionState {
return if (hasLocationPermission()) { return if (checkAndSyncPermission()) {
PermissionState.AUTHORIZED PermissionState.AUTHORIZED
} else { } else {
PermissionState.DENIED PermissionState.DENIED
} }
} }
private fun updatePermissionState() { private fun checkAndSyncPermission(): Boolean {
val newState = getCurrentPermissionStatus() 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") Log.d(TAG, "Permission state updated to: $newState")
_permissionState.value = newState _permissionState.value = newState
} }
private fun hasLocationPermission(): Boolean { return hasPermission
return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
} }
private fun computeChannels(location: Location) { private fun computeChannels(location: Location) {