diff --git a/app/build.gradle b/app/build.gradle index 794fb472d..90f219fa8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -85,4 +85,5 @@ dependencies { coreLibraryDesugaring libs.desugaring implementation libs.cronet.embedded + implementation libs.gson } diff --git a/app/src/main/java/com/github/libretube/MainActivity.kt b/app/src/main/java/com/github/libretube/MainActivity.kt index e6e7beee0..357250e75 100644 --- a/app/src/main/java/com/github/libretube/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/MainActivity.kt @@ -32,12 +32,11 @@ import androidx.fragment.app.Fragment import androidx.navigation.NavController import androidx.navigation.findNavController 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 @@ -54,32 +53,12 @@ class MainActivity : AppCompatActivity() { DynamicColors.applyToActivityIfAvailable(this) super.onCreate(savedInstanceState) 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) - ThemeHelper().updateTheme(this) - LocaleHelper().updateLanguage(this) + RetrofitInstance.url = + PreferenceHelper.getString(this, "selectInstance", "https://pipedapi.kavin.rocks/")!! + + ThemeHelper.updateTheme(this) + LocaleHelper.updateLanguage(this) // show noInternet Activity if no internet available on app startup if (!isNetworkAvailable(this)) { @@ -100,12 +79,11 @@ class MainActivity : AppCompatActivity() { bottomNavigationView.setupWithNavController(navController) // hide the trending page if enabled - val hideTrendingPage = sharedPreferences.getBoolean("hide_trending_page", false) + val hideTrendingPage = PreferenceHelper.getBoolean(this, "hide_trending_page", false) if (hideTrendingPage) bottomNavigationView.menu.findItem(R.id.home2).isVisible = false // navigate to the default start tab - val defaultTab = sharedPreferences.getString("default_tab", "home") - when (defaultTab) { + when (PreferenceHelper.getString(this, "default_tab", "home")) { "home" -> navController.navigate(R.id.home2) "subscriptions" -> navController.navigate(R.id.subscriptions) "library" -> navController.navigate(R.id.library) diff --git a/app/src/main/java/com/github/libretube/RouterActivity.kt b/app/src/main/java/com/github/libretube/RouterActivity.kt index 1292422a3..3033fcfbb 100644 --- a/app/src/main/java/com/github/libretube/RouterActivity.kt +++ b/app/src/main/java/com/github/libretube/RouterActivity.kt @@ -21,7 +21,7 @@ class RouterActivity : AppCompatActivity() { handleSendText(uri!!) } else { // start app as normal if URI not in host list - ThemeHelper().restartMainActivity(this) + ThemeHelper.restartMainActivity(this) } } diff --git a/app/src/main/java/com/github/libretube/SettingsActivity.kt b/app/src/main/java/com/github/libretube/SettingsActivity.kt index 27f6f5046..8e2a3f69b 100644 --- a/app/src/main/java/com/github/libretube/SettingsActivity.kt +++ b/app/src/main/java/com/github/libretube/SettingsActivity.kt @@ -19,7 +19,7 @@ class SettingsActivity : AppCompatActivity() { val TAG = "SettingsActivity" override fun onCreate(savedInstanceState: Bundle?) { DynamicColors.applyToActivityIfAvailable(this) - ThemeHelper().updateTheme(this) + ThemeHelper.updateTheme(this) // makes the preference dialogs use material dialogs setTheme(R.style.MaterialAlertDialog) @@ -54,7 +54,7 @@ class SettingsActivity : AppCompatActivity() { val nManager = this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager nManager.cancelAll() - ThemeHelper().restartMainActivity(this) + ThemeHelper.restartMainActivity(this) ActivityCompat.finishAffinity(this) } else { super.onBackPressed() diff --git a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt index 28e9065f0..448060c49 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt @@ -1,7 +1,6 @@ package com.github.libretube.adapters import android.app.Activity -import android.content.Context import android.os.Bundle import android.text.format.DateUtils import android.util.Log @@ -18,6 +17,7 @@ import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.fragments.PlayerFragment import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.StreamItem +import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.RetrofitInstance import com.squareup.picasso.Picasso import kotlinx.coroutines.CoroutineScope @@ -81,11 +81,7 @@ class PlaylistAdapter( val delete = holder.v.findViewById(R.id.delete_playlist) delete.visibility = View.VISIBLE delete.setOnClickListener { - val sharedPref = holder.v.context.getSharedPreferences( - "token", - Context.MODE_PRIVATE - ) - val token = sharedPref?.getString("token", "")!! + val token = PreferenceHelper.getToken(holder.v.context) removeFromPlaylist(token, position) } } diff --git a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt index 63fd37428..08bdb332c 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt @@ -1,7 +1,6 @@ package com.github.libretube.adapters import android.app.Activity -import android.content.Context import android.util.Log import android.view.LayoutInflater import android.view.View @@ -14,6 +13,7 @@ import com.github.libretube.MainActivity import com.github.libretube.R import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.Playlists +import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.RetrofitInstance import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.squareup.picasso.Picasso @@ -57,11 +57,7 @@ class PlaylistsAdapter( builder.setTitle(R.string.deletePlaylist) builder.setMessage(R.string.areYouSure) builder.setPositiveButton(R.string.yes) { _, _ -> - val sharedPref = holder.v.context.getSharedPreferences( - "token", - Context.MODE_PRIVATE - ) - val token = sharedPref?.getString("token", "")!! + val token = PreferenceHelper.getToken(holder.v.context) deletePlaylist(playlist.id!!, token, position) } builder.setNegativeButton(R.string.cancel) { _, _ -> @@ -96,10 +92,6 @@ class PlaylistsAdapter( playlists.removeAt(position) // FIXME: This needs to run on UI thread? activity.runOnUiThread { notifyDataSetChanged() } - - /*if(playlists.isEmpty()){ - view.findViewById(R.id.boogh2).visibility=View.VISIBLE - }*/ } } catch (e: Exception) { Log.e(TAG, e.toString()) diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index 7dfdeaa72..baeea812d 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -6,10 +6,10 @@ import android.view.View import android.view.ViewGroup import android.widget.EditText import android.widget.TextView -import androidx.preference.PreferenceManager import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R import com.github.libretube.fragments.SearchFragment +import com.github.libretube.util.PreferenceHelper import com.google.android.material.imageview.ShapeableImageView class SearchHistoryAdapter( @@ -35,9 +35,8 @@ class SearchHistoryAdapter( holder.v.findViewById(R.id.history_text).text = history holder.v.findViewById(R.id.delete_history).setOnClickListener { - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) historyList = historyList - history - sharedPreferences.edit().putStringSet("search_history", HashSet(historyList)).apply() + PreferenceHelper.saveHistory(context, historyList) notifyDataSetChanged() } diff --git a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt index dcd3b4e03..7ce63a5ba 100644 --- a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt @@ -13,6 +13,7 @@ import com.github.libretube.MainActivity import com.github.libretube.R import com.github.libretube.obj.Subscribe import com.github.libretube.obj.Subscription +import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.RetrofitInstance import com.squareup.picasso.Picasso import kotlinx.coroutines.CoroutineScope @@ -70,10 +71,9 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList(R.id.playlist_name) val createPlaylistBtn = view.findViewById