Sync : save token
This commit is contained in:
parent
44eb838610
commit
d9f1d3fc85
@ -30,7 +30,11 @@ class SyncModule : Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
scope(DefaultSession.SCOPE) {
|
scope(DefaultSession.SCOPE) {
|
||||||
SyncThread(get(), get())
|
SyncTokenStore(get())
|
||||||
|
}
|
||||||
|
|
||||||
|
scope(DefaultSession.SCOPE) {
|
||||||
|
SyncThread(get(), get(), get())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ class SyncRequest(private val syncAPI: SyncAPI,
|
|||||||
Failure.Unknown(RuntimeException("Sync response shouln't be null"))
|
Failure.Unknown(RuntimeException("Sync response shouln't be null"))
|
||||||
}.flatMap {
|
}.flatMap {
|
||||||
try {
|
try {
|
||||||
syncResponseHandler.handleResponse(it, null, false)
|
syncResponseHandler.handleResponse(it, token, false)
|
||||||
Either.right(it)
|
Either.right(it)
|
||||||
} catch (exception: Exception) {
|
} catch (exception: Exception) {
|
||||||
Either.Left(Failure.Unknown(exception))
|
Either.Left(Failure.Unknown(exception))
|
||||||
|
@ -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()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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.failure.Failure
|
||||||
import im.vector.matrix.android.api.util.Cancelable
|
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.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.events.sync.data.SyncResponse
|
||||||
import im.vector.matrix.android.internal.network.NetworkConnectivityChecker
|
import im.vector.matrix.android.internal.network.NetworkConnectivityChecker
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
@ -12,7 +13,8 @@ import java.util.concurrent.CountDownLatch
|
|||||||
private const val RETRY_WAIT_TIME_MS = 10_000L
|
private const val RETRY_WAIT_TIME_MS = 10_000L
|
||||||
|
|
||||||
class SyncThread(private val syncRequest: SyncRequest,
|
class SyncThread(private val syncRequest: SyncRequest,
|
||||||
private val networkConnectivityChecker: NetworkConnectivityChecker
|
private val networkConnectivityChecker: NetworkConnectivityChecker,
|
||||||
|
private val syncTokenStore: SyncTokenStore
|
||||||
) : Thread(), NetworkConnectivityChecker.Listener {
|
) : Thread(), NetworkConnectivityChecker.Listener {
|
||||||
|
|
||||||
enum class State {
|
enum class State {
|
||||||
@ -25,7 +27,7 @@ class SyncThread(private val syncRequest: SyncRequest,
|
|||||||
|
|
||||||
private var state: State = State.IDLE
|
private var state: State = State.IDLE
|
||||||
private val lock = Object()
|
private val lock = Object()
|
||||||
private var nextBatch: String? = null
|
private var nextBatch = syncTokenStore.getLastToken()
|
||||||
private var cancelableRequest: Cancelable? = null
|
private var cancelableRequest: Cancelable? = null
|
||||||
|
|
||||||
fun restart() {
|
fun restart() {
|
||||||
@ -75,6 +77,7 @@ class SyncThread(private val syncRequest: SyncRequest,
|
|||||||
cancelableRequest = syncRequest.execute(nextBatch, object : MatrixCallback<SyncResponse> {
|
cancelableRequest = syncRequest.execute(nextBatch, object : MatrixCallback<SyncResponse> {
|
||||||
override fun onSuccess(data: SyncResponse) {
|
override fun onSuccess(data: SyncResponse) {
|
||||||
nextBatch = data.nextBatch
|
nextBatch = data.nextBatch
|
||||||
|
syncTokenStore.saveToken(nextBatch)
|
||||||
latch.countDown()
|
latch.countDown()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user