From eb973138f72b181f79d84bbf16c06f92760118e0 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 11 Aug 2018 17:49:30 +0800 Subject: Fixed a bug which causes the app to crash when there is no comment in a post. Add a no comment placeholder which is displayed when there is no comment in a post. --- .../ml/docilealligator/infinityforreddit/FetchComment.java | 9 ++++++++- .../ml/docilealligator/infinityforreddit/ParseComment.java | 6 +++++- .../infinityforreddit/ViewPostDetailActivity.java | 11 +++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) (limited to 'app/src/main/java/ml/docilealligator') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index 5d89497f..3721947e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -1,5 +1,7 @@ package ml.docilealligator.infinityforreddit; +import android.net.Uri; + import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; @@ -25,7 +27,12 @@ class FetchComment { void queryComment(FetchCommentListener fetchCommentListener) { mFetchCommentListener = fetchCommentListener; - StringRequest commentRequest = new StringRequest(Request.Method.GET, RedditUtils.getQueryCommentUri(subredditName, article), new Response.Listener() { + + Uri uri = Uri.parse(RedditUtils.getQueryCommentUri(subredditName, article)) + .buildUpon().appendQueryParameter(RedditUtils.RAW_JSON_KEY, RedditUtils.RAW_JSON_VALUE) + .build(); + + StringRequest commentRequest = new StringRequest(Request.Method.GET, uri.toString(), new Response.Listener() { @Override public void onResponse(String response) { mFetchCommentListener.onFetchCommentSuccess(response); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index dc40018c..58b435f1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -53,8 +53,13 @@ class ParseComment { int actualCommentLength; JSONArray allComments = jsonResponse.getJSONObject(1).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + if(allComments.length() == 0) { + return null; + } + JSONObject more = allComments.getJSONObject(allComments.length() - 1).getJSONObject(JSONUtils.DATA_KEY); + //Maybe children contain only comments and no more info if(more.has(JSONUtils.COUNT_KEY)) { moreCommentCount = more.getInt(JSONUtils.COUNT_KEY); actualCommentLength = allComments.length() - 1; @@ -87,7 +92,6 @@ class ParseComment { } catch (JSONException e) { parseFailed = true; Log.i("parse comment error", e.getMessage()); - mParseCommentListener.onParseCommentFail(); } return null; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 3245ddf9..36bed4e1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -16,6 +16,7 @@ import android.support.v7.widget.RecyclerView; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.TextView; @@ -48,6 +49,9 @@ public class ViewPostDetailActivity extends AppCompatActivity { private CardView mCommentCardView; private RecyclerView mRecyclerView; + private LinearLayout mNoCommentWrapperLinearLayout; + private ImageView mNoCommentImageView; + private RequestQueue mVoteThingQueue; private RequestQueue mCommentQueue; @@ -86,6 +90,9 @@ public class ViewPostDetailActivity extends AppCompatActivity { mCommentCardView = findViewById(R.id.comment_card_view_view_post_detail); mRecyclerView = findViewById(R.id.recycler_view_view_post_detail); + mNoCommentWrapperLinearLayout = findViewById(R.id.no_comment_wrapper_linear_layout_view_post_detail); + mNoCommentImageView = findViewById(R.id.no_comment_image_view_view_post_detail); + if(mPostData.getSubredditIconUrl() == null) { new LoadSubredditIconAsyncTask(this, subredditIconCircleImageView, SubredditRoomDatabase.getDatabase(this).subredditDao(), mPostData.getSubredditName(), @@ -279,6 +286,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { private void queryComment() { mCommentProgressbar.setVisibility(View.VISIBLE); + mNoCommentWrapperLinearLayout.setVisibility(View.GONE); new FetchComment(mCommentQueue, mPostData.getSubredditName(), mPostData.getId()).queryComment(new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(String response) { @@ -291,6 +299,9 @@ public class ViewPostDetailActivity extends AppCompatActivity { CommentRecyclerViewAdapter adapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, commentData, mVoteThingQueue); mRecyclerView.setAdapter(adapter); mCommentCardView.setVisibility(View.VISIBLE); + } else { + mNoCommentWrapperLinearLayout.setVisibility(View.VISIBLE); + Glide.with(ViewPostDetailActivity.this).load(R.drawable.no_comment_indicator).into(mNoCommentImageView); } } -- cgit v1.2.3