Merge pull request #3350 from Isira-Seneviratne/Remove_runOnUiThread

Remove runOnUiThread().
This commit is contained in:
Bnyro 2023-03-23 19:39:25 +01:00 committed by GitHub
commit f1da5d2151
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 33 deletions

View File

@ -1,6 +1,5 @@
package com.github.libretube.ui.base package com.github.libretube.ui.base
import androidx.fragment.app.Fragment
import androidx.preference.EditTextPreference import androidx.preference.EditTextPreference
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
@ -76,10 +75,4 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat() {
else -> super.onDisplayPreferenceDialog(preference) else -> super.onDisplayPreferenceDialog(preference)
} }
} }
fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
} }

View File

@ -6,7 +6,6 @@ import android.util.Log
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.Toast import android.widget.Toast
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.github.libretube.R import com.github.libretube.R
@ -66,15 +65,13 @@ class AddToPlaylistDialog(
response.indexOfFirst { it.id == id }.takeIf { it >= 0 } ?: 0 response.indexOfFirst { it.id == id }.takeIf { it >= 0 } ?: 0
) )
} }
runOnUiThread { binding.addToPlaylist.setOnClickListener {
binding.addToPlaylist.setOnClickListener { val index = binding.playlistsSpinner.selectedItemPosition
val index = binding.playlistsSpinner.selectedItemPosition viewModel.lastSelectedPlaylistId = response[index].id!!
viewModel.lastSelectedPlaylistId = response[index].id!! dialog?.hide()
dialog?.hide() lifecycleScope.launch {
lifecycleScope.launch { addToPlaylist(response[index].id!!)
addToPlaylist(response[index].id!!) dialog?.dismiss()
dialog?.dismiss()
}
} }
} }
} }
@ -100,10 +97,4 @@ class AddToPlaylistDialog(
if (success) R.string.added_to_playlist else R.string.fail if (success) R.string.added_to_playlist else R.string.fail
) )
} }
private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
} }

View File

@ -154,16 +154,14 @@ class InstanceSettings : BasePreferenceFragment() {
instances.addAll(customInstances.map { Instances(it.name, it.apiUrl) }) instances.addAll(customInstances.map { Instances(it.name, it.apiUrl) })
runOnUiThread { for (instancePref in instancePrefs) {
for (instancePref in instancePrefs) { // add custom instances to the list preference
// add custom instances to the list preference instancePref.entries = instances.map { it.name }.toTypedArray()
instancePref.entries = instances.map { it.name }.toTypedArray() instancePref.entryValues = instances.map { it.apiUrl }.toTypedArray()
instancePref.entryValues = instances.map { it.apiUrl }.toTypedArray() instancePref.summaryProvider =
instancePref.summaryProvider = Preference.SummaryProvider<ListPreference> { preference ->
Preference.SummaryProvider<ListPreference> { preference -> preference.entry
preference.entry }
}
}
} }
} }
} }