From a927511d55d3f623f6093b73136aa5bd0e1b28f0 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 6 Mar 2023 20:50:52 +0530 Subject: [PATCH] Remove runOnUiThread(). --- .../ui/base/BasePreferenceFragment.kt | 7 ------ .../ui/dialogs/AddToPlaylistDialog.kt | 23 ++++++------------- .../ui/preferences/InstanceSettings.kt | 18 +++++++-------- 3 files changed, 15 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/base/BasePreferenceFragment.kt b/app/src/main/java/com/github/libretube/ui/base/BasePreferenceFragment.kt index 8fcc90d42..a97e8616f 100644 --- a/app/src/main/java/com/github/libretube/ui/base/BasePreferenceFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/base/BasePreferenceFragment.kt @@ -1,6 +1,5 @@ package com.github.libretube.ui.base -import androidx.fragment.app.Fragment import androidx.preference.EditTextPreference import androidx.preference.ListPreference import androidx.preference.Preference @@ -76,10 +75,4 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat() { else -> super.onDisplayPreferenceDialog(preference) } } - - fun Fragment?.runOnUiThread(action: () -> Unit) { - this ?: return - if (!isAdded) return // Fragment not attached to an Activity - activity?.runOnUiThread(action) - } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt index 6f0db4290..da9ad2a70 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt @@ -6,7 +6,6 @@ import android.util.Log import android.widget.ArrayAdapter import android.widget.Toast import androidx.fragment.app.DialogFragment -import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.lifecycle.lifecycleScope import com.github.libretube.R @@ -66,15 +65,13 @@ class AddToPlaylistDialog( response.indexOfFirst { it.id == id }.takeIf { it >= 0 } ?: 0 ) } - runOnUiThread { - binding.addToPlaylist.setOnClickListener { - val index = binding.playlistsSpinner.selectedItemPosition - viewModel.lastSelectedPlaylistId = response[index].id!! - dialog?.hide() - lifecycleScope.launch { - addToPlaylist(response[index].id!!) - dialog?.dismiss() - } + binding.addToPlaylist.setOnClickListener { + val index = binding.playlistsSpinner.selectedItemPosition + viewModel.lastSelectedPlaylistId = response[index].id!! + dialog?.hide() + lifecycleScope.launch { + addToPlaylist(response[index].id!!) + dialog?.dismiss() } } } @@ -100,10 +97,4 @@ class AddToPlaylistDialog( 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) - } } diff --git a/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt index 32d81da24..ebf95a81f 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt @@ -154,16 +154,14 @@ class InstanceSettings : BasePreferenceFragment() { instances.addAll(customInstances.map { Instances(it.name, it.apiUrl) }) - runOnUiThread { - 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 { 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 { preference -> + preference.entry + } } } }