Remove RequestExecutor as name is confusing at the moment.

This commit is contained in:
ganfra 2018-12-11 18:13:22 +01:00
parent 43a462f9cc
commit 17dcbc1cd6
3 changed files with 5 additions and 40 deletions

View File

@ -1,34 +0,0 @@
package im.vector.matrix.android.internal.network

import arrow.core.Try
import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers
import im.vector.matrix.android.internal.util.retry
import kotlinx.coroutines.withContext

internal class RequestExecutor(private val networkConnectivityChecker: NetworkConnectivityChecker,
private val coroutineDispatchers: MatrixCoroutineDispatchers) {


suspend fun <T> execute(retryTimes: Int = Int.MAX_VALUE,
initialDelay: Long = 100,
maxDelay: Long = 10_000,
factor: Double = 2.0,
block: suspend () -> Try<T>): Try<T> = withContext(coroutineDispatchers.io) {

retry(retryTimes, initialDelay, maxDelay, factor) {
executeIfConnected { block() }
}

}

private suspend fun <T> executeIfConnected(block: suspend () -> Try<T>): Try<T> {
return if (networkConnectivityChecker.isConnected()) {
block()
} else {
Try.raise(Failure.NetworkConnection())
}
}


}

View File

@ -9,13 +9,13 @@ import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.util.Cancelable import im.vector.matrix.android.api.util.Cancelable
import im.vector.matrix.android.internal.database.model.GroupSummaryEntity import im.vector.matrix.android.internal.database.model.GroupSummaryEntity
import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.network.RequestExecutor
import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.session.group.model.GroupRooms import im.vector.matrix.android.internal.session.group.model.GroupRooms
import im.vector.matrix.android.internal.session.group.model.GroupSummaryResponse import im.vector.matrix.android.internal.session.group.model.GroupSummaryResponse
import im.vector.matrix.android.internal.session.group.model.GroupUsers import im.vector.matrix.android.internal.session.group.model.GroupUsers
import im.vector.matrix.android.internal.util.CancelableCoroutine import im.vector.matrix.android.internal.util.CancelableCoroutine
import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers
import im.vector.matrix.android.internal.util.retry
import im.vector.matrix.android.internal.util.tryTransactionSync import im.vector.matrix.android.internal.util.tryTransactionSync
import io.realm.kotlin.createObject import io.realm.kotlin.createObject
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
@ -24,15 +24,14 @@ import kotlinx.coroutines.launch
internal class GetGroupDataRequest( internal class GetGroupDataRequest(
private val groupAPI: GroupAPI, private val groupAPI: GroupAPI,
private val monarchy: Monarchy, private val monarchy: Monarchy,
private val coroutineDispatchers: MatrixCoroutineDispatchers, private val coroutineDispatchers: MatrixCoroutineDispatchers
private val requestExecutor: RequestExecutor
) { ) {


fun execute(groupId: String, fun execute(groupId: String,
callback: MatrixCallback<Boolean> callback: MatrixCallback<Boolean>
): Cancelable { ): Cancelable {
val job = GlobalScope.launch(coroutineDispatchers.main) { val job = GlobalScope.launch(coroutineDispatchers.main) {
val groupOrFailure = requestExecutor.execute { getGroupData(groupId) } val groupOrFailure = retry { getGroupData(groupId) }
groupOrFailure.fold({ callback.onFailure(it) }, { callback.onSuccess(true) }) groupOrFailure.fold({ callback.onFailure(it) }, { callback.onSuccess(true) })
} }
return CancelableCoroutine(job) return CancelableCoroutine(job)
@ -64,7 +63,7 @@ internal class GetGroupDataRequest(
return monarchy return monarchy
.tryTransactionSync { realm -> .tryTransactionSync { realm ->
val groupSummaryEntity = GroupSummaryEntity.where(realm, groupId).findFirst() val groupSummaryEntity = GroupSummaryEntity.where(realm, groupId).findFirst()
?: realm.createObject(groupId) ?: realm.createObject(groupId)


groupSummaryEntity.avatarUrl = groupSummary.profile?.avatarUrl ?: "" groupSummaryEntity.avatarUrl = groupSummary.profile?.avatarUrl ?: ""
val name = groupSummary.profile?.name val name = groupSummary.profile?.name

View File

@ -16,7 +16,7 @@ class GroupModule : Module {
} }


scope(DefaultSession.SCOPE) { scope(DefaultSession.SCOPE) {
GetGroupDataRequest(get(), get(), get(), get()) GetGroupDataRequest(get(), get(), get())
} }


}.invoke() }.invoke()