Skip to content

Commit

Permalink
Parse favorite categories from user config page
Browse files Browse the repository at this point in the history
  • Loading branch information
FooIbar authored and xb2016 committed Apr 29, 2024
1 parent 5d0e59f commit 0d95ce8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
66 changes: 33 additions & 33 deletions app/src/main/java/com/hippo/ehviewer/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ object Settings {
putBoolean(KEY_APP_LINK_VERIFY_TIP, value)
}

val favCat: Array<String>
var favCat: Array<String>
get() = arrayOf(
sSettingsPre.getString(KEY_FAV_CAT_0, DEFAULT_FAV_CAT_0)!!,
sSettingsPre.getString(KEY_FAV_CAT_1, DEFAULT_FAV_CAT_1)!!,
Expand All @@ -683,23 +683,23 @@ object Settings {
sSettingsPre.getString(KEY_FAV_CAT_8, DEFAULT_FAV_CAT_8)!!,
sSettingsPre.getString(KEY_FAV_CAT_9, DEFAULT_FAV_CAT_9)!!,
)
fun putFavCat(value: Array<String?>) {
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()
}

val favCount: IntArray
set(value) {
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 favCount: IntArray
get() = intArrayOf(
sSettingsPre.getInt(KEY_FAV_COUNT_0, DEFAULT_FAV_COUNT),
sSettingsPre.getInt(KEY_FAV_COUNT_1, DEFAULT_FAV_COUNT),
Expand All @@ -712,21 +712,21 @@ object Settings {
sSettingsPre.getInt(KEY_FAV_COUNT_8, DEFAULT_FAV_COUNT),
sSettingsPre.getInt(KEY_FAV_COUNT_9, DEFAULT_FAV_COUNT),
)
fun putFavCount(count: IntArray) {
check(count.size == 10)
sSettingsPre.edit()
.putInt(KEY_FAV_COUNT_0, count[0])
.putInt(KEY_FAV_COUNT_1, count[1])
.putInt(KEY_FAV_COUNT_2, count[2])
.putInt(KEY_FAV_COUNT_3, count[3])
.putInt(KEY_FAV_COUNT_4, count[4])
.putInt(KEY_FAV_COUNT_5, count[5])
.putInt(KEY_FAV_COUNT_6, count[6])
.putInt(KEY_FAV_COUNT_7, count[7])
.putInt(KEY_FAV_COUNT_8, count[8])
.putInt(KEY_FAV_COUNT_9, count[9])
.apply()
}
set(count) {
check(count.size == 10)
sSettingsPre.edit()
.putInt(KEY_FAV_COUNT_0, count[0])
.putInt(KEY_FAV_COUNT_1, count[1])
.putInt(KEY_FAV_COUNT_2, count[2])
.putInt(KEY_FAV_COUNT_3, count[3])
.putInt(KEY_FAV_COUNT_4, count[4])
.putInt(KEY_FAV_COUNT_5, count[5])
.putInt(KEY_FAV_COUNT_6, count[6])
.putInt(KEY_FAV_COUNT_7, count[7])
.putInt(KEY_FAV_COUNT_8, count[8])
.putInt(KEY_FAV_COUNT_9, count[9])
.apply()
}

val favLocalCount: Int
get() = sSettingsPre.getInt(KEY_FAV_LOCAL, DEFAULT_FAV_COUNT)
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import com.hippo.ehviewer.client.parser.HomeParser
import com.hippo.ehviewer.client.parser.ProfileParser
import com.hippo.ehviewer.client.parser.RateGalleryParser
import com.hippo.ehviewer.client.parser.TorrentParser
import com.hippo.ehviewer.client.parser.UserConfigParser
import com.hippo.ehviewer.client.parser.VoteCommentParser
import com.hippo.ehviewer.client.parser.VoteTagParser
import com.hippo.network.StatusCodeException
Expand All @@ -72,7 +73,6 @@ private val okHttpClient = EhApplication.okHttpClient
private val MEDIA_TYPE_JSON: MediaType = "application/json; charset=utf-8".toMediaType()
private const val TAG = "EhEngine"
private const val MAX_REQUEST_SIZE = 25
private const val U_CONFIG_TEXT = "Selected Profile"
private val MEDIA_TYPE_JPEG: MediaType = "image/jpeg".toMediaType()
private var sEhFilter = EhFilter

Expand Down Expand Up @@ -551,9 +551,7 @@ object EhEngine {

private suspend fun getUConfigInternal(url: String) {
Log.d(TAG, url)
EhRequestBuilder(url).executeAndParsingWith {
check(contains(U_CONFIG_TEXT)) { "U_CONFIG_TEXT not found!" }
}
EhRequestBuilder(url).executeAndParsingWith(UserConfigParser::parse)
}

suspend fun getUConfig(url: String = EhUrl.uConfigUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ object FavoritesParser {
throw ParseException("Parse favorites error")
}
val result = GalleryListParser.parse(d, body)
return Result(catArray, countArray, result)
return Result(catArray.requireNoNulls(), countArray, result)
}

class Result(
val catArray: Array<String?>,
val catArray: Array<String>,
val countArray: IntArray,
galleryListResult: GalleryListParser.Result,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.hippo.ehviewer.client.parser

import com.hippo.ehviewer.Settings
import com.hippo.yorozuya.unescapeXml

object UserConfigParser {
private const val U_CONFIG_TEXT = "Selected Profile"
private val FAV_CAT_PATTERN = Regex("<input type=\"text\" name=\"favorite_\\d\" value=\"([^\"]+)\"")

fun parse(body: String) {
check(U_CONFIG_TEXT in body) { "Unable to load user config!" }
val iterator = FAV_CAT_PATTERN.findAll(body).iterator()
val favCat = Array(10) { iterator.next().groupValues[1].unescapeXml() }
Settings.favCat = favCat
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,8 @@ class FavoritesScene :
) : EhCallback<FavoritesScene?, FavoritesParser.Result>(context) {
override fun onSuccess(result: FavoritesParser.Result) {
// Put fav cat
Settings.putFavCat(result.catArray)
Settings.putFavCount(result.countArray)
Settings.favCat = result.catArray
Settings.favCount = result.countArray
val scene = this@FavoritesScene
scene.updateHistoryFavSlot(mGidArray, mSlot)
if (mLocal) {
Expand Down

0 comments on commit 0d95ce8

Please sign in to comment.