From 032b491dff41c50fd9741b8110d2099800ce310b Mon Sep 17 00:00:00 2001 From: Tom Rosier Date: Sat, 18 Apr 2015 16:33:53 +0100 Subject: [PATCH] got viewing of voting done --- .../prototype/prototype/ActionEffect.java | 9 ++++ .../ArrayAdapters/CommentArrayAdapter.java | 21 ++++++-- .../RestClient/CommentRestClient.java | 31 +++++++++-- .../prototype/RestClient/VoteRestClient.java | 53 +++++++++++++++++-- .../prototype/activities/ViewMessage.java | 41 ++++++++++++-- .../prototype/prototype/entities/Comment.java | 10 +++- .../prototype/prototype/entities/Vote.java | 32 +++++++++++ .../main/res/layout/activity_view_message.xml | 8 +-- app/src/main/res/layout/comment_list_item.xml | 3 +- .../res/layout/fragement_rate_comment.xml | 4 +- 10 files changed, 189 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/ActionEffect.java create mode 100644 app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/entities/Vote.java diff --git a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/ActionEffect.java b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/ActionEffect.java new file mode 100644 index 0000000..f489cbc --- /dev/null +++ b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/ActionEffect.java @@ -0,0 +1,9 @@ +package uk.co.tomrosier.xetk.losesono.prototype.prototype; + +/** + * Created by xetk on 18/04/15. + */ +public enum ActionEffect { + positive, + negative; +} diff --git a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/ArrayAdapters/CommentArrayAdapter.java b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/ArrayAdapters/CommentArrayAdapter.java index 0475788..905ff18 100644 --- a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/ArrayAdapters/CommentArrayAdapter.java +++ b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/ArrayAdapters/CommentArrayAdapter.java @@ -14,6 +14,7 @@ import uk.co.tomrosier.xetk.losesono.prototype.prototype.R; import uk.co.tomrosier.xetk.losesono.prototype.prototype.entities.Comment; import uk.co.tomrosier.xetk.losesono.prototype.prototype.entities.User; +import uk.co.tomrosier.xetk.losesono.prototype.prototype.entities.Vote; /** * This is for the listview on the adding friends to a message page. @@ -40,9 +41,12 @@ public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.comment_list_item, parent, false); } - TextView cName = (TextView) convertView.findViewById(R.id.lblCommenterName); - TextView cComment = (TextView) convertView.findViewById(R.id.lblCommentorComment); - TextView cTime = (TextView) convertView.findViewById(R.id.LBLCommentTime); + TextView cName = (TextView) convertView.findViewById(R.id.lblCommenterName); + TextView cComment = (TextView) convertView.findViewById(R.id.lblCommentorComment); + TextView cTime = (TextView) convertView.findViewById(R.id.LBLCommentTime); + TextView cUpVotes = (TextView) convertView.findViewById(R.id.lblUpVotes); + TextView cDownVotes = (TextView) convertView.findViewById(R.id.lblDownVotes); + User user = comment.getUser(); @@ -52,13 +56,22 @@ public View getView(int position, View convertView, ViewGroup parent) { SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm dd/MM/yy"); - String dateStr = dateFormat.format(createdDate); cName.setText(name); cComment.setText(comment.getContent()); cTime.setText(dateStr); + Vote vote = comment.getVote(); + + String lblPos = String.valueOf(vote.getPositive()); + String lblNeg = String.valueOf(vote.getNegative()); + + cUpVotes.setText(lblPos); + cDownVotes.setText(lblNeg); + + + // Return the completed view to render on screen return convertView; } diff --git a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/RestClient/CommentRestClient.java b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/RestClient/CommentRestClient.java index 748ebd7..ad8b2ef 100644 --- a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/RestClient/CommentRestClient.java +++ b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/RestClient/CommentRestClient.java @@ -10,7 +10,9 @@ import org.json.JSONException; import org.json.JSONObject; +import uk.co.tomrosier.xetk.losesono.prototype.prototype.VoteType; import uk.co.tomrosier.xetk.losesono.prototype.prototype.entities.Comment; +import uk.co.tomrosier.xetk.losesono.prototype.prototype.entities.Vote; import uk.co.tomrosier.xetk.losesono.prototype.prototype.utils.AjaxCompleteHandler; /** @@ -20,8 +22,11 @@ public class CommentRestClient { private RestClient restClient; + private Context context; + public CommentRestClient(Context context) { restClient = new RestClient(context); + this.context = context; } public void getCommentsByID(Integer msgId, final AjaxCompleteHandler handler) { @@ -39,14 +44,30 @@ public void onSuccess(int statusCode, Header[] headers, JSONArray response) { handler.handleAction(null); } else { for (int i = 0; i < response.length(); i++) { - JSONObject commentObj = response.getJSONObject(i); - - Comment comment = new Comment(commentObj); - handler.handleAction(comment); - + final JSONObject commentObj = response.getJSONObject(i); + + VoteRestClient vrc = new VoteRestClient(context); + + vrc.getVoteByID( + commentObj.getInt("comment_id"), + VoteType.comment, + new AjaxCompleteHandler() { + @Override + public void handleAction(Object someData) { + Vote vote = (Vote) someData; + try { + Comment comment = new Comment(commentObj, vote); + handler.handleAction(comment); + } catch (JSONException e) { + e.printStackTrace(); + } + } + } + ); } } + } catch (JSONException e) { e.printStackTrace(); } diff --git a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/RestClient/VoteRestClient.java b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/RestClient/VoteRestClient.java index 0c0ecbe..7d61d4a 100644 --- a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/RestClient/VoteRestClient.java +++ b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/RestClient/VoteRestClient.java @@ -1,21 +1,68 @@ package uk.co.tomrosier.xetk.losesono.prototype.prototype.RestClient; +import android.content.Context; + +import com.loopj.android.http.JsonHttpResponseHandler; + +import org.apache.http.Header; +import org.json.JSONException; +import org.json.JSONObject; + import uk.co.tomrosier.xetk.losesono.prototype.prototype.VoteType; +import uk.co.tomrosier.xetk.losesono.prototype.prototype.entities.Vote; +import uk.co.tomrosier.xetk.losesono.prototype.prototype.utils.AjaxCompleteHandler; /** * Created by xetk on 05/03/15. */ public class VoteRestClient { - public VoteRestClient() {} + private RestClient restClient; + + public VoteRestClient(Context context) { + restClient = new RestClient(context); + } // TODO: - public static Object getVoteByID(Integer voteID, VoteType voteType) { - return null; + public void getVoteByID(Integer voteID, VoteType voteType, final AjaxCompleteHandler handler) { + + String url = "vote/" + voteType(voteType) + "/" + voteID; + + restClient.get( + url, + null, + new JsonHttpResponseHandler() { + @Override + public void onSuccess(int statusCode, Header[] headers, JSONObject response) { + if (statusCode == 200) { + try { + Vote vote = new Vote(response); + + handler.handleAction(vote); + } catch (JSONException e) { + e.printStackTrace(); + } + + } else { + System.err.println("Getting Comments failed with status code of " + statusCode); + } + } + } + ); } // TODO: public static Object addVote() { return null; } + + + private String voteType(VoteType vt) { + if (vt == VoteType.comment) { + return "comment"; + } else if (vt == VoteType.message) { + return "message"; + } + return null; + } } diff --git a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/activities/ViewMessage.java b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/activities/ViewMessage.java index 7bc7b39..acef4c0 100644 --- a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/activities/ViewMessage.java +++ b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/activities/ViewMessage.java @@ -10,7 +10,9 @@ import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.ImageButton; import android.widget.ListView; +import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.maps.CameraUpdateFactory; @@ -30,9 +32,12 @@ import uk.co.tomrosier.xetk.losesono.prototype.prototype.RestClient.CommentRestClient; import uk.co.tomrosier.xetk.losesono.prototype.prototype.RestClient.MessageRestClient; import uk.co.tomrosier.xetk.losesono.prototype.prototype.RestClient.UserRestClient; +import uk.co.tomrosier.xetk.losesono.prototype.prototype.RestClient.VoteRestClient; +import uk.co.tomrosier.xetk.losesono.prototype.prototype.VoteType; import uk.co.tomrosier.xetk.losesono.prototype.prototype.entities.Comment; import uk.co.tomrosier.xetk.losesono.prototype.prototype.entities.Message; import uk.co.tomrosier.xetk.losesono.prototype.prototype.entities.User; +import uk.co.tomrosier.xetk.losesono.prototype.prototype.entities.Vote; import uk.co.tomrosier.xetk.losesono.prototype.prototype.utils.AjaxCompleteHandler; import uk.co.tomrosier.xetk.losesono.prototype.prototype.utils.Login; @@ -192,6 +197,8 @@ public void handleAction(Object someData) { } ); + ImageButton upVote = (ImageButton) findViewById(R.id.btnVMDownVote); + ImageButton downVote = (ImageButton) findViewById(R.id.btnVMUpVote); } @@ -215,7 +222,7 @@ private void populateComments() { @Override public void handleAction(Object someData) { - final Comment comment = (Comment)someData; + final Comment comment = (Comment) someData; if (comment == null) { swipeLayout.setRefreshing(false); @@ -231,7 +238,7 @@ public void handleAction(Object someData) { new AjaxCompleteHandler() { @Override public void handleAction(Object someData) { - User user = (User)someData; + User user = (User) someData; Comment userComment = comment; @@ -244,7 +251,7 @@ public void handleAction(Object someData) { @Override public int compare(Comment arg1, Comment arg0) { - int diff = (int)(arg1.getCreatedDate().getTime() - arg0.getCreatedDate().getTime()); + int diff = (int) (arg1.getCreatedDate().getTime() - arg0.getCreatedDate().getTime()); return diff; } @@ -259,6 +266,34 @@ public int compare(Comment arg1, Comment arg0) { } } ); + refreshVotes(); + } + + private void refreshVotes(){ + + VoteRestClient vrc = new VoteRestClient(getApplicationContext()); + + vrc.getVoteByID( + message.getMessageID(), + VoteType.message, + new AjaxCompleteHandler() { + @Override + public void handleAction(Object someData) { + + Vote vote = (Vote) someData; + + TextView upVote = (TextView) findViewById(R.id.VMUpVote); + TextView downVote = (TextView) findViewById(R.id.VMDownVote); + + String lblPos = String.valueOf(vote.getPositive()); + String lblNeg = String.valueOf(vote.getNegative()); + + upVote.setText(lblPos); + downVote.setText(lblNeg); + + } + } + ); } // Setup the map with the things we need. diff --git a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/entities/Comment.java b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/entities/Comment.java index 1c01152..6b6da22 100644 --- a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/entities/Comment.java +++ b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/entities/Comment.java @@ -23,8 +23,10 @@ public class Comment { private Date createdDate; + private Vote vote; - public Comment(JSONObject obj) throws JSONException { + + public Comment(JSONObject obj, Vote vote) throws JSONException { this.commentID = obj.getInt("comment_id"); this.messageID = obj.getInt("message_id"); this.userID = obj.getInt("user_id"); @@ -38,6 +40,8 @@ public Comment(JSONObject obj) throws JSONException { } catch (ParseException e) { e.printStackTrace(); } + + this.vote = vote; } public Comment(int messageID, User user, String content) { @@ -74,4 +78,8 @@ public void setUser(User user) { public User getUser() { return user; } + + public Vote getVote() { + return vote; + } } diff --git a/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/entities/Vote.java b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/entities/Vote.java new file mode 100644 index 0000000..700b5e4 --- /dev/null +++ b/app/src/main/java/uk/co/tomrosier/xetk/losesono/prototype/prototype/entities/Vote.java @@ -0,0 +1,32 @@ +package uk.co.tomrosier.xetk.losesono.prototype.prototype.entities; + +import org.json.JSONException; +import org.json.JSONObject; + +/** + * Created by xetk on 18/04/15. + */ +public class Vote { + + private int positive; + private int negative; + + public Vote(JSONObject jsonObj) throws JSONException { + this.positive = jsonObj.getInt("positive"); + this.negative = jsonObj.getInt("negative"); + } + + public Vote(int positive, int negative) { + this.positive = positive; + this.negative = negative; + } + + + public int getPositive() { + return positive; + } + + public int getNegative() { + return negative; + } +} diff --git a/app/src/main/res/layout/activity_view_message.xml b/app/src/main/res/layout/activity_view_message.xml index d1c04f3..cf62839 100644 --- a/app/src/main/res/layout/activity_view_message.xml +++ b/app/src/main/res/layout/activity_view_message.xml @@ -52,14 +52,14 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" - android:id="@+id/textView" + android:id="@+id/VMUpVote" android:layout_row="4" android:layout_column="0" /> diff --git a/app/src/main/res/layout/comment_list_item.xml b/app/src/main/res/layout/comment_list_item.xml index 66c7554..ef41690 100644 --- a/app/src/main/res/layout/comment_list_item.xml +++ b/app/src/main/res/layout/comment_list_item.xml @@ -47,7 +47,8 @@ android:layout_height="wrap_content" layout="@layout/fragement_rate_comment" android:layout_row="2" - android:layout_column="6" /> + android:layout_column="6" + android:layout_gravity="right" /> \ No newline at end of file diff --git a/app/src/main/res/layout/fragement_rate_comment.xml b/app/src/main/res/layout/fragement_rate_comment.xml index 4e1423c..48956e7 100644 --- a/app/src/main/res/layout/fragement_rate_comment.xml +++ b/app/src/main/res/layout/fragement_rate_comment.xml @@ -10,7 +10,7 @@ android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" - android:id="@+id/textView3" + android:id="@+id/lblUpVotes" android:layout_row="1" android:layout_column="1" /> @@ -47,7 +47,7 @@ android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" - android:id="@+id/textView4" + android:id="@+id/lblDownVotes" android:layout_row="1" android:layout_column="4" />