Skip to content

Commit

Permalink
remove unnecessary context (build failing)
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed Feb 2, 2024
1 parent 025d311 commit 97d062f
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MediaDetailsViewModel : ViewModel() {


fun loadSelected(media: Media, isDownload: Boolean = false): Selected {
val data = PrefManager.getNullableCustomVal<Selected?>("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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class AnimeWatchAdapter(
val episodes = media.anime.episodes!!.keys.toTypedArray()

val anilistEp = (media.userProgress ?: 0).plus(1)
val appEp = PrefManager.getNullableCustomVal<String?>("${media.id}_current_ep", null)?.toIntOrNull() ?: 1
val appEp = PrefManager.getCustomVal<String?>("${media.id}_current_ep", "")?.toIntOrNull() ?: 1

var continueEp = (if (anilistEp > appEp) anilistEp else appEp).toString()
if (episodes.contains(continueEp)) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/ani/dantotsu/media/anime/EpisodeAdapters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String?>("${media.id}_current_chp", null)?.toIntOrNull() ?: 1
val appEp = PrefManager.getCustomVal<String?>("${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]!!
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/ani/dantotsu/parsers/AnimeParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShowResponse?>("${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) }
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/ani/dantotsu/parsers/BaseParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ abstract class BaseParser {
* **/
open suspend fun loadSavedShowResponse(mediaId: Int): ShowResponse? {
checkIfVariablesAreEmpty()
return PrefManager.getNullableCustomVal<ShowResponse?>("${saveName}_$mediaId", null)
return PrefManager.getNullableCustomVal("${saveName}_$mediaId", null, ShowResponse::class.java)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
19 changes: 11 additions & 8 deletions app/src/main/java/ani/dantotsu/settings/saving/PrefManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,23 @@ object PrefManager {
}

@Suppress("UNCHECKED_CAST")
fun <T> getNullableCustomVal(key: String, default: T): T? {
fun <T> getNullableCustomVal(key: String, default: T?, clazz: Class<T>): 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) {
default
}
}


fun removeVal(prefName: PrefName) {
val pref = getPrefLocation(prefName.data.prefLocation)
with(pref.edit()) {
Expand Down Expand Up @@ -256,6 +257,7 @@ object PrefManager {
fun importAllPrefs(prefs: Map<String, *>, prefLocation: Location) {
val pref = getPrefLocation(prefLocation)
var hadError = false
pref.edit().clear().apply()
with(pref.edit()) {
prefs.forEach { (key, value) ->
when (value) {
Expand Down Expand Up @@ -361,6 +363,7 @@ object PrefManager {
}
} catch (e: Exception) {
snackString("Error deserializing preference: ${e.message}")
e.printStackTrace()
default
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SubscriptionHelper {
isAdult: Boolean,
isAnime: Boolean
): Selected {
val data = PrefManager.getNullableCustomVal<Selected?>("${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
Expand Down Expand Up @@ -124,7 +124,7 @@ class SubscriptionHelper {

private const val subscriptions = "subscriptions"
fun getSubscriptions(): Map<Int, SubscribeMedia> =
PrefManager.getNullableCustomVal<Map<Int, SubscribeMedia>?>(subscriptions, null)
PrefManager.getNullableCustomVal<Map<Int, SubscribeMedia>>(subscriptions, null, Map::class.java)!!
?: mapOf<Int, SubscribeMedia>().also { PrefManager.setCustomVal(subscriptions, it) }

fun saveSubscription(context: Context, media: Media, subscribed: Boolean) {
Expand Down

0 comments on commit 97d062f

Please sign in to comment.