From f284ac71b56a5881c8c5ce22299fd2f09b7d39b1 Mon Sep 17 00:00:00 2001 From: tarsin Date: Fri, 2 Jun 2023 14:47:56 +0800 Subject: [PATCH] Introduce stringArrayPref --- .../main/java/com/hippo/ehviewer/Settings.kt | 61 +++---------------- .../ehviewer/client/parser/FavoritesParser.kt | 4 +- .../hippo/ehviewer/ui/scene/FavoritesScene.kt | 4 +- 3 files changed, 14 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/Settings.kt b/app/src/main/java/com/hippo/ehviewer/Settings.kt index aedd09920..a8fcef6f5 100755 --- a/app/src/main/java/com/hippo/ehviewer/Settings.kt +++ b/app/src/main/java/com/hippo/ehviewer/Settings.kt @@ -78,26 +78,6 @@ object Settings : DefaultPreferences() { private val TAG = Settings::class.java.simpleName private const val KEY_LAUNCH_PAGE = "launch_page" private const val DEFAULT_LAUNCH_PAGE = 0 - private const val KEY_FAV_CAT_0 = "fav_cat_0" - private const val KEY_FAV_CAT_1 = "fav_cat_1" - private const val KEY_FAV_CAT_2 = "fav_cat_2" - private const val KEY_FAV_CAT_3 = "fav_cat_3" - private const val KEY_FAV_CAT_4 = "fav_cat_4" - private const val KEY_FAV_CAT_5 = "fav_cat_5" - private const val KEY_FAV_CAT_6 = "fav_cat_6" - private const val KEY_FAV_CAT_7 = "fav_cat_7" - private const val KEY_FAV_CAT_8 = "fav_cat_8" - private const val KEY_FAV_CAT_9 = "fav_cat_9" - private const val DEFAULT_FAV_CAT_0 = "Favorites 0" - private const val DEFAULT_FAV_CAT_1 = "Favorites 1" - private const val DEFAULT_FAV_CAT_2 = "Favorites 2" - private const val DEFAULT_FAV_CAT_3 = "Favorites 3" - private const val DEFAULT_FAV_CAT_4 = "Favorites 4" - private const val DEFAULT_FAV_CAT_5 = "Favorites 5" - private const val DEFAULT_FAV_CAT_6 = "Favorites 6" - private const val DEFAULT_FAV_CAT_7 = "Favorites 7" - private const val DEFAULT_FAV_CAT_8 = "Favorites 8" - private const val DEFAULT_FAV_CAT_9 = "Favorites 9" private lateinit var sSettingsPre: SharedPreferences fun initialize() { @@ -205,38 +185,8 @@ object Settings : DefaultPreferences() { putString(KEY_DOWNLOAD_SAVE_FRAGMENT, uri.encodedFragment) } - val favCat: Array - get() = arrayOf( - sSettingsPre.getString(KEY_FAV_CAT_0, DEFAULT_FAV_CAT_0)!!, - sSettingsPre.getString(KEY_FAV_CAT_1, DEFAULT_FAV_CAT_1)!!, - sSettingsPre.getString(KEY_FAV_CAT_2, DEFAULT_FAV_CAT_2)!!, - sSettingsPre.getString(KEY_FAV_CAT_3, DEFAULT_FAV_CAT_3)!!, - sSettingsPre.getString(KEY_FAV_CAT_4, DEFAULT_FAV_CAT_4)!!, - sSettingsPre.getString(KEY_FAV_CAT_5, DEFAULT_FAV_CAT_5)!!, - sSettingsPre.getString(KEY_FAV_CAT_6, DEFAULT_FAV_CAT_6)!!, - sSettingsPre.getString(KEY_FAV_CAT_7, DEFAULT_FAV_CAT_7)!!, - sSettingsPre.getString(KEY_FAV_CAT_8, DEFAULT_FAV_CAT_8)!!, - sSettingsPre.getString(KEY_FAV_CAT_9, DEFAULT_FAV_CAT_9)!!, - ) - - fun putFavCat(value: Array) { - check(value.size == 10) - sSettingsPre.edit() - .putString(KEY_FAV_CAT_0, value[0]) - .putString(KEY_FAV_CAT_1, value[1]) - .putString(KEY_FAV_CAT_2, value[2]) - .putString(KEY_FAV_CAT_3, value[3]) - .putString(KEY_FAV_CAT_4, value[4]) - .putString(KEY_FAV_CAT_5, value[5]) - .putString(KEY_FAV_CAT_6, value[6]) - .putString(KEY_FAV_CAT_7, value[7]) - .putString(KEY_FAV_CAT_8, value[8]) - .putString(KEY_FAV_CAT_9, value[9]) - .apply() - } - + var favCat by stringArrayPref("fav_cat", 10, "Favorites") var favCount by intArrayPref("fav_count", 10) - var archivePasswds by stringSetOrNullPref("archive_passwds") val downloadDelay by intFromStrPref("download_delay", 0) @@ -308,6 +258,15 @@ object Settings : DefaultPreferences() { edit { value.zip(_value) { v, d -> d.value = v } } } } + + private fun stringArrayPref(key: String, count: Int, defMetaValue: String) = object : Delegate> { + private var _value = (0 until count).map { stringPref("${key}_$it", "$defMetaValue $it") }.toTypedArray() + override fun getValue(thisRef: Any?, prop: KProperty<*>?): Array = _value.map { it.value }.toTypedArray() + override fun setValue(thisRef: Any?, prop: KProperty<*>?, value: Array) { + check(value.size == count) + edit { value.zip(_value) { v, d -> d.value = v } } + } + } } interface Delegate { diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt index 74529b4d5..9393acf4e 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt @@ -47,11 +47,11 @@ object FavoritesParser { throw ParseException("Parse favorites error", body) } val result = GalleryListParser.parse(d, body) - return Result(catArray, countArray, result) + return Result(catArray.requireNoNulls(), countArray, result) } class Result( - val catArray: Array, + val catArray: Array, val countArray: IntArray, galleryListResult: GalleryListParser.Result, ) { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt index c645c42a7..2c268c25e 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.kt @@ -908,7 +908,7 @@ class FavoritesScene : EhEngine.modifyFavorites(url, gidArray, mModifyFavCat) }.onSuccess { result -> // Put fav cat - Settings.putFavCat(result.catArray) + Settings.favCat = result.catArray Settings.favCount = result.countArray withUIContext { if (local) { @@ -940,7 +940,7 @@ class FavoritesScene : runSuspendCatching { EhEngine.getFavorites(url) }.onSuccess { result -> - Settings.putFavCat(result.catArray) + Settings.favCat = result.catArray Settings.favCount = result.countArray withUIContext { onGetFavoritesSuccess(result, taskId)