fix: deletion queries (#504)
* feat: introduce `deleteFromList` extension function for `Media` * refactor: remove redundant deletion code, migrate to `deleteFromList`
This commit is contained in:
parent
bd1f3388f7
commit
e8ca3f2222
3 changed files with 66 additions and 46 deletions
|
@ -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<Int>())
|
||||
PrefManager.setCustomVal(
|
||||
"removeList", removeList.minus(listId)
|
||||
)
|
||||
|
||||
onSuccess()
|
||||
} catch (e: Exception) {
|
||||
onError(e)
|
||||
}
|
||||
} ?: onNotFound()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun emptyMedia() = Media(
|
||||
id = 0,
|
||||
name = "No media found",
|
||||
|
|
|
@ -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))
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue