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.

This commit is contained in:
Alex Ning 2018-08-11 17:49:30 +08:00
parent c0eaf2d3bb
commit eb973138f7
6 changed files with 49 additions and 2 deletions

View File

@ -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<String>() {
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<String>() {
@Override
public void onResponse(String response) {
mFetchCommentListener.onFetchCommentSuccess(response);

View File

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

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

View File

@ -250,6 +250,30 @@
</android.support.v7.widget.CardView>
<LinearLayout
android:id="@+id/no_comment_wrapper_linear_layout_view_post_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:layout_marginBottom="48dp"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:id="@+id/no_comment_image_view_view_post_detail"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"
android:text="@string/no_comments_yet"/>
</LinearLayout>
</LinearLayout>

View File

@ -12,6 +12,7 @@
<string name="load_comment_failed">Error loading comments</string>
<string name="retry">Retry</string>
<string name="comments">Comments</string>
<string name="no_comments_yet">No comments yet. Write a comment?</string>
<string name="nsfw">NSFW</string>
<string name="karma_info">Karma: %1$d</string>