Skip to content

Commit 9128ee3

Browse files
authored
Update coroutine usage in ComplicationReceiver (#6542)
1 parent a653e23 commit 9128ee3

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

wear/src/main/kotlin/io/homeassistant/companion/android/complications/ComplicationReceiver.kt

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,35 @@ import androidx.wear.watchface.complications.datasource.ComplicationDataSourceSe
99
import androidx.wear.watchface.complications.datasource.ComplicationDataSourceUpdateRequester
1010
import dagger.hilt.android.AndroidEntryPoint
1111
import io.homeassistant.companion.android.common.data.servers.ServerManager
12+
import io.homeassistant.companion.android.common.util.launchAsync
1213
import io.homeassistant.companion.android.conversation.ConversationActivity
1314
import javax.inject.Inject
1415
import kotlinx.coroutines.CoroutineScope
1516
import kotlinx.coroutines.Dispatchers
1617
import kotlinx.coroutines.SupervisorJob
17-
import kotlinx.coroutines.launch
1818

1919
@AndroidEntryPoint
2020
class ComplicationReceiver : BroadcastReceiver() {
2121
@Inject
2222
lateinit var serverManager: ServerManager
2323

24-
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
24+
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
2525

2626
override fun onReceive(context: Context, intent: Intent) {
27-
val result = goAsync()
28-
29-
try {
30-
when (intent.action) {
31-
UPDATE_COMPLICATION -> updateComplication(context, intent.getIntExtra(EXTRA_ID, -1))
32-
Intent.ACTION_SCREEN_ON -> onScreenOn(context)
33-
}
34-
} finally {
35-
result.finish()
27+
when (intent.action) {
28+
UPDATE_COMPLICATION -> updateComplication(context, intent.getIntExtra(EXTRA_ID, -1))
29+
Intent.ACTION_SCREEN_ON -> launchAsync(scope) { onScreenOn(context) }
3630
}
3731
}
3832

3933
private fun updateComplication(context: Context, id: Int) {
40-
scope.launch {
41-
// Request an update for the complication that has just been toggled.
42-
ComplicationDataSourceUpdateRequester
43-
.create(
44-
context = context,
45-
complicationDataSourceComponent = ComponentName(context, EntityStateDataSourceService::class.java),
46-
)
47-
.requestUpdate(id)
48-
}
34+
// Request an update for the complication that has just been toggled.
35+
ComplicationDataSourceUpdateRequester
36+
.create(
37+
context = context,
38+
complicationDataSourceComponent = ComponentName(context, EntityStateDataSourceService::class.java),
39+
)
40+
.requestUpdate(id)
4941
}
5042

5143
private fun updateAllComplications(context: Context) {
@@ -57,11 +49,9 @@ class ComplicationReceiver : BroadcastReceiver() {
5749
.requestUpdateAll()
5850
}
5951

60-
private fun onScreenOn(context: Context) {
61-
scope.launch {
62-
if (!serverManager.isRegistered()) return@launch
63-
updateAllComplications(context)
64-
}
52+
private suspend fun onScreenOn(context: Context) {
53+
if (!serverManager.isRegistered()) return
54+
updateAllComplications(context)
6555
}
6656

6757
companion object {

0 commit comments

Comments
 (0)