Improve the dynamic instance list fetching

This commit is contained in:
Bnyro 2023-02-12 15:32:57 +01:00
parent cd54f2beb0
commit cd21f64be2
2 changed files with 27 additions and 34 deletions

View File

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

View File

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