Skip to content

Commit

Permalink
Issue ShutUpPaulo#22: Adding RatingBar of evaluateUser
Browse files Browse the repository at this point in the history
  • Loading branch information
igorribeiroduarte committed Nov 24, 2015
1 parent 5a2de6d commit d25c807
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
package com.mathheals.euvou.controller.user_profile;

import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.mathheals.euvou.R;
import com.mathheals.euvou.controller.utility.LoginUtility;

import org.json.JSONException;
import org.json.JSONObject;

import dao.UserDAO;
import dao.UserEvaluationDAO;
import model.UserEvaluation;

public class ShowUser extends Fragment {
private String idUser;
public class ShowUser extends android.support.v4.app.Fragment {
private UserEvaluation userEvaluation;
private final String SUCCESSFULL_EVALUATION_MESSAGE = "Avaliação cadastrada com sucesso";
private RatingBar ratingBar;
private View showUserView;
private String userEvaluatedId;
private int currentUserId;
private boolean isUserLoggedIn;
private TextView ratingMessage;
private final Integer LOGGED_OUT = -1;

public ShowUser()
{
Expand All @@ -25,13 +40,14 @@ public ShowUser()
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.show_user, container, false);
setShowUserView(inflater.inflate(R.layout.show_user, container, false));
UserDAO userDAO = new UserDAO(getActivity());

idUser=this.getArguments().getString("id");
userEvaluatedId=this.getArguments().getString("id");
setCurrentUserId(new LoginUtility(getActivity()).getUserId());
JSONObject userData = null;
try {
userData = new JSONObject(userDAO.searchUserById(Integer.parseInt(idUser)));
userData = new JSONObject(userDAO.searchUserById(Integer.parseInt(userEvaluatedId)));
} catch (JSONException e) {
e.printStackTrace();
}
Expand All @@ -41,9 +57,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
String birthDateDB = userData.getJSONObject("0").getString("birthDate");
String mailDB = userData.getJSONObject("0").getString("email");

TextView name= (TextView) view.findViewById(R.id.labelName);
TextView date = (TextView) view.findViewById(R.id.labelBirthDate);
TextView mail = (TextView) view.findViewById(R.id.labelMail);
TextView name= (TextView) showUserView.findViewById(R.id.labelName);
TextView date = (TextView) showUserView.findViewById(R.id.labelBirthDate);
TextView mail = (TextView) showUserView.findViewById(R.id.labelMail);
name.setText(nameUserDB);
date.setText(birthDateDB);
mail.setText(mailDB);
Expand All @@ -52,6 +68,82 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
e.printStackTrace();
}

return view;
setIsUserLoggedIn(currentUserId != LOGGED_OUT);
setRatingMessage(isUserLoggedIn);
setRatingBarIfNeeded();

return showUserView;
}

public void setIsUserLoggedIn(boolean isUserLoggedIn) {
this.isUserLoggedIn = isUserLoggedIn;
}

private void setRatingMessage(boolean isUserLoggedIn) {
final String LOGGED_IN_MESSAGE = "Sua avaliação:";
final String LOGGED_OUT_MESSAGE = "Faça login para avaliar este usuário!";
String message = isUserLoggedIn ? LOGGED_IN_MESSAGE : LOGGED_OUT_MESSAGE;

ratingMessage = (TextView) showUserView.findViewById(R.id.rate_user_text);
ratingMessage.setText(message);
}

public void setShowUserView(View showUserView) {
this.showUserView = showUserView;
}

private void setRatingBarIfNeeded() {
if(isUserLoggedIn)
setRatingBar();
}

public void setCurrentUserId(int currentUserId) {
this.currentUserId = currentUserId;
}

private void setRatingBar() {
ratingBar = (RatingBar) showUserView.findViewById(R.id.ratingBar);
ratingBar.setVisibility(View.VISIBLE);

UserEvaluationDAO userEvaluationDAO = new UserEvaluationDAO();

JSONObject evaluationJSON = userEvaluationDAO.searchUserEvaluation(Integer.parseInt(userEvaluatedId), currentUserId);

if(evaluationJSON!=null){
Float evaluation = null;
try {
evaluation = new Float(evaluationJSON.getJSONObject("0").getDouble("grade"));
} catch (JSONException e) {
e.printStackTrace();
}

ratingBar.setRating(evaluation);
}

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar arg0, float rateValue, boolean arg2) {
setUserEvaluation(rateValue, currentUserId, new Integer(userEvaluatedId));

UserEvaluationDAO userEvaluationDAO = new UserEvaluationDAO();

userEvaluationDAO.evaluateUser(getUserEvaluation());
}
});
setRatingBarStyle();
}

public UserEvaluation getUserEvaluation() {
return userEvaluation;
}

public void setUserEvaluation(Float rating, Integer userId, Integer userEvaluatedId) {
this.userEvaluation = new UserEvaluation(rating, userId, userEvaluatedId);
Toast.makeText(getActivity().getBaseContext(), SUCCESSFULL_EVALUATION_MESSAGE, Toast.LENGTH_LONG).show();
}

private void setRatingBarStyle() {
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(ContextCompat.getColor(getContext(), R.color.turquesa_app), PorterDuff.Mode.SRC_ATOP);
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/model/UserEvaluation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class UserEvaluation {
private Integer userEvaluatedId;

public UserEvaluation(Float rating, Integer userId, Integer userEvaluatedId){
this.rating=rating;
this.userId=userId;
this.userEvaluatedId=userEvaluatedId;
setRating(rating);
setUserId(userId);
setUserEvaluatedId(userEvaluatedId);
}

public Float getRating() {
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/res/layout/show_user.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,31 @@
android:textColor = "#000000"
android:id="@+id/labelMail" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/rate_user_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/grade"
android:text="Sua avaliação:"
android:layout_marginTop="10dp"
/>

<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.5"
android:rating="0.0"
android:scaleX="0.5"
android:scaleY="0.5"
android:visibility="invisible"
android:layout_marginBottom="20dp"/>
</LinearLayout>

</LinearLayout>

0 comments on commit d25c807

Please sign in to comment.