Sync : save token

This commit is contained in:
ganfra 2018-10-17 14:46:36 +02:00
parent 44eb838610
commit d9f1d3fc85
4 changed files with 37 additions and 4 deletions

View file

@ -30,7 +30,11 @@ class SyncModule : Module {
}
scope(DefaultSession.SCOPE) {
SyncThread(get(), get())
SyncTokenStore(get())
}
scope(DefaultSession.SCOPE) {
SyncThread(get(), get(), get())
}

View file

@ -46,7 +46,7 @@ class SyncRequest(private val syncAPI: SyncAPI,
Failure.Unknown(RuntimeException("Sync response shouln't be null"))
}.flatMap {
try {
syncResponseHandler.handleResponse(it, null, false)
syncResponseHandler.handleResponse(it, token, false)
Either.right(it)
} catch (exception: Exception) {
Either.Left(Failure.Unknown(exception))

View file

@ -0,0 +1,26 @@
package im.vector.matrix.android.internal.events.sync
import im.vector.matrix.android.internal.database.model.SyncEntity
import io.realm.Realm
import io.realm.RealmConfiguration
class SyncTokenStore(private val realmConfiguration: RealmConfiguration) {
fun getLastToken(): String? {
val realm = Realm.getInstance(realmConfiguration)
val token = realm.where(SyncEntity::class.java).findFirst()?.nextBatch
realm.close()
return token
}
fun saveToken(token: String?) {
val realm = Realm.getInstance(realmConfiguration)
realm.executeTransaction {
val sync = SyncEntity(token)
it.insertOrUpdate(sync)
}
realm.close()
}
}

View file

@ -4,6 +4,7 @@ import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.util.Cancelable
import im.vector.matrix.android.internal.events.sync.SyncRequest
import im.vector.matrix.android.internal.events.sync.SyncTokenStore
import im.vector.matrix.android.internal.events.sync.data.SyncResponse
import im.vector.matrix.android.internal.network.NetworkConnectivityChecker
import timber.log.Timber
@ -12,7 +13,8 @@ import java.util.concurrent.CountDownLatch
private const val RETRY_WAIT_TIME_MS = 10_000L
class SyncThread(private val syncRequest: SyncRequest,
private val networkConnectivityChecker: NetworkConnectivityChecker
private val networkConnectivityChecker: NetworkConnectivityChecker,
private val syncTokenStore: SyncTokenStore
) : Thread(), NetworkConnectivityChecker.Listener {
enum class State {
@ -25,7 +27,7 @@ class SyncThread(private val syncRequest: SyncRequest,
private var state: State = State.IDLE
private val lock = Object()
private var nextBatch: String? = null
private var nextBatch = syncTokenStore.getLastToken()
private var cancelableRequest: Cancelable? = null
fun restart() {
@ -75,6 +77,7 @@ class SyncThread(private val syncRequest: SyncRequest,
cancelableRequest = syncRequest.execute(nextBatch, object : MatrixCallback<SyncResponse> {
override fun onSuccess(data: SyncResponse) {
nextBatch = data.nextBatch
syncTokenStore.saveToken(nextBatch)
latch.countDown()
}