Skip to content

Commit

Permalink
fix: deletion queries (#504)
Browse files Browse the repository at this point in the history
* feat: introduce `deleteFromList` extension function for `Media`

* refactor: remove redundant deletion code, migrate to `deleteFromList`
  • Loading branch information
TobyBridle authored Oct 28, 2024
1 parent bd1f338 commit e8ca3f2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 46 deletions.
38 changes: 38 additions & 0 deletions app/src/main/java/ani/dantotsu/media/Media.kt
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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<Int>())
PrefManager.setCustomVal(
"removeList", removeList.minus(listId)
)

onSuccess()
} catch (e: Exception) {
onError(e)
}
} ?: onNotFound()
}
}
}
}

fun emptyMedia() = Media(
id = 0,
name = "No media found",
Expand Down
36 changes: 15 additions & 21 deletions app/src/main/java/ani/dantotsu/media/MediaListDialogFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,24 @@ class MediaListDialogSmallFragment : BottomSheetDialogFragment() {
binding.mediaListContainer.updateLayoutParams<ViewGroup.MarginLayoutParams> { 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))
}
})

}
}
}
Expand Down

0 comments on commit e8ca3f2

Please sign in to comment.