Use named constant instead of magic number

This commit is contained in:
FooIbar authored and tarsin committed 2023-07-04 21:20:24 +08:00
1 parent 652d6a2f28
commit 325909401c
9 files changed
+33 -19

No files matched your search

@@ -18,6 +18,7 @@ package com.hippo.ehviewer.client.data
import androidx.room.ColumnInfo
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
import kotlinx.parcelize.Parcelize
@Parcelize
@@ -72,7 +73,7 @@ open class BaseGalleryInfo(
override var simpleLanguage: String? = null,
@Ignore
override var favoriteSlot: Int = -2,
override var favoriteSlot: Int = NOT_FAVORITED,
@Ignore
override var favoriteName: String? = null,
@@ -127,5 +127,8 @@ interface GalleryInfo : AbstractGalleryInfo, Parcelable {
"language:hungarian",
"language:dutch",
)
const val NOT_FAVORITED = -2
const val LOCAL_FAVORITED = -1
}
}
@@ -25,6 +25,8 @@ import com.hippo.ehviewer.client.data.BaseGalleryInfo
import com.hippo.ehviewer.client.data.GalleryComment
import com.hippo.ehviewer.client.data.GalleryCommentList
import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
import com.hippo.ehviewer.client.data.GalleryPreview
import com.hippo.ehviewer.client.data.GalleryTagGroup
import com.hippo.ehviewer.client.data.LargeGalleryPreview
@@ -207,8 +209,8 @@ object GalleryDetailParser {
}
}
}
if (gd.favoriteSlot == -2 && EhDB.containLocalFavorites(gd.gid)) {
gd.favoriteSlot = -1
if (gd.favoriteSlot == NOT_FAVORITED && EhDB.containLocalFavorites(gd.gid)) {
gd.favoriteSlot = LOCAL_FAVORITED
}
} catch (e: Throwable) {
ExceptionUtils.throwIfFatal(e)
@@ -18,6 +18,8 @@ package com.hippo.ehviewer.client.parser
import android.util.Log
import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
import com.hippo.ehviewer.client.exception.ParseException
import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.yorozuya.toIntOrDefault
@@ -92,8 +94,8 @@ object GalleryListParser {
list.addAll(
es.mapNotNull {
runCatching { parseGalleryInfo(it.toString()) }.onFailure { it.printStackTrace() }.getOrNull()?.apply {
if (favoriteSlot == -2 && EhDB.containLocalFavorites(gid)) {
favoriteSlot = -1
if (favoriteSlot == NOT_FAVORITED && EhDB.containLocalFavorites(gid)) {
favoriteSlot = LOCAL_FAVORITED
}
generateSLang()
}
@@ -4,6 +4,7 @@ import androidx.room.ColumnInfo
import androidx.room.Entity
import com.hippo.ehviewer.client.data.BaseGalleryInfo
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED
@Entity(tableName = "LOCAL_FAVORITES")
class LocalFavoriteInfo() : BaseGalleryInfo() {
@@ -25,6 +26,6 @@ class LocalFavoriteInfo() : BaseGalleryInfo() {
}
init {
favoriteSlot = -1
favoriteSlot = LOCAL_FAVORITED
}
}
@@ -38,6 +38,8 @@ import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.EhEngine
import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
import com.hippo.ehviewer.client.exception.EhException
import com.hippo.ehviewer.dao.DownloadInfo
import com.hippo.ehviewer.download.DownloadManager
@@ -211,21 +213,21 @@ suspend fun DialogState.addToFavorites(galleryInfo: GalleryInfo): Boolean {
private suspend fun doAddToFavorites(
galleryInfo: GalleryInfo,
slot: Int = -2,
slot: Int = NOT_FAVORITED,
newFavoriteName: String? = null,
note: String? = null,
localFaved: Boolean = true,
): Boolean {
val add = when (slot) {
-2 -> {
NOT_FAVORITED -> {
EhDB.removeLocalFavorites(galleryInfo.gid)
if (galleryInfo.favoriteSlot >= 0) {
if (galleryInfo.favoriteSlot > LOCAL_FAVORITED) {
EhEngine.addFavorites(galleryInfo.gid, galleryInfo.token)
}
false
}
-1 -> {
LOCAL_FAVORITED -> {
if (localFaved) {
EhDB.removeLocalFavorites(galleryInfo.gid)
} else {
@@ -247,15 +249,15 @@ private suspend fun doAddToFavorites(
else -> throw EhException("Invalid favorite slot!")
}
if (add) {
if (slot != -1 || galleryInfo.favoriteSlot == -2) { // Cloud favorites have priority
if (slot != LOCAL_FAVORITED || galleryInfo.favoriteSlot == NOT_FAVORITED) { // Cloud favorites have priority
galleryInfo.favoriteSlot = slot
galleryInfo.favoriteName = newFavoriteName
withUIContext {
FavouriteStatusRouter.modifyFavourites(galleryInfo.gid, slot)
}
}
} else if (slot != -1 || galleryInfo.favoriteSlot == -1) {
val newSlot = if (slot >= 0 && localFaved) -1 else -2
} else if (slot != LOCAL_FAVORITED || galleryInfo.favoriteSlot == LOCAL_FAVORITED) {
val newSlot = if (slot > LOCAL_FAVORITED && localFaved) LOCAL_FAVORITED else NOT_FAVORITED
galleryInfo.favoriteSlot = newSlot
galleryInfo.favoriteName = null
withUIContext {
@@ -277,7 +279,7 @@ fun Context.navToReader(info: GalleryInfo, page: Int = -1) {
suspend fun DialogState.doGalleryInfoAction(info: GalleryInfo, context: Context) {
val downloaded = DownloadManager.getDownloadState(info.gid) != DownloadInfo.STATE_INVALID
val favourite = info.favoriteSlot != -2
val favourite = info.favoriteSlot != NOT_FAVORITED
val selected = if (!downloaded) {
showSelectItemWithIcon(
Icons.Default.MenuBook to R.string.read,
@@ -42,6 +42,7 @@ import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.EhUrl
import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED
import com.hippo.ehviewer.client.thumbUrl
import com.hippo.ehviewer.ui.scene.navWithUrl
import com.hippo.ehviewer.util.tellClipboardWithToast
@@ -96,7 +97,7 @@ fun GalleryInfoBottomSheet(
pages.toString(),
size,
favoriteCount.toString(),
(favoriteSlot >= 0).toString(),
(favoriteSlot > LOCAL_FAVORITED).toString(),
ratingCount.toString(),
rating.toString(),
torrentCount.toString(),
@@ -37,6 +37,7 @@ import androidx.constraintlayout.compose.ConstraintLayout
import com.hippo.ehviewer.FavouriteStatusRouter
import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
import com.hippo.ehviewer.download.DownloadManager
import com.hippo.ehviewer.ui.tools.CrystalCard
import com.hippo.ehviewer.ui.tools.ElevatedCard
@@ -64,9 +65,9 @@ fun GalleryInfoListItem(
)
}
val showFav by produceState(false, info.gid) {
value = info.favoriteSlot != -2 && !isInFavScene
value = info.favoriteSlot != NOT_FAVORITED && !isInFavScene
FavouriteStatusRouter.stateFlow(info.gid).collect {
value = it != -2 && !isInFavScene
value = it != NOT_FAVORITED && !isInFavScene
}
}
ConstraintLayout(modifier = Modifier.padding(8.dp, 4.dp).fillMaxSize()) {
@@ -116,6 +116,7 @@ import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.client.data.GalleryComment
import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
import com.hippo.ehviewer.client.data.GalleryTagGroup
import com.hippo.ehviewer.client.data.ListUrlBuilder
import com.hippo.ehviewer.client.exception.NoHAtHClientException
@@ -692,8 +693,8 @@ class GalleryDetailScene : BaseScene() {
horizontalArrangement = Arrangement.Center,
) {
val favored by produceState(initialValue = false) {
value = galleryDetail.favoriteSlot != -2
FavouriteStatusRouter.stateFlow(galleryDetail.gid).collect { value = it != -2 }
value = galleryDetail.favoriteSlot != NOT_FAVORITED
FavouriteStatusRouter.stateFlow(galleryDetail.gid).collect { value = it != NOT_FAVORITED }
}
FilledTertiaryIconToggleButton(
checked = favored,