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:
Joris Pelgröm 2023-10-20 23:45:15 +02:00 committed by GitHub
parent 6118933e4e
commit a94c90e5aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,7 +28,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.guava.future
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
@ -185,16 +185,17 @@ class CameraTile : TileService() {
builder.build()
}
override fun onTileAddEvent(requestParams: EventBuilders.TileAddEvent) {
serviceScope.launch {
AppDatabase.getInstance(this@CameraTile)
.cameraTileDao()
.add(CameraTile(id = requestParams.tileId))
override fun onTileAddEvent(requestParams: EventBuilders.TileAddEvent) = runBlocking {
withContext(Dispatchers.IO) {
val dao = AppDatabase.getInstance(this@CameraTile).cameraTileDao()
if (dao.get(requestParams.tileId) == null) {
dao.add(CameraTile(id = requestParams.tileId))
} // else already existing, don't overwrite existing tile data
}
}
override fun onTileRemoveEvent(requestParams: EventBuilders.TileRemoveEvent) {
serviceScope.launch {
override fun onTileRemoveEvent(requestParams: EventBuilders.TileRemoveEvent) = runBlocking {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(this@CameraTile)
.cameraTileDao()
.delete(requestParams.tileId)