Skip to content

Commit

Permalink
Fix that custom instances can't be used when the public list is down
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jul 6, 2023
1 parent 51f2858 commit f7b58b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
2 changes: 0 additions & 2 deletions app/src/main/java/com/github/libretube/api/InstanceHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ object InstanceHelper {
throw Exception(context.getString(R.string.failed_fetching_instances))
}
}
.sortedBy { it.name }
.toMutableList()
}

fun getInstancesFallback(context: Context): List<Instances> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package com.github.libretube.ui.preferences
import android.os.Bundle
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.SwitchPreferenceCompat
Expand Down Expand Up @@ -38,7 +36,24 @@ class InstanceSettings : BasePreferenceFragment() {
PreferenceKeys.AUTH_INSTANCE_TOGGLE
)!!
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 ->
RetrofitInstance.url = newValue.toString()
Expand Down Expand Up @@ -118,41 +133,32 @@ class InstanceSettings : BasePreferenceFragment() {
}
}

private fun initInstancesPref(instancePrefs: List<ListPreference>) {
val appContext = requireContext().applicationContext
private suspend fun initInstancesPref(
instancePrefs: List<ListPreference>,
publicInstances: List<Instances>
) {
val customInstances = withContext(Dispatchers.IO) {
Database.customInstanceDao().getAll()
}

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
val customInstances = withContext(Dispatchers.IO) {
Database.customInstanceDao().getAll()
for (instancePref in instancePrefs) {
instancePref.summaryProvider =
Preference.SummaryProvider<ListPreference> { preference ->
preference.entry
}
}

for (instancePref in instancePrefs) {
instancePref.summaryProvider =
Preference.SummaryProvider<ListPreference> { preference ->
preference.entry
}
}
val instances = (publicInstances + customInstances.map { Instances(it.name, it.apiUrl) })
.sortedBy { it.name }

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

Expand Down

0 comments on commit f7b58b4

Please sign in to comment.