From d25c8072829bde204997d7720c0f7b04c0fe1915 Mon Sep 17 00:00:00 2001 From: Igor Ribeiro Barbosa Duarte Date: Mon, 23 Nov 2015 22:33:55 -0200 Subject: [PATCH] Issue #22: Adding RatingBar of evaluateUser --- .../controller/user_profile/ShowUser.java | 110 ++++++++++++++++-- app/src/main/java/model/UserEvaluation.java | 6 +- app/src/main/res/layout/show_user.xml | 27 +++++ 3 files changed, 131 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/mathheals/euvou/controller/user_profile/ShowUser.java b/app/src/main/java/com/mathheals/euvou/controller/user_profile/ShowUser.java index 54b678d..fdd479b 100644 --- a/app/src/main/java/com/mathheals/euvou/controller/user_profile/ShowUser.java +++ b/app/src/main/java/com/mathheals/euvou/controller/user_profile/ShowUser.java @@ -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() { @@ -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(); } @@ -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); @@ -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); } } diff --git a/app/src/main/java/model/UserEvaluation.java b/app/src/main/java/model/UserEvaluation.java index 59b1a60..1503828 100644 --- a/app/src/main/java/model/UserEvaluation.java +++ b/app/src/main/java/model/UserEvaluation.java @@ -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() { diff --git a/app/src/main/res/layout/show_user.xml b/app/src/main/res/layout/show_user.xml index 522e014..e2a0fc0 100644 --- a/app/src/main/res/layout/show_user.xml +++ b/app/src/main/res/layout/show_user.xml @@ -64,4 +64,31 @@ android:textColor = "#000000" android:id="@+id/labelMail" /> + + + + + + + \ No newline at end of file