mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-11-04 11:34:06 +00:00 
			
		
		
		
	Parse comments
This commit is contained in:
		@@ -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<Comment> parse_json_array(@Nullable JSONArray array) throws JSONException
 | 
			
		||||
    {
 | 
			
		||||
        //Check if the comments are disabled on the post
 | 
			
		||||
        if(array == null)
 | 
			
		||||
            return null;
 | 
			
		||||
 | 
			
		||||
        ArrayList<Comment> 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;
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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<Comment> 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<Comment> comments_list) {
 | 
			
		||||
        this.comments_list = comments_list;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    public ArrayList<Comment> 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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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")){
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user