Refactor getPageData
This commit is contained in:
1 parent
416ef8d6d7
commit
b287af4824
10 files changed
+166
-303
No files matched your search
@@ -32,7 +32,7 @@ public class FavListUrlBuilder implements Parcelable {
|
||||
|
||||
public static final int FAV_CAT_ALL = -1;
|
||||
public static final int FAV_CAT_LOCAL = -2;
|
||||
public static final Parcelable.Creator<FavListUrlBuilder> CREATOR = new Parcelable.Creator<FavListUrlBuilder>() {
|
||||
public static final Parcelable.Creator<FavListUrlBuilder> CREATOR = new Parcelable.Creator<>() {
|
||||
|
||||
@Override
|
||||
public FavListUrlBuilder createFromParcel(Parcel source) {
|
||||
@@ -66,12 +66,14 @@ public class FavListUrlBuilder implements Parcelable {
|
||||
return favCat >= 0 && favCat <= 9;
|
||||
}
|
||||
|
||||
public void setPrev(String prev) {
|
||||
mPrev = prev;
|
||||
}
|
||||
|
||||
public void setNext(String next) {
|
||||
mNext = next;
|
||||
public void setIndex(String index, boolean isNext) {
|
||||
if (isNext) {
|
||||
mNext = index;
|
||||
mPrev = null;
|
||||
} else {
|
||||
mPrev = index;
|
||||
mNext = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setJumpTo(String jumpTo) {
|
||||
@@ -115,12 +117,6 @@ public class FavListUrlBuilder implements Parcelable {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
// Name
|
||||
ub.addQuery("sn", "on");
|
||||
// Tags
|
||||
ub.addQuery("st", "on");
|
||||
// Note
|
||||
ub.addQuery("sf", "on");
|
||||
}
|
||||
if (mPrev != null && !mPrev.isEmpty()) {
|
||||
ub.addQuery("prev", mPrev);
|
||||
|
||||
@@ -36,8 +36,8 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
@get:Mode
|
||||
@Mode
|
||||
var mode = MODE_NORMAL
|
||||
private var mPrevGid = 0
|
||||
private var mNextGid = 0
|
||||
private var mPrev: String? = null
|
||||
private var mNext: String? = null
|
||||
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()
|
||||
mPrevGid = parcel.readInt()
|
||||
mNextGid = parcel.readInt()
|
||||
mPrev = parcel.readString()
|
||||
mNext = parcel.readString()
|
||||
mJumpTo = parcel.readString()
|
||||
this.category = parcel.readInt()
|
||||
mKeyword = parcel.readString()
|
||||
@@ -72,8 +72,8 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
|
||||
fun reset() {
|
||||
mode = MODE_NORMAL
|
||||
mPrevGid = 0
|
||||
mNextGid = 0
|
||||
mPrev = null
|
||||
mNext = null
|
||||
mJumpTo = null
|
||||
this.category = EhUtils.NONE
|
||||
mKeyword = null
|
||||
@@ -96,12 +96,14 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
}
|
||||
}
|
||||
|
||||
fun setPrevGid(prevGid: Int) {
|
||||
mPrevGid = prevGid
|
||||
}
|
||||
|
||||
fun setNextGid(nextGid: Int) {
|
||||
mNextGid = nextGid
|
||||
fun setIndex(index: String?, isNext: Boolean = true) {
|
||||
if (isNext) {
|
||||
mNext = index
|
||||
mPrev = null
|
||||
} else {
|
||||
mPrev = index
|
||||
mNext = null
|
||||
}
|
||||
}
|
||||
|
||||
fun setJumpTo(jumpTo: String?) {
|
||||
@@ -121,8 +123,8 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
*/
|
||||
fun set(lub: ListUrlBuilder) {
|
||||
mode = lub.mode
|
||||
mPrevGid = lub.mPrevGid
|
||||
mNextGid = lub.mNextGid
|
||||
mPrev = lub.mPrev
|
||||
mNext = lub.mNext
|
||||
mJumpTo = lub.mJumpTo
|
||||
this.category = lub.category
|
||||
mKeyword = lub.mKeyword
|
||||
@@ -196,7 +198,7 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
if (TextUtils.isEmpty(query)) {
|
||||
return
|
||||
}
|
||||
val querys = StringUtils.split(query, '&')
|
||||
val queries = StringUtils.split(query, '&')
|
||||
var category = 0
|
||||
var keyword: String? = null
|
||||
var enableAdvanceSearch = false
|
||||
@@ -206,7 +208,7 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
var enablePage = false
|
||||
var pageFrom = -1
|
||||
var pageTo = -1
|
||||
for (str in querys) {
|
||||
for (str in queries) {
|
||||
val index = str.indexOf('=')
|
||||
if (index < 0) {
|
||||
continue
|
||||
@@ -348,19 +350,19 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mSHash != null) {
|
||||
ub.addQuery("f_shash", mSHash!!)
|
||||
mSHash?.let {
|
||||
ub.addQuery("f_shash", it)
|
||||
}
|
||||
mJumpTo?.let {
|
||||
ub.addQuery("seek", mJumpTo!!)
|
||||
mPrevGid = 0
|
||||
mNextGid = 0
|
||||
ub.addQuery("seek", it)
|
||||
mPrev = null
|
||||
mNext = null
|
||||
}
|
||||
if (mPrevGid != 0) {
|
||||
ub.addQuery("prev", mPrevGid)
|
||||
mPrev?.let {
|
||||
ub.addQuery("prev", it)
|
||||
}
|
||||
if (mNextGid != 0) {
|
||||
ub.addQuery("next", mNextGid)
|
||||
mNext?.let {
|
||||
ub.addQuery("next", it)
|
||||
}
|
||||
// Advance search
|
||||
if (advanceSearch != -1) {
|
||||
@@ -393,14 +395,14 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
} catch (e: UnsupportedEncodingException) {
|
||||
// Empty
|
||||
}
|
||||
if (mPrevGid != 0) {
|
||||
sb.append("?prev=").append(mPrevGid)
|
||||
mPrev?.let {
|
||||
sb.append("?prev=").append(it)
|
||||
}
|
||||
if (mNextGid != 0) {
|
||||
sb.append("?next=").append(mNextGid)
|
||||
mNext?.let {
|
||||
sb.append("?next=").append(it)
|
||||
}
|
||||
if (mJumpTo != null) {
|
||||
sb.append("&seek=").append(mJumpTo)
|
||||
mJumpTo?.let {
|
||||
sb.append("&seek=").append(it)
|
||||
}
|
||||
sb.toString()
|
||||
}
|
||||
@@ -413,14 +415,14 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
} catch (e: UnsupportedEncodingException) {
|
||||
// Empty
|
||||
}
|
||||
if (mPrevGid != 0) {
|
||||
sb.append("?prev=").append(mPrevGid)
|
||||
mPrev?.let {
|
||||
sb.append("?prev=").append(it)
|
||||
}
|
||||
if (mNextGid != 0) {
|
||||
sb.append("?next=").append(mNextGid)
|
||||
mNext?.let {
|
||||
sb.append("?next=").append(it)
|
||||
}
|
||||
if (mJumpTo != null) {
|
||||
sb.append("&seek=").append(mJumpTo)
|
||||
mJumpTo?.let {
|
||||
sb.append("&seek=").append(it)
|
||||
}
|
||||
sb.toString()
|
||||
}
|
||||
@@ -433,61 +435,15 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
try {
|
||||
sb.append(URLEncoder.encode(mKeyword, "UTF-8"))
|
||||
} catch (e: UnsupportedEncodingException) {
|
||||
// Empty
|
||||
e.printStackTrace()
|
||||
}
|
||||
if (mNextGid != 0) {
|
||||
sb.append("&p=").append(mNextGid)
|
||||
mJumpTo?.let {
|
||||
sb.append("&p=").append(it)
|
||||
}
|
||||
sb.toString()
|
||||
}
|
||||
|
||||
else -> {
|
||||
val url: String = if (mode == MODE_NORMAL) {
|
||||
EhUrl.getHost()
|
||||
} else {
|
||||
EhUrl.getWatchedUrl()
|
||||
}
|
||||
val ub = UrlBuilder(url)
|
||||
if (this.category != EhUtils.NONE) {
|
||||
ub.addQuery("f_cats", category.inv() and EhConfig.ALL_CATEGORY)
|
||||
}
|
||||
if (mKeyword != null) {
|
||||
val keyword = mKeyword!!.trim { it <= ' ' }
|
||||
if (keyword.isNotEmpty()) {
|
||||
try {
|
||||
ub.addQuery("f_search", URLEncoder.encode(mKeyword, "UTF-8"))
|
||||
} catch (_: UnsupportedEncodingException) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mSHash != null) {
|
||||
ub.addQuery("f_shash", mSHash!!)
|
||||
}
|
||||
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")
|
||||
if (advanceSearch and AdvanceSearchTable.STO != 0) ub.addQuery("f_sto", "on")
|
||||
if (advanceSearch and AdvanceSearchTable.SFL != 0) ub.addQuery("f_sfl", "on")
|
||||
if (advanceSearch and AdvanceSearchTable.SFU != 0) ub.addQuery("f_sfu", "on")
|
||||
if (advanceSearch and AdvanceSearchTable.SFT != 0) ub.addQuery("f_sft", "on")
|
||||
if (minRating != -1) {
|
||||
ub.addQuery("f_sr", "on")
|
||||
ub.addQuery("f_srdd", minRating)
|
||||
}
|
||||
if (pageFrom != -1 || pageTo != -1) {
|
||||
ub.addQuery("f_sp", "on")
|
||||
ub.addQuery("f_spf", if (pageFrom != -1) pageFrom.toString() else "")
|
||||
ub.addQuery("f_spt", if (pageTo != -1) pageTo.toString() else "")
|
||||
}
|
||||
}
|
||||
ub.build()
|
||||
}
|
||||
else -> throw java.lang.IllegalStateException("Unexpected value: $mode")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -497,8 +453,8 @@ open class ListUrlBuilder : Cloneable, Parcelable {
|
||||
|
||||
override fun writeToParcel(dest: Parcel, flags: Int) {
|
||||
dest.writeInt(mode)
|
||||
dest.writeInt(mPrevGid)
|
||||
dest.writeInt(mNextGid)
|
||||
dest.writeString(mPrev)
|
||||
dest.writeString(mNext)
|
||||
dest.writeString(mJumpTo)
|
||||
dest.writeInt(this.category)
|
||||
dest.writeString(mKeyword)
|
||||
|
||||
@@ -31,22 +31,17 @@ 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.</p>")) {
|
||||
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");
|
||||
@@ -64,24 +59,13 @@ public class FavoritesParser {
|
||||
throw new ParseException("Parse favorites error", body);
|
||||
}
|
||||
|
||||
Result re = new 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()) re.prevPage = matcherPrev.group(1);
|
||||
if (matcherNext.find()) re.nextPage = matcherNext.group(1);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
GalleryListParser.Result result = GalleryListParser.parse(body);
|
||||
|
||||
Result re = new Result();
|
||||
re.catArray = catArray;
|
||||
re.countArray = countArray;
|
||||
re.prev = result.prev;
|
||||
re.next = result.next;
|
||||
re.galleryInfoList = result.galleryInfoList;
|
||||
|
||||
return re;
|
||||
@@ -90,8 +74,8 @@ public class FavoritesParser {
|
||||
public static class Result {
|
||||
public String[] catArray; // Size 10
|
||||
public int[] countArray; // Size 10
|
||||
public String prevPage;
|
||||
public String nextPage;
|
||||
public String prev;
|
||||
public String next;
|
||||
public List<GalleryInfo> galleryInfoList;
|
||||
}
|
||||
}
|
||||
@@ -50,8 +50,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_PREV_PAGE = Pattern.compile("prev=(\\d+)");
|
||||
private static final Pattern PATTERN_NEXT_PAGE = Pattern.compile("next=(\\d+)");
|
||||
private static final Pattern PATTERN_PREV_PAGE = Pattern.compile("prev=(\\d+(-\\d+)?)");
|
||||
private static final Pattern PATTERN_NEXT_PAGE = Pattern.compile("next=(\\d+(-\\d+)?)");
|
||||
|
||||
private static final String[][] FAVORITE_SLOT_RGB = new String[][]{
|
||||
new String[]{"0", "0", "0"},
|
||||
@@ -256,11 +256,7 @@ public class GalleryListParser {
|
||||
if (div != null) {
|
||||
gi.disowned = div.attr("style").contains("opacity:0.5");
|
||||
Element a = div.children().first();
|
||||
if (a != null) {
|
||||
gi.uploader = a.text().trim();
|
||||
} else {
|
||||
gi.uploader = div.text().trim();
|
||||
}
|
||||
gi.uploader = a == null ? div.text().trim() : a.text().trim();
|
||||
}
|
||||
}
|
||||
if (children.size() > pagesIndex) {
|
||||
@@ -299,25 +295,41 @@ public class GalleryListParser {
|
||||
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);
|
||||
result.prev = matcherPrev.group(1);
|
||||
if (matcherNext.find())
|
||||
result.nextGid = NumberUtils.parseIntSafely(matcherNext.group(1), 0);
|
||||
result.founds = Integer.MAX_VALUE;
|
||||
result.next = matcherNext.group(1);
|
||||
} catch (Throwable e) {
|
||||
ExceptionUtils.throwIfFatal(e);
|
||||
result.noWatchedTags = body.contains("<p>You do not have any watched tags");
|
||||
if (body.contains("No hits found</p>")) {
|
||||
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 { // For toplists
|
||||
Element ptt = d.getElementsByClass("ptt").first();
|
||||
if (ptt != null) {
|
||||
Elements es = ptt.child(0).child(0).children();
|
||||
result.pages = Integer.parseInt(es.get(es.size() - 2).text().trim());
|
||||
|
||||
Element e = es.get(es.size() - 1);
|
||||
if (e != null) {
|
||||
e = e.children().first();
|
||||
if (e != null) {
|
||||
String href = e.attr("href");
|
||||
Matcher matcher = PATTERN_NEXT_PAGE.matcher(href);
|
||||
if (matcher.find()) {
|
||||
result.nextPage = NumberUtils.parseIntSafely(matcher.group(1), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
Element itg = d.getElementsByClass("itg").first();
|
||||
Elements es;
|
||||
@@ -350,9 +362,10 @@ public class GalleryListParser {
|
||||
}
|
||||
|
||||
public static class Result {
|
||||
public int founds;
|
||||
public int prevGid;
|
||||
public int nextGid;
|
||||
public int pages;
|
||||
public int nextPage;
|
||||
public String prev;
|
||||
public String next;
|
||||
public boolean noWatchedTags;
|
||||
public List<GalleryInfo> galleryInfoList;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package com.hippo.ehviewer.ui.fragment;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
@@ -333,14 +332,14 @@ public class AdvancedFragment extends BasePreferenceFragment {
|
||||
Log.d("LocalFavorites", "now backup page " + status);
|
||||
EhDB.putLocalFavorites(result.galleryInfoList);
|
||||
|
||||
if (result.nextPage != null) {
|
||||
if (result.next != null) {
|
||||
try {
|
||||
Thread.sleep(Settings.getDownloadDelay());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
favIndex++;
|
||||
favListUrlBuilder.setNext(result.nextPage);
|
||||
favListUrlBuilder.setIndex(result.next, true);
|
||||
request.setArgs(favListUrlBuilder.build());
|
||||
mClient.execute(request);
|
||||
} else {
|
||||
|
||||
@@ -588,7 +588,7 @@ public class FavoritesScene extends SearchBarScene implements
|
||||
// Refresh
|
||||
case 1 -> mHelper.refresh();
|
||||
// Last page
|
||||
case 2 -> mHelper.goToPage(Integer.MAX_VALUE - 1);
|
||||
case 2 -> mHelper.goTo("1-0", false);
|
||||
}
|
||||
view.setExpanded(false);
|
||||
return;
|
||||
@@ -734,28 +734,7 @@ public class FavoritesScene extends SearchBarScene implements
|
||||
}
|
||||
|
||||
updateSearchBar();
|
||||
|
||||
int pages;
|
||||
if (result.nextPage == null)
|
||||
pages = mHelper.pgCounter + 1;
|
||||
else
|
||||
pages = Integer.MAX_VALUE;
|
||||
|
||||
String prev = result.prevPage, next = result.nextPage;
|
||||
switch (mHelper.operation) {
|
||||
case -1:
|
||||
if (prev != null) mHelper.upperPage = prev;
|
||||
break;
|
||||
case 1:
|
||||
if (next != null) mHelper.lowerPage = next;
|
||||
break;
|
||||
default:
|
||||
mHelper.upperPage = prev;
|
||||
mHelper.lowerPage = next;
|
||||
break;
|
||||
}
|
||||
|
||||
mHelper.onGetPageData(taskId, pages, mHelper.pgCounter + 1, result.galleryInfoList);
|
||||
mHelper.onGetPageData(taskId, 0, 0, result.prev, result.next, result.galleryInfoList);
|
||||
|
||||
if (mDrawerAdapter != null) {
|
||||
mDrawerAdapter.notifyDataSetChanged();
|
||||
@@ -780,9 +759,9 @@ public class FavoritesScene extends SearchBarScene implements
|
||||
}
|
||||
|
||||
if (list.size() == 0) {
|
||||
mHelper.onGetPageData(taskId, 0, 0, Collections.EMPTY_LIST);
|
||||
mHelper.onGetPageData(taskId, 0, 0, null, null, Collections.EMPTY_LIST);
|
||||
} else {
|
||||
mHelper.onGetPageData(taskId, 1, 0, list);
|
||||
mHelper.onGetPageData(taskId, 1, 0, null, null, list);
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(keyword)) {
|
||||
@@ -1073,18 +1052,13 @@ public class FavoritesScene extends SearchBarScene implements
|
||||
}
|
||||
|
||||
private class FavoritesHelper extends GalleryInfoContentHelper {
|
||||
public String upperPage = null;
|
||||
public String lowerPage = null;
|
||||
public int operation = 0;
|
||||
public int pgCounter = 0;
|
||||
|
||||
@Override
|
||||
protected void getPageData(final int taskId, int type, int page) {
|
||||
protected void getPageData(final int taskId, int type, int page, String index, boolean isNext) {
|
||||
MainActivity activity = getMainActivity();
|
||||
if (null == activity || null == mUrlBuilder || null == mClient) {
|
||||
return;
|
||||
}
|
||||
pgCounter = page;
|
||||
|
||||
if (mEnableModify) {
|
||||
mEnableModify = false;
|
||||
@@ -1123,7 +1097,7 @@ public class FavoritesScene extends SearchBarScene implements
|
||||
url = mUrlBuilder.build();
|
||||
}
|
||||
|
||||
mUrlBuilder.setNext(lowerPage);
|
||||
mUrlBuilder.setIndex(index, true);
|
||||
EhRequest request = new EhRequest();
|
||||
request.setMethod(EhClient.METHOD_MODIFY_FAVORITES);
|
||||
request.setCallback(new GetFavoritesListener(getContext(),
|
||||
@@ -1136,28 +1110,8 @@ public class FavoritesScene extends SearchBarScene implements
|
||||
final String keyword = mUrlBuilder.getKeyword();
|
||||
SimpleHandler.getInstance().post(() -> onGetFavoritesLocal(keyword, taskId));
|
||||
} else {
|
||||
String prev = null, next = null;
|
||||
if (page == Integer.MAX_VALUE - 1) {
|
||||
prev = "1-0";
|
||||
jumpTo = null;
|
||||
} else if (jumpTo != null) {
|
||||
next = Integer.toString(minGid);
|
||||
operation = 0;
|
||||
} else if (page != 0 && mHelper != null) {
|
||||
if (page >= mHelper.getPageForTop()) {
|
||||
next = lowerPage;
|
||||
operation = 1;
|
||||
} else {
|
||||
prev = upperPage;
|
||||
operation = -1;
|
||||
}
|
||||
}
|
||||
mUrlBuilder.setPrev(prev);
|
||||
mUrlBuilder.setNext(next);
|
||||
|
||||
mUrlBuilder.setIndex(index, isNext);
|
||||
mUrlBuilder.setJumpTo(jumpTo);
|
||||
jumpTo = null;
|
||||
|
||||
String url = mUrlBuilder.build();
|
||||
EhRequest request = new EhRequest();
|
||||
request.setMethod(EhClient.METHOD_GET_FAVORITES);
|
||||
@@ -1225,26 +1179,17 @@ public class FavoritesScene extends SearchBarScene implements
|
||||
@Override
|
||||
protected void beforeRefresh() {
|
||||
super.beforeRefresh();
|
||||
upperPage = null;
|
||||
lowerPage = null;
|
||||
operation = 0;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected Parcelable saveInstanceState(@NonNull Parcelable superState) {
|
||||
Bundle bundle = (Bundle) super.saveInstanceState(superState);
|
||||
bundle.putString(KEY_UPPER_PAGE, upperPage);
|
||||
bundle.putString(KEY_LOWER_PAGE, lowerPage);
|
||||
return bundle;
|
||||
return (Bundle) super.saveInstanceState(superState);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected Parcelable restoreInstanceState(@NonNull Parcelable state) {
|
||||
Bundle bundle = (Bundle) state;
|
||||
upperPage = bundle.getString(KEY_UPPER_PAGE);
|
||||
lowerPage = bundle.getString(KEY_LOWER_PAGE);
|
||||
return super.restoreInstanceState(state);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -828,9 +828,9 @@ public final class GalleryListScene extends SearchBarScene
|
||||
return;
|
||||
}
|
||||
|
||||
if (ListUrlBuilder.MODE_TOPLIST == mUrlBuilder.getMode()) {
|
||||
if (mIsTopList) {
|
||||
final int page = mHelper.getPageForTop() + 1;
|
||||
final int pages = 200;
|
||||
final int pages = mHelper.getPages();
|
||||
String hint = getString(R.string.go_to_hint, page, pages);
|
||||
final EditTextDialogBuilder builder = new EditTextDialogBuilder(context, null, hint);
|
||||
builder.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
|
||||
@@ -856,7 +856,7 @@ public final class GalleryListScene extends SearchBarScene
|
||||
return;
|
||||
}
|
||||
builder.setError(null);
|
||||
mHelper.goToPage(goTo);
|
||||
mHelper.goTo(goTo);
|
||||
dialog.dismiss();
|
||||
});
|
||||
} else {
|
||||
@@ -897,7 +897,7 @@ public final class GalleryListScene extends SearchBarScene
|
||||
// Refresh
|
||||
case 1 -> mHelper.refresh();
|
||||
// Last page
|
||||
case 2 -> mHelper.goToPage(Integer.MAX_VALUE - 1);
|
||||
case 2 -> mHelper.goTo("1", false);
|
||||
}
|
||||
|
||||
view.setExpanded(false);
|
||||
@@ -1258,16 +1258,11 @@ public final class GalleryListScene extends SearchBarScene
|
||||
? R.string.gallery_list_empty_hit_subscription
|
||||
: R.string.gallery_list_empty_hit);
|
||||
mHelper.setEmptyString(emptyString);
|
||||
|
||||
int pages;
|
||||
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);
|
||||
if (mIsTopList) {
|
||||
mHelper.onGetPageData(taskId, result.pages, result.nextPage, null, null, result.galleryInfoList);
|
||||
} else {
|
||||
mHelper.onGetPageData(taskId, 0, 0, result.prev, result.next, result.galleryInfoList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1439,7 +1434,8 @@ public final class GalleryListScene extends SearchBarScene
|
||||
}
|
||||
|
||||
mUrlBuilder.set(mQuickSearchList.get(position));
|
||||
mUrlBuilder.setNextGid(0);
|
||||
mUrlBuilder.setIndex(null, true);
|
||||
mUrlBuilder.setJumpTo(null);
|
||||
onUpdateUrlBuilder();
|
||||
mHelper.refresh();
|
||||
setState(STATE_NORMAL);
|
||||
@@ -1456,7 +1452,8 @@ public final class GalleryListScene extends SearchBarScene
|
||||
}
|
||||
|
||||
mUrlBuilder.setKeyword(String.valueOf(keywords[position]));
|
||||
mUrlBuilder.setNextGid(0);
|
||||
mUrlBuilder.setIndex(null, true);
|
||||
mUrlBuilder.setJumpTo(null);
|
||||
onUpdateUrlBuilder();
|
||||
mHelper.refresh();
|
||||
setState(STATE_NORMAL);
|
||||
@@ -1586,41 +1583,20 @@ public final class GalleryListScene extends SearchBarScene
|
||||
}
|
||||
|
||||
private class GalleryListHelper extends GalleryInfoContentHelper {
|
||||
public int pgCounter = 0;
|
||||
|
||||
@Override
|
||||
protected void getPageData(int taskId, int type, int page) {
|
||||
pgCounter = page;
|
||||
protected void getPageData(int taskId, int type, int page, String index, boolean isNext) {
|
||||
MainActivity activity = getMainActivity();
|
||||
if (null == activity || null == mClient || null == mHelper || null == mUrlBuilder) {
|
||||
return;
|
||||
}
|
||||
|
||||
int prevGid = 0, nextGid = 0;
|
||||
if (mIsTopList) {
|
||||
if (jumpTo != null) {
|
||||
pgCounter = Integer.parseInt(jumpTo);
|
||||
nextGid = pgCounter;
|
||||
} else {
|
||||
nextGid = page;
|
||||
}
|
||||
} else if (page == Integer.MAX_VALUE - 1) {
|
||||
prevGid = 1;
|
||||
jumpTo = null;
|
||||
} else if (jumpTo != null) {
|
||||
nextGid = minGid;
|
||||
} else if (page != 0) {
|
||||
if (page >= mHelper.getPageForTop())
|
||||
nextGid = minGid;
|
||||
else
|
||||
prevGid = maxGid;
|
||||
mUrlBuilder.setJumpTo(String.valueOf(page));
|
||||
} else {
|
||||
mUrlBuilder.setIndex(index, isNext);
|
||||
mUrlBuilder.setJumpTo(jumpTo);
|
||||
}
|
||||
mUrlBuilder.setPrevGid(prevGid);
|
||||
mUrlBuilder.setNextGid(nextGid);
|
||||
|
||||
mUrlBuilder.setJumpTo(jumpTo);
|
||||
jumpTo = null;
|
||||
|
||||
if (ListUrlBuilder.MODE_IMAGE_SEARCH == mUrlBuilder.getMode()) {
|
||||
EhRequest request = new EhRequest();
|
||||
request.setMethod(EhClient.METHOD_IMAGE_SEARCH);
|
||||
|
||||
@@ -247,7 +247,7 @@ public class GalleryPreviewsScene extends ToolbarScene {
|
||||
list.add(previewSet.getGalleryPreview(mGalleryInfo.gid, i));
|
||||
}
|
||||
|
||||
mHelper.onGetPageData(taskId, result.second, 0, list);
|
||||
mHelper.onGetPageData(taskId, result.second, 0, null, null, list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,7 +343,7 @@ public class GalleryPreviewsScene extends ToolbarScene {
|
||||
private class GalleryPreviewHelper extends ContentLayout.ContentHelper<GalleryPreview> {
|
||||
|
||||
@Override
|
||||
protected void getPageData(final int taskId, int type, int page) {
|
||||
protected void getPageData(final int taskId, int type, int page, String index, boolean isNext) {
|
||||
MainActivity activity = getMainActivity();
|
||||
if (null == activity || null == mClient || null == mGalleryInfo) {
|
||||
onGetException(taskId, new EhException(getString(R.string.error_cannot_find_gallery)));
|
||||
|
||||
@@ -27,16 +27,8 @@ import java.time.Instant
|
||||
import java.time.ZoneOffset
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.Locale
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
abstract class GalleryInfoContentHelper : ContentHelper<GalleryInfo?>() {
|
||||
@JvmField
|
||||
var maxGid = -1
|
||||
|
||||
@JvmField
|
||||
var minGid = -1
|
||||
|
||||
@JvmField
|
||||
var jumpTo: String? = null
|
||||
private val listener: FavouriteStatusRouter.Listener
|
||||
@@ -65,10 +57,6 @@ abstract class GalleryInfoContentHelper : ContentHelper<GalleryInfo?>() {
|
||||
override fun onAddData(data: List<GalleryInfo?>) {
|
||||
for (info in data) {
|
||||
info?.let {
|
||||
if (maxGid == -1) maxGid = info.gid.toInt()
|
||||
if (minGid == -1) minGid = info.gid.toInt()
|
||||
maxGid = max(maxGid.toLong(), info.gid).toInt()
|
||||
minGid = min(minGid.toLong(), info.gid).toInt()
|
||||
map[info.gid] = info
|
||||
}
|
||||
}
|
||||
@@ -86,14 +74,6 @@ abstract class GalleryInfoContentHelper : ContentHelper<GalleryInfo?>() {
|
||||
|
||||
override fun onClearData() {
|
||||
map.clear()
|
||||
maxGid = -1
|
||||
minGid = -1
|
||||
}
|
||||
|
||||
override fun beforeRefresh() {
|
||||
super.beforeRefresh()
|
||||
maxGid = -1
|
||||
minGid = -1
|
||||
}
|
||||
|
||||
override fun saveInstanceState(superState: Parcelable): Parcelable {
|
||||
@@ -103,8 +83,6 @@ abstract class GalleryInfoContentHelper : ContentHelper<GalleryInfo?>() {
|
||||
val router = EhApplication.favouriteStatusRouter
|
||||
val id = router.saveDataMap(map)
|
||||
bundle.putInt(KEY_DATA_MAP, id)
|
||||
bundle.putInt(KEY_MIN_GID, minGid)
|
||||
bundle.putInt(KEY_MAX_GID, maxGid)
|
||||
return bundle
|
||||
}
|
||||
|
||||
@@ -118,8 +96,6 @@ abstract class GalleryInfoContentHelper : ContentHelper<GalleryInfo?>() {
|
||||
this.map = map
|
||||
}
|
||||
}
|
||||
minGid = bundle.getInt(KEY_MIN_GID)
|
||||
maxGid = bundle.getInt(KEY_MAX_GID)
|
||||
return super.restoreInstanceState(state)
|
||||
}
|
||||
|
||||
@@ -127,19 +103,11 @@ abstract class GalleryInfoContentHelper : ContentHelper<GalleryInfo?>() {
|
||||
val formatter = DateTimeFormatter
|
||||
.ofPattern("yyyy-MM-dd", Locale.US).withZone(ZoneOffset.UTC)
|
||||
jumpTo = formatter.format(Instant.ofEpochMilli(time))
|
||||
doGoToPage(Int.MAX_VALUE / 2)
|
||||
}
|
||||
|
||||
fun goToPage(page: Int) {
|
||||
jumpTo = page.toString()
|
||||
doGoToPage(page)
|
||||
doRefresh()
|
||||
jumpTo = null
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val KEY_DATA_MAP = "data_map"
|
||||
private const val KEY_MAX_GID = "max_gid"
|
||||
private const val KEY_MIN_GID = "min_gid"
|
||||
const val KEY_UPPER_PAGE = "upper_page"
|
||||
const val KEY_LOWER_PAGE = "lower_page"
|
||||
}
|
||||
}
|
||||
@@ -192,6 +192,8 @@ public class ContentLayout extends FrameLayout {
|
||||
private static final String KEY_START_PAGE = "start_page";
|
||||
private static final String KEY_END_PAGE = "end_page";
|
||||
private static final String KEY_PAGES = "pages";
|
||||
private static final String KEY_PREV = "prev";
|
||||
private static final String KEY_NEXT = "next";
|
||||
/**
|
||||
* Generate task id
|
||||
*/
|
||||
@@ -227,17 +229,23 @@ public class ContentLayout extends FrameLayout {
|
||||
*/
|
||||
private int mPages;
|
||||
private int mNextPage;
|
||||
private String mPrev;
|
||||
private String mNext;
|
||||
private int mCurrentTaskId;
|
||||
private int mCurrentTaskType;
|
||||
private int mCurrentTaskPage;
|
||||
private final SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
if (mStartPage > 0) {
|
||||
if (mPrev != null) {
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_PRE_PAGE_KEEP_POS;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, mPrev, false);
|
||||
} else if (mStartPage > 0) {
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_PRE_PAGE_KEEP_POS;
|
||||
mCurrentTaskPage = mStartPage - 1;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true);
|
||||
} else {
|
||||
doRefresh();
|
||||
}
|
||||
@@ -248,28 +256,32 @@ public class ContentLayout extends FrameLayout {
|
||||
private final RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
|
||||
@Override
|
||||
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||||
if (!mRefreshLayout.isRefreshing() && !recyclerView.canScrollVertically(1) && mEndPage < mPages) {
|
||||
if (!mRefreshLayout.isRefreshing() && !recyclerView.canScrollVertically(1)) {
|
||||
// Get next page
|
||||
mBottomProgress.show();
|
||||
if (mEndPage < mPages) {
|
||||
if (mNext != null) {
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_NEXT_PAGE_KEEP_POS;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, mNext, true);
|
||||
} else if (mEndPage < mPages) {
|
||||
// Get next page
|
||||
// Fill pages before NextPage with empty list
|
||||
while (mNextPage > mEndPage && mEndPage < mPages) {
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_NEXT_PAGE_KEEP_POS;
|
||||
mCurrentTaskPage = mEndPage;
|
||||
onGetPageData(mCurrentTaskId, mPages, mNextPage, Collections.emptyList());
|
||||
onGetPageData(mCurrentTaskId, mPages, mNextPage, null, null, Collections.emptyList());
|
||||
}
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_NEXT_PAGE_KEEP_POS;
|
||||
mCurrentTaskPage = mEndPage;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true);
|
||||
} else if (mEndPage == mPages) {
|
||||
// Refresh last page
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_REFRESH_PAGE;
|
||||
mCurrentTaskPage = mEndPage - 1;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true);
|
||||
} else {
|
||||
Log.e(TAG, "Try to footer refresh, but mEndPage = " + mEndPage + ", mPages = " + mPages);
|
||||
mBottomProgress.hide();
|
||||
@@ -304,12 +316,14 @@ public class ContentLayout extends FrameLayout {
|
||||
}
|
||||
|
||||
/**
|
||||
* Call {@link #onGetPageData(int, int, int, List)} when get data
|
||||
* Call {@link #onGetPageData(int, int, int, String, String, List)} when get data
|
||||
*
|
||||
* @param taskId task id
|
||||
* @param page the page to get
|
||||
* @param index the index to get
|
||||
* @param isNext the index is next or prev
|
||||
*/
|
||||
protected abstract void getPageData(int taskId, int type, int page);
|
||||
protected abstract void getPageData(int taskId, int type, int page, String index, boolean isNext);
|
||||
|
||||
protected abstract Context getContext();
|
||||
|
||||
@@ -398,7 +412,7 @@ public class ContentLayout extends FrameLayout {
|
||||
protected void onClearData() {
|
||||
}
|
||||
|
||||
public void onGetPageData(int taskId, int pages, int nextPage, List<E> data) {
|
||||
public void onGetPageData(int taskId, int pages, int nextPage, String prev, String next, List<E> data) {
|
||||
if (mCurrentTaskId == taskId) {
|
||||
int dataSize;
|
||||
|
||||
@@ -408,6 +422,8 @@ public class ContentLayout extends FrameLayout {
|
||||
mEndPage = 1;
|
||||
mPages = pages;
|
||||
mNextPage = nextPage;
|
||||
mPrev = prev;
|
||||
mNext = next;
|
||||
mPageDivider.clear();
|
||||
mPageDivider.add(data.size());
|
||||
if (data.isEmpty()) {
|
||||
@@ -449,6 +465,7 @@ public class ContentLayout extends FrameLayout {
|
||||
mPageDivider.add(0, dataSize);
|
||||
mStartPage--;
|
||||
mPages = Math.max(mEndPage, pages);
|
||||
mPrev = prev;
|
||||
// assert mStartPage >= 0
|
||||
|
||||
if (data.isEmpty()) {
|
||||
@@ -504,6 +521,7 @@ public class ContentLayout extends FrameLayout {
|
||||
mEndPage++;
|
||||
mNextPage = nextPage;
|
||||
mPages = Math.max(mEndPage, pages);
|
||||
mNext = next;
|
||||
if (data.isEmpty()) {
|
||||
// OK, that's all
|
||||
if (mData.isEmpty()) {
|
||||
@@ -551,6 +569,8 @@ public class ContentLayout extends FrameLayout {
|
||||
mEndPage = mCurrentTaskPage + 1;
|
||||
mNextPage = nextPage;
|
||||
mPages = pages;
|
||||
mPrev = prev;
|
||||
mNext = next;
|
||||
mPageDivider.clear();
|
||||
mPageDivider.add(data.size());
|
||||
if (data.isEmpty()) {
|
||||
@@ -684,7 +704,7 @@ public class ContentLayout extends FrameLayout {
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_REFRESH;
|
||||
mCurrentTaskPage = 0;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -763,7 +783,7 @@ public class ContentLayout extends FrameLayout {
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_PRE_PAGE;
|
||||
mCurrentTaskPage = page;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true);
|
||||
} else if (page == mEndPage) {
|
||||
mRefreshLayout.setRefreshing(false);
|
||||
mBottomProgress.hide();
|
||||
@@ -771,7 +791,7 @@ public class ContentLayout extends FrameLayout {
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_NEXT_PAGE;
|
||||
mCurrentTaskPage = page;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true);
|
||||
} else {
|
||||
mRefreshLayout.setRefreshing(false);
|
||||
mBottomProgress.hide();
|
||||
@@ -779,17 +799,17 @@ public class ContentLayout extends FrameLayout {
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_SOMEWHERE;
|
||||
mCurrentTaskPage = page;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true);
|
||||
}
|
||||
}
|
||||
|
||||
protected void doGoToPage(int page) {
|
||||
public void goTo(String index, boolean isNext) {
|
||||
mRefreshLayout.setRefreshing(false);
|
||||
mBottomProgress.hide();
|
||||
mCurrentTaskId = mIdGenerator.nextId();
|
||||
mCurrentTaskType = TYPE_SOMEWHERE;
|
||||
mCurrentTaskPage = page;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage);
|
||||
mCurrentTaskPage = 0;
|
||||
getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, index, isNext);
|
||||
}
|
||||
|
||||
protected Parcelable saveInstanceState(Parcelable superState) {
|
||||
@@ -813,6 +833,8 @@ public class ContentLayout extends FrameLayout {
|
||||
bundle.putInt(KEY_START_PAGE, mStartPage);
|
||||
bundle.putInt(KEY_END_PAGE, mEndPage);
|
||||
bundle.putInt(KEY_PAGES, mPages);
|
||||
bundle.putString(KEY_PREV, mPrev);
|
||||
bundle.putString(KEY_NEXT, mNext);
|
||||
return bundle;
|
||||
}
|
||||
|
||||
@@ -837,6 +859,8 @@ public class ContentLayout extends FrameLayout {
|
||||
mStartPage = bundle.getInt(KEY_START_PAGE);
|
||||
mEndPage = bundle.getInt(KEY_END_PAGE);
|
||||
mPages = bundle.getInt(KEY_PAGES);
|
||||
mPrev = bundle.getString(KEY_PREV);
|
||||
mNext = bundle.getString(KEY_NEXT);
|
||||
|
||||
notifyDataSetChanged();
|
||||
|
||||
@@ -845,6 +869,8 @@ public class ContentLayout extends FrameLayout {
|
||||
mStartPage = 0;
|
||||
mEndPage = 0;
|
||||
mPages = 0;
|
||||
mPrev = null;
|
||||
mNext = null;
|
||||
firstRefresh();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user