From d2224c85e2c0ccb6a87ba2cbc1d06cb393708ce2 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 21 Feb 2023 13:21:49 +0100 Subject: [PATCH] Fix the displayed chosen instance --- .../libretube/helpers/PreferenceHelper.kt | 5 ++++ .../ui/preferences/InstanceSettings.kt | 23 +++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt index c3f54fb8a..621415a70 100644 --- a/app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PreferenceHelper.kt @@ -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) { diff --git a/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt index f4de53566..32d81da24 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt @@ -123,23 +123,18 @@ class InstanceSettings : BasePreferenceFragment() { private fun initInstancesPref(instancePrefs: List) { 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 { 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 { 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 { preference -> + preference.entry + } } } }