diff --git a/app/build.gradle b/app/build.gradle index 76efe0d6cd..a16fe60af9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -114,7 +114,6 @@ dependencies { testImplementation "org.junit.jupiter:junit-jupiter-api:5.10.0" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.10.0" testImplementation 'com.facebook.soloader:soloader:0.10.5' - implementation "org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.9.0" testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3" debugImplementation("androidx.fragment:fragment-testing:1.6.2") testImplementation "commons-io:commons-io:2.6" diff --git a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailViewModel.kt b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailViewModel.kt index 1b485b42e0..6c4f180831 100644 --- a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailViewModel.kt +++ b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailViewModel.kt @@ -13,7 +13,6 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch -import kotlinx.coroutines.rx2.awaitSingle import timber.log.Timber import javax.inject.Inject @@ -39,7 +38,7 @@ class MediaDetailViewModel( try { val result = - okHttpJsonApiClient.getFileUsagesOnCommons(fileName, 10).awaitSingle() + okHttpJsonApiClient.getFileUsagesOnCommons(fileName, 10) val data = result?.query?.pages?.first()?.fileUsage?.map { it.toUiModel() } @@ -69,7 +68,7 @@ class MediaDetailViewModel( _globalContainerState.update { FileUsagesContainerState.Loading } try { - val result = okHttpJsonApiClient.getGlobalFileUsages(fileName, 10).awaitSingle() + val result = okHttpJsonApiClient.getGlobalFileUsages(fileName, 10) val data = result?.query?.pages?.first()?.fileUsage?.map { it.toUiModel() } diff --git a/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.kt b/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.kt index 97ecfb6dc2..a0d56d2de9 100644 --- a/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.kt +++ b/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.kt @@ -23,6 +23,8 @@ import fr.free.nrw.commons.utils.ConfigUtils.isBetaFlavour import fr.free.nrw.commons.wikidata.model.GetWikidataEditCountResponse import io.reactivex.Observable import io.reactivex.Single +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import okhttp3.HttpUrl import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.OkHttpClient @@ -53,8 +55,10 @@ class OkHttpJsonApiClient @Inject constructor( ): Observable { val fetchLeaderboardUrlTemplate = wikiMediaToolforgeUrl.toString() + LeaderboardConstants.LEADERBOARD_END_POINT - val url = String.format(Locale.ENGLISH, - fetchLeaderboardUrlTemplate, userName, duration, category, limit, offset) + val url = String.format( + Locale.ENGLISH, + fetchLeaderboardUrlTemplate, userName, duration, category, limit, offset + ) val urlBuilder: HttpUrl.Builder = url.toHttpUrlOrNull()!!.newBuilder() .addQueryParameter("user", userName) .addQueryParameter("duration", duration) @@ -83,66 +87,72 @@ class OkHttpJsonApiClient @Inject constructor( }) } - fun getFileUsagesOnCommons( + suspend fun getFileUsagesOnCommons( fileName: String?, pageSize: Int - ): Observable { - val urlBuilder = BuildConfig.FILE_USAGES_BASE_URL.toHttpUrlOrNull()!!.newBuilder() - urlBuilder.addQueryParameter("prop", "fileusage") - urlBuilder.addQueryParameter("titles", fileName) - urlBuilder.addQueryParameter("fulimit", pageSize.toString()) + ): FileUsagesResponse? { + return withContext(Dispatchers.IO) { - Timber.i("Url %s", urlBuilder.toString()) - val request: Request = Request.Builder() - .url(urlBuilder.toString()) - .build() + return@withContext try { - return Observable.fromCallable({ - val response: Response = okHttpClient.newCall(request).execute() - if (response.body != null && response.isSuccessful) { - val json: String = response.body!!.string() - try { - return@fromCallable gson.fromJson( + val urlBuilder = BuildConfig.FILE_USAGES_BASE_URL.toHttpUrlOrNull()!!.newBuilder() + urlBuilder.addQueryParameter("prop", "fileusage") + urlBuilder.addQueryParameter("titles", fileName) + urlBuilder.addQueryParameter("fulimit", pageSize.toString()) + + Timber.i("Url %s", urlBuilder.toString()) + val request: Request = Request.Builder() + .url(urlBuilder.toString()) + .build() + + val response: Response = okHttpClient.newCall(request).execute() + if (response.body != null && response.isSuccessful) { + val json: String = response.body!!.string() + gson.fromJson( json, FileUsagesResponse::class.java ) - } catch (e: java.lang.Exception) { - return@fromCallable null - } + } else null + } catch (e: Exception) { + Timber.e(e) + null } - null - }) + } } - fun getGlobalFileUsages( + suspend fun getGlobalFileUsages( fileName: String?, pageSize: Int - ): Observable { - val urlBuilder = BuildConfig.FILE_USAGES_BASE_URL.toHttpUrlOrNull()!!.newBuilder() - urlBuilder.addQueryParameter("prop", "globalusage") - urlBuilder.addQueryParameter("titles", fileName) - urlBuilder.addQueryParameter("gulimit", pageSize.toString()) + ): GlobalFileUsagesResponse? { - Timber.i("Url %s", urlBuilder.toString()) - val request: Request = Request.Builder() - .url(urlBuilder.toString()) - .build() + return withContext(Dispatchers.IO) { - return Observable.fromCallable({ - val response: Response = okHttpClient.newCall(request).execute() - if (response.body != null && response.isSuccessful) { - val json: String = response.body!!.string() - try { - return@fromCallable gson.fromJson( + return@withContext try { + + val urlBuilder = BuildConfig.FILE_USAGES_BASE_URL.toHttpUrlOrNull()!!.newBuilder() + urlBuilder.addQueryParameter("prop", "globalusage") + urlBuilder.addQueryParameter("titles", fileName) + urlBuilder.addQueryParameter("gulimit", pageSize.toString()) + + Timber.i("Url %s", urlBuilder.toString()) + val request: Request = Request.Builder() + .url(urlBuilder.toString()) + .build() + + val response: Response = okHttpClient.newCall(request).execute() + if (response.body != null && response.isSuccessful) { + val json: String = response.body!!.string() + + gson.fromJson( json, GlobalFileUsagesResponse::class.java ) - } catch (e: java.lang.Exception) { - return@fromCallable null - } + } else null + } catch (e: Exception) { + Timber.e(e) + null } - null - }) + } } fun setAvatar(username: String?, avatar: String?): Single {