Retrieve favorite notes when modifying favorites

This commit is contained in:
FooIbar committed 2024-07-09 18:54:57 +08:00
1 parent 306ce83de4
commit 1b9aa6a762
4 files changed
+18 -1

No files matched your search

@@ -297,6 +297,10 @@ object EhEngine {
}.executeSafely { }
}
suspend fun getFavoriteNote(gid: Long, token: String) =
ehRequest(EhUrl.getAddFavorites(gid, token), EhUrl.getGalleryDetailUrl(gid, token))
.fetchUsingAsText(FavoritesParser::parseNote)
suspend fun downloadArchive(gid: Long, token: String, or: String, res: String, isHAtH: Boolean): String? {
val url = EhUrl.getDownloadArchive(gid, token, or)
val referer = EhUrl.getGalleryDetailUrl(gid, token)
@@ -1,6 +1,7 @@
package com.hippo.ehviewer.client.parser
import com.hippo.ehviewer.client.exception.ParseException
import com.hippo.ehviewer.util.unescapeXml
import java.nio.ByteBuffer
import kotlinx.serialization.Serializable
import moe.tarsin.coroutines.runSuspendCatching
@@ -17,11 +18,16 @@ data class FavParserResult(
}
object FavoritesParser {
private val NoteRegex = Regex("<textarea[^>]*>([^<]*)</textarea>")
fun parse(body: ByteBuffer): FavParserResult = runSuspendCatching {
unmarshalParsingAs<FavParserResult>(body, ::parseFav)
}.getOrElse {
throw ParseException("Parse favorites error", it)
}
fun parseNote(body: String) = NoteRegex.find(body)?.run { groupValues[1].unescapeXml() }
?: throw ParseException("Can't parse favorite note")
}
private external fun parseFav(body: ByteBuffer, limit: Int = body.limit()): Int
@@ -57,6 +57,7 @@ import com.hippo.ehviewer.ui.destinations.ReaderScreenDestination
import com.hippo.ehviewer.ui.tools.DialogState
import com.hippo.ehviewer.ui.tools.LabeledCheckbox
import com.hippo.ehviewer.util.FavouriteStatusRouter
import com.hippo.ehviewer.util.bgWork
import com.hippo.ehviewer.util.findActivity
import com.hippo.ehviewer.util.isAtLeastT
import com.hippo.ehviewer.util.mapToLongArray
@@ -207,10 +208,14 @@ suspend fun DialogState.modifyFavorites(galleryInfo: BaseGalleryInfo): Boolean {
add(localFav)
addAll(cloudFav)
}
if (galleryInfo.favoriteSlot >= 0 && galleryInfo.favoriteNote == null) {
galleryInfo.favoriteNote = bgWork { EhEngine.getFavoriteNote(galleryInfo.gid, galleryInfo.token) }
}
val (slot, note) = awaitSelectItemWithIconAndTextField(
items,
title = R.string.add_favorites_dialog_title,
hint = R.string.favorite_note,
initialNote = galleryInfo.favoriteNote.orEmpty(),
maxChar = MAX_FAVNOTE_CHAR,
)
return doModifyFavorites(galleryInfo, if (isFavorited) slot - 2 else slot - 1, localFavorited, note)
@@ -249,6 +254,7 @@ private suspend fun doModifyFavorites(
in 0..9 -> {
EhEngine.modifyFavorites(galleryInfo.gid, galleryInfo.token, slot, note)
galleryInfo.favoriteNote = note
true
}
@@ -532,6 +532,7 @@ class DialogState {
items: List<Pair<ImageVector, String>>,
@StringRes title: Int,
@StringRes hint: Int,
initialNote: String,
maxChar: Int,
): Pair<Int, String> = showNoButton(false) {
Column {
@@ -540,7 +541,7 @@ class DialogState {
modifier = Modifier.fillMaxWidth().aspectRatio(1F),
placeFirstItemInCenter = true,
) {
var note by remember { mutableStateOf("") }
var note by remember { mutableStateOf(initialNote) }
TextField(
value = note,
onValueChange = { note = it },