Fix show discount price + Add message from server to User

This commit is contained in:
phanhuuloi27
2022-07-02 21:14:34 +07:00
parent 4f9728d2bc
commit 384ee553a3
25 changed files with 358 additions and 35 deletions
Binary file not shown.
@@ -357,7 +357,7 @@ public class Cart extends AppCompatActivity implements GoogleApiClient.Connectio
//Calculate total price
float total = 0;
for (Order order : cart)
total += (Float.parseFloat(order.getPrice())) * (Float.parseFloat(order.getQuantity()));
total += (Float.parseFloat(order.getPrice())) * (Float.parseFloat(order.getQuantity())) * (100 - Long.parseLong(order.getDiscount()))/100;;
Locale locale = new Locale("vi", "VN");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
@@ -12,6 +12,8 @@ import com.waterbase.foodify.Remote.RetrofitClient;
public class Common {
public static User currentUser;
public static String topicName = "News";
private static final String BASE_URL = "https://fcm.googleapis.com/";
private static final String GOOGLE_API_URL = "https://maps.googleapis.com/";
@@ -66,21 +66,21 @@ public class Database extends SQLiteAssetHelper {
}
//Favorites
public void addToFavorites(String foodId) {
public void addToFavorites(String foodId, String userPhone) {
SQLiteDatabase db = getReadableDatabase();
String query = String.format("INSERT INTO Favorites(FoodId) VALUES('%s');", foodId);
String query = String.format("INSERT INTO Favorites(FoodId,UserPhone) VALUES('%s', '%s');", foodId, userPhone);
db.execSQL(query);
}
public void removeFromFavorites(String foodId) {
public void removeFromFavorites(String foodId, String userPhone) {
SQLiteDatabase db = getReadableDatabase();
String query = String.format("DELETE FROM Favorites WHERE FoodId='%s';", foodId);
String query = String.format("DELETE FROM Favorites WHERE FoodId='%s' and UserPhone='%s';", foodId, userPhone);
db.execSQL(query);
}
public boolean isFavorite(String foodId) {
public boolean isFavorite(String foodId, String userPhone) {
SQLiteDatabase db = getReadableDatabase();
String query = String.format("SELECT * FROM Favorites WHERE FoodId='%s'", foodId);
String query = String.format("SELECT * FROM Favorites WHERE FoodId='%s' and UserPhone='%s'", foodId, userPhone);
Cursor cursor = db.rawQuery(query, null);
if (cursor.getCount() <= 0) {
cursor.close();
@@ -11,7 +11,10 @@ import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
@@ -273,7 +276,25 @@ public class FoodList extends AppCompatActivity {
@Override
protected void onBindViewHolder(@NonNull FoodViewHolder viewHolder, int position, @NonNull Food model) {
viewHolder.food_name.setText(model.getName());
viewHolder.food_price.setText(String.format("%s đ", model.getPrice()));
if(Integer.parseInt(model.getDiscount()) > 0)
{
String foodPrice = model.getPrice() + "đ";
long newFoodPrice = Long.parseLong(model.getPrice()) - Long.parseLong(model.getPrice())* Long.parseLong(model.getDiscount())/100;
SpannableStringBuilder spnBuilder = new SpannableStringBuilder(foodPrice);
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spnBuilder.setSpan(strikethroughSpan, 0, foodPrice.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
viewHolder.food_price.setText(spnBuilder);
viewHolder.newPrice.setText(newFoodPrice+"đ");
} else
{
viewHolder.food_price.setText(String.format("%s đ", model.getPrice()));
viewHolder.discount.setVisibility(View.GONE);
viewHolder.newPrice.setVisibility(View.GONE);
}
viewHolder.discount.setText("- " + model.getDiscount() + "%");
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.food_image);
//Quick Cart
@@ -293,20 +314,21 @@ public class FoodList extends AppCompatActivity {
}
});
//Add Favorites
if(localDB.isFavorite(adapter.getRef(position).getKey()))
if(localDB.isFavorite(adapter.getRef(position).getKey(),Common.currentUser.getPhone()))
viewHolder.fav_image.setImageResource(R.drawable.ic_baseline_favorite_24);
//Click to change state of Favorites
viewHolder.fav_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!localDB.isFavorite(adapter.getRef(viewHolder.getAdapterPosition()).getKey())){
localDB.addToFavorites(adapter.getRef(viewHolder.getAdapterPosition()).getKey());
if(!localDB.isFavorite(adapter.getRef(viewHolder.getAdapterPosition()).getKey(), Common.currentUser.getPhone())){
localDB.addToFavorites(adapter.getRef(viewHolder.getAdapterPosition()).getKey(), Common.currentUser.getPhone());
viewHolder.fav_image.setImageResource(R.drawable.ic_baseline_favorite_24);
Toast.makeText(FoodList.this, model.getName() + " đã thêm vào danh sách yêu thích", Toast.LENGTH_SHORT).show();
} else {
localDB.removeFromFavorites(adapter.getRef(viewHolder.getAdapterPosition()).getKey());
localDB.removeFromFavorites(adapter.getRef(viewHolder.getAdapterPosition()).getKey(), Common.currentUser.getPhone());
viewHolder.fav_image.setImageResource(R.drawable.ic_baseline_favorite_border_24);
Toast.makeText(FoodList.this, model.getName() + " đã xoá khỏi danh sách yêu thích", Toast.LENGTH_SHORT).show();
}
@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
@@ -11,6 +12,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
@@ -43,6 +45,7 @@ import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.squareup.picasso.Picasso;
import com.waterbase.foodify.Common.Common;
@@ -321,6 +324,8 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
showChangePasswordDialog();
} else if (id == R.id.nav_home_address) {
showHomeAddressDialog();
} else if (id == R.id.nav_setting) {
showSettingDialog();
}
else if (id == R.id.nav_log_out) {
@@ -340,6 +345,47 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
return true;
}
private void showSettingDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Home.this);
alertDialog.setTitle("Cài đặt");
LayoutInflater inflater = LayoutInflater.from(this);
View layout_setting = inflater.inflate(R.layout.setting_layout, null);
CheckBox ckb_subscribe_new = layout_setting.findViewById(R.id.ckb_sub_new);
//Add code remember state of Checkbox
Paper.init(this);
String isSubscribe = Paper.book().read("sub_new");
if(isSubscribe == null || TextUtils.isEmpty(isSubscribe) || isSubscribe.equals("false"))
ckb_subscribe_new.setChecked(false);
else
ckb_subscribe_new.setChecked(true);
alertDialog.setView(layout_setting);
alertDialog.setPositiveButton("Xong", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if(ckb_subscribe_new.isChecked())
{
FirebaseMessaging.getInstance().subscribeToTopic(Common.topicName);
//Write value
Paper.book().write("sub_new", "true");
}
else
{
FirebaseMessaging.getInstance().unsubscribeFromTopic(Common.topicName);
//Write value
Paper.book().write("sub_new", "false");
}
}
});
alertDialog.show();
}
private void showHomeAddressDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Home.this);
alertDialog.setTitle("Đặt địa chỉ mặc định");
@@ -17,9 +17,11 @@ public class MyFirebaseIdService extends FirebaseInstanceIdService {
}
private void updateTokenToFirebase(String tokenRefreshed) {
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference tokens = db.getReference("Tokens");
Token token = new Token(tokenRefreshed, false);
tokens.child(Common.currentUser.getPhone()).setValue(token);
if (Common.currentUser != null) {
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference tokens = db.getReference("Tokens");
Token token = new Token(tokenRefreshed, false);
tokens.child(Common.currentUser.getPhone()).setValue(token);
}
}
}
@@ -31,7 +31,7 @@ import java.util.Locale;
class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener {
public TextView txt_card_item, txt_price;
public TextView txt_card_item, txt_price, txtDiscount;
public ElegantNumberButton btn_quantity;
public ImageView cart_image;
@@ -45,6 +45,7 @@ class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickList
super(itemView);
txt_card_item = (TextView) itemView.findViewById(R.id.cart_item_name);
txt_price = (TextView) itemView.findViewById(R.id.cart_item_Price);
txtDiscount = (TextView) itemView.findViewById(R.id.txtDiscount);
btn_quantity = (ElegantNumberButton) itemView.findViewById(R.id.btn_quantity);
cart_image = (ImageView) itemView.findViewById(R.id.cart_image);
@@ -102,10 +103,10 @@ public class CartAdapter extends RecyclerView.Adapter<CartViewHolder>{
float total = 0;
List<Order> orders = new Database(cart).getCarts();
for (Order item : orders)
total += (Float.parseFloat(order.getPrice())) * (Float.parseFloat(item.getQuantity()));
total += (Float.parseFloat(item.getPrice())) * (Float.parseFloat(item.getQuantity())) * (100 - Long.parseLong(item.getDiscount()))/100;
Locale locale = new Locale("vi", "VN");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
float price = newValue*(Float.parseFloat(order.getPrice()));
float price = newValue*(Float.parseFloat(order.getPrice())) * (100 - Long.parseLong(order.getDiscount()))/100;
holder.txt_price.setText(fmt.format(price));
cart.txtTotalPrice.setText(fmt.format(total));
@@ -114,8 +115,13 @@ public class CartAdapter extends RecyclerView.Adapter<CartViewHolder>{
Locale locale = new Locale("vi", "VN");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
float price = (Float.parseFloat(listData.get(position).getPrice()))*(Float.parseFloat(listData.get(position).getQuantity()));
float price = (Float.parseFloat(listData.get(position).getPrice()))*(Float.parseFloat(listData.get(position).getQuantity())) * (100 - Long.parseLong(listData.get(position).getDiscount()))/100;
holder.txt_price.setText(fmt.format(price));
if(Integer.parseInt(listData.get(position).getDiscount()) > 0)
{
holder.txtDiscount.setVisibility(View.VISIBLE);
holder.txtDiscount.setText("-" + listData.get(position).getDiscount() + "%");
}
holder.txt_card_item.setText(listData.get(position).getProductName());
}
@@ -12,7 +12,7 @@ import com.waterbase.foodify.R;
public class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView food_name, food_price;
public TextView food_name, food_price, discount, newPrice;
public ImageView food_image, fav_image, quick_cart;
private ItemClickListener itemClickListener;
@@ -28,6 +28,8 @@ public class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnCl
food_image = (ImageView) itemView.findViewById(R.id.food_image);
fav_image = (ImageView) itemView.findViewById(R.id.fav);
food_price = (TextView) itemView.findViewById(R.id.food_price);
discount = (TextView) itemView.findViewById(R.id.discount);
newPrice = (TextView) itemView.findViewById(R.id.new_Price);
quick_cart = (ImageView) itemView.findViewById(R.id.btn_quick_cart);
itemView.setOnClickListener(this);
@@ -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="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z"/>
</vector>
@@ -38,16 +38,28 @@
android:text="Food 01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/cart_item_Price"
android:layout_marginLeft="10dp"
android:gravity="center_vertical|start"
android:textAllCaps="true"
android:textStyle="italic"
android:text="100,000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtDiscount"
android:layout_marginLeft="10dp"
android:textAllCaps="true"
android:textStyle="italic"
android:textSize="10sp"
android:visibility="gone"
android:textColor="@color/red"
android:text="-50%"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/cart_item_Price"
android:layout_marginLeft="10dp"
android:gravity="center_vertical|start"
android:textAllCaps="true"
android:textStyle="italic"
android:text="100,000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
@@ -18,6 +18,7 @@
<ImageView
android:id="@+id/food_image"
android:src="@drawable/bg4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
@@ -63,6 +64,27 @@
android:text="$100"
android:textSize="20sp" />
<TextView
android:id="@+id/discount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginLeft="30dp"
android:layout_toEndOf="@+id/food_price"
android:text="$100"
android:textColor="@color/red"
android:textSize="10sp" />
<TextView
android:id="@+id/new_Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@+id/discount"
android:textStyle="bold"
android:text="$100"
android:textSize="25sp" />
<ImageView
android:id="@+id/btn_quick_cart"
android:src="@drawable/ic_baseline_shopping_cart_24"
@@ -14,7 +14,7 @@
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/edtHomeAddress"
android:hint="Home Address"
android:hint="Địa chỉ mặc định"
android:inputType="textMultiLine"
android:textColor="@color/purple_500"
android:textColorHint="@color/purple_500"
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardElevation="4dp"
>
<LinearLayout
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/ckb_sub_new"
android:text="Nhận thông báo từ hệ thống"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
@@ -25,6 +25,12 @@
android:title="Đổi mật khẩu"
android:iconTint="@color/white"
/>
<item
android:id="@+id/nav_setting"
android:icon="@drawable/ic_baseline_settings_24"
android:title="Cài đặt"
android:iconTint="@color/white"
/>
<item
android:id="@+id/nav_log_out"
android:icon="@drawable/ic_baseline_exit_to_app_24"
@@ -17,4 +17,5 @@
<color name="overlayBackground">#7f333639</color>
<color name="overlayActionBar">#0e0d0e</color>
<color name="red">#FA0202</color>
</resources>