backend preference wrapper
This commit is contained in:
parent
eb5b83564f
commit
8020b32541
66 changed files with 482 additions and 458 deletions
|
@ -16,7 +16,6 @@ import androidx.viewpager2.adapter.FragmentStateAdapter
|
|||
import androidx.viewpager2.widget.ViewPager2
|
||||
import ani.dantotsu.*
|
||||
import ani.dantotsu.databinding.ActivityExtensionsBinding
|
||||
import ani.dantotsu.others.LangSet
|
||||
import ani.dantotsu.themes.ThemeManager
|
||||
import com.google.android.material.tabs.TabLayout
|
||||
import com.google.android.material.tabs.TabLayoutMediator
|
||||
|
@ -27,7 +26,7 @@ class ExtensionsActivity : AppCompatActivity() {
|
|||
@SuppressLint("SetTextI18n")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
LangSet.setLocale(this)
|
||||
|
||||
ThemeManager(this).applyTheme()
|
||||
binding = ActivityExtensionsBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
|
|
@ -7,7 +7,6 @@ import ani.dantotsu.R
|
|||
import ani.dantotsu.currContext
|
||||
import ani.dantotsu.databinding.ActivityFaqBinding
|
||||
import ani.dantotsu.initActivity
|
||||
import ani.dantotsu.others.LangSet
|
||||
import ani.dantotsu.themes.ThemeManager
|
||||
|
||||
class FAQActivity : AppCompatActivity() {
|
||||
|
@ -101,7 +100,7 @@ class FAQActivity : AppCompatActivity() {
|
|||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
LangSet.setLocale(this)
|
||||
|
||||
ThemeManager(this).applyTheme()
|
||||
binding = ActivityFaqBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
|
|
@ -17,7 +17,6 @@ import ani.dantotsu.initActivity
|
|||
import ani.dantotsu.loadData
|
||||
import ani.dantotsu.media.Media
|
||||
import ani.dantotsu.navBarHeight
|
||||
import ani.dantotsu.others.LangSet
|
||||
import ani.dantotsu.others.getSerialized
|
||||
import ani.dantotsu.parsers.Subtitle
|
||||
import ani.dantotsu.saveData
|
||||
|
@ -39,7 +38,7 @@ class PlayerSettingsActivity : AppCompatActivity() {
|
|||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
LangSet.setLocale(this)
|
||||
|
||||
ThemeManager(this).applyTheme()
|
||||
binding = ActivityPlayerSettingsBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
|
|
@ -9,7 +9,6 @@ import ani.dantotsu.databinding.ActivityReaderSettingsBinding
|
|||
import ani.dantotsu.initActivity
|
||||
import ani.dantotsu.loadData
|
||||
import ani.dantotsu.navBarHeight
|
||||
import ani.dantotsu.others.LangSet
|
||||
import ani.dantotsu.saveData
|
||||
import ani.dantotsu.snackString
|
||||
import ani.dantotsu.statusBarHeight
|
||||
|
@ -20,7 +19,7 @@ class ReaderSettingsActivity : AppCompatActivity() {
|
|||
private val reader = "reader_settings"
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
LangSet.setLocale(this)
|
||||
|
||||
ThemeManager(this).applyTheme()
|
||||
binding = ActivityReaderSettingsBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
|
|
@ -32,9 +32,10 @@ import ani.dantotsu.download.DownloadsManager
|
|||
import ani.dantotsu.download.video.ExoplayerDownloadService
|
||||
import ani.dantotsu.others.AppUpdater
|
||||
import ani.dantotsu.others.CustomBottomDialog
|
||||
import ani.dantotsu.others.LangSet
|
||||
import ani.dantotsu.parsers.AnimeSources
|
||||
import ani.dantotsu.parsers.MangaSources
|
||||
import ani.dantotsu.settings.saving.PrefName
|
||||
import ani.dantotsu.settings.saving.PrefWrapper
|
||||
import ani.dantotsu.subcriptions.Notifications
|
||||
import ani.dantotsu.subcriptions.Notifications.Companion.openSettings
|
||||
import ani.dantotsu.subcriptions.Subscription.Companion.defaultTime
|
||||
|
@ -71,7 +72,7 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
@SuppressLint("SetTextI18n")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
LangSet.setLocale(this)
|
||||
|
||||
ThemeManager(this).applyTheme()
|
||||
binding = ActivitySettingsBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
@ -96,26 +97,16 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
onBackPressedDispatcher.onBackPressed()
|
||||
}
|
||||
|
||||
binding.settingsUseMaterialYou.isChecked =
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getBoolean(
|
||||
"use_material_you",
|
||||
false
|
||||
)
|
||||
binding.settingsUseMaterialYou.isChecked = PrefWrapper.getVal(PrefName.UseMaterialYou, false)
|
||||
binding.settingsUseMaterialYou.setOnCheckedChangeListener { _, isChecked ->
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).edit()
|
||||
.putBoolean("use_material_you", isChecked).apply()
|
||||
PrefWrapper.setVal(PrefName.UseMaterialYou, isChecked)
|
||||
if (isChecked) binding.settingsUseCustomTheme.isChecked = false
|
||||
restartApp()
|
||||
}
|
||||
|
||||
binding.settingsUseCustomTheme.isChecked =
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getBoolean(
|
||||
"use_custom_theme",
|
||||
false
|
||||
)
|
||||
binding.settingsUseCustomTheme.isChecked = PrefWrapper.getVal(PrefName.UseCustomTheme, false)
|
||||
binding.settingsUseCustomTheme.setOnCheckedChangeListener { _, isChecked ->
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).edit()
|
||||
.putBoolean("use_custom_theme", isChecked).apply()
|
||||
PrefWrapper.setVal(PrefName.UseCustomTheme, isChecked)
|
||||
if (isChecked) {
|
||||
binding.settingsUseMaterialYou.isChecked = false
|
||||
}
|
||||
|
@ -123,26 +114,19 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
restartApp()
|
||||
}
|
||||
|
||||
binding.settingsUseSourceTheme.isChecked =
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getBoolean(
|
||||
"use_source_theme",
|
||||
false
|
||||
)
|
||||
binding.settingsUseSourceTheme.isChecked = PrefWrapper.getVal(PrefName.UseSourceTheme, false)
|
||||
binding.settingsUseSourceTheme.setOnCheckedChangeListener { _, isChecked ->
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).edit()
|
||||
.putBoolean("use_source_theme", isChecked).apply()
|
||||
}
|
||||
|
||||
binding.settingsUseOLED.isChecked =
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getBoolean("use_oled", false)
|
||||
binding.settingsUseOLED.setOnCheckedChangeListener { _, isChecked ->
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).edit()
|
||||
.putBoolean("use_oled", isChecked).apply()
|
||||
PrefWrapper.setVal(PrefName.UseSourceTheme, isChecked)
|
||||
restartApp()
|
||||
}
|
||||
|
||||
val themeString =
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getString("theme", "PURPLE")!!
|
||||
binding.settingsUseOLED.isChecked = PrefWrapper.getVal(PrefName.UseOLED, false)
|
||||
binding.settingsUseOLED.setOnCheckedChangeListener { _, isChecked ->
|
||||
PrefWrapper.setVal(PrefName.UseOLED, isChecked)
|
||||
restartApp()
|
||||
}
|
||||
|
||||
val themeString = PrefWrapper.getVal(PrefName.Theme, "PURPLE")
|
||||
binding.themeSwitcher.setText(
|
||||
themeString.substring(0, 1) + themeString.substring(1).lowercase()
|
||||
)
|
||||
|
@ -151,13 +135,12 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
ArrayAdapter(
|
||||
this,
|
||||
R.layout.item_dropdown,
|
||||
ThemeManager.Companion.Theme.values()
|
||||
ThemeManager.Companion.Theme.entries
|
||||
.map { it.theme.substring(0, 1) + it.theme.substring(1).lowercase() })
|
||||
)
|
||||
|
||||
binding.themeSwitcher.setOnItemClickListener { _, _, i, _ ->
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).edit()
|
||||
.putString("theme", ThemeManager.Companion.Theme.values()[i].theme).apply()
|
||||
PrefWrapper.setVal(PrefName.Theme, ThemeManager.Companion.Theme.entries[i].theme)
|
||||
//ActivityHelper.shouldRefreshMainActivity = true
|
||||
binding.themeSwitcher.clearFocus()
|
||||
restartApp()
|
||||
|
@ -166,10 +149,7 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
|
||||
|
||||
binding.customTheme.setOnClickListener {
|
||||
val originalColor = getSharedPreferences("Dantotsu", MODE_PRIVATE).getInt(
|
||||
"custom_theme_int",
|
||||
Color.parseColor("#6200EE")
|
||||
)
|
||||
val originalColor = PrefWrapper.getVal(PrefName.CustomThemeInt, Color.parseColor("#6200EE"))
|
||||
|
||||
class CustomColorDialog : SimpleColorDialog() { //idk where to put it
|
||||
override fun onPositiveButtonClick() {
|
||||
|
@ -196,11 +176,8 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
val names = animeSourcesWithoutDownloadsSource.map { it.name }
|
||||
val pinnedSourcesBoolean =
|
||||
animeSourcesWithoutDownloadsSource.map { it.name in AnimeSources.pinnedAnimeSources }
|
||||
val pinnedSourcesOriginal = getSharedPreferences(
|
||||
"Dantotsu",
|
||||
Context.MODE_PRIVATE
|
||||
).getStringSet("pinned_anime_sources", null)
|
||||
val pinnedSources = pinnedSourcesOriginal?.toMutableSet() ?: mutableSetOf()
|
||||
val pinnedSourcesOriginal = PrefWrapper.getVal(PrefName.PinnedAnimeSources, setOf<String>())
|
||||
val pinnedSources = pinnedSourcesOriginal.toMutableSet() ?: mutableSetOf()
|
||||
val alertDialog = AlertDialog.Builder(this, R.style.MyPopup)
|
||||
.setTitle("Pinned Anime Sources")
|
||||
.setMultiChoiceItems(
|
||||
|
@ -214,8 +191,7 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
}
|
||||
}
|
||||
.setPositiveButton("OK") { dialog, _ ->
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).edit()
|
||||
.putStringSet("pinned_anime_sources", pinnedSources).apply()
|
||||
PrefWrapper.setVal(PrefName.PinnedAnimeSources, pinnedSources)
|
||||
AnimeSources.pinnedAnimeSources = pinnedSources
|
||||
AnimeSources.performReorderAnimeSources()
|
||||
dialog.dismiss()
|
||||
|
@ -395,15 +371,9 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
binding.settingsRecentlyListOnly.setOnCheckedChangeListener { _, isChecked ->
|
||||
saveData("recently_list_only", isChecked)
|
||||
}
|
||||
binding.settingsShareUsername.isChecked = getSharedPreferences(
|
||||
getString(R.string.preference_file_key),
|
||||
Context.MODE_PRIVATE
|
||||
).getBoolean("shared_user_id", true)
|
||||
binding.settingsShareUsername.isChecked = PrefWrapper.getVal(PrefName.SharedUserID, true)
|
||||
binding.settingsShareUsername.setOnCheckedChangeListener { _, isChecked ->
|
||||
getSharedPreferences(
|
||||
getString(R.string.preference_file_key),
|
||||
Context.MODE_PRIVATE
|
||||
).edit().putBoolean("shared_user_id", isChecked).apply()
|
||||
PrefWrapper.setVal(PrefName.SharedUserID, isChecked)
|
||||
}
|
||||
|
||||
binding.settingsPreferDub.isChecked = loadData("settings_prefer_dub") ?: false
|
||||
|
@ -417,11 +387,8 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
val names = mangaSourcesWithoutDownloadsSource.map { it.name }
|
||||
val pinnedSourcesBoolean =
|
||||
mangaSourcesWithoutDownloadsSource.map { it.name in MangaSources.pinnedMangaSources }
|
||||
val pinnedSourcesOriginal = getSharedPreferences(
|
||||
"Dantotsu",
|
||||
Context.MODE_PRIVATE
|
||||
).getStringSet("pinned_manga_sources", null)
|
||||
val pinnedSources = pinnedSourcesOriginal?.toMutableSet() ?: mutableSetOf()
|
||||
val pinnedSourcesOriginal = PrefWrapper.getVal(PrefName.PinnedMangaSources, setOf<String>())
|
||||
val pinnedSources = pinnedSourcesOriginal.toMutableSet()
|
||||
val alertDialog = AlertDialog.Builder(this, R.style.MyPopup)
|
||||
.setTitle("Pinned Manga Sources")
|
||||
.setMultiChoiceItems(
|
||||
|
@ -435,8 +402,7 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
}
|
||||
}
|
||||
.setPositiveButton("OK") { dialog, _ ->
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).edit()
|
||||
.putStringSet("pinned_manga_sources", pinnedSources).apply()
|
||||
PrefWrapper.setVal(PrefName.PinnedMangaSources, pinnedSources)
|
||||
MangaSources.pinnedMangaSources = pinnedSources
|
||||
MangaSources.performReorderMangaSources()
|
||||
dialog.dismiss()
|
||||
|
@ -605,13 +571,7 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
Toast.makeText(this, "youwu have been cuwsed :pwayge:", Toast.LENGTH_LONG).show()
|
||||
val url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
||||
openLinkInBrowser(url)
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).edit().putBoolean(
|
||||
"use_cursed_lang",
|
||||
getSharedPreferences(
|
||||
"Dantotsu",
|
||||
Context.MODE_PRIVATE
|
||||
).getBoolean("use_cursed_lang", false).not()
|
||||
).apply()
|
||||
//PrefWrapper.setVal(PrefName.SomethingSpecial, !PrefWrapper.getVal(PrefName.SomethingSpecial, false))
|
||||
} else {
|
||||
snackString(array[(Math.random() * array.size).toInt()], this)
|
||||
}
|
||||
|
@ -772,18 +732,9 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
}
|
||||
|
||||
if (Discord.token != null) {
|
||||
val id = getSharedPreferences(
|
||||
getString(R.string.preference_file_key),
|
||||
Context.MODE_PRIVATE
|
||||
).getString("discord_id", null)
|
||||
val avatar = getSharedPreferences(
|
||||
getString(R.string.preference_file_key),
|
||||
Context.MODE_PRIVATE
|
||||
).getString("discord_avatar", null)
|
||||
val username = getSharedPreferences(
|
||||
getString(R.string.preference_file_key),
|
||||
Context.MODE_PRIVATE
|
||||
).getString("discord_username", null)
|
||||
val id = PrefWrapper.getVal(PrefName.DiscordId, null as String?)
|
||||
val avatar = PrefWrapper.getVal(PrefName.DiscordAvatar, null as String?)
|
||||
val username = PrefWrapper.getVal(PrefName.DiscordUserName, null as String?)
|
||||
if (id != null && avatar != null) {
|
||||
binding.settingsDiscordAvatar.loadImage("https://cdn.discordapp.com/avatars/$id/$avatar.png")
|
||||
}
|
||||
|
@ -838,8 +789,7 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
|
|||
if (which == BUTTON_POSITIVE) {
|
||||
if (dialogTag == "colorPicker") {
|
||||
val color = extras.getInt(SimpleColorDialog.COLOR)
|
||||
getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).edit()
|
||||
.putInt("custom_theme_int", color).apply()
|
||||
PrefWrapper.setVal(PrefName.CustomThemeInt, color)
|
||||
logger("Custom Theme: $color")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package ani.dantotsu.settings
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
|
@ -26,6 +25,8 @@ import ani.dantotsu.offline.OfflineFragment
|
|||
import ani.dantotsu.openLinkInBrowser
|
||||
import ani.dantotsu.others.imagesearch.ImageSearchActivity
|
||||
import ani.dantotsu.setSafeOnClickListener
|
||||
import ani.dantotsu.settings.saving.PrefName
|
||||
import ani.dantotsu.settings.saving.PrefWrapper
|
||||
import ani.dantotsu.startMainActivity
|
||||
|
||||
class SettingsDialogFragment : BottomSheetDialogFragment() {
|
||||
|
@ -75,14 +76,10 @@ class SettingsDialogFragment : BottomSheetDialogFragment() {
|
|||
}
|
||||
|
||||
binding.settingsIncognito.isChecked =
|
||||
context?.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE)?.getBoolean(
|
||||
"incognito",
|
||||
false
|
||||
) ?: false
|
||||
PrefWrapper.getVal(PrefName.Incognito, false)
|
||||
|
||||
binding.settingsIncognito.setOnCheckedChangeListener { _, isChecked ->
|
||||
context?.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE)?.edit()
|
||||
?.putBoolean("incognito", isChecked)?.apply()
|
||||
PrefWrapper.setVal(PrefName.Incognito, isChecked)
|
||||
incognitoNotification(requireContext())
|
||||
}
|
||||
binding.settingsExtensionSettings.setSafeOnClickListener {
|
||||
|
@ -102,9 +99,7 @@ class SettingsDialogFragment : BottomSheetDialogFragment() {
|
|||
dismiss()
|
||||
}
|
||||
|
||||
binding.settingsDownloads.isChecked =
|
||||
context?.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE)
|
||||
?.getBoolean("offlineMode", false) ?: false
|
||||
binding.settingsDownloads.isChecked = PrefWrapper.getVal(PrefName.OfflineMode, false)
|
||||
binding.settingsDownloads.setOnCheckedChangeListener { _, isChecked ->
|
||||
when (pageType) {
|
||||
PageType.MANGA -> {
|
||||
|
@ -154,8 +149,7 @@ class SettingsDialogFragment : BottomSheetDialogFragment() {
|
|||
}
|
||||
|
||||
dismiss()
|
||||
context?.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE)?.edit()
|
||||
?.putBoolean("offlineMode", isChecked)?.apply()
|
||||
PrefWrapper.setVal(PrefName.OfflineMode, isChecked)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@ import ani.dantotsu.databinding.ActivityUserInterfaceSettingsBinding
|
|||
import ani.dantotsu.initActivity
|
||||
import ani.dantotsu.loadData
|
||||
import ani.dantotsu.navBarHeight
|
||||
import ani.dantotsu.others.LangSet
|
||||
import ani.dantotsu.saveData
|
||||
import ani.dantotsu.statusBarHeight
|
||||
import ani.dantotsu.themes.ThemeManager
|
||||
|
@ -22,7 +21,7 @@ class UserInterfaceSettingsActivity : AppCompatActivity() {
|
|||
private val ui = "ui_settings"
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
LangSet.setLocale(this)
|
||||
|
||||
ThemeManager(this).applyTheme()
|
||||
binding = ActivityUserInterfaceSettingsBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
|
171
app/src/main/java/ani/dantotsu/settings/saving/PrefWrapper.kt
Normal file
171
app/src/main/java/ani/dantotsu/settings/saving/PrefWrapper.kt
Normal file
|
@ -0,0 +1,171 @@
|
|||
package ani.dantotsu.settings.saving
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import ani.dantotsu.settings.saving.internal.Location
|
||||
|
||||
object PrefWrapper {
|
||||
|
||||
private var generalPreferences: SharedPreferences? = null
|
||||
private var animePreferences: SharedPreferences? = null
|
||||
private var mangaPreferences: SharedPreferences? = null
|
||||
private var playerPreferences: SharedPreferences? = null
|
||||
private var readerPreferences: SharedPreferences? = null
|
||||
private var irrelevantPreferences: SharedPreferences? = null
|
||||
private var animeDownloadsPreferences: SharedPreferences? = null
|
||||
private var protectedPreferences: SharedPreferences? = null
|
||||
|
||||
fun init(context: Context) {
|
||||
generalPreferences = context.getSharedPreferences(Location.General.location, Context.MODE_PRIVATE)
|
||||
animePreferences = context.getSharedPreferences(Location.Anime.location, Context.MODE_PRIVATE)
|
||||
mangaPreferences = context.getSharedPreferences(Location.Manga.location, Context.MODE_PRIVATE)
|
||||
playerPreferences = context.getSharedPreferences(Location.Player.location, Context.MODE_PRIVATE)
|
||||
readerPreferences = context.getSharedPreferences(Location.Reader.location, Context.MODE_PRIVATE)
|
||||
irrelevantPreferences = context.getSharedPreferences(Location.Irrelevant.location, Context.MODE_PRIVATE)
|
||||
animeDownloadsPreferences = context.getSharedPreferences(Location.AnimeDownloads.location, Context.MODE_PRIVATE)
|
||||
protectedPreferences = context.getSharedPreferences(Location.Protected.location, Context.MODE_PRIVATE)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T> setVal(prefName: PrefName, value: T) {
|
||||
val pref = when (prefName.data.prefLocation) {
|
||||
Location.General -> generalPreferences
|
||||
Location.Anime -> animePreferences
|
||||
Location.Manga -> mangaPreferences
|
||||
Location.Player -> playerPreferences
|
||||
Location.Reader -> readerPreferences
|
||||
Location.Irrelevant -> irrelevantPreferences
|
||||
Location.AnimeDownloads -> animeDownloadsPreferences
|
||||
Location.Protected -> protectedPreferences
|
||||
}
|
||||
with(pref!!.edit()) {
|
||||
when (prefName.data.type) {
|
||||
Boolean::class -> putBoolean(prefName.name, value as Boolean)
|
||||
Int::class -> putInt(prefName.name, value as Int)
|
||||
Float::class -> putFloat(prefName.name, value as Float)
|
||||
Long::class -> putLong(prefName.name, value as Long)
|
||||
String::class -> putString(prefName.name, value as String)
|
||||
Set::class -> putStringSet(prefName.name, value as Set<String>)
|
||||
else -> throw IllegalArgumentException("Type not supported")
|
||||
}
|
||||
apply()
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T> getVal(prefName: PrefName, default: T) : T {
|
||||
return try {
|
||||
val pref = when (prefName.data.prefLocation) {
|
||||
Location.General -> generalPreferences
|
||||
Location.Anime -> animePreferences
|
||||
Location.Manga -> mangaPreferences
|
||||
Location.Player -> playerPreferences
|
||||
Location.Reader -> readerPreferences
|
||||
Location.Irrelevant -> irrelevantPreferences
|
||||
Location.AnimeDownloads -> animeDownloadsPreferences
|
||||
Location.Protected -> protectedPreferences
|
||||
}
|
||||
when (prefName.data.type) {
|
||||
Boolean::class -> pref!!.getBoolean(prefName.name, default as Boolean) as T
|
||||
Int::class -> pref!!.getInt(prefName.name, default as Int) as T
|
||||
Float::class -> pref!!.getFloat(prefName.name, default as Float) as T
|
||||
Long::class -> pref!!.getLong(prefName.name, default as Long) as T
|
||||
String::class -> pref!!.getString(prefName.name, default as String) as T
|
||||
Set::class -> pref!!.getStringSet(prefName.name, default as Set<String>) as T
|
||||
else -> throw IllegalArgumentException("Type not supported")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
default
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T> getLiveVal(prefName: PrefName, default: T) : SharedPreferenceLiveData<T> {
|
||||
val pref = when (prefName.data.prefLocation) {
|
||||
Location.General -> generalPreferences
|
||||
Location.Anime -> animePreferences
|
||||
Location.Manga -> mangaPreferences
|
||||
Location.Player -> playerPreferences
|
||||
Location.Reader -> readerPreferences
|
||||
Location.Irrelevant -> irrelevantPreferences
|
||||
Location.AnimeDownloads -> animeDownloadsPreferences
|
||||
Location.Protected -> protectedPreferences
|
||||
}
|
||||
return when (prefName.data.type) {
|
||||
Boolean::class -> SharedPreferenceBooleanLiveData(
|
||||
pref!!,
|
||||
prefName.name,
|
||||
default as Boolean
|
||||
) as SharedPreferenceLiveData<T>
|
||||
Int::class -> SharedPreferenceIntLiveData(
|
||||
pref!!,
|
||||
prefName.name,
|
||||
default as Int
|
||||
) as SharedPreferenceLiveData<T>
|
||||
Float::class -> SharedPreferenceFloatLiveData(
|
||||
pref!!,
|
||||
prefName.name,
|
||||
default as Float
|
||||
) as SharedPreferenceLiveData<T>
|
||||
Long::class -> SharedPreferenceLongLiveData(
|
||||
pref!!,
|
||||
prefName.name,
|
||||
default as Long
|
||||
) as SharedPreferenceLiveData<T>
|
||||
String::class -> SharedPreferenceStringLiveData(
|
||||
pref!!,
|
||||
prefName.name,
|
||||
default as String
|
||||
) as SharedPreferenceLiveData<T>
|
||||
Set::class -> SharedPreferenceStringSetLiveData(
|
||||
pref!!,
|
||||
prefName.name,
|
||||
default as Set<String>
|
||||
) as SharedPreferenceLiveData<T>
|
||||
else -> throw IllegalArgumentException("Type not supported")
|
||||
}
|
||||
}
|
||||
|
||||
fun removeVal(prefName: PrefName) {
|
||||
val pref = when (prefName.data.prefLocation) {
|
||||
Location.General -> generalPreferences
|
||||
Location.Anime -> animePreferences
|
||||
Location.Manga -> mangaPreferences
|
||||
Location.Player -> playerPreferences
|
||||
Location.Reader -> readerPreferences
|
||||
Location.Irrelevant -> irrelevantPreferences
|
||||
Location.AnimeDownloads -> animeDownloadsPreferences
|
||||
Location.Protected -> protectedPreferences
|
||||
}
|
||||
with(pref!!.edit()) {
|
||||
remove(prefName.name)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
|
||||
fun SharedPreferenceLiveData<*>.asLiveBool(): SharedPreferenceBooleanLiveData =
|
||||
this as? SharedPreferenceBooleanLiveData
|
||||
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Boolean>")
|
||||
|
||||
fun SharedPreferenceLiveData<*>.asLiveInt(): SharedPreferenceIntLiveData =
|
||||
this as? SharedPreferenceIntLiveData
|
||||
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Int>")
|
||||
|
||||
fun SharedPreferenceLiveData<*>.asLiveFloat(): SharedPreferenceFloatLiveData =
|
||||
this as? SharedPreferenceFloatLiveData
|
||||
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Float>")
|
||||
|
||||
fun SharedPreferenceLiveData<*>.asLiveLong(): SharedPreferenceLongLiveData =
|
||||
this as? SharedPreferenceLongLiveData
|
||||
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Long>")
|
||||
|
||||
fun SharedPreferenceLiveData<*>.asLiveString(): SharedPreferenceStringLiveData =
|
||||
this as? SharedPreferenceStringLiveData
|
||||
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<String>")
|
||||
|
||||
fun SharedPreferenceLiveData<*>.asLiveStringSet(): SharedPreferenceStringSetLiveData =
|
||||
this as? SharedPreferenceStringSetLiveData
|
||||
?: throw ClassCastException("Cannot cast to SharedPreferenceLiveData<Set<String>>")
|
||||
|
||||
fun getAnimeDownloadPreferences(): SharedPreferences = animeDownloadsPreferences!! //needs to be used externally
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package ani.dantotsu.settings.saving
|
||||
|
||||
import ani.dantotsu.settings.saving.internal.Pref
|
||||
import ani.dantotsu.settings.saving.internal.Location
|
||||
|
||||
enum class PrefName(val data: Pref) {
|
||||
//General
|
||||
SharedUserID(Pref(Location.General, Boolean::class)),
|
||||
OfflineView(Pref(Location.General, Int::class)),
|
||||
UseOLED(Pref(Location.General, Boolean::class)),
|
||||
UseCustomTheme(Pref(Location.General, Boolean::class)),
|
||||
CustomThemeInt(Pref(Location.General, Int::class)),
|
||||
UseSourceTheme(Pref(Location.General, Boolean::class)),
|
||||
UseMaterialYou(Pref(Location.General, Boolean::class)),
|
||||
Theme(Pref(Location.General, String::class)),
|
||||
|
||||
//Anime
|
||||
AnimeListSortOrder(Pref(Location.Anime, String::class)),
|
||||
PinnedAnimeSources(Pref(Location.Anime, Set::class)),
|
||||
PopularAnimeList(Pref(Location.Anime, Boolean::class)),
|
||||
AnimeSearchHistory(Pref(Location.Anime, Set::class)),
|
||||
|
||||
//Manga
|
||||
MangaListSortOrder(Pref(Location.Manga, String::class)),
|
||||
PinnedMangaSources(Pref(Location.Manga, Set::class)),
|
||||
PopularMangaList(Pref(Location.Manga, Boolean::class)),
|
||||
MangaSearchHistory(Pref(Location.Manga, Set::class)),
|
||||
|
||||
|
||||
//Irrelevant
|
||||
Incognito(Pref(Location.Irrelevant, Boolean::class)),
|
||||
OfflineMode(Pref(Location.Irrelevant, Boolean::class)),
|
||||
DownloadsKeys(Pref(Location.Irrelevant, String::class)),
|
||||
NovelLastExtCheck(Pref(Location.Irrelevant, Long::class)),
|
||||
SomethingSpecial(Pref(Location.Irrelevant, Boolean::class)),
|
||||
|
||||
//Protected
|
||||
DiscordToken(Pref(Location.Protected, String::class)),
|
||||
DiscordId(Pref(Location.Protected, String::class)),
|
||||
DiscordUserName(Pref(Location.Protected, String::class)),
|
||||
DiscordAvatar(Pref(Location.Protected, String::class)),
|
||||
AnilistUserName(Pref(Location.Protected, String::class)),
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
package ani.dantotsu.settings.saving
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import androidx.lifecycle.LiveData
|
||||
|
||||
abstract class SharedPreferenceLiveData<T>(
|
||||
val sharedPrefs: SharedPreferences,
|
||||
val key: String,
|
||||
val defValue: T
|
||||
) : LiveData<T>() {
|
||||
|
||||
private val preferenceChangeListener =
|
||||
SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
||||
if (key == this.key) {
|
||||
value = getValueFromPreferences(key, defValue)
|
||||
}
|
||||
}
|
||||
|
||||
abstract fun getValueFromPreferences(key: String, defValue: T): T
|
||||
|
||||
override fun onActive() {
|
||||
super.onActive()
|
||||
value = getValueFromPreferences(key, defValue)
|
||||
sharedPrefs.registerOnSharedPreferenceChangeListener(preferenceChangeListener)
|
||||
}
|
||||
|
||||
override fun onInactive() {
|
||||
sharedPrefs.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener)
|
||||
super.onInactive()
|
||||
}
|
||||
}
|
||||
|
||||
class SharedPreferenceIntLiveData(sharedPrefs: SharedPreferences, key: String, defValue: Int) :
|
||||
SharedPreferenceLiveData<Int>(sharedPrefs, key, defValue) {
|
||||
override fun getValueFromPreferences(key: String, defValue: Int): Int =
|
||||
sharedPrefs.getInt(key, defValue)
|
||||
}
|
||||
|
||||
class SharedPreferenceStringLiveData(
|
||||
sharedPrefs: SharedPreferences,
|
||||
key: String,
|
||||
defValue: String
|
||||
) :
|
||||
SharedPreferenceLiveData<String>(sharedPrefs, key, defValue) {
|
||||
override fun getValueFromPreferences(key: String, defValue: String): String =
|
||||
sharedPrefs.getString(key, defValue).toString()
|
||||
}
|
||||
|
||||
class SharedPreferenceBooleanLiveData(
|
||||
sharedPrefs: SharedPreferences,
|
||||
key: String,
|
||||
defValue: Boolean
|
||||
) :
|
||||
SharedPreferenceLiveData<Boolean>(sharedPrefs, key, defValue) {
|
||||
override fun getValueFromPreferences(key: String, defValue: Boolean): Boolean =
|
||||
sharedPrefs.getBoolean(key, defValue)
|
||||
}
|
||||
|
||||
class SharedPreferenceFloatLiveData(sharedPrefs: SharedPreferences, key: String, defValue: Float) :
|
||||
SharedPreferenceLiveData<Float>(sharedPrefs, key, defValue) {
|
||||
override fun getValueFromPreferences(key: String, defValue: Float): Float =
|
||||
sharedPrefs.getFloat(key, defValue)
|
||||
}
|
||||
|
||||
class SharedPreferenceLongLiveData(sharedPrefs: SharedPreferences, key: String, defValue: Long) :
|
||||
SharedPreferenceLiveData<Long>(sharedPrefs, key, defValue) {
|
||||
override fun getValueFromPreferences(key: String, defValue: Long): Long =
|
||||
sharedPrefs.getLong(key, defValue)
|
||||
}
|
||||
|
||||
class SharedPreferenceStringSetLiveData(
|
||||
sharedPrefs: SharedPreferences,
|
||||
key: String,
|
||||
defValue: Set<String>
|
||||
) :
|
||||
SharedPreferenceLiveData<Set<String>>(sharedPrefs, key, defValue) {
|
||||
override fun getValueFromPreferences(key: String, defValue: Set<String>): Set<String> =
|
||||
sharedPrefs.getStringSet(key, defValue)?.toSet() ?: defValue
|
||||
}
|
||||
|
||||
fun SharedPreferences.intLiveData(key: String, defValue: Int): SharedPreferenceLiveData<Int> {
|
||||
return SharedPreferenceIntLiveData(this, key, defValue)
|
||||
}
|
||||
|
||||
fun SharedPreferences.stringLiveData(
|
||||
key: String,
|
||||
defValue: String
|
||||
): SharedPreferenceLiveData<String> {
|
||||
return SharedPreferenceStringLiveData(this, key, defValue)
|
||||
}
|
||||
|
||||
fun SharedPreferences.booleanLiveData(
|
||||
key: String,
|
||||
defValue: Boolean
|
||||
): SharedPreferenceLiveData<Boolean> {
|
||||
return SharedPreferenceBooleanLiveData(this, key, defValue)
|
||||
}
|
||||
|
||||
fun SharedPreferences.floatLiveData(key: String, defValue: Float): SharedPreferenceLiveData<Float> {
|
||||
return SharedPreferenceFloatLiveData(this, key, defValue)
|
||||
}
|
||||
|
||||
fun SharedPreferences.longLiveData(key: String, defValue: Long): SharedPreferenceLiveData<Long> {
|
||||
return SharedPreferenceLongLiveData(this, key, defValue)
|
||||
}
|
||||
|
||||
fun SharedPreferences.stringSetLiveData(
|
||||
key: String,
|
||||
defValue: Set<String>
|
||||
): SharedPreferenceLiveData<Set<String>> {
|
||||
return SharedPreferenceStringSetLiveData(this, key, defValue)
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package ani.dantotsu.settings.saving.internal
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
|
||||
data class Pref(
|
||||
val prefLocation: Location,
|
||||
val type: KClass<*>
|
||||
)
|
||||
enum class Location(val location: String) {
|
||||
General("ani.dantotsu.general"),
|
||||
Anime("ani.dantotsu.anime"),
|
||||
Manga("ani.dantotsu.manga"),
|
||||
Player("ani.dantotsu.player"),
|
||||
Reader("ani.dantotsu.reader"),
|
||||
Irrelevant("ani.dantotsu.irrelevant"),
|
||||
AnimeDownloads("animeDownloads"), //different for legacy reasons
|
||||
Protected("ani.dantotsu.protected")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue