Skip to content

Commit

Permalink
Merge pull request #5624 from Bnyro/master
Browse files Browse the repository at this point in the history
feat: display currently selected instance as grayed out if not available
  • Loading branch information
Bnyro authored Feb 12, 2024
2 parents 485ec6f + 214aded commit 7a638a2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
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 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<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 @@ 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.
Expand Down

0 comments on commit 7a638a2

Please sign in to comment.