Fix the displayed chosen instance

This commit is contained in:
Bnyro 2023-02-21 13:21:49 +01:00
parent 03cd871d02
commit d2224c85e2
2 changed files with 16 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package com.github.libretube.helpers
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.github.libretube.constants.PIPED_API_URL
import com.github.libretube.constants.PreferenceKeys
import java.time.Instant
@ -28,6 +29,10 @@ object PreferenceHelper {
authSettings = getAuthenticationPreferences(context)
authEditor = authSettings.edit()
if (getString(PreferenceKeys.FETCH_INSTANCE, "").isBlank()) {
putString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL)
}
}
fun putString(key: String, value: String) {

View File

@ -123,23 +123,18 @@ class InstanceSettings : BasePreferenceFragment() {
private fun initInstancesPref(instancePrefs: List<ListPreference>) {
val appContext = requireContext().applicationContext
instancePrefs.forEach { instancePref ->
if (instancePref.entry.isNullOrBlank()) {
instancePref.value = context?.resources?.getStringArray(R.array.instancesValue)?.first().orEmpty()
}
instancePref.summaryProvider =
Preference.SummaryProvider<ListPreference> { preference ->
preference.entry.takeIf { !it.isNullOrBlank() }
?: context?.resources?.getStringArray(R.array.instances)?.first().orEmpty()
}
}
lifecycleScope.launchWhenCreated {
val customInstances = withContext(Dispatchers.IO) {
Database.customInstanceDao().getAll()
}
for (instancePref in instancePrefs) {
instancePref.summaryProvider =
Preference.SummaryProvider<ListPreference> { preference ->
preference.entry
}
}
// fetch official public instances from kavin.rocks as well as tokhmi.xyz as fallback
val instances = withContext(Dispatchers.IO) {
runCatching {
@ -164,6 +159,10 @@ class InstanceSettings : BasePreferenceFragment() {
// 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
}
}
}
}