Skip to content

Commit

Permalink
Refactored getBookDetails()
Browse files Browse the repository at this point in the history
  • Loading branch information
knjk04 committed Aug 16, 2018
1 parent 540fea5 commit 4de91fb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 53 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/com/presentedbykaran/bookshelf/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ public void setDescription(String description) {
else this.description = description;
}

public String getSelfLink() {
return selfLink;
}

public void setSelfLink(String selfLink) {
this.selfLink = selfLink;
}

// This method converts a http link to a https link
// This is needed in order to use Fresco to set the image. It won't work with a regular http link

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ private Book[] getBookDetails(String jsonData) throws JSONException {

Book book = new Book(this);

if (volumeInfo.has("title"))
book.setBookTitle(volumeInfo.getString("title"));
else {
// Do not set a generic title. There should really be a title at the very least
Log.d(TAG, "No title for index " + i);
}
book.setBookTitle(getJSONString(volumeInfo, "title"));
// if (volumeInfo.has("title"))
// book.setBookTitle(volumeInfo.getString("title"));
// else {
// // Do not set a generic title. There should really be a title at the very least
// Log.d(TAG, "No title for index " + i);
// }

if (volumeInfo.has("authors")) {
JSONArray jsonAuthors = volumeInfo.getJSONArray("authors");
Expand All @@ -172,11 +173,9 @@ private Book[] getBookDetails(String jsonData) throws JSONException {
} else {
// if there isn't an authors property, let the Book class handle the logic
book.setAuthors(Arrays.asList(""));
// book.setAuthors(Arrays.asList("No authors found"));
Log.d(TAG, "No authors");
}

// Ratings don't work for some reason. It can't find averageRating, so exception thrown
if (volumeInfo.has("averageRating")) {
double rating = volumeInfo.getDouble("averageRating");
book.setRating(rating);
Expand All @@ -185,51 +184,57 @@ private Book[] getBookDetails(String jsonData) throws JSONException {
book.setRating(0);
}

if (volumeInfo.has("ratingsCount")) {
int ratingsCount = volumeInfo.getInt("ratingsCount");
book.setRatingsCount(ratingsCount);
} else {
Log.d(TAG,"No ratingsCount for index " + i);
book.setRatingsCount(0);
}

if (volumeInfo.has("publisher")) {
String publisher = volumeInfo.getString("publisher");
book.setPublisher(publisher);
} else {
Log.d(TAG,"No publisher for index " + i);
// if there isn't a publisher property, let the Book class handle the logic
book.setPublisher("");
}

if (volumeInfo.has("publishedDate")) {
String publishedDate = volumeInfo.getString("publishedDate");
book.setPublishedDate(publishedDate);
} else {
Log.d(TAG,"No published date for index " + i);
// if there isn't a published date property, let the Book class handle the logic
book.setPublishedDate("");
}

if (volumeInfo.has("pageCount")) {
int pageCount = volumeInfo.getInt("pageCount");
book.setPageCount(pageCount);
} else {
Log.d(TAG,"No publisher for index " + i);
// if there isn't a page count property, set the page count to 0 and let the Book
// class handle what to set the data as
book.setPageCount(0);
}

if (volumeInfo.has("description")) {
String description = volumeInfo.getString("description");
book.setDescription(description);
} else {
Log.d(TAG,"No description for index " + i);
// if there isn't a description property, let the Book class handle the logic
book.setDescription("");
}

book.setRatingsCount(getJSONInt(volumeInfo, "ratingsCount"));
// if (volumeInfo.has("ratingsCount")) {
// int ratingsCount = volumeInfo.getInt("ratingsCount");
// book.setRatingsCount(ratingsCount);
// } else {
// Log.d(TAG,"No ratingsCount for index " + i);
// book.setRatingsCount(0);
// }

book.setPublisher(getJSONString(volumeInfo, "publisher"));
// if (volumeInfo.has("publisher")) {
// String publisher = volumeInfo.getString("publisher");
// book.setPublisher(publisher);
// } else {
// Log.d(TAG,"No publisher for index " + i);
// // if there isn't a publisher property, let the Book class handle the logic
// book.setPublisher("");
// }

book.setPublishedDate(getJSONString(volumeInfo, "publishedDate"));
// if (volumeInfo.has("publishedDate")) {
// String publishedDate = volumeInfo.getString("publishedDate");
// book.setPublishedDate(publishedDate);
// } else {
// Log.d(TAG,"No published date for index " + i);
// // if there isn't a published date property, let the Book class handle the logic
// book.setPublishedDate("");
// }

book.setPageCount(getJSONInt(volumeInfo, "pageCount"));
// if (volumeInfo.has("pageCount")) {
// int pageCount = volumeInfo.getInt("pageCount");
// book.setPageCount(pageCount);
// } else {
// Log.d(TAG,"No publisher for index " + i);
// // if there isn't a page count property, set the page count to 0 and let the Book
// // class handle what to set the data as
// book.setPageCount(0);
// }

book.setDescription(getJSONString(volumeInfo, "description"));
// if (volumeInfo.has("description")) {
// String description = volumeInfo.getString("description");
// book.setDescription(description);
// } else {
// Log.d(TAG,"No description for index " + i);
// // if there isn't a description property, let the Book class handle the logic
// book.setDescription("");
// }

// Do not require an else clause for this since there is a placeholder
if (volumeInfo.has("imageLinks")) {
JSONObject imageLinks = volumeInfo.getJSONObject("imageLinks");
String strSmallThumbnailURL = imageLinks.getString("smallThumbnail");
Expand All @@ -241,6 +246,14 @@ private Book[] getBookDetails(String jsonData) throws JSONException {
return books;
}

private String getJSONString(JSONObject jsonObject, String property) throws JSONException {
return (jsonObject.has(property)) ? jsonObject.getString(property) : "";
}

private int getJSONInt(JSONObject jsonObject, String property) throws JSONException {
return (jsonObject.has(property)) ? jsonObject.getInt(property) : 0;
}

private void noInternetConnectionSnackbar() {
Snackbar snackbar = Snackbar.make(findViewById(R.id.searchableLayout),
R.string.no_connection, Snackbar.LENGTH_LONG);
Expand Down

0 comments on commit 4de91fb

Please sign in to comment.