Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: display currently selected instance as grayed out if not available #5624

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/src/main/java/com/github/libretube/api/RetrofitInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,7 +21,7 @@ object RetrofitInstance {
PreferenceKeys.AUTH_INSTANCE,
PIPED_API_URL
)
false -> url
false -> apiUrl
}

val lazyMgr = resettableManager()
Expand All @@ -30,7 +30,7 @@ object RetrofitInstance {

val api by resettableLazy(lazyMgr) {
Retrofit.Builder()
.baseUrl(url)
.baseUrl(apiUrl)
.callFactory(CronetHelper.callFactory)
.addConverterFactory(kotlinxConverterFactory)
.build()
Expand All @@ -48,7 +48,7 @@ object RetrofitInstance {

val externalApi by resettableLazy(lazyMgr) {
Retrofit.Builder()
.baseUrl(url)
.baseUrl(apiUrl)
.callFactory(CronetHelper.callFactory)
.addConverterFactory(kotlinxConverterFactory)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull

Check failure on line 33 in app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Unused import Raw Output: app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt:33:1: error: Unused import (standard:no-unused-imports)

class InstanceSettings : BasePreferenceFragment() {
override val titleResourceId: Int = R.string.instance
private val token get() = PreferenceHelper.getToken()
private var instances = listOf<PipedInstance>()
private var instances = mutableListOf<PipedInstance>()
private val authInstanceToggle get() = findPreference<SwitchPreferenceCompat>(PreferenceKeys.AUTH_INSTANCE_TOGGLE)!!

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
Expand Down Expand Up @@ -138,9 +140,18 @@
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.
Expand Down
Loading