From f90bd4d1cce5f5ca523e4cae395a652a67811615 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 23 Aug 2018 17:27:16 +0800 Subject: Change the logic of parsing the subreddit icon. Click the icon and the banner of the subreddit to view the image in ViewImageActivity. --- .../infinityforreddit/JSONUtils.java | 1 + .../infinityforreddit/ParseSubredditData.java | 20 ++++++--- .../infinityforreddit/ParseSubscribedThing.java | 18 ++++++-- .../infinityforreddit/PostRecyclerViewAdapter.java | 4 +- .../infinityforreddit/ViewImageActivity.java | 6 +-- .../infinityforreddit/ViewPostDetailActivity.java | 4 +- .../ViewSubredditDetailActivity.java | 50 +++++++++++++++++++--- 7 files changed, 80 insertions(+), 23 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index d798f8dc..78b2255e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -53,6 +53,7 @@ class JSONUtils { static final String SCORE_HIDDEN_KEY = "score_hidden"; static final String SUBREDDIT_KEY = "subreddit"; static final String BANNER_IMG_KEY = "banner_img"; + static final String BANNER_BACKGROUND_IMAGE_KEY = "banner_background_image"; static final String ICON_IMG_KEY = "icon_img"; static final String COMMUNITY_ICON_KEY = "community_icon"; static final String LINK_KARMA_KEY = "link_karma"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java index 47fa851e..9a2fe016 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java @@ -44,17 +44,23 @@ class ParseSubredditData { String id = data.getString(JSONUtils.NAME_KEY); String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_PREFIXED); String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION).trim(); - String bannerImageUrl = data.getString(JSONUtils.BANNER_IMG_KEY); - String iconImageUrl = data.getString(JSONUtils.ICON_IMG_KEY); - if(iconImageUrl.equals("") || iconImageUrl.equals("null")) { - iconImageUrl = data.getString(JSONUtils.COMMUNITY_ICON_KEY); - if(iconImageUrl.equals("null")) { - iconImageUrl = ""; + String bannerImageUrl = data.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY); + if(bannerImageUrl.equals("") || bannerImageUrl.equals("null")) { + bannerImageUrl= data.getString(JSONUtils.BANNER_IMG_KEY); + if(bannerImageUrl.equals("null")) { + bannerImageUrl = ""; + } + } + String iconUrl = data.getString(JSONUtils.COMMUNITY_ICON_KEY); + if(iconUrl.equals("") || iconUrl.equals("null")) { + iconUrl = data.getString(JSONUtils.ICON_IMG_KEY); + if(iconUrl.equals("null")) { + iconUrl = ""; } } int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); int nCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT); - subredditData = new SubredditData(id, subredditFullName, iconImageUrl, bannerImageUrl, description, nSubscribers); + subredditData = new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, nSubscribers); mNCurrentOnlineSubscribers = nCurrentOnlineSubscribers; } catch (JSONException e) { parseFailed = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java index 63b82702..5d0c4606 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java @@ -65,8 +65,20 @@ class ParseSubscribedThing { for(int i = 0; i < children.length(); i++) { JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); String name = data.getString(JSONUtils.DISPLAY_NAME); - String bannerUrl = data.getString(JSONUtils.BANNER_IMG_KEY); - String iconUrl = data.getString(JSONUtils.ICON_IMG_KEY); + String bannerImageUrl = data.getString(JSONUtils.BANNER_IMG_KEY); + if(bannerImageUrl.equals("") || bannerImageUrl.equals("null")) { + bannerImageUrl= data.getString(JSONUtils.BANNER_IMG_KEY); + if(bannerImageUrl.equals("null")) { + bannerImageUrl = ""; + } + } + String iconUrl = data.getString(JSONUtils.COMMUNITY_ICON_KEY); + if(iconUrl.equals("") || iconUrl.equals("null")) { + iconUrl = data.getString(JSONUtils.ICON_IMG_KEY); + if(iconUrl.equals("null")) { + iconUrl = ""; + } + } String id = data.getString(JSONUtils.NAME_KEY); if(iconUrl.equals("") || iconUrl.equals("null")) { iconUrl = data.getString(JSONUtils.COMMUNITY_ICON_KEY); @@ -84,7 +96,7 @@ class ParseSubscribedThing { String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION).trim(); int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl)); - newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl, bannerUrl, description, nSubscribers)); + newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, nSubscribers)); } } lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index b7a918cd..933af11d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -170,8 +170,8 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { @Override - public void onChanged(@Nullable SubredditData subredditData) { + public void onChanged(@Nullable final SubredditData subredditData) { if(subredditData != null) { - if(!subredditData.getBannerUrl().equals("") && !subredditData.getBannerUrl().equals("null")) { + if(subredditData.getBannerUrl().equals("")) { + iconCircleImageView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + //Do nothing as it has no image + } + }); + } else { glide.load(subredditData.getBannerUrl()).into(bannerImageView); + bannerImageView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageActivity.class); + intent.putExtra(ViewImageActivity.TITLE_KEY, title); + intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, subredditData.getBannerUrl()); + intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + "-banner"); + startActivity(intent); + } + }); } - if(subredditData.getIconUrl().equals("") || subredditData.getIconUrl().equals("null")) { + if(subredditData.getIconUrl().equals("")) { glide.load(getDrawable(R.drawable.subreddit_default_icon)).into(iconCircleImageView); + iconCircleImageView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + //Do nothing as it is a default icon + } + }); } else { glide.load(subredditData.getIconUrl()).into(iconCircleImageView); + iconCircleImageView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewImageActivity.class); + intent.putExtra(ViewImageActivity.TITLE_KEY, title); + intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, subredditData.getIconUrl()); + intent.putExtra(ViewImageActivity.FILE_NAME_KEY, subredditName + "-icon"); + startActivity(intent); + } + }); } subredditNameTextView.setText(subredditData.getName()); String nSubscribers = getString(R.string.subscribers_number_detail, subredditData.getNSubscribers()); nSubscribersTextView.setText(nSubscribers); - descriptionTextView.setText(subredditData.getDescription()); + if(subredditData.getDescription().equals("")) { + descriptionTextView.setVisibility(View.GONE); + } else { + descriptionTextView.setVisibility(View.VISIBLE); + descriptionTextView.setText(subredditData.getDescription()); + } } } }); -- cgit v1.2.3