Merge pull request #2406 from Dominaezzz/suspend_functions

Convert RawService to suspend functions
This commit is contained in:
Benoit Marty 2020-11-16 15:47:50 +01:00 committed by GitHub
commit 2b1b9d35a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 33 deletions

View File

@ -16,9 +16,6 @@
package org.matrix.android.sdk.api.raw package org.matrix.android.sdk.api.raw
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.util.Cancelable
/** /**
* Useful methods to fetch raw data from the server. The access token will not be used to fetched the data * Useful methods to fetch raw data from the server. The access token will not be used to fetched the data
*/ */
@ -26,17 +23,15 @@ interface RawService {
/** /**
* Get a URL, either from cache or from the remote server, depending on the cache strategy * Get a URL, either from cache or from the remote server, depending on the cache strategy
*/ */
fun getUrl(url: String, suspend fun getUrl(url: String, rawCacheStrategy: RawCacheStrategy): String
rawCacheStrategy: RawCacheStrategy,
matrixCallback: MatrixCallback<String>): Cancelable
/** /**
* Specific case for the well-known file. Cache validity is 8 hours * Specific case for the well-known file. Cache validity is 8 hours
*/ */
fun getWellknown(userId: String, matrixCallback: MatrixCallback<String>): Cancelable suspend fun getWellknown(userId: String): String
/** /**
* Clear all the cache data * Clear all the cache data
*/ */
fun clearCache(matrixCallback: MatrixCallback<Unit>): Cancelable suspend fun clearCache()
} }

View File

@ -16,45 +16,28 @@
package org.matrix.android.sdk.internal.raw package org.matrix.android.sdk.internal.raw
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.raw.RawCacheStrategy import org.matrix.android.sdk.api.raw.RawCacheStrategy
import org.matrix.android.sdk.api.raw.RawService import org.matrix.android.sdk.api.raw.RawService
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.configureWith
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
internal class DefaultRawService @Inject constructor( internal class DefaultRawService @Inject constructor(
private val taskExecutor: TaskExecutor,
private val getUrlTask: GetUrlTask, private val getUrlTask: GetUrlTask,
private val cleanRawCacheTask: CleanRawCacheTask private val cleanRawCacheTask: CleanRawCacheTask
) : RawService { ) : RawService {
override fun getUrl(url: String, override suspend fun getUrl(url: String, rawCacheStrategy: RawCacheStrategy): String {
rawCacheStrategy: RawCacheStrategy, return getUrlTask.execute(GetUrlTask.Params(url, rawCacheStrategy))
matrixCallback: MatrixCallback<String>): Cancelable {
return getUrlTask
.configureWith(GetUrlTask.Params(url, rawCacheStrategy)) {
callback = matrixCallback
}
.executeBy(taskExecutor)
} }
override fun getWellknown(userId: String, override suspend fun getWellknown(userId: String): String {
matrixCallback: MatrixCallback<String>): Cancelable {
val homeServerDomain = userId.substringAfter(":") val homeServerDomain = userId.substringAfter(":")
return getUrl( return getUrl(
"https://$homeServerDomain/.well-known/matrix/client", "https://$homeServerDomain/.well-known/matrix/client",
RawCacheStrategy.TtlCache(TimeUnit.HOURS.toMillis(8), false), RawCacheStrategy.TtlCache(TimeUnit.HOURS.toMillis(8), false)
matrixCallback
) )
} }
override fun clearCache(matrixCallback: MatrixCallback<Unit>): Cancelable { override suspend fun clearCache() {
return cleanRawCacheTask cleanRawCacheTask.execute(Unit)
.configureWith(Unit) {
callback = matrixCallback
}
.executeBy(taskExecutor)
} }
} }

View File

@ -18,10 +18,9 @@ package im.vector.app.features.raw.wellknown
import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.raw.RawService import org.matrix.android.sdk.api.raw.RawService
import org.matrix.android.sdk.internal.util.awaitCallback
suspend fun RawService.getElementWellknown(userId: String): ElementWellKnown? { suspend fun RawService.getElementWellknown(userId: String): ElementWellKnown? {
return tryOrNull { awaitCallback<String> { getWellknown(userId, it) } } return tryOrNull { getWellknown(userId) }
?.let { ElementWellKnownMapper.from(it) } ?.let { ElementWellKnownMapper.from(it) }
} }