From e8ca3f2222f4a032cbf517fa1506ddaadd658541 Mon Sep 17 00:00:00 2001 From: Toby Bridle <52335751+TobyBridle@users.noreply.github.com> Date: Mon, 28 Oct 2024 14:27:52 +0000 Subject: [PATCH] fix: deletion queries (#504) * feat: introduce `deleteFromList` extension function for `Media` * refactor: remove redundant deletion code, migrate to `deleteFromList` --- app/src/main/java/ani/dantotsu/media/Media.kt | 38 +++++++++++++++++++ .../dantotsu/media/MediaListDialogFragment.kt | 36 ++++++++---------- .../media/MediaListDialogSmallFragment.kt | 38 +++++++------------ 3 files changed, 66 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/ani/dantotsu/media/Media.kt b/app/src/main/java/ani/dantotsu/media/Media.kt index 4b9136c594..a3453c992a 100644 --- a/app/src/main/java/ani/dantotsu/media/Media.kt +++ b/app/src/main/java/ani/dantotsu/media/Media.kt @@ -1,15 +1,22 @@ package ani.dantotsu.media import android.graphics.Bitmap +import ani.dantotsu.connections.anilist.Anilist import ani.dantotsu.connections.anilist.api.FuzzyDate import ani.dantotsu.connections.anilist.api.MediaEdge import ani.dantotsu.connections.anilist.api.MediaList import ani.dantotsu.connections.anilist.api.MediaStreamingEpisode import ani.dantotsu.connections.anilist.api.MediaType import ani.dantotsu.connections.anilist.api.Query +import ani.dantotsu.connections.mal.MAL import ani.dantotsu.media.anime.Anime import ani.dantotsu.media.manga.Manga import ani.dantotsu.profile.User +import ani.dantotsu.settings.saving.PrefManager +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.io.Serializable import ani.dantotsu.connections.anilist.api.Media as ApiMedia @@ -130,6 +137,37 @@ data class Media( fun mangaName() = if (countryOfOrigin != "JP") mainName() else nameRomaji } +fun Media?.deleteFromList( + scope: CoroutineScope, + onSuccess: suspend () -> Unit, + onError: suspend (e: Exception) -> Unit, + onNotFound: suspend () -> Unit +) { + val id = this?.userListId + scope.launch { + withContext(Dispatchers.IO) { + this@deleteFromList?.let { media -> + val _id = id ?: Anilist.query.userMediaDetails(media).userListId; + _id?.let { listId -> + try { + Anilist.mutation.deleteList(listId) + MAL.query.deleteList(media.anime != null, media.idMAL) + + val removeList = PrefManager.getCustomVal("removeList", setOf()) + PrefManager.setCustomVal( + "removeList", removeList.minus(listId) + ) + + onSuccess() + } catch (e: Exception) { + onError(e) + } + } ?: onNotFound() + } + } + } +} + fun emptyMedia() = Media( id = 0, name = "No media found", diff --git a/app/src/main/java/ani/dantotsu/media/MediaListDialogFragment.kt b/app/src/main/java/ani/dantotsu/media/MediaListDialogFragment.kt index 1117f34203..ea0450a751 100644 --- a/app/src/main/java/ani/dantotsu/media/MediaListDialogFragment.kt +++ b/app/src/main/java/ani/dantotsu/media/MediaListDialogFragment.kt @@ -271,29 +271,23 @@ class MediaListDialogFragment : BottomSheetDialogFragment() { } binding.mediaListDelete.setOnClickListener { - var id = media!!.userListId scope.launch { - withContext(Dispatchers.IO) { - if (id != null) { - Anilist.mutation.deleteList(id!!) - MAL.query.deleteList(media?.anime != null, media?.idMAL) - } else { - val profile = Anilist.query.userMediaDetails(media!!) - profile.userListId?.let { listId -> - id = listId - Anilist.mutation.deleteList(listId) - MAL.query.deleteList(media?.anime != null, media?.idMAL) - } + media?.deleteFromList(scope, onSuccess = { + Refresh.all() + snackString(getString(R.string.deleted_from_list)) + dismissAllowingStateLoss() + }, onError = { e -> + withContext(Dispatchers.Main) { + snackString( + getString( + R.string.delete_fail_reason, e.message + ) + ) } - } - PrefManager.setCustomVal("removeList", removeList.minus(media?.id)) - } - if (id != null) { - Refresh.all() - snackString(getString(R.string.deleted_from_list)) - dismissAllowingStateLoss() - } else { - snackString(getString(R.string.no_list_id)) + }, onNotFound = { + snackString(getString(R.string.no_list_id)) + }) + } } } diff --git a/app/src/main/java/ani/dantotsu/media/MediaListDialogSmallFragment.kt b/app/src/main/java/ani/dantotsu/media/MediaListDialogSmallFragment.kt index caf882e6c3..53a2d6f2c8 100644 --- a/app/src/main/java/ani/dantotsu/media/MediaListDialogSmallFragment.kt +++ b/app/src/main/java/ani/dantotsu/media/MediaListDialogSmallFragment.kt @@ -63,36 +63,24 @@ class MediaListDialogSmallFragment : BottomSheetDialogFragment() { binding.mediaListContainer.updateLayoutParams { bottomMargin += navBarHeight } val scope = viewLifecycleOwner.lifecycleScope binding.mediaListDelete.setOnClickListener { - var id = media.userListId viewLifecycleOwner.lifecycleScope.launch { - withContext(Dispatchers.IO) { - if (id != null) { - try { - Anilist.mutation.deleteList(id!!) - MAL.query.deleteList(media.anime != null, media.idMAL) - } catch (e: Exception) { - withContext(Dispatchers.Main) { - snackString(getString(R.string.delete_fail_reason, e.message)) - } - return@withContext - } - } else { - val profile = Anilist.query.userMediaDetails(media) - profile.userListId?.let { listId -> - id = listId - Anilist.mutation.deleteList(listId) - MAL.query.deleteList(media.anime != null, media.idMAL) - } - } - } - withContext(Dispatchers.Main) { - if (id != null) { + scope.launch { + media.deleteFromList(scope, onSuccess = { Refresh.all() snackString(getString(R.string.deleted_from_list)) dismissAllowingStateLoss() - } else { + }, onError = { e -> + withContext(Dispatchers.Main) { + snackString( + getString( + R.string.delete_fail_reason, e.message + ) + ) + } + }, onNotFound = { snackString(getString(R.string.no_list_id)) - } + }) + } } }