mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-23 04:09:54 +00:00
Rating food
This commit is contained in:
@@ -8,6 +8,7 @@ import android.os.Bundle;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.RatingBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
@@ -18,26 +19,34 @@ import com.google.firebase.database.DataSnapshot;
|
||||
import com.google.firebase.database.DatabaseError;
|
||||
import com.google.firebase.database.DatabaseReference;
|
||||
import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.google.firebase.database.Query;
|
||||
import com.google.firebase.database.ValueEventListener;
|
||||
import com.squareup.picasso.Picasso;
|
||||
import com.stepstone.apprating.AppRatingDialog;
|
||||
import com.stepstone.apprating.listener.RatingDialogListener;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Database.Database;
|
||||
import com.waterbase.foodify.Model.Food;
|
||||
import com.waterbase.foodify.Model.Order;
|
||||
import com.waterbase.foodify.Model.Rating;
|
||||
|
||||
public class FoodDetail extends AppCompatActivity {
|
||||
import java.util.Arrays;
|
||||
|
||||
public class FoodDetail extends AppCompatActivity implements RatingDialogListener {
|
||||
|
||||
|
||||
TextView food_name, food_price, food_description;
|
||||
ImageView food_image;
|
||||
CollapsingToolbarLayout collapsingToolbarLayout;
|
||||
FloatingActionButton btnCart;
|
||||
FloatingActionButton btnCart, btnRating;
|
||||
ElegantNumberButton numberButton;
|
||||
RatingBar ratingBar;
|
||||
|
||||
String foodId = "";
|
||||
|
||||
FirebaseDatabase database;
|
||||
DatabaseReference foods;
|
||||
DatabaseReference ratingTbl;
|
||||
|
||||
Food currentFood;
|
||||
|
||||
@@ -49,10 +58,20 @@ public class FoodDetail extends AppCompatActivity {
|
||||
//Firebase
|
||||
database = FirebaseDatabase.getInstance();
|
||||
foods = database.getReference("Foods");
|
||||
ratingTbl = database.getReference("Rating");
|
||||
|
||||
//Init view
|
||||
numberButton = (ElegantNumberButton) findViewById(R.id.number_button);
|
||||
btnCart = (FloatingActionButton) findViewById(R.id.btnCart);
|
||||
btnRating = (FloatingActionButton) findViewById(R.id.btnRating);
|
||||
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
|
||||
|
||||
btnRating.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
showRatingDialog();
|
||||
}
|
||||
});
|
||||
|
||||
btnCart.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@@ -79,12 +98,13 @@ public class FoodDetail extends AppCompatActivity {
|
||||
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);
|
||||
|
||||
//Get Food Id from Intent
|
||||
if(getIntent() != null)
|
||||
if (getIntent() != null)
|
||||
foodId = getIntent().getStringExtra("FoodId");
|
||||
if(!foodId.isEmpty()) {
|
||||
if(Common.isConnectedToInternet(getBaseContext()))
|
||||
if (!foodId.isEmpty()) {
|
||||
if (Common.isConnectedToInternet(getBaseContext())) {
|
||||
getDetailFood(foodId);
|
||||
else {
|
||||
getRatingFood(foodId);
|
||||
} else {
|
||||
Toast.makeText(FoodDetail.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
@@ -100,6 +120,52 @@ public class FoodDetail extends AppCompatActivity {
|
||||
|
||||
}
|
||||
|
||||
private void getRatingFood(String foodId) {
|
||||
|
||||
Query foodRating = ratingTbl.orderByChild("foodId").equalTo(foodId);
|
||||
|
||||
foodRating.addValueEventListener(new ValueEventListener() {
|
||||
int count = 0, sum = 0;
|
||||
|
||||
@Override
|
||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
|
||||
Rating item = postSnapshot.getValue(Rating.class);
|
||||
sum += Integer.parseInt(item.getRateValue());
|
||||
count++;
|
||||
}
|
||||
if (count != 0) {
|
||||
float average = sum / count;
|
||||
ratingBar.setRating(average);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showRatingDialog() {
|
||||
new AppRatingDialog.Builder()
|
||||
.setPositiveButtonText("Gửi")
|
||||
.setNegativeButtonText("Thoát")
|
||||
.setNoteDescriptions(Arrays.asList("Rất tệ", "Tệ", "Bình thường", "Tốt", "Rất tốt"))
|
||||
.setDefaultRating(1)
|
||||
.setTitle("Đánh giá món ăn này")
|
||||
.setDescription("Vui lòng đánh giá và để lại bình luận dưới đây")
|
||||
.setTitleTextColor(R.color.purple_500)
|
||||
.setDescriptionTextColor(R.color.colorPrimary)
|
||||
.setHint("Điền đánh giá bạn tại đây")
|
||||
.setHintTextColor(R.color.colorAccent)
|
||||
.setCommentTextColor(android.R.color.white)
|
||||
.setCommentBackgroundColor(R.color.colorPrimaryDark)
|
||||
.setWindowAnimation(R.style.RatingDialogFadeAnim)
|
||||
.create(FoodDetail.this)
|
||||
.show();
|
||||
}
|
||||
|
||||
private void getDetailFood(String foodId) {
|
||||
foods.child(foodId).addValueEventListener(new ValueEventListener() {
|
||||
@Override
|
||||
@@ -134,4 +200,39 @@ public class FoodDetail extends AppCompatActivity {
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNegativeButtonClicked() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPositiveButtonClicked(int value, @NonNull String comments) {
|
||||
//Get rating and upload to firebase
|
||||
Rating rating = new Rating(Common.currentUser.getPhone(),
|
||||
foodId,
|
||||
String.valueOf(value),
|
||||
comments);
|
||||
ratingTbl.child(Common.currentUser.getPhone()).addValueEventListener(new ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||
if (dataSnapshot.child(Common.currentUser.getPhone()).exists()) {
|
||||
//Remove old feedback
|
||||
ratingTbl.child(Common.currentUser.getPhone()).removeValue();
|
||||
//Update new feedback
|
||||
ratingTbl.child(Common.currentUser.getPhone()).setValue(rating);
|
||||
} else {
|
||||
|
||||
//Update new value
|
||||
ratingTbl.child(Common.currentUser.getPhone()).setValue(rating);
|
||||
}
|
||||
Toast.makeText(FoodDetail.this, "Cảm ơn bạn đã đánh giá món ăn!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.waterbase.foodify.Model;
|
||||
|
||||
public class Rating {
|
||||
private String userPhone;
|
||||
private String foodId;
|
||||
private String rateValue;
|
||||
private String comment;
|
||||
|
||||
public Rating(String userPhone, String foodId, String rateValue, String comment) {
|
||||
this.userPhone = userPhone;
|
||||
this.foodId = foodId;
|
||||
this.rateValue = rateValue;
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public Rating() {
|
||||
}
|
||||
|
||||
public String getUserPhone() {
|
||||
return userPhone;
|
||||
}
|
||||
|
||||
public void setUserPhone(String userPhone) {
|
||||
this.userPhone = userPhone;
|
||||
}
|
||||
|
||||
public String getFoodId() {
|
||||
return foodId;
|
||||
}
|
||||
|
||||
public void setFoodId(String foodId) {
|
||||
this.foodId = foodId;
|
||||
}
|
||||
|
||||
public String getRateValue() {
|
||||
return rateValue;
|
||||
}
|
||||
|
||||
public void setRateValue(String rateValue) {
|
||||
this.rateValue = rateValue;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public void setComment(String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user