diff --git a/app/src/main/java/ani/dantotsu/connections/anilist/AnilistViewModel.kt b/app/src/main/java/ani/dantotsu/connections/anilist/AnilistViewModel.kt index 721b738481d..6a173584c9c 100644 --- a/app/src/main/java/ani/dantotsu/connections/anilist/AnilistViewModel.kt +++ b/app/src/main/java/ani/dantotsu/connections/anilist/AnilistViewModel.kt @@ -97,7 +97,7 @@ class AnilistHomeViewModel : ViewModel() { suspend fun setRecommendation() = recommendation.postValue(Anilist.query.recommendations()) suspend fun loadMain(context: FragmentActivity) { - Anilist.getSavedToken(context) + Anilist.getSavedToken() MAL.getSavedToken(context) Discord.getSavedToken(context) if (PrefManager.getVal(PrefName.CheckUpdate)) AppUpdater.check(context) diff --git a/app/src/main/java/ani/dantotsu/media/MediaDetailsViewModel.kt b/app/src/main/java/ani/dantotsu/media/MediaDetailsViewModel.kt index 35aa6415af0..9c3f39d60f9 100644 --- a/app/src/main/java/ani/dantotsu/media/MediaDetailsViewModel.kt +++ b/app/src/main/java/ani/dantotsu/media/MediaDetailsViewModel.kt @@ -43,7 +43,7 @@ class MediaDetailsViewModel : ViewModel() { fun loadSelected(media: Media, isDownload: Boolean = false): Selected { - val data = PrefManager.getNullableCustomVal("Selected-${media.id}", null) + val data = PrefManager.getNullableCustomVal("Selected-${media.id}", null, Selected::class.java) ?: Selected().let { it.sourceIndex = 0 it.preferDub = PrefManager.getVal(PrefName.SettingsPreferDub) diff --git a/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchAdapter.kt b/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchAdapter.kt index 5026829e2a1..6950366e858 100644 --- a/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchAdapter.kt +++ b/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchAdapter.kt @@ -356,7 +356,7 @@ class AnimeWatchAdapter( val episodes = media.anime.episodes!!.keys.toTypedArray() val anilistEp = (media.userProgress ?: 0).plus(1) - val appEp = PrefManager.getNullableCustomVal("${media.id}_current_ep", null)?.toIntOrNull() ?: 1 + val appEp = PrefManager.getCustomVal("${media.id}_current_ep", "")?.toIntOrNull() ?: 1 var continueEp = (if (anilistEp > appEp) anilistEp else appEp).toString() if (episodes.contains(continueEp)) { diff --git a/app/src/main/java/ani/dantotsu/media/anime/EpisodeAdapters.kt b/app/src/main/java/ani/dantotsu/media/anime/EpisodeAdapters.kt index dc61a781922..53278072c23 100644 --- a/app/src/main/java/ani/dantotsu/media/anime/EpisodeAdapters.kt +++ b/app/src/main/java/ani/dantotsu/media/anime/EpisodeAdapters.kt @@ -29,8 +29,8 @@ import kotlin.math.ln import kotlin.math.pow fun handleProgress(cont: LinearLayout, bar: View, empty: View, mediaId: Int, ep: String) { - val curr = PrefManager.getNullableCustomVal("${mediaId}_${ep}", null as Long?) - val max = PrefManager.getNullableCustomVal("${mediaId}_${ep}_max", null as Long?) + val curr = PrefManager.getNullableCustomVal("${mediaId}_${ep}", null, Long::class.java) + val max = PrefManager.getNullableCustomVal("${mediaId}_${ep}_max", null, Long::class.java) if (curr != null && max != null) { cont.visibility = View.VISIBLE val div = curr.toFloat() / max.toFloat() diff --git a/app/src/main/java/ani/dantotsu/media/manga/MangaReadAdapter.kt b/app/src/main/java/ani/dantotsu/media/manga/MangaReadAdapter.kt index 8e931503702..c39e0325083 100644 --- a/app/src/main/java/ani/dantotsu/media/manga/MangaReadAdapter.kt +++ b/app/src/main/java/ani/dantotsu/media/manga/MangaReadAdapter.kt @@ -389,7 +389,7 @@ class MangaReadAdapter( if (media.manga?.chapters != null) { val chapters = media.manga.chapters!!.keys.toTypedArray() val anilistEp = (media.userProgress ?: 0).plus(1) - val appEp = PrefManager.getNullableCustomVal("${media.id}_current_chp", null)?.toIntOrNull() ?: 1 + val appEp = PrefManager.getCustomVal("${media.id}_current_chp", null)?.toIntOrNull() ?: 1 var continueEp = (if (anilistEp > appEp) anilistEp else appEp).toString() val filteredChapters = chapters.filter { chapterKey -> val chapter = media.manga.chapters!![chapterKey]!! diff --git a/app/src/main/java/ani/dantotsu/parsers/AnimeParser.kt b/app/src/main/java/ani/dantotsu/parsers/AnimeParser.kt index 001197db48b..4710808024b 100644 --- a/app/src/main/java/ani/dantotsu/parsers/AnimeParser.kt +++ b/app/src/main/java/ani/dantotsu/parsers/AnimeParser.kt @@ -181,7 +181,7 @@ abstract class AnimeParser : BaseParser() { override suspend fun loadSavedShowResponse(mediaId: Int): ShowResponse? { checkIfVariablesAreEmpty() val dub = if (isDubAvailableSeparately()) "_${if (selectDub) "dub" else "sub"}" else "" - var loaded = PrefManager.getNullableCustomVal("${saveName}${dub}_$mediaId", null) + var loaded = PrefManager.getNullableCustomVal("${saveName}${dub}_$mediaId", null, ShowResponse::class.java) if (loaded == null && malSyncBackupName.isNotEmpty()) loaded = MalSyncBackup.get(mediaId, malSyncBackupName, selectDub) ?.also { saveShowResponse(mediaId, it, true) } diff --git a/app/src/main/java/ani/dantotsu/parsers/BaseParser.kt b/app/src/main/java/ani/dantotsu/parsers/BaseParser.kt index b47ebc567cc..79cbb9a7cf9 100644 --- a/app/src/main/java/ani/dantotsu/parsers/BaseParser.kt +++ b/app/src/main/java/ani/dantotsu/parsers/BaseParser.kt @@ -134,7 +134,7 @@ abstract class BaseParser { * **/ open suspend fun loadSavedShowResponse(mediaId: Int): ShowResponse? { checkIfVariablesAreEmpty() - return PrefManager.getNullableCustomVal("${saveName}_$mediaId", null) + return PrefManager.getNullableCustomVal("${saveName}_$mediaId", null, ShowResponse::class.java) } /** diff --git a/app/src/main/java/ani/dantotsu/settings/SettingsActivity.kt b/app/src/main/java/ani/dantotsu/settings/SettingsActivity.kt index 432e2d9c767..03d40308f00 100644 --- a/app/src/main/java/ani/dantotsu/settings/SettingsActivity.kt +++ b/app/src/main/java/ani/dantotsu/settings/SettingsActivity.kt @@ -752,7 +752,7 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene if (Anilist.token != null) { binding.settingsAnilistLogin.setText(R.string.logout) binding.settingsAnilistLogin.setOnClickListener { - Anilist.removeSavedToken(it.context) + Anilist.removeSavedToken() restartMainActivity.isEnabled = true reload() } diff --git a/app/src/main/java/ani/dantotsu/settings/SettingsDialogFragment.kt b/app/src/main/java/ani/dantotsu/settings/SettingsDialogFragment.kt index 4d9202384a0..6d4b728508c 100644 --- a/app/src/main/java/ani/dantotsu/settings/SettingsDialogFragment.kt +++ b/app/src/main/java/ani/dantotsu/settings/SettingsDialogFragment.kt @@ -60,7 +60,7 @@ class SettingsDialogFragment : BottomSheetDialogFragment() { if (Anilist.token != null) { binding.settingsLogin.setText(R.string.logout) binding.settingsLogin.setOnClickListener { - Anilist.removeSavedToken(it.context) + Anilist.removeSavedToken() dismiss() startMainActivity(requireActivity()) } diff --git a/app/src/main/java/ani/dantotsu/settings/saving/PrefManager.kt b/app/src/main/java/ani/dantotsu/settings/saving/PrefManager.kt index dce2c608c9e..46f53740b4b 100644 --- a/app/src/main/java/ani/dantotsu/settings/saving/PrefManager.kt +++ b/app/src/main/java/ani/dantotsu/settings/saving/PrefManager.kt @@ -125,15 +125,15 @@ object PrefManager { } @Suppress("UNCHECKED_CAST") - fun getNullableCustomVal(key: String, default: T): T? { + fun getNullableCustomVal(key: String, default: T?, clazz: Class): T? { return try { - when (default) { - is Boolean -> irrelevantPreferences!!.getBoolean(key, default) as T? - is Int -> irrelevantPreferences!!.getInt(key, default) as T? - is Float -> irrelevantPreferences!!.getFloat(key, default) as T? - is Long -> irrelevantPreferences!!.getLong(key, default) as T? - is String -> irrelevantPreferences!!.getString(key, default) as T? - is Set<*> -> convertFromStringSet(irrelevantPreferences!!.getStringSet(key, null), default) as T? + when { + clazz.isAssignableFrom(Boolean::class.java) -> irrelevantPreferences!!.getBoolean(key, default as? Boolean ?: false) as T? + clazz.isAssignableFrom(Int::class.java) -> irrelevantPreferences!!.getInt(key, default as? Int ?: 0) as T? + clazz.isAssignableFrom(Float::class.java) -> irrelevantPreferences!!.getFloat(key, default as? Float ?: 0f) as T? + clazz.isAssignableFrom(Long::class.java) -> irrelevantPreferences!!.getLong(key, default as? Long ?: 0L) as T? + clazz.isAssignableFrom(String::class.java) -> irrelevantPreferences!!.getString(key, default as? String) as T? + clazz.isAssignableFrom(Set::class.java) -> convertFromStringSet(irrelevantPreferences!!.getStringSet(key, null), default) as T? else -> deserializeClass(key, default, Location.Irrelevant) } } catch (e: Exception) { @@ -141,6 +141,7 @@ object PrefManager { } } + fun removeVal(prefName: PrefName) { val pref = getPrefLocation(prefName.data.prefLocation) with(pref.edit()) { @@ -256,6 +257,7 @@ object PrefManager { fun importAllPrefs(prefs: Map, prefLocation: Location) { val pref = getPrefLocation(prefLocation) var hadError = false + pref.edit().clear().apply() with(pref.edit()) { prefs.forEach { (key, value) -> when (value) { @@ -361,6 +363,7 @@ object PrefManager { } } catch (e: Exception) { snackString("Error deserializing preference: ${e.message}") + e.printStackTrace() default } } diff --git a/app/src/main/java/ani/dantotsu/subcriptions/SubscriptionHelper.kt b/app/src/main/java/ani/dantotsu/subcriptions/SubscriptionHelper.kt index bdf74660751..da4468c2053 100644 --- a/app/src/main/java/ani/dantotsu/subcriptions/SubscriptionHelper.kt +++ b/app/src/main/java/ani/dantotsu/subcriptions/SubscriptionHelper.kt @@ -27,7 +27,7 @@ class SubscriptionHelper { isAdult: Boolean, isAnime: Boolean ): Selected { - val data = PrefManager.getNullableCustomVal("${mediaId}-select", null) ?: Selected().let { + val data = PrefManager.getNullableCustomVal("${mediaId}-select", null, Selected::class.java) ?: Selected().let { it.sourceIndex = 0 it.preferDub = PrefManager.getVal(PrefName.SettingsPreferDub) it @@ -124,7 +124,7 @@ class SubscriptionHelper { private const val subscriptions = "subscriptions" fun getSubscriptions(): Map = - PrefManager.getNullableCustomVal?>(subscriptions, null) + PrefManager.getNullableCustomVal>(subscriptions, null, Map::class.java)!! ?: mapOf().also { PrefManager.setCustomVal(subscriptions, it) } fun saveSubscription(context: Context, media: Media, subscribed: Boolean) {