@@ -102,6 +102,7 @@ import me.kavishdevar.librepods.utils.AirPodsNotifications
102102@Composable
103103fun AirPodsSettingsScreen (dev : BluetoothDevice ? , service : AirPodsService ,
104104 navController : NavController , isConnected : Boolean , isRemotelyConnected : Boolean ) {
105+ var isLocallyConnected by remember { mutableStateOf(isConnected) }
105106 var isRemotelyConnected by remember { mutableStateOf(isRemotelyConnected) }
106107 val sharedPreferences = LocalContext .current.getSharedPreferences(" settings" , MODE_PRIVATE )
107108 var device by remember { mutableStateOf(dev) }
@@ -113,6 +114,10 @@ fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService,
113114 )
114115 }
115116
117+ LaunchedEffect (service) {
118+ isLocallyConnected = service.isConnectedLocally
119+ }
120+
116121 val nameChangeListener = remember {
117122 SharedPreferences .OnSharedPreferenceChangeListener { _, key ->
118123 if (key == " name" ) {
@@ -144,22 +149,37 @@ fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService,
144149 }
145150
146151 val context = LocalContext .current
147- val bluetoothReceiver = remember {
152+
153+ val connectionReceiver = remember {
148154 object : BroadcastReceiver () {
149155 override fun onReceive (context : Context ? , intent : Intent ? ) {
150- if (intent?.action == " me.kavishdevar.librepods.AIRPODS_CONNECTED_REMOTELY" ) {
151- coroutineScope.launch {
152- handleRemoteConnection(true )
156+ when (intent?.action) {
157+ " me.kavishdevar.librepods.AIRPODS_CONNECTED_REMOTELY" -> {
158+ coroutineScope.launch {
159+ handleRemoteConnection(true )
160+ }
161+ }
162+ " me.kavishdevar.librepods.AIRPODS_DISCONNECTED_REMOTELY" -> {
163+ coroutineScope.launch {
164+ handleRemoteConnection(false )
165+ }
153166 }
154- } else if (intent?.action == " me.kavishdevar.librepods.AIRPODS_DISCONNECTED_REMOTELY" ) {
155- coroutineScope.launch {
156- handleRemoteConnection(false )
167+ AirPodsNotifications .AIRPODS_CONNECTED -> {
168+ coroutineScope.launch {
169+ isLocallyConnected = true
170+ }
157171 }
158- } else if (intent?.action == AirPodsNotifications .DISCONNECT_RECEIVERS ) {
159- try {
160- context?.unregisterReceiver(this )
161- } catch (e: IllegalArgumentException ) {
162- e.printStackTrace()
172+ AirPodsNotifications .AIRPODS_DISCONNECTED -> {
173+ coroutineScope.launch {
174+ isLocallyConnected = false
175+ }
176+ }
177+ AirPodsNotifications .DISCONNECT_RECEIVERS -> {
178+ try {
179+ context?.unregisterReceiver(this )
180+ } catch (e: IllegalArgumentException ) {
181+ e.printStackTrace()
182+ }
163183 }
164184 }
165185 }
@@ -170,16 +190,22 @@ fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService,
170190 val filter = IntentFilter ().apply {
171191 addAction(" me.kavishdevar.librepods.AIRPODS_CONNECTED_REMOTELY" )
172192 addAction(" me.kavishdevar.librepods.AIRPODS_DISCONNECTED_REMOTELY" )
193+ addAction(AirPodsNotifications .AIRPODS_CONNECTED )
194+ addAction(AirPodsNotifications .AIRPODS_DISCONNECTED )
173195 addAction(AirPodsNotifications .DISCONNECT_RECEIVERS )
174196 }
175197
176198 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
177- context.registerReceiver(bluetoothReceiver , filter, RECEIVER_EXPORTED )
199+ context.registerReceiver(connectionReceiver , filter, RECEIVER_EXPORTED )
178200 } else {
179- context.registerReceiver(bluetoothReceiver , filter)
201+ context.registerReceiver(connectionReceiver , filter)
180202 }
181203 onDispose {
182- context.unregisterReceiver(bluetoothReceiver)
204+ try {
205+ context.unregisterReceiver(connectionReceiver)
206+ } catch (e: Exception ) {
207+ e.printStackTrace()
208+ }
183209 }
184210 }
185211
@@ -266,7 +292,7 @@ fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService,
266292 },
267293 snackbarHost = { SnackbarHost (snackbarHostState) }
268294 ) { paddingValues ->
269- if (isConnected == true || isRemotelyConnected == true ) {
295+ if (isLocallyConnected || isRemotelyConnected) {
270296 Column (
271297 modifier = Modifier
272298 .haze(hazeState)
0 commit comments