mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 08:20:32 +05:30
Fix that custom instances can't be used when the public list is down
This commit is contained in:
parent
51f2858ea5
commit
f7b58b4966
@ -22,8 +22,6 @@ object InstanceHelper {
|
|||||||
throw Exception(context.getString(R.string.failed_fetching_instances))
|
throw Exception(context.getString(R.string.failed_fetching_instances))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.sortedBy { it.name }
|
|
||||||
.toMutableList()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getInstancesFallback(context: Context): List<Instances> {
|
fun getInstancesFallback(context: Context): List<Instances> {
|
||||||
|
@ -3,9 +3,7 @@ package com.github.libretube.ui.preferences
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.lifecycle.Lifecycle
|
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.lifecycle.repeatOnLifecycle
|
|
||||||
import androidx.preference.ListPreference
|
import androidx.preference.ListPreference
|
||||||
import androidx.preference.Preference
|
import androidx.preference.Preference
|
||||||
import androidx.preference.SwitchPreferenceCompat
|
import androidx.preference.SwitchPreferenceCompat
|
||||||
@ -38,7 +36,24 @@ class InstanceSettings : BasePreferenceFragment() {
|
|||||||
PreferenceKeys.AUTH_INSTANCE_TOGGLE
|
PreferenceKeys.AUTH_INSTANCE_TOGGLE
|
||||||
)!!
|
)!!
|
||||||
val authInstance = findPreference<ListPreference>(PreferenceKeys.AUTH_INSTANCE)!!
|
val authInstance = findPreference<ListPreference>(PreferenceKeys.AUTH_INSTANCE)!!
|
||||||
initInstancesPref(listOf(instancePref, authInstance))
|
val instancePrefs = listOf(instancePref, authInstance)
|
||||||
|
|
||||||
|
val appContext = requireContext().applicationContext
|
||||||
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
|
// update the instances to also show custom ones
|
||||||
|
initInstancesPref(instancePrefs, InstanceHelper.getInstancesFallback(appContext))
|
||||||
|
|
||||||
|
// try to fetch the public list of instances async
|
||||||
|
val instances = try {
|
||||||
|
InstanceHelper.getInstances(appContext)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
appContext.toastFromMainDispatcher(e.message.orEmpty())
|
||||||
|
InstanceHelper.getInstancesFallback(requireContext())
|
||||||
|
}
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
initInstancesPref(instancePrefs, instances)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
instancePref.setOnPreferenceChangeListener { _, newValue ->
|
instancePref.setOnPreferenceChangeListener { _, newValue ->
|
||||||
RetrofitInstance.url = newValue.toString()
|
RetrofitInstance.url = newValue.toString()
|
||||||
@ -118,41 +133,32 @@ class InstanceSettings : BasePreferenceFragment() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initInstancesPref(instancePrefs: List<ListPreference>) {
|
private suspend fun initInstancesPref(
|
||||||
val appContext = requireContext().applicationContext
|
instancePrefs: List<ListPreference>,
|
||||||
|
publicInstances: List<Instances>
|
||||||
|
) {
|
||||||
|
val customInstances = withContext(Dispatchers.IO) {
|
||||||
|
Database.customInstanceDao().getAll()
|
||||||
|
}
|
||||||
|
|
||||||
lifecycleScope.launch {
|
for (instancePref in instancePrefs) {
|
||||||
repeatOnLifecycle(Lifecycle.State.CREATED) {
|
instancePref.summaryProvider =
|
||||||
val customInstances = withContext(Dispatchers.IO) {
|
Preference.SummaryProvider<ListPreference> { preference ->
|
||||||
Database.customInstanceDao().getAll()
|
preference.entry
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (instancePref in instancePrefs) {
|
val instances = (publicInstances + customInstances.map { Instances(it.name, it.apiUrl) })
|
||||||
instancePref.summaryProvider =
|
.sortedBy { it.name }
|
||||||
Preference.SummaryProvider<ListPreference> { preference ->
|
|
||||||
preference.entry
|
for (instancePref in instancePrefs) {
|
||||||
}
|
// add custom instances to the list preference
|
||||||
|
instancePref.entries = instances.map { it.name }.toTypedArray()
|
||||||
|
instancePref.entryValues = instances.map { it.apiUrl }.toTypedArray()
|
||||||
|
instancePref.summaryProvider =
|
||||||
|
Preference.SummaryProvider<ListPreference> { preference ->
|
||||||
|
preference.entry
|
||||||
}
|
}
|
||||||
|
|
||||||
val instances = try {
|
|
||||||
InstanceHelper.getInstances(appContext)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
appContext.toastFromMainDispatcher(e.message.orEmpty())
|
|
||||||
InstanceHelper.getInstancesFallback(requireContext())
|
|
||||||
}.toMutableList()
|
|
||||||
|
|
||||||
instances.addAll(customInstances.map { Instances(it.name, it.apiUrl) })
|
|
||||||
|
|
||||||
for (instancePref in instancePrefs) {
|
|
||||||
// add custom instances to the list preference
|
|
||||||
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