feat: view subscriptions in settings

This commit is contained in:
rebelonion 2024-05-17 08:57:59 -05:00
parent f1d16ba16a
commit 6c1176a182
14 changed files with 221 additions and 15 deletions

View file

@ -5,14 +5,18 @@ import ani.dantotsu.currContext
import ani.dantotsu.media.Media
import ani.dantotsu.media.MediaNameAdapter
import ani.dantotsu.media.Selected
import ani.dantotsu.media.emptyMedia
import ani.dantotsu.parsers.AnimeParser
import ani.dantotsu.parsers.AnimeSources
import ani.dantotsu.parsers.BaseParser
import ani.dantotsu.parsers.Episode
import ani.dantotsu.parsers.MangaChapter
import ani.dantotsu.parsers.MangaParser
import ani.dantotsu.parsers.MangaSources
import ani.dantotsu.parsers.ShowResponse
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.toast
import ani.dantotsu.tryWithSuspend
import ani.dantotsu.util.Logger
import kotlinx.coroutines.withTimeoutOrNull
@ -50,16 +54,18 @@ class SubscriptionHelper {
suspend fun getEpisode(
parser: AnimeParser,
id: Int
subscribeMedia: SubscribeMedia
): Episode? {
val selected = loadSelected(id)
val selected = loadSelected(subscribeMedia.id)
val ep = withTimeoutOrNull(10 * 1000) {
tryWithSuspend {
val show = parser.loadSavedShowResponse(id) ?: throw Exception(
val show = parser.loadSavedShowResponse(subscribeMedia.id)
?: forceLoadShowResponse(subscribeMedia, selected, parser)
?: throw Exception(
currContext()?.getString(
R.string.failed_to_load_data,
id
subscribeMedia.id
)
)
show.sAnime?.let {
@ -73,7 +79,7 @@ class SubscriptionHelper {
return ep?.apply {
selected.latest = number.toFloat()
saveSelected(id, selected)
saveSelected(subscribeMedia.id, selected)
}
}
@ -89,15 +95,17 @@ class SubscriptionHelper {
suspend fun getChapter(
parser: MangaParser,
id: Int
subscribeMedia: SubscribeMedia
): MangaChapter? {
val selected = loadSelected(id)
val selected = loadSelected(subscribeMedia.id)
val chp = withTimeoutOrNull(10 * 1000) {
tryWithSuspend {
val show = parser.loadSavedShowResponse(id) ?: throw Exception(
val show = parser.loadSavedShowResponse(subscribeMedia.id)
?: forceLoadShowResponse(subscribeMedia, selected, parser)
?: throw Exception(
currContext()?.getString(
R.string.failed_to_load_data,
id
subscribeMedia.id
)
)
show.sManga?.let {
@ -111,10 +119,28 @@ class SubscriptionHelper {
return chp?.apply {
selected.latest = MediaNameAdapter.findChapterNumber(number) ?: 0f
saveSelected(id, selected)
saveSelected(subscribeMedia.id, selected)
}
}
private suspend fun forceLoadShowResponse(subscribeMedia: SubscribeMedia, selected: Selected, parser: BaseParser): ShowResponse? {
val tempMedia = Media(
id = subscribeMedia.id,
name = null,
nameRomaji = subscribeMedia.name,
userPreferredName = subscribeMedia.name,
isAdult = subscribeMedia.isAdult,
isFav = false,
isListPrivate = false,
userScore = 0,
userRepeat = 0,
format = null,
selected = selected
)
parser.autoSearch(tempMedia)
return parser.loadSavedShowResponse(subscribeMedia.id)
}
data class SubscribeMedia(
val isAnime: Boolean,
val isAdult: Boolean,
@ -134,6 +160,19 @@ class SubscriptionHelper {
) as? Map<Int, SubscribeMedia>)
?: mapOf<Int, SubscribeMedia>().also { PrefManager.setCustomVal(SUBSCRIPTIONS, it) }
@Suppress("UNCHECKED_CAST")
fun deleteSubscription(id: Int, showSnack: Boolean = false) {
val data = PrefManager.getNullableCustomVal(
SUBSCRIPTIONS,
null,
Map::class.java
) as? MutableMap<Int, SubscribeMedia>
?: mutableMapOf()
data.remove(id)
PrefManager.setCustomVal(SUBSCRIPTIONS, data)
if (showSnack) toast(R.string.subscription_deleted)
}
@Suppress("UNCHECKED_CAST")
fun saveSubscription(media: Media, subscribed: Boolean) {
val data = PrefManager.getNullableCustomVal(

View file

@ -98,7 +98,7 @@ class SubscriptionNotificationTask : Task {
val ep: Episode? =
SubscriptionHelper.getEpisode(
parser,
media.id
media
)
if (ep != null) context.getString(R.string.episode) + "${ep.number}${
if (ep.title != null) " : ${ep.title}" else ""
@ -113,7 +113,7 @@ class SubscriptionNotificationTask : Task {
val ep: MangaChapter? =
SubscriptionHelper.getChapter(
parser,
media.id
media
)
if (ep != null) ep.number + " " + context.getString(R.string.just_released) to null
else null