Skip to content

Commit

Permalink
got viewing of voting done
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Rosier committed Apr 18, 2015
1 parent 93a757d commit 032b491
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package uk.co.tomrosier.xetk.losesono.prototype.prototype;

/**
* Created by xetk on 18/04/15.
*/
public enum ActionEffect {
positive,
negative;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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) {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -192,6 +197,8 @@ public void handleAction(Object someData) {
}
);

ImageButton upVote = (ImageButton) findViewById(R.id.btnVMDownVote);
ImageButton downVote = (ImageButton) findViewById(R.id.btnVMUpVote);

}

Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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;
}
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) {
Expand Down Expand Up @@ -74,4 +78,8 @@ public void setUser(User user) {
public User getUser() {
return user;
}

public Vote getVote() {
return vote;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 4 additions & 4 deletions app/src/main/res/layout/activity_view_message.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<ImageButton
android:layout_width="20dp"
android:layout_height="wrap_content"
android:id="@+id/ibTest1"
android:id="@+id/btnVMUpVote"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:cropToPadding="false"
Expand All @@ -73,7 +73,7 @@
<ImageButton
android:layout_width="20dp"
android:layout_height="wrap_content"
android:id="@+id/ibTest"
android:id="@+id/btnVMDownVote"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:cropToPadding="false"
Expand All @@ -89,7 +89,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2"
android:id="@+id/VMDownVote"
android:layout_row="4"
android:layout_column="5" />

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/comment_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />


</GridLayout>
Loading

0 comments on commit 032b491

Please sign in to comment.