Skip to content

Commit

Permalink
Fix photo whose page > 40 can't be loaded #3
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Mar 2, 2014
1 parent d0a050a commit 7fe592c
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions ehreader/src/tw/skyarrow/ehreader/api/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ public List<Photo> getPhotoList(Gallery gallery, int page) throws ApiCallExcepti
Matcher matcher = pPhotoUrl.matcher(content);

while (matcher.find()) {
Photo photo = getPhotoInDb(galleryId, page);
String token = matcher.group(2);
int photoPage = Integer.parseInt(matcher.group(4));
Photo photo = getPhotoInDb(galleryId, photoPage);

L.d("Photo found: {galleryId: %d, token: %s, page: %d}", galleryId, token, photoPage);

Expand Down Expand Up @@ -249,10 +249,8 @@ public Photo getPhotoInfo(Gallery gallery, int page) throws ApiCallException {
}

int galleryPage = page / Constant.PHOTO_PER_PAGE;

getPhotoList(gallery, galleryPage);

photo = getPhotoInDb(gallery, page);
List<Photo> list = getPhotoList(gallery, galleryPage);
photo = list.get((page - 1) % Constant.PHOTO_PER_PAGE);

if (photo != null) {
return getPhotoInfo(gallery, photo);
Expand All @@ -267,6 +265,7 @@ private Photo getPhotoInDb(long galleryId, int page) {
PhotoDao.Properties.GalleryId.eq(galleryId),
PhotoDao.Properties.Page.eq(page)
));
qb.orderDesc(PhotoDao.Properties.Id);

if (qb.count() > 0) {
return qb.list().get(0);
Expand Down Expand Up @@ -363,9 +362,17 @@ public String getShowkey(Gallery gallery) throws ApiCallException {
L.d("Get show key callback: %s", content);

if (content.equals("Invalid page.")) {
// TODO retry getShowKey again
getPhotoList(galleryId, photo.getPage() / Constant.PHOTO_PER_PAGE);
throw new ApiCallException(ApiErrorCode.SHOWKEY_EXPIRED, url, response);
list = getPhotoList(galleryId, photo.getPage() / Constant.PHOTO_PER_PAGE);
photo = list.get(0);
httpGet = new HttpGet(photo.getUrl(isLoggedIn));
response = getHttpResponse(httpGet);
content = HttpRequestHelper.readResponse(response);

L.d("Get show key callback (retry): %s", content);

if (content.equals("Invalid page.")) {
throw new ApiCallException(ApiErrorCode.SHOWKEY_EXPIRED, url, response);
}
}

Matcher matcher = pShowkey.matcher(content);
Expand Down

0 comments on commit 7fe592c

Please sign in to comment.