2019-01-18 10:12:08 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* * Copyright 2019 New Vector Ltd
|
|
|
|
* *
|
|
|
|
* * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* * you may not use this file except in compliance with the License.
|
|
|
|
* * You may obtain a copy of the License at
|
|
|
|
* *
|
|
|
|
* * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* *
|
|
|
|
* * Unless required by applicable law or agreed to in writing, software
|
|
|
|
* * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* * See the License for the specific language governing permissions and
|
|
|
|
* * limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-11-08 18:08:14 +00:00
|
|
|
package im.vector.matrix.android.internal.session.room.send
|
|
|
|
|
2019-01-03 16:10:02 +00:00
|
|
|
import androidx.work.*
|
2018-11-25 15:17:47 +00:00
|
|
|
import com.zhuinden.monarchy.Monarchy
|
|
|
|
import im.vector.matrix.android.api.MatrixCallback
|
|
|
|
import im.vector.matrix.android.api.session.events.model.Event
|
2019-01-18 15:26:17 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.send.SendService
|
2018-11-08 18:08:14 +00:00
|
|
|
import im.vector.matrix.android.api.util.Cancelable
|
2018-11-28 17:28:35 +00:00
|
|
|
import im.vector.matrix.android.internal.database.helper.add
|
2018-11-25 15:17:47 +00:00
|
|
|
import im.vector.matrix.android.internal.database.helper.updateDisplayIndexes
|
|
|
|
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
|
|
|
import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom
|
|
|
|
import im.vector.matrix.android.internal.session.room.timeline.PaginationDirection
|
2018-11-08 18:08:14 +00:00
|
|
|
import im.vector.matrix.android.internal.util.CancelableWork
|
2018-11-09 13:06:23 +00:00
|
|
|
import im.vector.matrix.android.internal.util.WorkerParamsFactory
|
2018-11-25 15:17:47 +00:00
|
|
|
import im.vector.matrix.android.internal.util.tryTransactionAsync
|
2018-11-08 18:08:14 +00:00
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
2018-11-09 13:06:23 +00:00
|
|
|
private const val SEND_WORK = "SEND_WORK"
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2018-11-25 15:17:47 +00:00
|
|
|
internal class DefaultSendService(private val roomId: String,
|
|
|
|
private val eventFactory: EventFactory,
|
|
|
|
private val monarchy: Monarchy) : SendService {
|
2018-11-08 18:08:14 +00:00
|
|
|
|
|
|
|
private val sendConstraints = Constraints.Builder()
|
|
|
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
|
|
|
.build()
|
|
|
|
|
2018-11-25 15:17:47 +00:00
|
|
|
override fun sendTextMessage(text: String, callback: MatrixCallback<Event>): Cancelable {
|
|
|
|
val event = eventFactory.createTextEvent(roomId, text)
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2018-11-25 15:17:47 +00:00
|
|
|
monarchy.tryTransactionAsync { realm ->
|
|
|
|
val chunkEntity = ChunkEntity.findLastLiveChunkFromRoom(realm, roomId)
|
2019-01-03 16:10:02 +00:00
|
|
|
?: return@tryTransactionAsync
|
|
|
|
chunkEntity.add(roomId, event, PaginationDirection.FORWARDS)
|
2018-11-25 15:17:47 +00:00
|
|
|
chunkEntity.updateDisplayIndexes()
|
|
|
|
}
|
|
|
|
|
|
|
|
val sendContentWorkerParams = SendEventWorker.Params(roomId, event)
|
2018-11-09 13:06:23 +00:00
|
|
|
val workData = WorkerParamsFactory.toData(sendContentWorkerParams)
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2018-11-09 13:06:23 +00:00
|
|
|
val sendWork = OneTimeWorkRequestBuilder<SendEventWorker>()
|
2018-11-08 18:08:14 +00:00
|
|
|
.setConstraints(sendConstraints)
|
|
|
|
.setInputData(workData)
|
|
|
|
.setBackoffCriteria(BackoffPolicy.LINEAR, 10_000, TimeUnit.MILLISECONDS)
|
|
|
|
.build()
|
|
|
|
|
2018-12-13 10:00:50 +00:00
|
|
|
WorkManager.getInstance()
|
2018-11-09 13:06:23 +00:00
|
|
|
.beginUniqueWork(SEND_WORK, ExistingWorkPolicy.APPEND, sendWork)
|
2018-11-08 18:08:14 +00:00
|
|
|
.enqueue()
|
|
|
|
|
2018-12-13 10:00:50 +00:00
|
|
|
return CancelableWork(sendWork.id)
|
2018-11-08 18:08:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-25 15:17:47 +00:00
|
|
|
}
|