Merge pull request #2460 from Dominaezzz/suspend_functions

Convert TermsService to suspend functions
This commit is contained in:
Benoit Marty 2020-11-30 10:47:55 +01:00 committed by GitHub
commit abcb02d4aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 45 deletions

View file

@ -16,22 +16,16 @@
package org.matrix.android.sdk.api.session.terms
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.util.Cancelable
interface TermsService {
enum class ServiceType {
IntegrationManager,
IdentityService
}
fun getTerms(serviceType: ServiceType,
baseUrl: String,
callback: MatrixCallback<GetTermsResponse>): Cancelable
suspend fun getTerms(serviceType: ServiceType, baseUrl: String): GetTermsResponse
fun agreeToTerms(serviceType: ServiceType,
baseUrl: String,
agreedUrls: List<String>,
token: String?,
callback: MatrixCallback<Unit>): Cancelable
suspend fun agreeToTerms(serviceType: ServiceType,
baseUrl: String,
agreedUrls: List<String>,
token: String?)
}

View file

@ -17,11 +17,10 @@
package org.matrix.android.sdk.internal.session.terms
import dagger.Lazy
import org.matrix.android.sdk.api.MatrixCallback
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.terms.GetTermsResponse
import org.matrix.android.sdk.api.session.terms.TermsService
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.di.UnauthenticatedWithCertificate
import org.matrix.android.sdk.internal.network.NetworkConstants
import org.matrix.android.sdk.internal.network.RetrofitFactory
@ -33,8 +32,6 @@ import org.matrix.android.sdk.internal.session.sync.model.accountdata.AcceptedTe
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
import org.matrix.android.sdk.internal.session.user.accountdata.AccountDataDataSource
import org.matrix.android.sdk.internal.session.user.accountdata.UpdateUserAccountDataTask
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.launchToCallback
import org.matrix.android.sdk.internal.util.MatrixCoroutineDispatchers
import org.matrix.android.sdk.internal.util.ensureTrailingSlash
import okhttp3.OkHttpClient
@ -49,13 +46,11 @@ internal class DefaultTermsService @Inject constructor(
private val getOpenIdTokenTask: GetOpenIdTokenTask,
private val identityRegisterTask: IdentityRegisterTask,
private val updateUserAccountDataTask: UpdateUserAccountDataTask,
private val coroutineDispatchers: MatrixCoroutineDispatchers,
private val taskExecutor: TaskExecutor
private val coroutineDispatchers: MatrixCoroutineDispatchers
) : TermsService {
override fun getTerms(serviceType: TermsService.ServiceType,
baseUrl: String,
callback: MatrixCallback<GetTermsResponse>): Cancelable {
return taskExecutor.executorScope.launchToCallback(coroutineDispatchers.main, callback) {
override suspend fun getTerms(serviceType: TermsService.ServiceType,
baseUrl: String): GetTermsResponse {
return withContext(coroutineDispatchers.main) {
val url = buildUrl(baseUrl, serviceType)
val termsResponse = executeRequest<TermsResponse>(null) {
apiCall = termsAPI.getTerms("${url}terms")
@ -64,12 +59,11 @@ internal class DefaultTermsService @Inject constructor(
}
}
override fun agreeToTerms(serviceType: TermsService.ServiceType,
baseUrl: String,
agreedUrls: List<String>,
token: String?,
callback: MatrixCallback<Unit>): Cancelable {
return taskExecutor.executorScope.launchToCallback(coroutineDispatchers.main, callback) {
override suspend fun agreeToTerms(serviceType: TermsService.ServiceType,
baseUrl: String,
agreedUrls: List<String>,
token: String?) {
withContext(coroutineDispatchers.main) {
val url = buildUrl(baseUrl, serviceType)
val tokenToUse = token?.takeIf { it.isNotEmpty() } ?: getToken(baseUrl)

View file

@ -31,7 +31,6 @@ import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.identity.IdentityServiceError
import org.matrix.android.sdk.api.session.terms.GetTermsResponse
import org.matrix.android.sdk.api.session.terms.TermsService
import org.matrix.android.sdk.internal.util.awaitCallback
import java.net.UnknownHostException
@ -117,9 +116,7 @@ class SetIdentityServerViewModel @AssistedInject constructor(
private suspend fun checkTerms(baseUrl: String) {
try {
val data = awaitCallback<GetTermsResponse> {
mxSession.getTerms(TermsService.ServiceType.IdentityService, baseUrl, it)
}
val data = mxSession.getTerms(TermsService.ServiceType.IdentityService, baseUrl)
// has all been accepted?
val resp = data.serverResponse

View file

@ -28,8 +28,6 @@ import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.platform.VectorViewModel
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.terms.GetTermsResponse
import org.matrix.android.sdk.internal.util.awaitCallback
import timber.log.Timber
class ReviewTermsViewModel @AssistedInject constructor(
@ -94,15 +92,12 @@ class ReviewTermsViewModel @AssistedInject constructor(
viewModelScope.launch {
try {
awaitCallback<Unit> {
session.agreeToTerms(
termsArgs.type,
termsArgs.baseURL,
agreedUrls,
termsArgs.token,
it
)
}
session.agreeToTerms(
termsArgs.type,
termsArgs.baseURL,
agreedUrls,
termsArgs.token
)
_viewEvents.post(ReviewTermsViewEvents.Success)
} catch (failure: Throwable) {
Timber.e(failure, "Failed to agree to terms")
@ -122,9 +117,7 @@ class ReviewTermsViewModel @AssistedInject constructor(
viewModelScope.launch {
try {
val data = awaitCallback<GetTermsResponse> {
session.getTerms(termsArgs.type, termsArgs.baseURL, it)
}
val data = session.getTerms(termsArgs.type, termsArgs.baseURL)
val terms = data.serverResponse.getLocalizedTerms(action.preferredLanguageCode).map {
Term(it.localizedUrl ?: "",
it.localizedName ?: "",