mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-03-18 20:29:10 +03:00
Schedule work during aggregation
This commit is contained in:
parent
47eb7173f0
commit
683a9cdfff
2 changed files with 52 additions and 4 deletions
|
@ -86,4 +86,10 @@ internal class DeactivateLiveLocationShareWorker(context: Context, params: Worke
|
|||
override fun buildErrorParams(params: Params, message: String): Params {
|
||||
return params.copy(lastFailureMessage = params.lastFailureMessage ?: message)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val WORK_NAME_PREFIX = "DeactivateLiveLocationWork-"
|
||||
|
||||
fun getWorkName(eventId: String, roomId: String) = "${WORK_NAME_PREFIX}$eventId-$roomId"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.matrix.android.sdk.internal.session.room.aggregation.livelocation
|
||||
|
||||
import androidx.work.ExistingWorkPolicy
|
||||
import io.realm.Realm
|
||||
import org.matrix.android.sdk.api.extensions.orTrue
|
||||
import org.matrix.android.sdk.api.session.events.model.Event
|
||||
|
@ -26,17 +27,27 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconLocati
|
|||
import org.matrix.android.sdk.internal.database.mapper.ContentMapper
|
||||
import org.matrix.android.sdk.internal.database.model.livelocation.LiveLocationShareAggregatedSummaryEntity
|
||||
import org.matrix.android.sdk.internal.database.query.getOrCreate
|
||||
import org.matrix.android.sdk.internal.di.SessionId
|
||||
import org.matrix.android.sdk.internal.di.WorkManagerProvider
|
||||
import org.matrix.android.sdk.internal.util.time.Clock
|
||||
import org.matrix.android.sdk.internal.worker.WorkerParamsFactory
|
||||
import timber.log.Timber
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class LiveLocationAggregationProcessor @Inject constructor() {
|
||||
internal class LiveLocationAggregationProcessor @Inject constructor(
|
||||
@SessionId private val sessionId: String,
|
||||
private val workManagerProvider: WorkManagerProvider,
|
||||
private val clock: Clock,
|
||||
) {
|
||||
|
||||
fun handleBeaconInfo(realm: Realm, event: Event, content: MessageBeaconInfoContent, roomId: String, isLocalEcho: Boolean) {
|
||||
if (event.senderId.isNullOrEmpty() || isLocalEcho) {
|
||||
return
|
||||
}
|
||||
|
||||
val targetEventId = if (content.isLive.orTrue()) {
|
||||
val isLive = content.isLive.orTrue()
|
||||
val targetEventId = if (isLive) {
|
||||
event.eventId
|
||||
} else {
|
||||
// when live is set to false, we use the id of the event that should have been replaced
|
||||
|
@ -56,8 +67,39 @@ internal class LiveLocationAggregationProcessor @Inject constructor() {
|
|||
|
||||
Timber.d("updating summary of id=$targetEventId with isLive=${content.isLive}")
|
||||
|
||||
aggregatedSummary.endOfLiveTimestampMillis = content.getBestTimestampMillis()?.let { it + (content.timeout ?: 0) }
|
||||
aggregatedSummary.isActive = content.isLive
|
||||
val endOfLiveTimestampMillis = content.getBestTimestampMillis()?.let { it + (content.timeout ?: 0) }
|
||||
aggregatedSummary.endOfLiveTimestampMillis = endOfLiveTimestampMillis
|
||||
aggregatedSummary.isActive = isLive
|
||||
|
||||
if (isLive) {
|
||||
scheduleDeactivationAfterTimeout(targetEventId, roomId, endOfLiveTimestampMillis)
|
||||
} else {
|
||||
cancelDeactivationAfterTimeout(targetEventId, roomId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun scheduleDeactivationAfterTimeout(eventId: String, roomId: String, endOfLiveTimestampMillis: Long?) {
|
||||
endOfLiveTimestampMillis?.let { endOfLiveMillis ->
|
||||
val workParams = DeactivateLiveLocationShareWorker.Params(sessionId = sessionId, eventId = eventId, roomId = roomId)
|
||||
val workData = WorkerParamsFactory.toData(workParams)
|
||||
val workName = DeactivateLiveLocationShareWorker.getWorkName(eventId = eventId, roomId = roomId)
|
||||
val workDelayMillis = (endOfLiveMillis - clock.epochMillis()).coerceAtLeast(0)
|
||||
val workRequest = workManagerProvider.matrixOneTimeWorkRequestBuilder<DeactivateLiveLocationShareWorker>()
|
||||
.setInitialDelay(workDelayMillis, TimeUnit.MILLISECONDS)
|
||||
.setInputData(workData)
|
||||
.build()
|
||||
|
||||
workManagerProvider.workManager.enqueueUniqueWork(
|
||||
workName,
|
||||
ExistingWorkPolicy.KEEP,
|
||||
workRequest
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun cancelDeactivationAfterTimeout(eventId: String, roomId: String) {
|
||||
val workName = DeactivateLiveLocationShareWorker.getWorkName(eventId = eventId, roomId = roomId)
|
||||
workManagerProvider.workManager.cancelUniqueWork(workName)
|
||||
}
|
||||
|
||||
fun handleBeaconLocationData(
|
||||
|
|
Loading…
Add table
Reference in a new issue