From 9a358c42c5748e376c814023f75ced59ab009e63 Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Wed, 27 Dec 2023 11:52:41 +0530 Subject: [PATCH] fix: handle exceptions while fetching contributors list --- .../androidide/contributors/Contributors.kt | 66 +++++++++++++++++++ .../contributors/CrowdinTranslator.kt | 28 ++------ .../contributors/GitHubContributors.kt | 29 ++------ .../viewmodel/ContributorsViewModel.kt | 6 -- 4 files changed, 74 insertions(+), 55 deletions(-) create mode 100644 app/src/main/java/com/itsaky/androidide/contributors/Contributors.kt diff --git a/app/src/main/java/com/itsaky/androidide/contributors/Contributors.kt b/app/src/main/java/com/itsaky/androidide/contributors/Contributors.kt new file mode 100644 index 000000000..ef97d7d04 --- /dev/null +++ b/app/src/main/java/com/itsaky/androidide/contributors/Contributors.kt @@ -0,0 +1,66 @@ +/* + * This file is part of AndroidIDE. + * + * AndroidIDE is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AndroidIDE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AndroidIDE. If not, see . + */ + +package com.itsaky.androidide.contributors + +import com.itsaky.androidide.utils.ILogger +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import retrofit2.Call +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory + +/** + * @author Akash Yadav + */ +internal object Contributors { + + val log: ILogger = ILogger.newInstance("Contributors") + + @JvmStatic + suspend inline fun getAllContributors( + baseUrl: String, + crossinline action: (Service) -> Call> + ): List { + val retrofit = Retrofit.Builder() + .baseUrl(baseUrl) + .addConverterFactory(GsonConverterFactory.create()) + .build() + + val service = retrofit.create(Service::class.java) + return withContext(Dispatchers.IO) { + try { + val response = action(service).execute() + if (!response.isSuccessful) { + log.error( + "Failed to get contributors list [${Model::class.java.name}], request unsuccessful", + response.errorBody()?.string() ?: "(empty error response)" + ) + return@withContext emptyList() + } + + return@withContext response.body() ?: run { + log.error("Response body is null") + emptyList() + } + } catch (err: Throwable) { + err.printStackTrace() + emptyList() + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/itsaky/androidide/contributors/CrowdinTranslator.kt b/app/src/main/java/com/itsaky/androidide/contributors/CrowdinTranslator.kt index a9f09dd43..14cb66217 100644 --- a/app/src/main/java/com/itsaky/androidide/contributors/CrowdinTranslator.kt +++ b/app/src/main/java/com/itsaky/androidide/contributors/CrowdinTranslator.kt @@ -18,12 +18,7 @@ package com.itsaky.androidide.contributors import com.google.gson.annotations.SerializedName -import com.itsaky.androidide.utils.ILogger -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext import retrofit2.Call -import retrofit2.Retrofit -import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.GET private interface CrowdinTranslatorsService { @@ -35,30 +30,15 @@ private interface CrowdinTranslatorsService { object CrowdinTranslators { internal const val CROWDIN_CONTRIBUTORS_JSON = "crowdin-contributors.json" - private val log = ILogger.newInstance("CrowdinTranslators") /** * Get all Crowdin translators. */ suspend fun getAllTranslators(): List { - val retrofit = Retrofit.Builder() - .baseUrl("${GITHUB_RAW_API_REPO_BRANCH_URL}/") - .addConverterFactory(GsonConverterFactory.create()) - .build() - - val service = retrofit.create(CrowdinTranslatorsService::class.java) - val response = withContext(Dispatchers.IO) { service.getAllTranslators().execute() } - - if (!response.isSuccessful) { - log.error("Failed to get Crowdin translators list, request unsuccessful", - response.errorBody()?.string() ?: "(empty error response)") - return emptyList() - } - - return response.body() ?: run { - log.error("Response body is null") - emptyList() - } + return Contributors.getAllContributors( + "${GITHUB_RAW_API_REPO_BRANCH_URL}/", + CrowdinTranslatorsService::getAllTranslators + ) } } diff --git a/app/src/main/java/com/itsaky/androidide/contributors/GitHubContributors.kt b/app/src/main/java/com/itsaky/androidide/contributors/GitHubContributors.kt index 44011e8d5..a81ea72ba 100644 --- a/app/src/main/java/com/itsaky/androidide/contributors/GitHubContributors.kt +++ b/app/src/main/java/com/itsaky/androidide/contributors/GitHubContributors.kt @@ -18,12 +18,7 @@ package com.itsaky.androidide.contributors import com.google.gson.annotations.SerializedName -import com.itsaky.androidide.utils.ILogger -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext import retrofit2.Call -import retrofit2.Retrofit -import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.GET /** @@ -37,30 +32,14 @@ interface GitHubContributorsService { object GitHubContributors { - private val log = ILogger.newInstance("GitHubContributors") - /** * Get all GitHub contributors. */ suspend fun getAllContributors(): List { - val retrofit = Retrofit.Builder() - .baseUrl("${GITHUB_API_REPO_URL}/") - .addConverterFactory(GsonConverterFactory.create()) - .build() - - val service = retrofit.create(GitHubContributorsService::class.java) - val response = withContext(Dispatchers.IO) { service.getAllContributors().execute() } - - if (!response.isSuccessful) { - log.error("Failed to get GitHub contributors list, request unsuccessful", - response.errorBody()?.string() ?: "(empty error response)") - return emptyList() - } - - return response.body() ?: run { - log.error("Response body is null") - emptyList() - } + return Contributors.getAllContributors( + "${GITHUB_API_REPO_URL}/", + GitHubContributorsService::getAllContributors + ) } } diff --git a/app/src/main/java/com/itsaky/androidide/viewmodel/ContributorsViewModel.kt b/app/src/main/java/com/itsaky/androidide/viewmodel/ContributorsViewModel.kt index 05dc498dd..3eabd583b 100644 --- a/app/src/main/java/com/itsaky/androidide/viewmodel/ContributorsViewModel.kt +++ b/app/src/main/java/com/itsaky/androidide/viewmodel/ContributorsViewModel.kt @@ -41,12 +41,6 @@ class ContributorsViewModel : ViewModel() { private const val CONTRIBUTORS_MAX_SIZE = 30 } - val crowdinTranslators: List - get() = _crowdinTranslators.value ?: emptyList() - - val githubContributors: List - get() = _githubContributors.value ?: emptyList() - fun fetchCrowdinTranslators() { viewModelScope.launch(Dispatchers.Default) { val translators = CrowdinTranslators.getAllTranslators()