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