diff --git a/app/src/main/java/com/github/libretube/api/ExternalApi.kt b/app/src/main/java/com/github/libretube/api/ExternalApi.kt index 196ff53cb..d3ba49f7a 100644 --- a/app/src/main/java/com/github/libretube/api/ExternalApi.kt +++ b/app/src/main/java/com/github/libretube/api/ExternalApi.kt @@ -1,14 +1,15 @@ package com.github.libretube.api +import com.github.libretube.api.obj.Instances import com.github.libretube.constants.GITHUB_API_URL -import com.github.libretube.constants.PIPED_INSTANCES_URL import com.github.libretube.obj.update.UpdateInfo import retrofit2.http.GET +import retrofit2.http.Url interface ExternalApi { // only for fetching servers list - @GET(PIPED_INSTANCES_URL) - suspend fun getInstances(): List + @GET + suspend fun getInstances(@Url url: String): List // fetch latest version info @GET(GITHUB_API_URL) diff --git a/app/src/main/java/com/github/libretube/constants/Constants.kt b/app/src/main/java/com/github/libretube/constants/Constants.kt index 4019f5d69..25da67196 100644 --- a/app/src/main/java/com/github/libretube/constants/Constants.kt +++ b/app/src/main/java/com/github/libretube/constants/Constants.kt @@ -34,6 +34,7 @@ const val YOUTUBE_FRONTEND_URL = "https://www.youtube.com" */ const val PIPED_API_URL = "https://pipedapi.kavin.rocks/" const val PIPED_INSTANCES_URL = "https://piped-instances.kavin.rocks/" +const val FALLBACK_INSTANCES_URL = "https://instances.tokhmi.xyz" /** * Notification IDs diff --git a/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt index ba18f4b13..a7931a840 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt @@ -8,9 +8,12 @@ import androidx.preference.Preference import androidx.preference.SwitchPreferenceCompat import com.github.libretube.R import com.github.libretube.api.RetrofitInstance +import com.github.libretube.constants.FALLBACK_INSTANCES_URL +import com.github.libretube.constants.PIPED_INSTANCES_URL import com.github.libretube.constants.PreferenceKeys import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.extensions.awaitQuery +import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.dialogs.CustomInstanceDialog import com.github.libretube.ui.dialogs.DeleteAccountDialog @@ -110,6 +113,7 @@ class InstanceSettings : BasePreferenceFragment() { } private fun initCustomInstances(instancePref: ListPreference) { + val appContext = requireContext().applicationContext lifecycleScope.launchWhenCreated { val customInstances = awaitQuery { Database.customInstanceDao().getAll() @@ -118,23 +122,23 @@ class InstanceSettings : BasePreferenceFragment() { val instanceNames = arrayListOf() val instanceValues = arrayListOf() - // fetch official public instances + // fetch official public instances from kavin.rocks as well as tokhmi.xyz as fallback + val response = runCatching { + RetrofitInstance.externalApi.getInstances(PIPED_INSTANCES_URL).toMutableList() + }.getOrNull() ?: runCatching { + RetrofitInstance.externalApi.getInstances(FALLBACK_INSTANCES_URL).toMutableList() + }.getOrNull() - val response = try { - RetrofitInstance.externalApi.getInstances().toMutableList() - } catch (e: Exception) { - e.printStackTrace() - mutableListOf() + if (response == null) { + appContext.toastFromMainThread(R.string.failed_fetching_instances) + instanceNames.addAll(resources.getStringArray(R.array.instances)) + instanceValues.addAll(resources.getStringArray(R.array.instancesValue)) } - response.sortBy { it.name } + response?.sortBy { it.name } - response.forEach { - if (it.name != null && it.api_url != null) { - instanceNames += it.name!! - instanceValues += it.api_url!! - } - } + instanceNames.addAll(response.orEmpty().map { it.name ?: "" }) + instanceValues.addAll(response.orEmpty().map { it.api_url ?: "" }) customInstances.forEach { instance -> instanceNames += instance.name diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bbd9eeff2..9979b3d99 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -417,6 +417,7 @@ Play latest videos Nothing selected! Versatile Violet + Failed to fetch available instances. Download Service