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

Cleanup the instance preference #3129

Merged
merged 1 commit into from
Feb 20, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import android.text.format.DateUtils
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.util.Base64
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
Expand Down Expand Up @@ -49,7 +48,6 @@ import com.github.libretube.api.obj.PipedStream
import com.github.libretube.api.obj.Segment
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.api.obj.Streams
import com.github.libretube.api.obj.Token
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentPlayerBinding
Expand All @@ -58,7 +56,6 @@ import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.enums.PlayerEvent
import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.hideKeyboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.dpToPx
import com.github.libretube.extensions.query
import com.github.libretube.extensions.serializable
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.ui.adapters.PlaylistAdapter
import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.extensions.serializable
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.github.libretube.util.PlayingQueue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ class InstanceSettings : BasePreferenceFragment() {
setPreferencesFromResource(R.xml.instance_settings, rootKey)

val instancePref = findPreference<ListPreference>(PreferenceKeys.FETCH_INSTANCE)!!
initCustomInstances(instancePref)
val authInstanceToggle = findPreference<SwitchPreferenceCompat>(
PreferenceKeys.AUTH_INSTANCE_TOGGLE
)!!
val authInstance = findPreference<ListPreference>(PreferenceKeys.AUTH_INSTANCE)!!
initInstancesPref(listOf(instancePref, authInstance))

instancePref.setOnPreferenceChangeListener { _, newValue ->
RetrofitInstance.url = newValue.toString()
if (!PreferenceHelper.getBoolean(PreferenceKeys.AUTH_INSTANCE_TOGGLE, false)) {
if (!authInstanceToggle.isChecked) {
RetrofitInstance.authUrl = newValue.toString()
logoutAndUpdateUI()
}
Expand All @@ -45,12 +50,8 @@ class InstanceSettings : BasePreferenceFragment() {
true
}

val authInstance = findPreference<ListPreference>(PreferenceKeys.AUTH_INSTANCE)
initCustomInstances(authInstance!!)
// hide auth instance if option deselected
if (!PreferenceHelper.getBoolean(PreferenceKeys.AUTH_INSTANCE_TOGGLE, false)) {
authInstance.isVisible = false
}
authInstance.isVisible = authInstanceToggle.isChecked
authInstance.setOnPreferenceChangeListener { _, newValue ->
// save new auth url
RetrofitInstance.authUrl = newValue.toString()
Expand All @@ -59,9 +60,7 @@ class InstanceSettings : BasePreferenceFragment() {
true
}

val authInstanceToggle =
findPreference<SwitchPreferenceCompat>(PreferenceKeys.AUTH_INSTANCE_TOGGLE)
authInstanceToggle?.setOnPreferenceChangeListener { _, newValue ->
authInstanceToggle.setOnPreferenceChangeListener { _, newValue ->
authInstance.isVisible = newValue == true
// either use new auth url or the normal api url if auth instance disabled
RetrofitInstance.authUrl = if (newValue == false) {
Expand Down Expand Up @@ -121,7 +120,7 @@ class InstanceSettings : BasePreferenceFragment() {
}
}

private fun initCustomInstances(instancePref: ListPreference) {
private fun initInstancesPref(instancePrefs: List<ListPreference>) {
val appContext = requireContext().applicationContext
lifecycleScope.launchWhenCreated {
val customInstances = withContext(Dispatchers.IO) {
Expand All @@ -148,13 +147,15 @@ class InstanceSettings : BasePreferenceFragment() {
instances.addAll(customInstances.map { Instances(it.name, it.apiUrl) })

runOnUiThread {
// 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<ListPreference> { preference ->
preference.entry
}
for (instancePref in instancePrefs) {
// 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<ListPreference> { preference ->
preference.entry
}
}
}
}
}
Expand Down