From 84b328b60b84e5491d0033db6af4709ae853f21f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=99=BD-=E7=99=BD?= Date: Tue, 15 Nov 2022 12:08:08 +0800 Subject: [PATCH] Init prev page load --- .../client/data/FavListUrlBuilder.java | 10 +++++ .../ehviewer/client/data/ListUrlBuilder.kt | 34 +++++++++------ .../client/parser/FavoritesParser.java | 23 +++++----- .../client/parser/GalleryListParser.java | 42 ++++++++++++------- .../hippo/ehviewer/ui/CommonOperations.java | 4 -- .../ehviewer/ui/scene/FavoritesScene.java | 12 +++--- .../ehviewer/ui/scene/GalleryListScene.java | 19 ++++++--- 7 files changed, 94 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/FavListUrlBuilder.java b/app/src/main/java/com/hippo/ehviewer/client/data/FavListUrlBuilder.java index dda1924e9..35f904239 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/FavListUrlBuilder.java +++ b/app/src/main/java/com/hippo/ehviewer/client/data/FavListUrlBuilder.java @@ -44,6 +44,7 @@ public class FavListUrlBuilder implements Parcelable { } }; private static final String TAG = FavListUrlBuilder.class.getSimpleName(); + private String mPrev; private String mNext; private String mJumpTo; private String mKeyword; @@ -53,6 +54,7 @@ public class FavListUrlBuilder implements Parcelable { } protected FavListUrlBuilder(Parcel in) { + this.mPrev = in.readString(); this.mNext = in.readString(); this.mJumpTo = in.readString(); this.mKeyword = in.readString(); @@ -63,6 +65,10 @@ public class FavListUrlBuilder implements Parcelable { return favCat >= 0 && favCat <= 9; } + public void setPrev(String prev) { + mPrev = prev; + } + public void setNext(String next) { mNext = next; } @@ -111,6 +117,9 @@ public class FavListUrlBuilder implements Parcelable { Log.e(TAG, "Can't URLEncoder.encode " + mKeyword); } } + if (mPrev != null && !mPrev.isEmpty()) { + ub.addQuery("prev", mPrev); + } if (mNext != null && !mNext.isEmpty()) { ub.addQuery("next", mNext); } @@ -127,6 +136,7 @@ public class FavListUrlBuilder implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { + dest.writeString(this.mPrev); dest.writeString(this.mNext); dest.writeString(this.mJumpTo); dest.writeString(this.mKeyword); diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/ListUrlBuilder.kt b/app/src/main/java/com/hippo/ehviewer/client/data/ListUrlBuilder.kt index fdb5e23ad..3696a48db 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/ListUrlBuilder.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/data/ListUrlBuilder.kt @@ -36,8 +36,8 @@ open class ListUrlBuilder : Cloneable, Parcelable { @get:Mode @Mode var mode = MODE_NORMAL - private var mNextGid = 0 private var mPrevGid = 0 + private var mNextGid = 0 private var mJumpTo: String? = null var category = EhUtils.NONE private var mKeyword: String? = null @@ -54,8 +54,8 @@ open class ListUrlBuilder : Cloneable, Parcelable { constructor() protected constructor(parcel: Parcel) { mode = parcel.readInt() - mNextGid = parcel.readInt() mPrevGid = parcel.readInt() + mNextGid = parcel.readInt() mJumpTo = parcel.readString() this.category = parcel.readInt() mKeyword = parcel.readString() @@ -72,8 +72,8 @@ open class ListUrlBuilder : Cloneable, Parcelable { fun reset() { mode = MODE_NORMAL - mNextGid = 0 mPrevGid = 0 + mNextGid = 0 mJumpTo = null this.category = EhUtils.NONE mKeyword = null @@ -96,6 +96,10 @@ open class ListUrlBuilder : Cloneable, Parcelable { } } + fun setPrevGid(prevGid: Int) { + mPrevGid = prevGid + } + fun setNextGid(nextGid: Int) { mNextGid = nextGid } @@ -117,8 +121,8 @@ open class ListUrlBuilder : Cloneable, Parcelable { */ fun set(lub: ListUrlBuilder) { mode = lub.mode - mNextGid = lub.mNextGid mPrevGid = lub.mPrevGid + mNextGid = lub.mNextGid mJumpTo = lub.mJumpTo this.category = lub.category mKeyword = lub.mKeyword @@ -349,15 +353,15 @@ open class ListUrlBuilder : Cloneable, Parcelable { } mJumpTo?.let { ub.addQuery("seek", mJumpTo!!) - mNextGid = 0 mPrevGid = 0 - } - if (mNextGid != 0) { - ub.addQuery("next", mNextGid) + mNextGid = 0 } if (mPrevGid != 0) { ub.addQuery("prev", mPrevGid) } + if (mNextGid != 0) { + ub.addQuery("next", mNextGid) + } // Advance search if (advanceSearch != -1) { ub.addQuery("advsearch", "1") @@ -389,6 +393,9 @@ open class ListUrlBuilder : Cloneable, Parcelable { } catch (e: UnsupportedEncodingException) { // Empty } + if (mPrevGid != 0) { + sb.append("?prev=").append(mPrevGid) + } if (mNextGid != 0) { sb.append("?next=").append(mNextGid) } @@ -403,6 +410,9 @@ open class ListUrlBuilder : Cloneable, Parcelable { } catch (e: UnsupportedEncodingException) { // Empty } + if (mPrevGid != 0) { + sb.append("?prev=").append(mPrevGid) + } if (mNextGid != 0) { sb.append("?next=").append(mNextGid) } @@ -447,12 +457,12 @@ open class ListUrlBuilder : Cloneable, Parcelable { if (mSHash != null) { ub.addQuery("f_shash", mSHash!!) } - if (mNextGid != 0) { - ub.addQuery("next", mNextGid) - } if (mPrevGid != 0) { ub.addQuery("prev", mPrevGid) } + if (mNextGid != 0) { + ub.addQuery("next", mNextGid) + } if (advanceSearch != -1) { ub.addQuery("advsearch", "1") if (advanceSearch and AdvanceSearchTable.SH != 0) ub.addQuery("f_sh", "on") @@ -481,8 +491,8 @@ open class ListUrlBuilder : Cloneable, Parcelable { override fun writeToParcel(dest: Parcel, flags: Int) { dest.writeInt(mode) - dest.writeInt(mNextGid) dest.writeInt(mPrevGid) + dest.writeInt(mNextGid) dest.writeString(mJumpTo) dest.writeInt(this.category) dest.writeString(mKeyword) diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java index 4a940bfa4..fea25f709 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java @@ -31,20 +31,22 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.util.List; +import java.util.regex.Matcher; import java.util.regex.Pattern; public class FavoritesParser { + private static final Pattern PATTERN_PREV_PAGE = Pattern.compile("prev=(\\d+(-\\d+)?)"); private static final Pattern PATTERN_NEXT_PAGE = Pattern.compile("next=(\\d+(-\\d+)?)"); public static Result parse(String body) throws Exception { if (body.contains("This page requires you to log on.

")) { throw new EhException(GetText.getString(R.string.need_sign_in)); } + Document d = Jsoup.parse(body); + String[] catArray = new String[10]; int[] countArray = new int[10]; - try { - Document d = Jsoup.parse(body); Element ido = JsoupUtils.getElementByClass(d, "ido"); //noinspection ConstantConditions Elements fps = ido.getElementsByClass("fp"); @@ -61,17 +63,17 @@ public class FavoritesParser { e.printStackTrace(); throw new ParseException("Parse favorites error", body); } - Result re = new Result(); + Result re = new Result(); try { - var d = Jsoup.parse(body); - var next = d.getElementById("unext"); + Element prev = d.getElementById("uprev"); + Element next = d.getElementById("unext"); + assert prev != null; assert next != null; - var href = next.attr("href"); - var matcher = PATTERN_NEXT_PAGE.matcher(href); - if (matcher.find()) { - re.nextPage = matcher.group(1); - } + Matcher matcherPrev = PATTERN_PREV_PAGE.matcher(prev.attr("href")); + Matcher matcherNext = PATTERN_NEXT_PAGE.matcher(next.attr("href")); + if (matcherPrev.find()) re.prevPage = matcherPrev.group(1); + if (matcherNext.find()) re.nextPage = matcherNext.group(1); } catch (Throwable e) { e.printStackTrace(); } @@ -88,6 +90,7 @@ public class FavoritesParser { public static class Result { public String[] catArray; // Size 10 public int[] countArray; // Size 10 + public String prevPage; public String nextPage; public List galleryInfoList; } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java index d662d62df..f241d91cf 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java @@ -51,8 +51,8 @@ public class GalleryListParser { private static final Pattern PATTERN_THUMB_SIZE = Pattern.compile("height:(\\d+)px;width:(\\d+)px"); private static final Pattern PATTERN_FAVORITE_SLOT = Pattern.compile("background-color:rgba\\((\\d+),(\\d+),(\\d+),"); private static final Pattern PATTERN_PAGES = Pattern.compile("(\\d+) page"); - - private static final Pattern PATTERN_GETINT = Pattern.compile("[^0-9]"); + private static final Pattern PATTERN_PREV_PAGE = Pattern.compile("prev=(\\d+)"); + private static final Pattern PATTERN_NEXT_PAGE = Pattern.compile("next=(\\d+)"); private static final String[][] FAVORITE_SLOT_RGB = new String[][]{ new String[]{"0", "0", "0"}, @@ -298,21 +298,33 @@ public class GalleryListParser { return gi; } - private static int getInt(String str) { - var m = PATTERN_GETINT.matcher(str); - return Integer.parseInt(m.replaceAll("").trim()); - } - public static Result parse(@NonNull String body) throws Exception { + Result result = new Result(); Document d = Jsoup.parse(body); - - result.founds = Integer.MAX_VALUE; - result.noWatchedTags = body.contains("

You do not have any watched tags"); - if (body.contains("No hits found

")) { - result.founds = 0; - result.galleryInfoList = Collections.EMPTY_LIST; - return result; + try { + Element prev = d.getElementById("uprev"); + Element next = d.getElementById("unext"); + assert prev != null; + assert next != null; + Matcher matcherPrev = PATTERN_PREV_PAGE.matcher(prev.attr("href")); + Matcher matcherNext = PATTERN_NEXT_PAGE.matcher(next.attr("href")); + if (matcherPrev.find()) result.prevGid = NumberUtils.parseIntSafely(matcherPrev.group(1), 0); + if (matcherNext.find()) result.nextGid = NumberUtils.parseIntSafely(matcherNext.group(1), 0); + result.founds = Integer.MAX_VALUE; + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); + result.noWatchedTags = body.contains("

You do not have any watched tags"); + if (body.contains("No hits found

")) { + result.founds = 0; + //noinspection unchecked + result.galleryInfoList = Collections.EMPTY_LIST; + return result; + } else if (d.getElementsByClass("ptt").isEmpty()) { + result.founds = 1; + } else { + result.founds = Integer.MAX_VALUE; + } } try { @@ -348,6 +360,8 @@ public class GalleryListParser { public static class Result { public int founds; + public int prevGid; + public int nextGid; public boolean noWatchedTags; public List galleryInfoList; } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java index e9ff330e3..84e3bfa44 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java @@ -47,10 +47,6 @@ import java.util.Objects; public final class CommonOperations { - public static int getPagesForFounds(int founds, int per) { - return (int)Math.ceil((double) founds/per); - } - private static void doAddToFavorites(Activity activity, GalleryInfo galleryInfo, int slot, EhClient.Callback listener) { if (slot == -1) { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java index b687c616e..6c92bc898 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java @@ -829,12 +829,14 @@ public class FavoritesScene extends BaseScene implements } updateSearchBar(); - int pages = 0; assert mUrlBuilder != null; - if (FavListUrlBuilder.isValidFavCat(mUrlBuilder.getFavCat())) - pages = CommonOperations.getPagesForFounds(mFavCountArray[mUrlBuilder.getFavCat()], 50); - else if (mUrlBuilder.getFavCat() == FavListUrlBuilder.FAV_CAT_ALL) - pages = CommonOperations.getPagesForFounds(mFavCountSum, 50); + + int pages = 0; + if (result.nextPage == null) + pages = mHelper.pgCounter + 1; + else + pages = Integer.MAX_VALUE; + mHelper.nextPg = result.nextPage; mHelper.onGetPageData(taskId, pages, mHelper.pgCounter + 1, result.galleryInfoList); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java index 7f9b8ae2c..cf3ea43d3 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java @@ -861,7 +861,7 @@ public final class GalleryListScene extends BaseScene } if (ListUrlBuilder.MODE_TOPLIST == mUrlBuilder.getMode()) { - final int page = (mHelper.getPageForTop() == 0 ? mHelper.pgCounter : mHelper.getPageForTop()) + 1; + final int page = mHelper.getPageForTop() + 1; final int pages = 200; String hint = getString(R.string.go_to_hint, page, pages); final EditTextDialogBuilder builder = new EditTextDialogBuilder(context, null, hint); @@ -878,12 +878,12 @@ public final class GalleryListScene extends BaseScene String text = builder.getText().trim(); int goTo; try { - goTo = Integer.parseInt(text); + goTo = Integer.parseInt(text) - 1; } catch (NumberFormatException e) { builder.setError(getString(R.string.error_invalid_number)); return; } - if (goTo <= 0 || goTo > pages) { + if (goTo < 0 || goTo >= pages) { builder.setError(getString(R.string.error_out_of_range)); return; } @@ -1405,7 +1405,16 @@ public final class GalleryListScene extends BaseScene ? R.string.gallery_list_empty_hit_subscription : R.string.gallery_list_empty_hit); mHelper.setEmptyString(emptyString); - mHelper.onGetPageData(taskId, CommonOperations.getPagesForFounds(result.founds, 25), mHelper.pgCounter + 1, result.galleryInfoList); + + int pages = 0; + if (mIsTopList) + pages = 200; + else if (result.nextGid == 0) + pages = mHelper.pgCounter + 1; + else + pages = result.founds; + + mHelper.onGetPageData(taskId, pages, mHelper.pgCounter + 1, result.galleryInfoList); } } @@ -1741,7 +1750,7 @@ public final class GalleryListScene extends BaseScene mUrlBuilder.setNextGid(0); if (jumpTo != null && mIsTopList) { - pgCounter = Integer.parseInt(jumpTo) - 1; + pgCounter = Integer.parseInt(jumpTo); mUrlBuilder.setNextGid(pgCounter); } mUrlBuilder.setJumpTo(jumpTo);