Suspendize addToFavorites
This commit is contained in:
1 parent
d72a44b9f2
commit
a8988ac17b
5 files changed
+104
-126
No files matched your search
@@ -29,7 +29,7 @@ import com.hippo.yorozuya.ViewUtils
|
||||
class ListCheckBoxDialogBuilder(
|
||||
context: Context,
|
||||
items: List<CharSequence>,
|
||||
listener: (ListCheckBoxDialogBuilder?, AlertDialog?, Int) -> Unit,
|
||||
listener: (ListCheckBoxDialogBuilder, AlertDialog?, Int) -> Unit,
|
||||
checkText: String?,
|
||||
checked: Boolean,
|
||||
) : BaseDialogBuilder(
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.hippo.ehviewer.ui
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
@@ -27,91 +28,24 @@ import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhClient
|
||||
import com.hippo.ehviewer.client.EhEngine
|
||||
import com.hippo.ehviewer.client.EhRequest
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.exception.EhException
|
||||
import com.hippo.ehviewer.download.DownloadService
|
||||
import com.hippo.ehviewer.ui.scene.BaseScene
|
||||
import com.hippo.unifile.UniFile
|
||||
import com.hippo.util.requestPermission
|
||||
import com.hippo.yorozuya.collect.LongList
|
||||
import eu.kanade.tachiyomi.util.lang.launchNow
|
||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlin.coroutines.resume
|
||||
import com.hippo.ehviewer.download.DownloadManager as downloadManager
|
||||
|
||||
object CommonOperations {
|
||||
private fun doAddToFavorites(
|
||||
activity: Activity,
|
||||
galleryInfo: GalleryInfo,
|
||||
slot: Int,
|
||||
listener: EhClient.Callback<Unit>,
|
||||
) {
|
||||
when (slot) {
|
||||
-1 -> {
|
||||
EhDB.putLocalFavorites(galleryInfo)
|
||||
listener.onSuccess(Unit)
|
||||
}
|
||||
|
||||
in 0..9 -> {
|
||||
val request = EhRequest()
|
||||
request.setMethod(EhClient.METHOD_ADD_FAVORITES)
|
||||
request.setArgs(galleryInfo.gid, galleryInfo.token, slot, "")
|
||||
request.setCallback(listener)
|
||||
request.enqueue(activity)
|
||||
}
|
||||
|
||||
else -> {
|
||||
listener.onFailure(Exception()) // TODO Add text
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun addToFavorites(
|
||||
activity: Activity,
|
||||
galleryInfo: GalleryInfo,
|
||||
listener: EhClient.Callback<Unit>,
|
||||
select: Boolean = false,
|
||||
) {
|
||||
val slot = Settings.defaultFavSlot
|
||||
val localFav = activity.getString(R.string.local_favorites)
|
||||
val items = Settings.favCat.toMutableList().apply { add(0, localFav) }
|
||||
if (!select && slot >= -1 && slot <= 9) {
|
||||
val newFavoriteName = if (slot >= 0) items[slot + 1] else null
|
||||
doAddToFavorites(
|
||||
activity,
|
||||
galleryInfo,
|
||||
slot,
|
||||
DelegateFavoriteCallback(listener, galleryInfo, newFavoriteName, slot),
|
||||
)
|
||||
} else {
|
||||
ListCheckBoxDialogBuilder(
|
||||
activity,
|
||||
items,
|
||||
{ builder: ListCheckBoxDialogBuilder?, _: AlertDialog?, position: Int ->
|
||||
val slot1 = position - 1
|
||||
val newFavoriteName = if (slot1 in 0..9) items[slot1 + 1] else null
|
||||
doAddToFavorites(
|
||||
activity,
|
||||
galleryInfo,
|
||||
slot1,
|
||||
DelegateFavoriteCallback(listener, galleryInfo, newFavoriteName, slot1),
|
||||
)
|
||||
if (builder?.isChecked == true) {
|
||||
Settings.putDefaultFavSlot(slot1)
|
||||
} else {
|
||||
Settings.putDefaultFavSlot(Settings.INVALID_DEFAULT_FAV_SLOT)
|
||||
}
|
||||
},
|
||||
activity.getString(R.string.remember_favorite_collection),
|
||||
false,
|
||||
)
|
||||
.setTitle(R.string.add_favorites_dialog_title)
|
||||
.setOnCancelListener { listener.onCancel() }
|
||||
.show()
|
||||
}
|
||||
}
|
||||
|
||||
fun removeFromFavorites(
|
||||
activity: Activity?,
|
||||
galleryInfo: GalleryInfo,
|
||||
@@ -279,3 +213,52 @@ suspend fun keepNoMediaFileStatus() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun Context.addToFavorites(galleryInfo: GalleryInfo, select: Boolean = false) {
|
||||
val slot = Settings.defaultFavSlot
|
||||
val localFav = getString(R.string.local_favorites)
|
||||
val items = Settings.favCat.toMutableList().apply { add(0, localFav) }
|
||||
suspend fun doAddToFavorites(slot: Int, newFavoriteName: String?) {
|
||||
when (slot) {
|
||||
-1 -> {
|
||||
EhDB.putLocalFavorites(galleryInfo)
|
||||
}
|
||||
|
||||
in 0..9 -> {
|
||||
EhEngine.addFavorites(galleryInfo.gid, galleryInfo.token, slot, "")
|
||||
}
|
||||
|
||||
else -> {
|
||||
throw EhException("Invalid favslot!")
|
||||
}
|
||||
}
|
||||
galleryInfo.favoriteName = newFavoriteName
|
||||
galleryInfo.favoriteSlot = slot
|
||||
withUIContext {
|
||||
favouriteStatusRouter.modifyFavourites(galleryInfo.gid, slot)
|
||||
}
|
||||
}
|
||||
if (!select && slot >= -1 && slot <= 9) {
|
||||
val newFavoriteName = if (slot >= 0) items[slot + 1] else null
|
||||
doAddToFavorites(slot, newFavoriteName)
|
||||
} else {
|
||||
val (slot1, checked) = withUIContext {
|
||||
suspendCancellableCoroutine { cont ->
|
||||
ListCheckBoxDialogBuilder(
|
||||
this@addToFavorites,
|
||||
items,
|
||||
{ b, _, position -> cont.resume(position - 1 to b.isChecked) },
|
||||
getString(R.string.remember_favorite_collection),
|
||||
false,
|
||||
).setTitle(R.string.add_favorites_dialog_title).setOnCancelListener { cont.cancel() }.show()
|
||||
}
|
||||
}
|
||||
val newFavoriteName = if (slot1 in 0..9) items[slot1 + 1] else null
|
||||
doAddToFavorites(slot1, newFavoriteName)
|
||||
if (checked) {
|
||||
Settings.putDefaultFavSlot(slot1)
|
||||
} else {
|
||||
Settings.putDefaultFavSlot(Settings.INVALID_DEFAULT_FAV_SLOT)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,6 +156,7 @@ import com.hippo.ehviewer.spider.SpiderQueen.Companion.MODE_READ
|
||||
import com.hippo.ehviewer.ui.CommonOperations
|
||||
import com.hippo.ehviewer.ui.GalleryInfoBottomSheet
|
||||
import com.hippo.ehviewer.ui.MainActivity
|
||||
import com.hippo.ehviewer.ui.addToFavorites
|
||||
import com.hippo.ehviewer.ui.scene.GalleryListScene.Companion.toStartArgs
|
||||
import com.hippo.ehviewer.ui.widget.CrystalCard
|
||||
import com.hippo.ehviewer.ui.widget.EhAsyncPreview
|
||||
@@ -177,6 +178,7 @@ import com.hippo.yorozuya.collect.IntList
|
||||
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import kotlin.math.roundToInt
|
||||
@@ -978,15 +980,20 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
|
||||
)
|
||||
remove = true
|
||||
}
|
||||
withUIContext {
|
||||
if (!remove) {
|
||||
mModifyingFavorites = true
|
||||
CommonOperations.addToFavorites(
|
||||
requireActivity(),
|
||||
galleryDetail,
|
||||
ModifyFavoritesListener(requireContext(), false),
|
||||
)
|
||||
if (!remove) {
|
||||
mModifyingFavorites = true
|
||||
runCatching {
|
||||
requireContext().addToFavorites(galleryDetail)
|
||||
showTip(R.string.add_to_favorite_success, LENGTH_SHORT)
|
||||
withUIContext {
|
||||
onModifyFavoritesSuccess(false)
|
||||
}
|
||||
}.onFailure {
|
||||
if (it !is CancellationException) showTip(R.string.add_to_favorite_failure, LENGTH_LONG)
|
||||
mModifyingFavorites = false
|
||||
}
|
||||
}
|
||||
withUIContext {
|
||||
// Update UI
|
||||
updateFavoriteDrawable()
|
||||
}
|
||||
@@ -1442,16 +1449,20 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
|
||||
)
|
||||
remove = true
|
||||
}
|
||||
withUIContext {
|
||||
if (!remove) {
|
||||
mModifyingFavorites = true
|
||||
CommonOperations.addToFavorites(
|
||||
requireActivity(),
|
||||
composeBindingGD!!,
|
||||
ModifyFavoritesListener(requireActivity(), false),
|
||||
true,
|
||||
)
|
||||
if (!remove) {
|
||||
mModifyingFavorites = true
|
||||
runCatching {
|
||||
requireContext().addToFavorites(composeBindingGD!!, true)
|
||||
showTip(R.string.add_to_favorite_success, LENGTH_SHORT)
|
||||
withUIContext {
|
||||
onModifyFavoritesSuccess(false)
|
||||
}
|
||||
}.onFailure {
|
||||
if (it !is CancellationException) showTip(R.string.add_to_favorite_failure, LENGTH_LONG)
|
||||
mModifyingFavorites = false
|
||||
}
|
||||
}
|
||||
withUIContext {
|
||||
// Update UI
|
||||
updateFavoriteDrawable()
|
||||
}
|
||||
|
||||
@@ -92,6 +92,7 @@ import com.hippo.ehviewer.databinding.SceneGalleryListBinding
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
import com.hippo.ehviewer.download.DownloadManager.DownloadInfoListener
|
||||
import com.hippo.ehviewer.ui.CommonOperations
|
||||
import com.hippo.ehviewer.ui.addToFavorites
|
||||
import com.hippo.ehviewer.ui.dialog.SelectItemWithIconAdapter
|
||||
import com.hippo.ehviewer.widget.GalleryInfoContentHelper
|
||||
import com.hippo.ehviewer.widget.SearchLayout
|
||||
@@ -110,6 +111,7 @@ import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import eu.kanade.tachiyomi.util.lang.launchUI
|
||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
import kotlinx.coroutines.delay
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
import rikka.core.res.resolveColor
|
||||
import java.io.File
|
||||
import java.time.LocalDateTime
|
||||
@@ -798,11 +800,14 @@ class GalleryListScene :
|
||||
RemoveFromFavoriteListener(context),
|
||||
)
|
||||
} else {
|
||||
CommonOperations.addToFavorites(
|
||||
activity,
|
||||
gi,
|
||||
AddToFavoriteListener(context),
|
||||
)
|
||||
lifecycleScope.launchIO {
|
||||
runSuspendCatching {
|
||||
requireContext().addToFavorites(gi)
|
||||
showTip(R.string.add_to_favorite_success, LENGTH_SHORT)
|
||||
}.onFailure {
|
||||
showTip(R.string.add_to_favorite_failure, LENGTH_LONG)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
3 -> {
|
||||
@@ -1120,19 +1125,6 @@ class GalleryListScene :
|
||||
override fun onCancel() {}
|
||||
}
|
||||
|
||||
private class AddToFavoriteListener(context: Context) :
|
||||
EhCallback<GalleryListScene, Unit>(context) {
|
||||
override fun onSuccess(result: Unit) {
|
||||
showTip(R.string.add_to_favorite_success, LENGTH_SHORT)
|
||||
}
|
||||
|
||||
override fun onFailure(e: Exception) {
|
||||
showTip(R.string.add_to_favorite_failure, LENGTH_LONG)
|
||||
}
|
||||
|
||||
override fun onCancel() {}
|
||||
}
|
||||
|
||||
private class RemoveFromFavoriteListener(context: Context) :
|
||||
EhCallback<GalleryListScene, Unit>(context) {
|
||||
override fun onSuccess(result: Unit) {
|
||||
|
||||
@@ -56,6 +56,7 @@ import com.hippo.ehviewer.databinding.SceneHistoryBinding
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
import com.hippo.ehviewer.download.DownloadManager.DownloadInfoListener
|
||||
import com.hippo.ehviewer.ui.CommonOperations
|
||||
import com.hippo.ehviewer.ui.addToFavorites
|
||||
import com.hippo.ehviewer.ui.dialog.SelectItemWithIconAdapter
|
||||
import com.hippo.ehviewer.ui.widget.ListInfoCard
|
||||
import com.hippo.view.ViewTransition
|
||||
@@ -67,6 +68,7 @@ import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
import eu.kanade.tachiyomi.util.system.pxToDp
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
import rikka.core.res.resolveColor
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
@@ -308,11 +310,14 @@ class HistoryScene : BaseToolbarScene() {
|
||||
RemoveFromFavoriteListener(context),
|
||||
)
|
||||
} else {
|
||||
CommonOperations.addToFavorites(
|
||||
activity,
|
||||
gi,
|
||||
AddToFavoriteListener(context),
|
||||
)
|
||||
lifecycleScope.launchIO {
|
||||
runSuspendCatching {
|
||||
requireContext().addToFavorites(gi)
|
||||
showTip(R.string.add_to_favorite_success, LENGTH_SHORT)
|
||||
}.onFailure {
|
||||
showTip(R.string.add_to_favorite_failure, LENGTH_LONG)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
3 -> {
|
||||
@@ -336,19 +341,6 @@ class HistoryScene : BaseToolbarScene() {
|
||||
}.show()
|
||||
}
|
||||
|
||||
private class AddToFavoriteListener(context: Context) :
|
||||
EhCallback<GalleryListScene?, Unit>(context) {
|
||||
override fun onSuccess(result: Unit) {
|
||||
showTip(R.string.add_to_favorite_success, LENGTH_SHORT)
|
||||
}
|
||||
|
||||
override fun onFailure(e: Exception) {
|
||||
showTip(R.string.add_to_favorite_failure, LENGTH_LONG)
|
||||
}
|
||||
|
||||
override fun onCancel() {}
|
||||
}
|
||||
|
||||
private class RemoveFromFavoriteListener(context: Context) :
|
||||
EhCallback<GalleryListScene?, Unit>(context) {
|
||||
override fun onSuccess(result: Unit) {
|
||||
|
||||
Reference in new issue
Block a user