mirror of
https://github.com/home-assistant/android
synced 2024-10-04 15:19:30 +00:00
Improve camera tile add/remove event handling (#3954)
- If there is already a camera tile with the ID of the tile in `onTileAddEvent` stored in the database, don't overwrite it with a new blank tile as the user configuration might be lost. - Run add/remove event code blocking to prevent the system immediately destroying the service and as a result cancelling the work to save tile data in the database. Without the blocking code I could semi-reliably get it to destroy the service within several milliseconds of adding/removing, often preventing the database code from running.
This commit is contained in:
parent
6118933e4e
commit
a94c90e5aa
|
@ -28,7 +28,7 @@ import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.cancel
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.guava.future
|
import kotlinx.coroutines.guava.future
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
|
@ -185,16 +185,17 @@ class CameraTile : TileService() {
|
||||||
builder.build()
|
builder.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onTileAddEvent(requestParams: EventBuilders.TileAddEvent) {
|
override fun onTileAddEvent(requestParams: EventBuilders.TileAddEvent) = runBlocking {
|
||||||
serviceScope.launch {
|
withContext(Dispatchers.IO) {
|
||||||
AppDatabase.getInstance(this@CameraTile)
|
val dao = AppDatabase.getInstance(this@CameraTile).cameraTileDao()
|
||||||
.cameraTileDao()
|
if (dao.get(requestParams.tileId) == null) {
|
||||||
.add(CameraTile(id = requestParams.tileId))
|
dao.add(CameraTile(id = requestParams.tileId))
|
||||||
|
} // else already existing, don't overwrite existing tile data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onTileRemoveEvent(requestParams: EventBuilders.TileRemoveEvent) {
|
override fun onTileRemoveEvent(requestParams: EventBuilders.TileRemoveEvent) = runBlocking {
|
||||||
serviceScope.launch {
|
withContext(Dispatchers.IO) {
|
||||||
AppDatabase.getInstance(this@CameraTile)
|
AppDatabase.getInstance(this@CameraTile)
|
||||||
.cameraTileDao()
|
.cameraTileDao()
|
||||||
.delete(requestParams.tileId)
|
.delete(requestParams.tileId)
|
||||||
|
|
Loading…
Reference in a new issue