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

This commit is contained in:
Bnyro 2024-02-12 11:39:20 +01:00
parent 4180ddee61
commit 214aded22f
4 changed files with 24 additions and 10 deletions

View File

@ -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()

View File

@ -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,
)

View File

@ -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 ->

View File

@ -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?) {
@ -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.