mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 15:30:31 +05:30
Improve the dynamic instance list fetching
This commit is contained in:
parent
cd54f2beb0
commit
cd21f64be2
@ -7,10 +7,10 @@ import kotlinx.serialization.Serializable
|
||||
data class Instances(
|
||||
val name: String,
|
||||
@SerialName("api_url") val apiUrl: String,
|
||||
val locations: String,
|
||||
val version: String,
|
||||
@SerialName("up_to_date") val upToDate: Boolean,
|
||||
val cdn: Boolean,
|
||||
val registered: Long,
|
||||
@SerialName("last_checked") val lastChecked: Long
|
||||
val locations: String = "",
|
||||
val version: String = "",
|
||||
@SerialName("up_to_date") val upToDate: Boolean = true,
|
||||
val cdn: Boolean = false,
|
||||
val registered: Long = 0,
|
||||
@SerialName("last_checked") val lastChecked: Long = 0
|
||||
)
|
||||
|
@ -9,6 +9,7 @@ import androidx.preference.Preference
|
||||
import androidx.preference.SwitchPreferenceCompat
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.api.RetrofitInstance
|
||||
import com.github.libretube.api.obj.Instances
|
||||
import com.github.libretube.constants.FALLBACK_INSTANCES_URL
|
||||
import com.github.libretube.constants.PIPED_INSTANCES_URL
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
@ -30,10 +31,9 @@ class InstanceSettings : BasePreferenceFragment() {
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.instance_settings, rootKey)
|
||||
|
||||
val instance = findPreference<ListPreference>(PreferenceKeys.FETCH_INSTANCE)
|
||||
// fetchInstance()
|
||||
initCustomInstances(instance!!)
|
||||
instance.setOnPreferenceChangeListener { _, newValue ->
|
||||
val instancePref = findPreference<ListPreference>(PreferenceKeys.FETCH_INSTANCE)!!
|
||||
initCustomInstances(instancePref)
|
||||
instancePref.setOnPreferenceChangeListener { _, newValue ->
|
||||
RetrofitInstance.url = newValue.toString()
|
||||
if (!PreferenceHelper.getBoolean(PreferenceKeys.AUTH_INSTANCE_TOGGLE, false)) {
|
||||
RetrofitInstance.authUrl = newValue.toString()
|
||||
@ -122,36 +122,29 @@ class InstanceSettings : BasePreferenceFragment() {
|
||||
Database.customInstanceDao().getAll()
|
||||
}
|
||||
|
||||
val instanceNames = arrayListOf<String>()
|
||||
val instanceValues = arrayListOf<String>()
|
||||
|
||||
// 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()
|
||||
|
||||
if (response == null) {
|
||||
appContext.toastFromMainThread(R.string.failed_fetching_instances)
|
||||
instanceNames.addAll(resources.getStringArray(R.array.instances))
|
||||
instanceValues.addAll(resources.getStringArray(R.array.instancesValue))
|
||||
val instances = withContext(Dispatchers.IO) {
|
||||
runCatching {
|
||||
RetrofitInstance.externalApi.getInstances(PIPED_INSTANCES_URL).toMutableList()
|
||||
}.getOrNull() ?: runCatching {
|
||||
RetrofitInstance.externalApi.getInstances(FALLBACK_INSTANCES_URL).toMutableList()
|
||||
}.getOrNull() ?: run {
|
||||
appContext.toastFromMainThread(R.string.failed_fetching_instances)
|
||||
val instanceNames = resources.getStringArray(R.array.instances)
|
||||
resources.getStringArray(R.array.instancesValue).mapIndexed { index, instanceValue ->
|
||||
Instances(instanceNames[index], instanceValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
.sortedBy { it.name }
|
||||
.toMutableList()
|
||||
|
||||
response?.sortBy { it.name }
|
||||
|
||||
instanceNames.addAll(response.orEmpty().map { it.name })
|
||||
instanceValues.addAll(response.orEmpty().map { it.apiUrl })
|
||||
|
||||
customInstances.forEach { instance ->
|
||||
instanceNames += instance.name
|
||||
instanceValues += instance.apiUrl
|
||||
}
|
||||
instances.addAll(customInstances.map { Instances(it.name, it.apiUrl) })
|
||||
|
||||
runOnUiThread {
|
||||
// add custom instances to the list preference
|
||||
instancePref.entries = instanceNames.toTypedArray()
|
||||
instancePref.entryValues = instanceValues.toTypedArray()
|
||||
instancePref.entries = instances.map { it.name }.toTypedArray()
|
||||
instancePref.entryValues = instances.map { it.apiUrl }.toTypedArray()
|
||||
instancePref.summaryProvider =
|
||||
Preference.SummaryProvider<ListPreference> { preference ->
|
||||
preference.entry
|
||||
|
Loading…
x
Reference in New Issue
Block a user