From 11b10a13efba296de76da70224e820a45294d468 Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 19 Feb 2018 16:39:35 +0100 Subject: [PATCH] Parse comments --- .../client/data/comments/CommentsHelper.java | 74 +++++++++++++++++++ .../comunic/client/data/posts/Post.java | 25 ++++++- .../client/data/posts/PostsHelper.java | 2 + 3 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/comments/CommentsHelper.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/comments/CommentsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/comments/CommentsHelper.java new file mode 100644 index 0000000..95998ad --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/comments/CommentsHelper.java @@ -0,0 +1,74 @@ +package org.communiquons.android.comunic.client.data.comments; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +/** + * Comments helper + * + * @author Pierre HUBERT + * Created by pierre on 2/19/18. + */ + +public class CommentsHelper { + + /** + * Parse a json array that contains comment and return the list of comments as an object + * + * @param array The JSON array to parse + * @return The generated list of comments objects / null if comments are disabled for the post + * @throws JSONException In case of failure while decoding the list + */ + @Nullable + public static ArrayList parse_json_array(@Nullable JSONArray array) throws JSONException + { + //Check if the comments are disabled on the post + if(array == null) + return null; + + ArrayList list = new ArrayList<>(); + + //Process each element + for (int i = 0; i < array.length(); i++){ + + //Parse JSON object + list.add(parse_json_comment(array.getJSONObject(i))); + + } + + return list; + } + + /** + * Parse a JSON Object into a comment object + * + * @param json The JSON Object to parse + * @return Generated comment object + * @throws JSONException if the object is invalid + */ + @NonNull + private static Comment parse_json_comment(@NonNull JSONObject json) throws JSONException{ + + Comment comment = new Comment(); + + //Parse comment object + comment.setId(json.getInt("ID")); + comment.setUserID(json.getInt("userID")); + comment.setPostID(json.getInt("postID")); + comment.setTime_sent(json.getInt("time_sent")); + comment.setContent(json.getString("content")); + comment.setImage_path(json.getString("img_path")); + comment.setImage_url(json.getString("img_url")); + comment.setLikes(json.getInt("likes")); + comment.setUser_like(json.getBoolean("userlike")); + + return comment; + + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java index 040e3ce..b3bcb8a 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/Post.java @@ -1,5 +1,11 @@ package org.communiquons.android.comunic.client.data.posts; +import android.support.annotation.Nullable; + +import org.communiquons.android.comunic.client.data.comments.Comment; + +import java.util.ArrayList; + /** * Post model * @@ -18,6 +24,7 @@ public class Post { private String content; private PostTypes type; private PostVisibilityLevels visibilityLevel; + private ArrayList comments_list; //Files specific private String file_path_url; @@ -44,7 +51,7 @@ public class Post { //Set and get the post creation time - public void setPost_time(int post_time) { + void setPost_time(int post_time) { this.post_time = post_time; } @@ -63,7 +70,7 @@ public class Post { } //Set and get the type of the post - public void setType(PostTypes type) { + void setType(PostTypes type) { this.type = type; } @@ -73,7 +80,7 @@ public class Post { //Set and get post visibility level - public void setVisibilityLevel(PostVisibilityLevels visibilityLevel) { + void setVisibilityLevel(PostVisibilityLevels visibilityLevel) { this.visibilityLevel = visibilityLevel; } @@ -81,8 +88,18 @@ public class Post { return visibilityLevel; } + //Set and get comments list + void setComments_list(ArrayList comments_list) { + this.comments_list = comments_list; + } + + @Nullable + public ArrayList getComments_list() { + return comments_list; + } + //Set and get file path url - public void setFile_path_url(String file_path_url) { + void setFile_path_url(String file_path_url) { this.file_path_url = file_path_url; } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java index a1d7872..3274cdc 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/posts/PostsHelper.java @@ -6,6 +6,7 @@ import android.support.annotation.Nullable; import org.communiquons.android.comunic.client.api.APIRequest; import org.communiquons.android.comunic.client.api.APIRequestParameters; import org.communiquons.android.comunic.client.api.APIResponse; +import org.communiquons.android.comunic.client.data.comments.CommentsHelper; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -85,6 +86,7 @@ public class PostsHelper { post.setUserID(json.getInt("userID")); post.setPost_time(json.getInt("post_time")); post.setContent(json.getString("content")); + post.setComments_list(CommentsHelper.parse_json_array(json.getJSONArray("comments"))); //Determine the visibility level of the post switch (json.getString("visibility_level")){