Don't show cloud favorites when not logged in

This commit is contained in:
FooIbar authored and tarsin committed 2023-08-04 17:37:26 +08:00
1 parent d98292da1d
commit 22b87de29d
6 files changed
+92 -77

No files matched your search

@@ -22,7 +22,6 @@ object Settings : DataStorePreferences(null) {
val favChangesFlow = _favFlow.debounce(1000)
var favCat by stringArrayPref("fav_cat", 10, "Favorites").emitTo(_favFlow)
var favCount by intArrayPref("fav_count", 10).emitTo(_favFlow)
var favLocalCount by intPref("fav_local", 0).emitTo(_favFlow)
var favCloudCount by intPref("fav_cloud", 0).emitTo(_favFlow)
var downloadScheme by stringOrNullPref("image_scheme", null)
@@ -66,14 +65,13 @@ object Settings : DataStorePreferences(null) {
var enableQuic by boolPref("enable_quic", true)
var bypassCloudflare by boolPref("bypass_cloudflare", false)
var thumbSizeDp by intPref("thumb_size_", 120)
var recentFavCat by intPref("recent_fav_cat", FavListUrlBuilder.FAV_CAT_ALL)
var recentFavCat by intPref("recent_fav_cat", FavListUrlBuilder.FAV_CAT_LOCAL)
var securityDelay by intPref("require_unlock_delay", 0)
var clipboardTextHashCode by intPref("clipboard_text_hash_code", 0)
var listThumbSize by intPref("list_tile_size", 40)
var searchCategory by intPref("search_pref", EhUtils.ALL_CATEGORY)
var requestNewsTimerHour by intPref("request_news_timer_hour", -1)
var requestNewsTimerMinute by intPref("request_news_timer_minute", -1)
var dataMapNextId by intPref("data_map_next_id", 0)
var updateIntervalDays by intPref("update_interval_days", 0)
var recentDownloadLabel by stringOrNullPref("recent_download_label", null)
var defaultDownloadLabel by stringOrNullPref("default_download_label", null)
@@ -290,7 +290,7 @@ object EhEngine {
* @param dstCat -1 for delete, 0 - 9 for cloud favorite, others throw Exception
* @param note max 250 characters
*/
suspend fun addFavorites(
suspend fun modifyFavorites(
gid: Long,
token: String?,
dstCat: Int = -1,
@@ -519,7 +519,7 @@ object EhEngine {
if (filter) list.removeAllSuspend { EhFilter.filterUploader(it) || EhFilter.filterTag(it) || EhFilter.filterTagNamespace(it) }
}
suspend fun addFavoritesRange(galleryList: List<Pair<Long, String>>, dstCat: Int) {
galleryList.forEach { (gid, token) -> addFavorites(gid, token, dstCat) }
suspend fun modifyFavoritesRange(galleryList: List<Pair<Long, String>>, dstCat: Int) {
galleryList.forEach { (gid, token) -> modifyFavorites(gid, token, dstCat) }
}
}
@@ -34,6 +34,7 @@ import androidx.core.content.ContextCompat
import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.EhCookieStore
import com.hippo.ehviewer.client.EhEngine
import com.hippo.ehviewer.client.EhUtils
import com.hippo.ehviewer.client.data.BaseGalleryInfo
@@ -195,31 +196,36 @@ suspend fun keepNoMediaFileStatus() {
}
}
suspend fun DialogState.addToFavorites(galleryInfo: BaseGalleryInfo): Boolean {
fun getFavoriteIcon(favorited: Boolean) =
if (favorited) Icons.Default.Favorite else Icons.Default.FavoriteBorder
suspend fun DialogState.modifyFavorites(galleryInfo: BaseGalleryInfo): Boolean {
val localFavorited = EhDB.containLocalFavorites(galleryInfo.gid)
val localFav = if (localFavorited) {
Icons.Default.Favorite
} else {
Icons.Default.FavoriteBorder
} to appCtx.getString(R.string.local_favorites)
val items = Settings.favCat.mapIndexed { index, name ->
if (galleryInfo.favoriteSlot == index) {
Icons.Default.Favorite
if (EhCookieStore.hasSignedIn()) {
val localFav = getFavoriteIcon(localFavorited) to appCtx.getString(R.string.local_favorites)
val cloudFav = Settings.favCat.mapIndexed { index, name ->
getFavoriteIcon(galleryInfo.favoriteSlot == index) to name
}.toTypedArray()
val isFavorited = galleryInfo.favoriteSlot != NOT_FAVORITED
val items = if (isFavorited) {
val remove = Icons.Default.HeartBroken to appCtx.getString(R.string.remove_from_favourites)
arrayOf(remove, localFav, *cloudFav)
} else {
Icons.Default.FavoriteBorder
} to name
}.toTypedArray()
val isFavorited = galleryInfo.favoriteSlot != NOT_FAVORITED
val (slot, note) = if (isFavorited) {
val remove = Icons.Default.HeartBroken to appCtx.getString(R.string.remove_from_favourites)
showSelectItemWithIconAndTextField(remove, localFav, *items, title = R.string.add_favorites_dialog_title, hint = R.string.favorite_note, maxChar = MAX_FAVNOTE_CHAR)
arrayOf(localFav, *cloudFav)
}
val (slot, note) = showSelectItemWithIconAndTextField(
*items,
title = R.string.add_favorites_dialog_title,
hint = R.string.favorite_note,
maxChar = MAX_FAVNOTE_CHAR,
)
return doModifyFavorites(galleryInfo, if (isFavorited) slot - 2 else slot - 1, note, localFavorited)
} else {
showSelectItemWithIconAndTextField(localFav, *items, title = R.string.add_favorites_dialog_title, hint = R.string.favorite_note, maxChar = MAX_FAVNOTE_CHAR)
return doModifyFavorites(galleryInfo, LOCAL_FAVORITED, localFavorited = localFavorited)
}
return doAddToFavorites(galleryInfo, if (isFavorited) slot - 2 else slot - 1, note, localFavorited)
}
private suspend fun doAddToFavorites(
private suspend fun doModifyFavorites(
galleryInfo: BaseGalleryInfo,
slot: Int = NOT_FAVORITED,
note: String = "",
@@ -228,7 +234,7 @@ private suspend fun doAddToFavorites(
val add = when (slot) {
NOT_FAVORITED -> { // Remove from cloud favorites first
if (galleryInfo.favoriteSlot > LOCAL_FAVORITED) {
EhEngine.addFavorites(galleryInfo.gid, galleryInfo.token)
EhEngine.modifyFavorites(galleryInfo.gid, galleryInfo.token)
} else if (localFavorited) {
EhDB.removeLocalFavorites(galleryInfo)
}
@@ -245,7 +251,7 @@ private suspend fun doAddToFavorites(
}
in 0..9 -> {
EhEngine.addFavorites(galleryInfo.gid, galleryInfo.token, slot, note)
EhEngine.modifyFavorites(galleryInfo.gid, galleryInfo.token, slot, note)
true
}
@@ -266,7 +272,7 @@ private suspend fun doAddToFavorites(
return add
}
suspend fun removeFromFavorites(galleryInfo: BaseGalleryInfo) = doAddToFavorites(galleryInfo)
suspend fun removeFromFavorites(galleryInfo: BaseGalleryInfo) = doModifyFavorites(galleryInfo)
fun Context.navToReader(info: BaseGalleryInfo, page: Int = -1) {
val intent = Intent(this, ReaderActivity::class.java)
@@ -316,7 +322,7 @@ suspend fun DialogState.doGalleryInfoAction(info: BaseGalleryInfo, context: Cont
}
} else {
runSuspendCatching {
addToFavorites(info)
modifyFavorites(info)
showTip(R.string.add_to_favorite_success, BaseScene.LENGTH_SHORT)
}.onFailure {
showTip(R.string.add_to_favorite_failure, BaseScene.LENGTH_LONG)
@@ -61,6 +61,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.EhCookieStore
import com.hippo.ehviewer.client.EhEngine
import com.hippo.ehviewer.client.EhUrl
import com.hippo.ehviewer.client.data.BaseGalleryInfo
@@ -271,9 +272,12 @@ class FavoritesScene : SearchBarScene() {
6 -> {
val helper = MoveDialogHelper()
// First is local favorite, the other 10 is cloud favorite
val array = arrayOfNulls<String>(11)
array[0] = getString(R.string.local_favorites)
System.arraycopy(Settings.favCat, 0, array, 1, 10)
val localFav = getString(R.string.local_favorites)
val array = if (EhCookieStore.hasSignedIn()) {
arrayOf(localFav, *Settings.favCat)
} else {
arrayOf(localFav)
}
BaseDialogBuilder(context).setTitle(R.string.move_favorites_dialog_title).setItems(array, helper).show()
}
}
@@ -414,11 +418,16 @@ class FavoritesScene : SearchBarScene() {
scope.invalidate()
}
}
val faves = arrayOf(
stringResource(id = R.string.local_favorites) to localFavCount,
stringResource(id = R.string.cloud_favorites) to Settings.favCloudCount,
*Settings.favCat.zip(Settings.favCount.toTypedArray()).toTypedArray(),
)
val localFav = stringResource(id = R.string.local_favorites) to localFavCount
val faves = if (EhCookieStore.hasSignedIn()) {
arrayOf(
localFav,
stringResource(id = R.string.cloud_favorites) to Settings.favCloudCount,
*Settings.favCat.zip(Settings.favCount.toTypedArray()).toTypedArray(),
)
} else {
arrayOf(localFav)
}
LazyColumn {
itemsIndexed(faves) { index, (name, count) ->
ListItem(
@@ -484,7 +493,7 @@ class FavoritesScene : SearchBarScene() {
EhDB.removeLocalFavorites(info)
} else {
val delList = info.map { it.gid to it.token!! }
EhEngine.addFavoritesRange(delList, -1)
EhEngine.modifyFavoritesRange(delList, -1)
}
mAdapter?.refresh()
}
@@ -503,7 +512,7 @@ class FavoritesScene : SearchBarScene() {
EhDB.removeLocalFavorites(info)
val galleryList = info.map { it.gid to it.token!! }
runCatching {
EhEngine.addFavoritesRange(galleryList, dstCat)
EhEngine.modifyFavoritesRange(galleryList, dstCat)
}
} else if (dstCat == FavListUrlBuilder.FAV_CAT_LOCAL) {
// Move from cloud to local
@@ -58,8 +58,6 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.CloudDone
import androidx.compose.material.icons.filled.Difference
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material.icons.filled.ImageSearch
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Share
@@ -140,7 +138,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.getFavoriteIcon
import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder
import com.hippo.ehviewer.ui.legacy.CheckBoxDialogBuilder
import com.hippo.ehviewer.ui.legacy.CoilImageGetter
@@ -150,6 +148,7 @@ import com.hippo.ehviewer.ui.main.EhPreviewItem
import com.hippo.ehviewer.ui.main.GalleryDetailErrorTip
import com.hippo.ehviewer.ui.main.GalleryDetailHeaderCard
import com.hippo.ehviewer.ui.main.GalleryTags
import com.hippo.ehviewer.ui.modifyFavorites
import com.hippo.ehviewer.ui.navToReader
import com.hippo.ehviewer.ui.openBrowser
import com.hippo.ehviewer.ui.scene.GalleryListScene.Companion.toStartArgs
@@ -694,7 +693,7 @@ class GalleryDetailScene : BaseScene() {
onCheckedChange = { modifyFavourite() },
) {
Icon(
imageVector = if (favored) Icons.Default.Favorite else Icons.Default.FavoriteBorder,
imageVector = getFavoriteIcon(favored),
contentDescription = null,
)
}
@@ -875,7 +874,7 @@ class GalleryDetailScene : BaseScene() {
favoritesLock.withLock {
var remove = false
runCatching {
remove = !dialogState.addToFavorites(galleryDetail.galleryInfo)
remove = !dialogState.modifyFavorites(galleryDetail.galleryInfo)
if (remove) {
showTip(R.string.remove_from_favorite_success, LENGTH_SHORT)
} else {
@@ -33,6 +33,7 @@ import com.hippo.ehviewer.BuildConfig
import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.EhCookieStore
import com.hippo.ehviewer.client.EhEngine
import com.hippo.ehviewer.client.data.FavListUrlBuilder
import com.hippo.ehviewer.ui.LocalNavController
@@ -176,41 +177,43 @@ fun AdvancedScreen() {
}
}
}
val backupNothing = stringResource(id = R.string.settings_advanced_backup_favorite_nothing)
val backupFailed = stringResource(id = R.string.settings_advanced_backup_favorite_failed)
val backupSucceed = stringResource(id = R.string.settings_advanced_backup_favorite_success)
Preference(
title = stringResource(id = R.string.settings_advanced_backup_favorite),
summary = stringResource(id = R.string.settings_advanced_backup_favorite_summary),
) {
val favListUrlBuilder = FavListUrlBuilder()
var favTotal = 0
var favIndex = 0
tailrec suspend fun doBackup() {
val result = EhEngine.getFavorites(favListUrlBuilder.build())
if (result.galleryInfoList.isEmpty()) {
launchSnackBar(backupNothing)
} else {
if (favTotal == 0) favTotal = result.countArray.sum()
favIndex += result.galleryInfoList.size
val status = "($favIndex/$favTotal)"
EhDB.putLocalFavorites(result.galleryInfoList)
launchSnackBar(context.getString(R.string.settings_advanced_backup_favorite_start, status))
if (result.next != null) {
delay(Settings.downloadDelay.toLong())
favListUrlBuilder.setIndex(result.next, true)
doBackup()
if (EhCookieStore.hasSignedIn()) {
val backupNothing = stringResource(id = R.string.settings_advanced_backup_favorite_nothing)
val backupFailed = stringResource(id = R.string.settings_advanced_backup_favorite_failed)
val backupSucceed = stringResource(id = R.string.settings_advanced_backup_favorite_success)
Preference(
title = stringResource(id = R.string.settings_advanced_backup_favorite),
summary = stringResource(id = R.string.settings_advanced_backup_favorite_summary),
) {
val favListUrlBuilder = FavListUrlBuilder()
var favTotal = 0
var favIndex = 0
tailrec suspend fun doBackup() {
val result = EhEngine.getFavorites(favListUrlBuilder.build())
if (result.galleryInfoList.isEmpty()) {
launchSnackBar(backupNothing)
} else {
if (favTotal == 0) favTotal = result.countArray.sum()
favIndex += result.galleryInfoList.size
val status = "($favIndex/$favTotal)"
EhDB.putLocalFavorites(result.galleryInfoList)
launchSnackBar(context.getString(R.string.settings_advanced_backup_favorite_start, status))
if (result.next != null) {
delay(Settings.downloadDelay.toLong())
favListUrlBuilder.setIndex(result.next, true)
doBackup()
}
}
}
}
coroutineScope.launch {
runSuspendCatching {
doBackup()
}.onSuccess {
launchSnackBar(backupSucceed)
}.onFailure {
it.printStackTrace()
launchSnackBar(backupFailed)
coroutineScope.launch {
runSuspendCatching {
doBackup()
}.onSuccess {
launchSnackBar(backupSucceed)
}.onFailure {
it.printStackTrace()
launchSnackBar(backupFailed)
}
}
}
}