mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Rating food
This commit is contained in:
@@ -58,7 +58,6 @@
|
||||
android:name=".SignIn"
|
||||
android:exported="false"
|
||||
android:theme="@style/Theme.Foodify.NoActionBar" />
|
||||
/>
|
||||
<activity
|
||||
android:name=".Welcome"
|
||||
android:exported="true"
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:tint="#000000"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
|
||||
</vector>
|
||||
@@ -50,6 +50,20 @@
|
||||
|
||||
/>
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/btnRating"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:backgroundTint="@color/white"
|
||||
app:elevation="6dp"
|
||||
android:src="@drawable/ic_baseline_star_24"
|
||||
app:layout_anchor="@id/app_bar_layout"
|
||||
app:layout_anchorGravity="bottom|right|start"
|
||||
app:pressedTranslationZ="12dp"
|
||||
app:useCompatPadding="true"
|
||||
|
||||
/>
|
||||
|
||||
<androidx.core.widget.NestedScrollView
|
||||
android:id="@+id/nestedScrollView"
|
||||
android:layout_width="match_parent"
|
||||
@@ -132,16 +146,31 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/food_description"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:lineSpacingMultiplier="1.5"
|
||||
android:padding="12dp"
|
||||
android:text="Description"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="14sp" />
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<RatingBar
|
||||
android:id="@+id/ratingBar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:rating="0"
|
||||
android:max="5"
|
||||
android:isIndicator="true"
|
||||
/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/food_description"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:lineSpacingMultiplier="1.5"
|
||||
android:padding="12dp"
|
||||
android:text="Description"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="14sp" />
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
|
||||
@@ -33,4 +33,22 @@
|
||||
<item name="android:textColor">@color/purple_500</item>
|
||||
<item name="android:textStyle">normal</item>
|
||||
</style>
|
||||
|
||||
<!--Rating Dialog -->
|
||||
<style name="RatingDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
|
||||
<item name="background">@android:color/white</item>
|
||||
<item name="android:buttonBarPositiveButtonStyle">@style/RatingDialogButtonStyle</item>
|
||||
<item name="android:buttonBarNegativeButtonStyle">@style/RatingDialogButtonStyle</item>
|
||||
</style>
|
||||
|
||||
<style name="RatingDialogButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
|
||||
<item name="android:textColor">@color/colorAccent</item>
|
||||
<item name="android:textSize">@dimen/text_size_medium</item>
|
||||
</style>
|
||||
|
||||
<style name="RatingDialogFadeAnim">
|
||||
<item name="android:windowEnterAnimation">@android:anim/fade_in</item>
|
||||
<item name="android:windowExitAnimation">@android:anim/fade_out</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
Reference in New Issue
Block a user