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 1, 2014
1 parent a488e99 commit 786f209
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 48 deletions.
2 changes: 1 addition & 1 deletion android-stackblur
1 change: 1 addition & 0 deletions ehreader/src/tw/skyarrow/ehreader/api/ApiErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public class ApiErrorCode {
public static final int TOKEN_NOT_FOUND = 10;
public static final int TOKEN_OR_PAGE_INVALID = 11;
public static final int TOKEN_INVALID = 12;
public static final int SHOWKEY_EXPIRED = 13;
}
92 changes: 57 additions & 35 deletions ehreader/src/tw/skyarrow/ehreader/api/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import tw.skyarrow.ehreader.R;
import tw.skyarrow.ehreader.db.DaoMaster;
import tw.skyarrow.ehreader.db.DaoSession;
import tw.skyarrow.ehreader.db.DownloadDao;
import tw.skyarrow.ehreader.db.Gallery;
import tw.skyarrow.ehreader.db.GalleryDao;
import tw.skyarrow.ehreader.db.Photo;
Expand All @@ -52,7 +51,6 @@ public class DataLoader {
private SQLiteDatabase db;
private GalleryDao galleryDao;
private PhotoDao photoDao;
private DownloadDao downloadDao;
private HttpContext httpContext;
private boolean isLoggedIn;

Expand Down Expand Up @@ -88,7 +86,6 @@ private void setupDatabase() {
DaoSession daoSession = daoMaster.newSession();
galleryDao = daoSession.getGalleryDao();
photoDao = daoSession.getPhotoDao();
downloadDao = daoSession.getDownloadDao();
}

private void setupHttpContext() {
Expand Down Expand Up @@ -137,6 +134,8 @@ private HttpResponse getHttpResponse(HttpRequestBase httpRequest) throws IOExcep
}

public JSONObject callApi(JSONObject json) throws ApiCallException {
String responseStr = "";

try {
String url = isLoggedIn ? Constant.API_URL_EX : Constant.API_URL;
HttpPost httpPost = new HttpPost(url);
Expand All @@ -146,7 +145,11 @@ public JSONObject callApi(JSONObject json) throws ApiCallException {
httpPost.setEntity(new StringEntity(json.toString()));

HttpResponse response = getHttpResponse(httpPost);
JSONObject result = new JSONObject(HttpRequestHelper.readResponse(response));
responseStr = HttpRequestHelper.readResponse(response);

L.d("Api callback: %s", responseStr);

JSONObject result = new JSONObject(responseStr);

if (result.has("error")) {
String error = result.getString("error");
Expand All @@ -164,6 +167,10 @@ public JSONObject callApi(JSONObject json) throws ApiCallException {
} catch (IOException e) {
throw new ApiCallException(ApiErrorCode.IO_ERROR, e);
} catch (JSONException e) {
if (responseStr != null && !responseStr.isEmpty()) {
L.e("Api call error: %s", responseStr);
}

throw new ApiCallException(ApiErrorCode.JSON_ERROR, e);
}
}
Expand Down Expand Up @@ -203,36 +210,29 @@ public List<Photo> getPhotoList(Gallery gallery, int page) throws ApiCallExcepti
Matcher matcher = pPhotoUrl.matcher(content);

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

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

photo.setGalleryId(galleryId);
photo.setToken(token);
photo.setPage(photoPage);
list.add(photo);

QueryBuilder qb = photoDao.queryBuilder();
qb.where(qb.and(
PhotoDao.Properties.GalleryId.eq(galleryId),
PhotoDao.Properties.Page.eq(page)
));
qb.limit(1);

if (qb.count() > 0) {
Photo qPhoto = (Photo) qb.list().get(0);

qPhoto.setToken(token);
if (photo != null) {
photo.setToken(token);
photoDao.updateInTx(photo);
} else {
photo = new Photo();

photo.setGalleryId(galleryId);
photo.setToken(token);
photo.setPage(photoPage);
photo.setDownloaded(false);
photo.setBookmarked(false);
photo.setInvalid(false);

photoDao.insertInTx(photo);
}

list.add(photo);
}

return list;
Expand All @@ -242,31 +242,43 @@ public List<Photo> getPhotoList(Gallery gallery, int page) throws ApiCallExcepti
}

public Photo getPhotoInfo(Gallery gallery, int page) throws ApiCallException {
QueryBuilder qb = photoDao.queryBuilder();
qb.where(qb.and(
PhotoDao.Properties.GalleryId.eq(gallery.getId()),
PhotoDao.Properties.Page.eq(page)
));
qb.limit(1);
List<Photo> list = qb.list();
Photo photo = getPhotoInDb(gallery, page);

if (list.size() > 0) {
return getPhotoInfo(gallery, list.get(0));
if (photo != null) {
return getPhotoInfo(gallery, photo);
}

int galleryPage = page / Constant.PHOTO_PER_PAGE;

getPhotoList(gallery, galleryPage);

list = qb.list();
photo = getPhotoInDb(gallery, page);

if (list.size() > 0) {
return getPhotoInfo(gallery, list.get(0));
if (photo != null) {
return getPhotoInfo(gallery, photo);
} else {
throw new ApiCallException(ApiErrorCode.PHOTO_NOT_EXIST);
}
}

private Photo getPhotoInDb(long galleryId, int page) {
QueryBuilder<Photo> qb = photoDao.queryBuilder();
qb.where(qb.and(
PhotoDao.Properties.GalleryId.eq(galleryId),
PhotoDao.Properties.Page.eq(page)
));

if (qb.count() > 0) {
return qb.list().get(0);
} else {
return null;
}
}

private Photo getPhotoInDb(Gallery gallery, int page) {
return getPhotoInDb(gallery.getId(), page);
}

public Photo getPhotoInfo(Gallery gallery, Photo photo) throws ApiCallException {
String src = photo.getSrc();

Expand Down Expand Up @@ -329,8 +341,9 @@ public JSONObject getPhotoRaw(Gallery gallery, Photo photo) throws ApiCallExcept
}

public String getShowkey(Gallery gallery) throws ApiCallException {
QueryBuilder qb = photoDao.queryBuilder();
qb.where(PhotoDao.Properties.GalleryId.eq(gallery.getId())).limit(1);
long galleryId = gallery.getId();
QueryBuilder<Photo> qb = photoDao.queryBuilder();
qb.where(PhotoDao.Properties.GalleryId.eq(galleryId)).limit(1);
List<Photo> list = qb.list();
Photo photo = list.get(0);

Expand All @@ -346,6 +359,15 @@ public String getShowkey(Gallery gallery) throws ApiCallException {
HttpGet httpGet = new HttpGet(url);
HttpResponse response = getHttpResponse(httpGet);
String content = HttpRequestHelper.readResponse(response);

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);
}

Matcher matcher = pShowkey.matcher(content);
String showkey = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ public void onDestroyView() {
private class GalleryDeleteTask extends AsyncTask<Integer, Integer, String> {
@Override
protected String doInBackground(Integer... integers) {
QueryBuilder qb = photoDao.queryBuilder();
QueryBuilder<Photo> qb = photoDao.queryBuilder();
qb.where(PhotoDao.Properties.GalleryId.eq(galleryId));
List<Photo> photos = qb.list();
File galleryFolder = gallery.getFolder();

for (Photo photo : photos) {
File file = photo.getFile();
Expand All @@ -104,6 +105,10 @@ protected String doInBackground(Integer... integers) {
publishProgress(1);
}

if (galleryFolder.exists()) {
galleryFolder.delete();
}

return null;
}

Expand All @@ -114,12 +119,6 @@ protected void onProgressUpdate(Integer... values) {

@Override
protected void onPostExecute(String s) {
File galleryFolder = gallery.getFolder();

if (galleryFolder.exists()) {
galleryFolder.delete();
}

downloadDao.delete(download);

EventBus.getDefault().post(new GalleryDeleteEvent(galleryId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tw.skyarrow.ehreader.R;
import tw.skyarrow.ehreader.db.DaoMaster;
import tw.skyarrow.ehreader.db.DaoSession;
import tw.skyarrow.ehreader.db.Gallery;
import tw.skyarrow.ehreader.db.GalleryDao;

/**
Expand All @@ -42,7 +43,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
DaoSession daoSession = daoMaster.newSession();
GalleryDao galleryDao = daoSession.getGalleryDao();

QueryBuilder qb = galleryDao.queryBuilder();
QueryBuilder<Gallery> qb = galleryDao.queryBuilder();
qb.where(GalleryDao.Properties.Lastread.isNotNull());
qb.orderDesc(GalleryDao.Properties.Lastread);
getList().addAll(qb.list());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import tw.skyarrow.ehreader.R;
import tw.skyarrow.ehreader.db.DaoMaster;
import tw.skyarrow.ehreader.db.DaoSession;
import tw.skyarrow.ehreader.db.Gallery;
import tw.skyarrow.ehreader.db.GalleryDao;

/**
Expand All @@ -42,7 +43,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
DaoSession daoSession = daoMaster.newSession();
GalleryDao galleryDao = daoSession.getGalleryDao();

QueryBuilder qb = galleryDao.queryBuilder();
QueryBuilder<Gallery> qb = galleryDao.queryBuilder();
qb.where(GalleryDao.Properties.Starred.eq(true));
qb.orderDesc(GalleryDao.Properties.Lastread);
getList().addAll(qb.list());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private Uri getPhotoUri() {
}

private void displayPhoto() {
QueryBuilder qb = photoDao.queryBuilder();
QueryBuilder<Photo> qb = photoDao.queryBuilder();
qb.where(qb.and(
PhotoDao.Properties.GalleryId.eq(galleryId),
PhotoDao.Properties.Page.eq(page)
Expand Down Expand Up @@ -366,8 +366,11 @@ void onRetryBtnClick() {
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);

photo.setInvalid(true);
photoDao.update(photo);
if (photo != null) {
photo.setInvalid(true);
photoDao.update(photo);
}

callService();

BaseApplication.getTracker().send(MapBuilder.createEvent(
Expand Down

0 comments on commit 786f209

Please sign in to comment.