mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-13 13:50:30 +05:30
introduce preferences helper
This commit is contained in:
parent
0d30ad1227
commit
8be32bc5af
@ -17,8 +17,8 @@ import android.os.IBinder
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.arthenica.ffmpegkit.FFmpegKit
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import java.io.File
|
||||
|
||||
var IS_DOWNLOAD_RUNNING = false
|
||||
@ -48,8 +48,7 @@ class DownloadService : Service() {
|
||||
videoUrl = intent.getStringExtra("videoUrl")!!
|
||||
audioUrl = intent.getStringExtra("audioUrl")!!
|
||||
duration = intent.getIntExtra("duration", 1)
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
|
||||
extension = sharedPreferences.getString("video_format", ".mp4")!!
|
||||
extension = PreferenceHelper.getString(this, "video_format", ".mp4")!!
|
||||
downloadType = if (audioUrl != "" && videoUrl != "") "mux"
|
||||
else if (audioUrl != "") "audio"
|
||||
else if (videoUrl != "") "video"
|
||||
@ -83,9 +82,8 @@ class DownloadService : Service() {
|
||||
Log.e(TAG, "Directory already have")
|
||||
}
|
||||
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
|
||||
val downloadLocationPref = sharedPreferences.getString("download_location", "")
|
||||
val folderName = sharedPreferences.getString("download_folder", "")
|
||||
val downloadLocationPref = PreferenceHelper.getString(this, "download_location", "")
|
||||
val folderName = PreferenceHelper.getString(this, "download_folder", "LibreTube")
|
||||
|
||||
val location = when (downloadLocationPref) {
|
||||
"downloads" -> Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS)
|
||||
|
@ -9,12 +9,12 @@ import android.widget.ProgressBar
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.adapters.TrendingAdapter
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.github.libretube.util.RetrofitInstance
|
||||
import retrofit2.HttpException
|
||||
import java.io.IOException
|
||||
@ -41,8 +41,8 @@ class Home : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
val recyclerView = view.findViewById<RecyclerView>(R.id.recview)
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
val grid = sharedPreferences.getString(
|
||||
val grid = PreferenceHelper.getString(
|
||||
requireContext(),
|
||||
"grid",
|
||||
resources.getInteger(R.integer.grid_items).toString()
|
||||
)!!
|
||||
@ -61,9 +61,8 @@ class Home : Fragment() {
|
||||
fun run() {
|
||||
lifecycleScope.launchWhenCreated {
|
||||
val response = try {
|
||||
val sharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
RetrofitInstance.api.getTrending(sharedPreferences.getString("region", "US")!!)
|
||||
val region = PreferenceHelper.getString(requireContext(), "region", "US")
|
||||
RetrofitInstance.api.getTrending(region!!)
|
||||
} catch (e: IOException) {
|
||||
println(e)
|
||||
Log.e(TAG, "IOException, you might not have internet connection")
|
||||
|
@ -58,6 +58,7 @@ 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
|
||||
import com.github.libretube.util.RetrofitInstance
|
||||
import com.github.libretube.util.formatShort
|
||||
import com.google.android.exoplayer2.C
|
||||
@ -347,9 +348,11 @@ class PlayerFragment : Fragment() {
|
||||
|
||||
override fun onPause() {
|
||||
// pause the player if the screen is turned off
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
val pausePlayerOnScreenOffEnabled = sharedPreferences
|
||||
.getBoolean("pause_screen_off", false)
|
||||
val pausePlayerOnScreenOffEnabled = PreferenceHelper.getBoolean(
|
||||
requireContext(),
|
||||
"pause_screen_off",
|
||||
false
|
||||
)
|
||||
|
||||
// check whether the screen is on
|
||||
val pm = context?.getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||
@ -717,11 +720,8 @@ class PlayerFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun setResolutionAndSubtitles(view: View, response: Streams) {
|
||||
val sharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
|
||||
val videoFormatPreference = sharedPreferences.getString("player_video_format", "WEBM")
|
||||
val defres = sharedPreferences.getString("default_res", "")!!
|
||||
val videoFormatPreference = PreferenceHelper.getString(requireContext(), "player_video_format", "WEBM")
|
||||
val defres = PreferenceHelper.getString(requireContext(), "default_res", "")!!
|
||||
|
||||
val qualityText = view.findViewById<TextView>(R.id.quality_text)
|
||||
val qualitySelect = view.findViewById<ImageButton>(R.id.quality_select)
|
||||
|
@ -27,6 +27,7 @@ import com.github.libretube.adapters.SearchAdapter
|
||||
import com.github.libretube.adapters.SearchHistoryAdapter
|
||||
import com.github.libretube.adapters.SearchSuggestionsAdapter
|
||||
import com.github.libretube.hideKeyboard
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.github.libretube.util.RetrofitInstance
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
@ -276,7 +277,7 @@ class SearchFragment : Fragment() {
|
||||
|
||||
private fun addToHistory(query: String) {
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
val searchHistoryEnabled = sharedPreferences.getBoolean("search_history_toggle", true)
|
||||
val searchHistoryEnabled = PreferenceHelper.getBoolean(requireContext(), "search_history_toggle", true)
|
||||
if (searchHistoryEnabled) {
|
||||
var historyList = getHistory()
|
||||
|
||||
|
@ -5,9 +5,9 @@ import android.os.Bundle
|
||||
import android.widget.TextView
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.requireMainActivityRestart
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
|
||||
class AdvancedSettings : PreferenceFragmentCompat() {
|
||||
@ -21,9 +21,7 @@ class AdvancedSettings : PreferenceFragmentCompat() {
|
||||
|
||||
val clearHistory = findPreference<Preference>("clear_history")
|
||||
clearHistory?.setOnPreferenceClickListener {
|
||||
val sharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
sharedPreferences.edit().remove("search_history").commit()
|
||||
PreferenceHelper.removePreference(requireContext(), "search_history")
|
||||
true
|
||||
}
|
||||
|
||||
@ -38,9 +36,7 @@ class AdvancedSettings : PreferenceFragmentCompat() {
|
||||
MaterialAlertDialogBuilder(requireContext())
|
||||
.setPositiveButton(R.string.reset) { _, _ ->
|
||||
// clear default preferences
|
||||
val sharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
sharedPreferences.edit().clear().commit()
|
||||
PreferenceHelper.clearPreferences(requireContext())
|
||||
|
||||
// clear login token
|
||||
val sharedPrefToken =
|
||||
|
@ -25,6 +25,7 @@ import com.github.libretube.dialogs.CustomInstanceDialog
|
||||
import com.github.libretube.dialogs.DeleteAccountDialog
|
||||
import com.github.libretube.dialogs.LoginDialog
|
||||
import com.github.libretube.requireMainActivityRestart
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.github.libretube.util.RetrofitInstance
|
||||
import org.json.JSONObject
|
||||
import org.json.JSONTokener
|
||||
@ -134,11 +135,9 @@ class InstanceSettings : PreferenceFragmentCompat() {
|
||||
|
||||
val clearCustomInstances = findPreference<Preference>("clearCustomInstances")
|
||||
clearCustomInstances?.setOnPreferenceClickListener {
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
sharedPreferences.edit()
|
||||
.remove("custom_instances_name")
|
||||
.remove("custom_instances_url")
|
||||
.commit()
|
||||
PreferenceHelper.removePreference(requireContext(), "custom_instances_name")
|
||||
PreferenceHelper.removePreference(requireContext(), "custom_instances_url")
|
||||
PreferenceHelper.removePreference(requireContext(), "custom_instances_frontend_url")
|
||||
activity?.recreate()
|
||||
true
|
||||
}
|
||||
|
@ -2,14 +2,12 @@ package com.github.libretube.util
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import androidx.preference.PreferenceManager
|
||||
import java.util.*
|
||||
|
||||
class LocaleHelper {
|
||||
|
||||
fun updateLanguage(context: Context) {
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
val languageName = sharedPreferences.getString("language", "sys")
|
||||
val languageName = PreferenceHelper.getString(context, "language", "en")
|
||||
if (languageName != "") {
|
||||
setLanguage(context, languageName!!)
|
||||
}
|
||||
|
@ -0,0 +1,70 @@
|
||||
package com.github.libretube.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import androidx.preference.PreferenceManager
|
||||
|
||||
object PreferenceHelper {
|
||||
fun setString(context: Context, key: String?, value: String?) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
editor.putString(key, value)
|
||||
editor.apply()
|
||||
}
|
||||
|
||||
fun setInt(context: Context, key: String?, value: Int) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
editor.putInt(key, value)
|
||||
editor.apply()
|
||||
}
|
||||
|
||||
fun setLong(context: Context, key: String?, value: Long) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
editor.putLong(key, value)
|
||||
editor.apply()
|
||||
}
|
||||
|
||||
fun setBoolean(context: Context, key: String?, value: Boolean) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
editor.putBoolean(key, value)
|
||||
editor.apply()
|
||||
}
|
||||
|
||||
fun getString(context: Context, key: String?, defValue: String?): String? {
|
||||
val settings: SharedPreferences = getDefaultSharedPreferences(context)
|
||||
return settings.getString(key, defValue)
|
||||
}
|
||||
|
||||
fun getInt(context: Context, key: String?, defValue: Int): Int {
|
||||
val settings: SharedPreferences = getDefaultSharedPreferences(context)
|
||||
return settings.getInt(key, defValue)
|
||||
}
|
||||
|
||||
fun getLong(context: Context, key: String?, defValue: Long): Long {
|
||||
val settings: SharedPreferences = getDefaultSharedPreferences(context)
|
||||
return settings.getLong(key, defValue)
|
||||
}
|
||||
|
||||
fun getBoolean(context: Context, key: String?, defValue: Boolean): Boolean {
|
||||
val settings: SharedPreferences = getDefaultSharedPreferences(context)
|
||||
return settings.getBoolean(key, defValue)
|
||||
}
|
||||
|
||||
fun clearPreferences(context: Context) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
editor.clear()
|
||||
editor.commit()
|
||||
}
|
||||
|
||||
fun removePreference(context: Context, value: String?) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
editor.remove(value)
|
||||
editor.commit()
|
||||
}
|
||||
|
||||
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context)
|
||||
}
|
||||
private fun getDefaultSharedPreferencesEditor(context: Context): SharedPreferences.Editor {
|
||||
return getDefaultSharedPreferences(context).edit()
|
||||
}
|
||||
}
|
@ -7,7 +7,6 @@ import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.github.libretube.R
|
||||
|
||||
class ThemeHelper {
|
||||
@ -17,10 +16,8 @@ class ThemeHelper {
|
||||
updateThemeMode(context)
|
||||
}
|
||||
|
||||
fun updateAccentColor(context: Context) {
|
||||
val colorAccent =
|
||||
PreferenceManager.getDefaultSharedPreferences(context).getString("accent_color", "red")
|
||||
when (colorAccent) {
|
||||
private fun updateAccentColor(context: Context) {
|
||||
when (PreferenceHelper.getString(context, "accent_color", "red")) {
|
||||
"my" -> context.setTheme(R.style.Theme_MY)
|
||||
"red" -> context.setTheme(R.style.Theme_Red)
|
||||
"blue" -> context.setTheme(R.style.Theme_Blue)
|
||||
@ -30,10 +27,8 @@ class ThemeHelper {
|
||||
}
|
||||
}
|
||||
|
||||
fun updateThemeMode(context: Context) {
|
||||
val themeMode =
|
||||
PreferenceManager.getDefaultSharedPreferences(context).getString("theme_togglee", "A")
|
||||
when (themeMode) {
|
||||
private fun updateThemeMode(context: Context) {
|
||||
when (PreferenceHelper.getString(context, "theme_togglee", "A")) {
|
||||
"A" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
||||
"L" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||
"D" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
||||
|
Loading…
Reference in New Issue
Block a user