fetch available instance from tokhmi as fallback, otherwise use the hardcoded instances

This commit is contained in:
Bnyro 2022-12-20 17:24:53 +01:00
parent b799bbefd1
commit 7f2f012a1c
4 changed files with 23 additions and 16 deletions

View File

@ -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<com.github.libretube.api.obj.Instances>
@GET
suspend fun getInstances(@Url url: String): List<Instances>
// fetch latest version info
@GET(GITHUB_API_URL)

View File

@ -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

View File

@ -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<String>()
val instanceValues = arrayListOf<String>()
// 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

View File

@ -417,6 +417,7 @@
<string name="play_latest_videos">Play latest videos</string>
<string name="nothing_selected">Nothing selected!</string>
<string name="color_violet">Versatile Violet</string>
<string name="failed_fetching_instances">Failed to fetch available instances.</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>