From 255677133a0888098daadd11f1bd0e0a282f90ff Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 26 Jun 2022 10:36:34 +0200 Subject: [PATCH] cleanup prefs --- .../java/com/github/libretube/MainActivity.kt | 24 +----- .../github/libretube/dialogs/LoginDialog.kt | 2 +- .../libretube/fragments/PlayerFragment.kt | 57 ++++++++++----- .../github/libretube/obj/SponsorBlockPrefs.kt | 14 ++++ .../preferences/SponsorBlockSettings.kt | 73 ------------------- .../github/libretube/util/PreferenceHelper.kt | 5 ++ 6 files changed, 62 insertions(+), 113 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/obj/SponsorBlockPrefs.kt diff --git a/app/src/main/java/com/github/libretube/MainActivity.kt b/app/src/main/java/com/github/libretube/MainActivity.kt index e6e7beee0..a3d6ec727 100644 --- a/app/src/main/java/com/github/libretube/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/MainActivity.kt @@ -35,9 +35,9 @@ import androidx.navigation.ui.setupWithNavController import androidx.preference.PreferenceManager import com.github.libretube.fragments.PlayerFragment import com.github.libretube.fragments.isFullScreen -import com.github.libretube.preferences.SponsorBlockSettings import com.github.libretube.util.CronetHelper import com.github.libretube.util.LocaleHelper +import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.ThemeHelper import com.google.android.material.bottomnavigation.BottomNavigationView @@ -56,27 +56,7 @@ class MainActivity : AppCompatActivity() { CronetHelper.initCronet(this.applicationContext) val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) RetrofitInstance.url = - sharedPreferences.getString("selectInstance", "https://pipedapi.kavin.rocks/")!! - SponsorBlockSettings.sponsorBlockEnabled = - sharedPreferences.getBoolean("sb_enabled_key", true) - SponsorBlockSettings.sponsorNotificationsEnabled = - sharedPreferences.getBoolean("sb_notifications_key", true) - SponsorBlockSettings.introEnabled = - sharedPreferences.getBoolean("intro_category_key", false) - SponsorBlockSettings.selfPromoEnabled = - sharedPreferences.getBoolean("selfpromo_category_key", false) - SponsorBlockSettings.interactionEnabled = - sharedPreferences.getBoolean("interaction_category_key", false) - SponsorBlockSettings.sponsorsEnabled = - sharedPreferences.getBoolean("sponsors_category_key", true) - SponsorBlockSettings.outroEnabled = - sharedPreferences.getBoolean("outro_category_key", false) - SponsorBlockSettings.fillerEnabled = - sharedPreferences.getBoolean("filler_category_key", false) - SponsorBlockSettings.musicOfftopicEnabled = - sharedPreferences.getBoolean("music_offtopic_category_key", false) - SponsorBlockSettings.previewEnabled = - sharedPreferences.getBoolean("preview_category_key", false) + PreferenceHelper.getString(this, "selectInstance", "https://pipedapi.kavin.rocks/")!! ThemeHelper().updateTheme(this) LocaleHelper().updateLanguage(this) diff --git a/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt b/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt index 8d1cc33f8..e32467d77 100644 --- a/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/LoginDialog.kt @@ -99,7 +99,7 @@ class LoginDialog : DialogFragment() { Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show() return@launchWhenCreated } catch (e: Exception) { - Log.e(TAG, "dafaq?" + e.toString()) + Log.e(TAG, "dafaq?$e") return@launchWhenCreated } if (response.error != null) { diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 6da3a05ef..91ffc4a2e 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -52,10 +52,10 @@ import com.github.libretube.obj.ChapterSegment import com.github.libretube.obj.PipedStream import com.github.libretube.obj.Segment import com.github.libretube.obj.Segments +import com.github.libretube.obj.SponsorBlockPrefs import com.github.libretube.obj.StreamItem import com.github.libretube.obj.Streams import com.github.libretube.obj.Subscribe -import com.github.libretube.preferences.SponsorBlockSettings import com.github.libretube.util.CronetHelper import com.github.libretube.util.DescriptionAdapter import com.github.libretube.util.PreferenceHelper @@ -131,6 +131,7 @@ class PlayerFragment : Fragment() { private lateinit var title: String private lateinit var uploader: String private lateinit var thumbnailUrl: String + private val sponsorBlockPrefs = SponsorBlockPrefs() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -152,6 +153,7 @@ class PlayerFragment : Fragment() { super.onViewCreated(view, savedInstanceState) hideKeyboard() + setSponsorBlockPrefs() initializeTransitionLayout(view) fetchJsonAndInitPlayer(view) } @@ -382,7 +384,7 @@ class PlayerFragment : Fragment() { } private fun checkForSegments() { - if (!exoPlayer.isPlaying || !SponsorBlockSettings.sponsorBlockEnabled) return + if (!exoPlayer.isPlaying || !sponsorBlockPrefs.sponsorBlockEnabled) return exoPlayerView.postDelayed(this::checkForSegments, 100) @@ -395,7 +397,7 @@ class PlayerFragment : Fragment() { val segmentEnd = (segment.segment[1] * 1000.0f).toLong() val currentPosition = exoPlayer.currentPosition if (currentPosition in segmentStart until segmentEnd) { - if (SponsorBlockSettings.sponsorNotificationsEnabled) { + if (sponsorBlockPrefs.sponsorNotificationsEnabled) { Toast.makeText(context, R.string.segment_skipped, Toast.LENGTH_SHORT).show() } exoPlayer.seekTo(segmentEnd) @@ -424,10 +426,8 @@ class PlayerFragment : Fragment() { thumbnailUrl = response.thumbnailUrl!! // check whether related streams and autoplay are enabled - val sharedPreferences = PreferenceManager - .getDefaultSharedPreferences(requireContext()) - autoplay = sharedPreferences.getBoolean("autoplay", false) - relatedStreamsEnabled = sharedPreferences.getBoolean("related_streams_toggle", true) + autoplay = PreferenceHelper.getBoolean(requireContext(),"autoplay", false) + relatedStreamsEnabled = PreferenceHelper.getBoolean(requireContext(), "related_streams_toggle", true) // save related streams for autoplay relatedStreams = response.relatedStreams runOnUiThread { @@ -452,33 +452,56 @@ class PlayerFragment : Fragment() { run() } + private fun setSponsorBlockPrefs() { + sponsorBlockPrefs.sponsorBlockEnabled = + PreferenceHelper.getBoolean(requireContext(), "sb_enabled_key", true) + sponsorBlockPrefs.sponsorNotificationsEnabled = + PreferenceHelper.getBoolean(requireContext(), "sb_notifications_key", true) + sponsorBlockPrefs.introEnabled = + PreferenceHelper.getBoolean(requireContext(), "intro_category_key", false) + sponsorBlockPrefs.selfPromoEnabled = + PreferenceHelper.getBoolean(requireContext(), "selfpromo_category_key", false) + sponsorBlockPrefs.interactionEnabled = + PreferenceHelper.getBoolean(requireContext(), "interaction_category_key", false) + sponsorBlockPrefs.sponsorsEnabled = + PreferenceHelper.getBoolean(requireContext(), "sponsors_category_key", true) + sponsorBlockPrefs.outroEnabled = + PreferenceHelper.getBoolean(requireContext(), "outro_category_key", false) + sponsorBlockPrefs.fillerEnabled = + PreferenceHelper.getBoolean(requireContext(), "filler_category_key", false) + sponsorBlockPrefs.musicOffTopicEnabled = + PreferenceHelper.getBoolean(requireContext(), "music_offtopic_category_key", false) + sponsorBlockPrefs.previewEnabled = + PreferenceHelper.getBoolean(requireContext(), "preview_category_key", false) + } + private fun fetchSponsorBlockSegments() { fun run() { lifecycleScope.launchWhenCreated { - if (SponsorBlockSettings.sponsorBlockEnabled) { + if (sponsorBlockPrefs.sponsorBlockEnabled) { val categories: ArrayList = arrayListOf() - if (SponsorBlockSettings.introEnabled) { + if (sponsorBlockPrefs.introEnabled) { categories.add("intro") } - if (SponsorBlockSettings.selfPromoEnabled) { + if (sponsorBlockPrefs.selfPromoEnabled) { categories.add("selfpromo") } - if (SponsorBlockSettings.interactionEnabled) { + if (sponsorBlockPrefs.interactionEnabled) { categories.add("interaction") } - if (SponsorBlockSettings.sponsorsEnabled) { + if (sponsorBlockPrefs.sponsorsEnabled) { categories.add("sponsor") } - if (SponsorBlockSettings.outroEnabled) { + if (sponsorBlockPrefs.outroEnabled) { categories.add("outro") } - if (SponsorBlockSettings.fillerEnabled) { + if (sponsorBlockPrefs.fillerEnabled) { categories.add("filler") } - if (SponsorBlockSettings.musicOfftopicEnabled) { + if (sponsorBlockPrefs.musicOffTopicEnabled) { categories.add("music_offtopic") } - if (SponsorBlockSettings.previewEnabled) { + if (sponsorBlockPrefs.previewEnabled) { categories.add("preview") } if (categories.size > 0) { @@ -534,7 +557,7 @@ class PlayerFragment : Fragment() { // Listener for play and pause icon change exoPlayer.addListener(object : Player.Listener { override fun onIsPlayingChanged(isPlaying: Boolean) { - if (isPlaying && SponsorBlockSettings.sponsorBlockEnabled) { + if (isPlaying && sponsorBlockPrefs.sponsorBlockEnabled) { exoPlayerView.postDelayed( this@PlayerFragment::checkForSegments, 100 diff --git a/app/src/main/java/com/github/libretube/obj/SponsorBlockPrefs.kt b/app/src/main/java/com/github/libretube/obj/SponsorBlockPrefs.kt new file mode 100644 index 000000000..73118d5ca --- /dev/null +++ b/app/src/main/java/com/github/libretube/obj/SponsorBlockPrefs.kt @@ -0,0 +1,14 @@ +package com.github.libretube.obj + +class SponsorBlockPrefs( + var sponsorBlockEnabled: Boolean = false, + var sponsorNotificationsEnabled: Boolean = false, + var sponsorsEnabled: Boolean = false, + var selfPromoEnabled: Boolean = false, + var interactionEnabled: Boolean = false, + var introEnabled: Boolean = false, + var outroEnabled: Boolean = false, + var fillerEnabled: Boolean = false, + var musicOffTopicEnabled: Boolean = false, + var previewEnabled: Boolean = false +) diff --git a/app/src/main/java/com/github/libretube/preferences/SponsorBlockSettings.kt b/app/src/main/java/com/github/libretube/preferences/SponsorBlockSettings.kt index 1236a8b5c..4d8def0b3 100644 --- a/app/src/main/java/com/github/libretube/preferences/SponsorBlockSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/SponsorBlockSettings.kt @@ -3,88 +3,15 @@ package com.github.libretube.preferences import android.os.Bundle import android.widget.TextView import androidx.preference.PreferenceFragmentCompat -import androidx.preference.SwitchPreferenceCompat import com.github.libretube.R class SponsorBlockSettings : PreferenceFragmentCompat() { private val TAG = "SponsorBlockSettings" - companion object { - var sponsorBlockEnabled: Boolean = false - var sponsorNotificationsEnabled: Boolean = false - var sponsorsEnabled: Boolean = false - var selfPromoEnabled: Boolean = false - var interactionEnabled: Boolean = false - var introEnabled: Boolean = false - var outroEnabled: Boolean = false - var fillerEnabled: Boolean = false - var musicOfftopicEnabled: Boolean = false - var previewEnabled: Boolean = false - } - override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.sponsorblock_settings, rootKey) val topBarTextView = activity?.findViewById(R.id.topBar_textView) topBarTextView?.text = getString(R.string.sponsorblock) - - val sponsorBlockToggle = findPreference("sb_enabled_key") - sponsorBlockToggle?.setOnPreferenceChangeListener { _, newValue -> - sponsorBlockEnabled = newValue as Boolean - true - } - - val notificationsToggle = findPreference("sb_notifications_key") - notificationsToggle?.setOnPreferenceChangeListener { _, newValue -> - sponsorNotificationsEnabled = newValue as Boolean - true - } - - val sponsorToggle = findPreference("sponsors_category_key") - sponsorToggle?.setOnPreferenceChangeListener { _, newValue -> - sponsorsEnabled = newValue as Boolean - true - } - val selfPromoToggle = findPreference("selfpromo_category_key") - selfPromoToggle?.setOnPreferenceChangeListener { _, newValue -> - selfPromoEnabled = newValue as Boolean - true - } - - val interactionToggle = findPreference("interaction_category_key") - interactionToggle?.setOnPreferenceChangeListener { _, newValue -> - interactionEnabled = newValue as Boolean - true - } - - val introToggle = findPreference("intro_category_key") - introToggle?.setOnPreferenceChangeListener { _, newValue -> - introEnabled = newValue as Boolean - true - } - - val outroToggle = findPreference("outro_category_key") - outroToggle?.setOnPreferenceChangeListener { _, newValue -> - outroEnabled = newValue as Boolean - true - } - - val fillerToggle = findPreference("filler_category_key") - fillerToggle?.setOnPreferenceChangeListener { _, newValue -> - fillerEnabled = newValue as Boolean - true - } - - val musicToggle = findPreference("music_offtopic_category_key") - musicToggle?.setOnPreferenceChangeListener { _, newValue -> - musicOfftopicEnabled = newValue as Boolean - true - } - - val previewToggle = findPreference("preview_category_key") - previewToggle?.setOnPreferenceChangeListener { _, newValue -> - previewEnabled = newValue as Boolean - true - } } } diff --git a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt index d97357f65..c0cbc257f 100644 --- a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt @@ -61,6 +61,11 @@ object PreferenceHelper { editor.commit() } + fun setPrivateStringPref(context: Context, key: String, newValue: String) { + val editor = context.getSharedPreferences(key, Context.MODE_PRIVATE).edit() + editor.putString(key, newValue).apply() + } + private fun getDefaultSharedPreferences(context: Context): SharedPreferences { return PreferenceManager.getDefaultSharedPreferences(context) }