From 214aded22fb7151c5cfcdee1e2de970d869b53d9 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 12 Feb 2024 11:39:20 +0100 Subject: [PATCH] feat: display currently selected instance as grayed out if not available --- .../github/libretube/api/RetrofitInstance.kt | 10 +++++----- .../github/libretube/api/obj/PipedInstance.kt | 3 ++- .../libretube/ui/adapters/InstancesAdapter.kt | 2 ++ .../ui/preferences/InstanceSettings.kt | 19 +++++++++++++++---- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt b/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt index 07d2dc5b1..2a6abcc3a 100644 --- a/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt +++ b/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt @@ -9,8 +9,8 @@ import retrofit2.create object RetrofitInstance { private const val PIPED_API_URL = "https://pipedapi.kavin.rocks" - private val url get() = PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL) - private val authUrl + val apiUrl get() = PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL) + val authUrl get() = when ( PreferenceHelper.getBoolean( PreferenceKeys.AUTH_INSTANCE_TOGGLE, @@ -21,7 +21,7 @@ object RetrofitInstance { PreferenceKeys.AUTH_INSTANCE, PIPED_API_URL ) - false -> url + false -> apiUrl } val lazyMgr = resettableManager() @@ -30,7 +30,7 @@ object RetrofitInstance { val api by resettableLazy(lazyMgr) { Retrofit.Builder() - .baseUrl(url) + .baseUrl(apiUrl) .callFactory(CronetHelper.callFactory) .addConverterFactory(kotlinxConverterFactory) .build() @@ -48,7 +48,7 @@ object RetrofitInstance { val externalApi by resettableLazy(lazyMgr) { Retrofit.Builder() - .baseUrl(url) + .baseUrl(apiUrl) .callFactory(CronetHelper.callFactory) .addConverterFactory(kotlinxConverterFactory) .build() diff --git a/app/src/main/java/com/github/libretube/api/obj/PipedInstance.kt b/app/src/main/java/com/github/libretube/api/obj/PipedInstance.kt index 902d036a7..fa9a0479b 100644 --- a/app/src/main/java/com/github/libretube/api/obj/PipedInstance.kt +++ b/app/src/main/java/com/github/libretube/api/obj/PipedInstance.kt @@ -19,5 +19,6 @@ data class PipedInstance( @SerialName("registration_disabled") val registrationDisabled: Boolean = false, @SerialName("uptime_24h") val uptimeToday: Float? = null, @SerialName("uptime_7d") val uptimeWeek: Float? = null, - @SerialName("uptime_30d") val uptimeMonth: Float? = null + @SerialName("uptime_30d") val uptimeMonth: Float? = null, + val isCurrentlyDown: Boolean = false, ) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/InstancesAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/InstancesAdapter.kt index 19f8b76a2..f2a424e7c 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/InstancesAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/InstancesAdapter.kt @@ -42,6 +42,8 @@ class InstancesAdapter( } radioButton.text = instanceText + radioButton.alpha = if (instance.isCurrentlyDown) 0.5f else 1f + radioButton.setOnCheckedChangeListener(null) radioButton.isChecked = selectedInstanceIndex == position radioButton.setOnCheckedChangeListener { _, isChecked -> 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 a46e6abe5..963eb49cd 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 @@ -29,11 +29,13 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import okhttp3.HttpUrl.Companion.toHttpUrl +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull class InstanceSettings : BasePreferenceFragment() { override val titleResourceId: Int = R.string.instance private val token get() = PreferenceHelper.getToken() - private var instances = listOf() + private var instances = mutableListOf() private val authInstanceToggle get() = findPreference(PreferenceKeys.AUTH_INSTANCE_TOGGLE)!! override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { @@ -138,9 +140,18 @@ class InstanceSettings : BasePreferenceFragment() { Database.customInstanceDao().getAll() }.map { PipedInstance(it.name, it.apiUrl) } - instances = publicInstances - .plus(customInstances) - .sortedBy { it.name } + instances = publicInstances.plus(customInstances).toMutableList() + + // add the currently used instances to the list if they're currently down / not part + // of the public instances list + for (apiUrl in listOf(RetrofitInstance.apiUrl, RetrofitInstance.authUrl)) { + if (instances.none { it.apiUrl == apiUrl }) { + val origin = apiUrl.toHttpUrl().host + instances.add(PipedInstance(origin, apiUrl, isCurrentlyDown = true)) + } + } + + instances.sortBy { it.name } // If any preference dialog is visible in this fragment, it's one of the instance selection // dialogs. In order to prevent UX issues, we don't update the instances list then.