mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 13:38:41 +00:00
Fix bug and create order complete
This commit is contained in:
Generated
+5
-5
@@ -1,17 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="deploymentTargetDropDown">
|
||||
<targetSelectedWithDropDown>
|
||||
<runningDeviceTargetSelectedWithDropDown>
|
||||
<Target>
|
||||
<type value="QUICK_BOOT_TARGET" />
|
||||
<type value="RUNNING_DEVICE_TARGET" />
|
||||
<deviceKey>
|
||||
<Key>
|
||||
<type value="VIRTUAL_DEVICE_PATH" />
|
||||
<value value="C:\Users\phanh\.android\avd\Pixel_5_API_31.avd" />
|
||||
<value value="C:\Users\phanh\.android\avd\Pixel_3a_API_33_x86_64.avd" />
|
||||
</Key>
|
||||
</deviceKey>
|
||||
</Target>
|
||||
</targetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2023-03-18T03:45:12.257058300Z" />
|
||||
</runningDeviceTargetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2023-03-29T09:35:50.968141600Z" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -41,7 +41,7 @@ public interface FoodApi {
|
||||
@GET("products/enable?pageNo=0&pageSize=10&sortBy=createdTime&sortDir=desc")
|
||||
Call<Foods> recentFood();
|
||||
|
||||
@GET("shops?pageNo=0&pageSize=10&sortBy=id&sortDir=asc")
|
||||
@GET("shops/enable?pageNo=0&pageSize=10&sortBy=id&sortDir=asc")
|
||||
Call<Shops> allShops();
|
||||
|
||||
@GET("products/search/{name}")
|
||||
|
||||
@@ -81,7 +81,7 @@ public interface FoodApiToken {
|
||||
@DELETE("users/{userId}/addresses/{addressId}")
|
||||
Call<CustomResponse> deleteAddressUser(@Path("userId") int userId, @Path("addressId") int addressId);
|
||||
|
||||
@POST("user/{userId}/orders")
|
||||
@POST("users/{userId}/orders")
|
||||
Call<Order> createOrder(@Path("userId") int userId, @Body Order order);
|
||||
|
||||
@POST("products/{productId}/comments")
|
||||
|
||||
@@ -571,11 +571,12 @@ public class FoodDetailActivity extends AppCompatActivity {
|
||||
confirm.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
if(comment_content.getText().toString().trim().length() > 0){
|
||||
progressLayout.setVisibility(View.VISIBLE);
|
||||
|
||||
if(actionCode == CREATE_COMMENT){
|
||||
//Create comment
|
||||
|
||||
createComment(comment_content.getText().toString(), ratingBarDialog);
|
||||
|
||||
dialog.dismiss();
|
||||
@@ -590,7 +591,9 @@ public class FoodDetailActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
comment_content.setError("Khung bình luận không được để trống!");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -14,10 +14,12 @@ import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.splashscreen.SplashScreen;
|
||||
@@ -70,6 +72,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
BottomNavigationView bottomNavigationView;
|
||||
private FirebaseAuth mAuth;
|
||||
private FirebaseUser user;
|
||||
private ConstraintLayout progressLayout;
|
||||
|
||||
//Location
|
||||
private static final int REQUEST_CHECK_SETTINGS = 100;
|
||||
@@ -98,6 +101,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
user = mAuth.getCurrentUser();
|
||||
|
||||
if (user != null && Common.CURRENT_USER == null) {
|
||||
progressLayout.setVisibility(View.VISIBLE);
|
||||
user.getIdToken(true)
|
||||
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
|
||||
@Override
|
||||
@@ -189,10 +193,13 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
|
||||
private void getLocationUser(){
|
||||
|
||||
}
|
||||
private void initComponent() {
|
||||
bottomNavigationView = findViewById(R.id.bottom_nav);
|
||||
viewPager2 = findViewById(R.id.viewPager);
|
||||
progressLayout = findViewById(R.id.progress_layout);
|
||||
}
|
||||
|
||||
private void bottomNavigation() {
|
||||
|
||||
@@ -29,15 +29,20 @@ import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.capstone.foodify.API.FoodApi;
|
||||
import com.capstone.foodify.API.FoodApiToken;
|
||||
import com.capstone.foodify.API.GoogleMapApi;
|
||||
import com.capstone.foodify.Adapter.OrderDetailAdapter;
|
||||
import com.capstone.foodify.Common;
|
||||
import com.capstone.foodify.Model.Address;
|
||||
import com.capstone.foodify.Model.Basket;
|
||||
import com.capstone.foodify.Model.DistrictWardResponse;
|
||||
import com.capstone.foodify.Model.GoogleMap.GoogleMapResponse;
|
||||
import com.capstone.foodify.Model.GoogleMap.Location;
|
||||
import com.capstone.foodify.Model.Order;
|
||||
import com.capstone.foodify.Model.OrderDetail;
|
||||
import com.capstone.foodify.R;
|
||||
import com.capstone.foodify.ZaloPay.Api.CreateOrder;
|
||||
import com.capstone.foodify.ZaloPay.Helper.Helpers;
|
||||
import com.google.android.gms.maps.model.LatLng;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
@@ -63,9 +68,13 @@ import vn.zalopay.sdk.ZaloPaySDK;
|
||||
import vn.zalopay.sdk.listeners.PayOrderListener;
|
||||
|
||||
public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
private static final String TAKE_FOOD_FROM_SHOP = "Đến shop lấy";
|
||||
private static final String ZALO_PAYMENT_METHOD = "ZALO PAY";
|
||||
private static final String CASH_PAYMENT = "CASH";
|
||||
private static final Double LAT_SHOP = 16.0500622;
|
||||
private static final Double LNG_SHOP = 108.2351611;
|
||||
private static String finalAddress = null;
|
||||
private static double finalLat, finalLng = 0;
|
||||
ImageView back_image;
|
||||
OrderDetailAdapter adapter;
|
||||
RecyclerView recyclerView;
|
||||
@@ -74,11 +83,11 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
EditText edt_address;
|
||||
Spinner spn_list_address, spn_district, spn_ward;
|
||||
ConstraintLayout manual_input_address_layout;
|
||||
Button change_address_button, confirm_address_button, btn_ZaloPay;
|
||||
Button change_address_button, confirm_address_button, btn_ZaloPay, btn_Pay;
|
||||
RadioButton list_address_input, auto_detect_location, manual_input_address, radio_button_selected, take_food_from_shop;
|
||||
RadioGroup address_option;
|
||||
ConstraintLayout progress_layout;
|
||||
float total;
|
||||
float total, shipCost;
|
||||
private static final ArrayList<String> wardList = new ArrayList<>();
|
||||
private static final ArrayList<String> districtList = new ArrayList<>();
|
||||
|
||||
@@ -118,6 +127,7 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
txt_distance = findViewById(R.id.txt_distance);
|
||||
txt_ship_cost = findViewById(R.id.txt_ship_cost);
|
||||
btn_ZaloPay = findViewById(R.id.btnZaloPay);
|
||||
btn_Pay = findViewById(R.id.btnPay);
|
||||
|
||||
//Reload user data
|
||||
Common.reloadUser(this);
|
||||
@@ -193,7 +203,7 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
|
||||
if(radio_button_selected == take_food_from_shop){
|
||||
//Action 4
|
||||
finalAddress = "Đến shop lấy";
|
||||
finalAddress = TAKE_FOOD_FROM_SHOP;
|
||||
}
|
||||
|
||||
if(finalAddress == null){
|
||||
@@ -219,6 +229,28 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
});
|
||||
|
||||
initZaloPaymentMethod();
|
||||
|
||||
//Set event for button pay by CASH
|
||||
btn_Pay.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
progress_layout.setVisibility(View.VISIBLE);
|
||||
|
||||
if(!Common.CURRENT_USER.isLocked()){
|
||||
if(finalAddress != null){
|
||||
createOrderFood(Helpers.getAppTransId(), CASH_PAYMENT);
|
||||
} else {
|
||||
Toast.makeText(OrderCheckOutActivity.this, "Bạn chưa xác nhận địa chỉ!", Toast.LENGTH_SHORT).show();
|
||||
progress_layout.setVisibility(View.GONE);
|
||||
}
|
||||
} else {
|
||||
//Show notification account locked
|
||||
FirebaseAuth.getInstance().signOut();
|
||||
Common.showErrorServerNotification(OrderCheckOutActivity.this, "Tài khoản của bạn đã bị khoá!");
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void getAddress() {
|
||||
@@ -246,6 +278,7 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void initZaloPaymentMethod(){
|
||||
|
||||
// handle CreateOrder
|
||||
btn_ZaloPay.setOnClickListener(new View.OnClickListener() {
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
@@ -257,7 +290,6 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
|
||||
if(!Common.CURRENT_USER.isLocked()){
|
||||
//Create order
|
||||
|
||||
//Check address is null or not
|
||||
if(finalAddress != null){
|
||||
|
||||
@@ -278,24 +310,7 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new AestheticDialog.Builder(OrderCheckOutActivity.this, DialogStyle.FLAT, DialogType.SUCCESS)
|
||||
.setTitle("Thành công!")
|
||||
.setMessage("Bạn đã thanh toán thành công cho đơn hàng #" + transactionId)
|
||||
.setCancelable(true)
|
||||
.setOnClickListener(new OnDialogClickListener() {
|
||||
@Override
|
||||
public void onClick(@NonNull AestheticDialog.Builder builder) {
|
||||
|
||||
createOrderFood(transactionId);
|
||||
|
||||
Common.LIST_BASKET_FOOD.clear();
|
||||
startActivity(new Intent(OrderCheckOutActivity.this, MainActivity.class));
|
||||
finish();
|
||||
}
|
||||
})
|
||||
.show();
|
||||
|
||||
progress_layout.setVisibility(View.GONE);
|
||||
createOrderFood(transactionId, ZALO_PAYMENT_METHOD);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -321,6 +336,7 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onClick(@NonNull AestheticDialog.Builder builder) {
|
||||
startActivity(new Intent(OrderCheckOutActivity.this, MainActivity.class));
|
||||
builder.dismiss();
|
||||
finish();
|
||||
}
|
||||
})
|
||||
@@ -348,27 +364,50 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
|
||||
private void createOrderFood(String transactionId) {
|
||||
private void createOrderFood(String transactionId, String paymentMethod) {
|
||||
|
||||
// //Create order object
|
||||
// Order order = new Order(transactionId, "");
|
||||
//
|
||||
// FoodApiToken.apiService.createOrder(Common.CURRENT_USER.getId()).enqueue(new Callback<Order>() {
|
||||
// @Override
|
||||
// public void onResponse(Call<Order> call, Response<Order> response) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onFailure(Call<Order> call, Throwable t) {
|
||||
//
|
||||
// }
|
||||
// });
|
||||
//Create list order detail object
|
||||
List<OrderDetail> listOrderDetail = new ArrayList<>();
|
||||
for(Basket tempFood: Common.LIST_BASKET_FOOD){
|
||||
OrderDetail orderTemp = new OrderDetail(Integer.parseInt(tempFood.getId()), tempFood.getQuantity());
|
||||
listOrderDetail.add(orderTemp);
|
||||
}
|
||||
|
||||
//Create order object
|
||||
Order order = new Order(transactionId, paymentMethod, shipCost, "AWAITING" , finalAddress, listOrderDetail, String.valueOf(finalLat), String.valueOf(finalLng));
|
||||
|
||||
FoodApiToken.apiService.createOrder(Common.CURRENT_USER.getId(), order).enqueue(new Callback<Order>() {
|
||||
@Override
|
||||
public void onResponse(Call<Order> call, Response<Order> response) {
|
||||
if(response.code() == 201){
|
||||
new AestheticDialog.Builder(OrderCheckOutActivity.this, DialogStyle.FLAT, DialogType.SUCCESS)
|
||||
.setTitle("Thành công!")
|
||||
.setMessage("Bạn đã đặt hàng thành công cho đơn hàng #" + transactionId)
|
||||
.setCancelable(true)
|
||||
.setOnClickListener(new OnDialogClickListener() {
|
||||
@Override
|
||||
public void onClick(@NonNull AestheticDialog.Builder builder) {
|
||||
Common.LIST_BASKET_FOOD.clear();
|
||||
startActivity(new Intent(OrderCheckOutActivity.this, MainActivity.class));
|
||||
builder.dismiss();
|
||||
finish();
|
||||
}
|
||||
}).show();
|
||||
} else {
|
||||
Toast.makeText(OrderCheckOutActivity.this, "Đã có lỗi hệ thống! Mã lỗi: " + response.code(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Order> call, Throwable t) {
|
||||
Toast.makeText(OrderCheckOutActivity.this, "Đã xảy ra lỗi kết nối hệ thống!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void getDistanceAndCalculateShipCost(String address) {
|
||||
//Get location from address
|
||||
|
||||
if(!address.equals(TAKE_FOOD_FROM_SHOP)){
|
||||
GoogleMapApi.apiService.getGeoCode(address, Common.MAP_API).enqueue(new Callback<GoogleMapResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<GoogleMapResponse> call, Response<GoogleMapResponse> response) {
|
||||
@@ -377,24 +416,24 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
Double lat = jsonResponse.getResults().get(0).getGeometry().getLocation().getLat();
|
||||
Double lng = jsonResponse.getResults().get(0).getGeometry().getLocation().getLng();
|
||||
|
||||
finalLat = lat;
|
||||
finalLng = lng;
|
||||
|
||||
LatLng addressFrom = new LatLng(lat, lng);
|
||||
LatLng addressTo = new LatLng(LAT_SHOP, LNG_SHOP);
|
||||
|
||||
double distance = CalculationByDistance(addressFrom, addressTo);
|
||||
|
||||
double roundDistance = Math.round(distance * 100.0) / 100.0;
|
||||
int distance = CalculationByDistance(addressFrom, addressTo);
|
||||
|
||||
//Show distance to user
|
||||
txt_distance.setText(roundDistance + " km");
|
||||
txt_distance.setText(distance + " km");
|
||||
|
||||
//Calculate ship cost
|
||||
float shipCost = 0;
|
||||
if(roundDistance <= 3){
|
||||
if(distance <= 3){
|
||||
shipCost = 15000;
|
||||
} else if(roundDistance > 3 && roundDistance <= 10){
|
||||
shipCost = 5000 * (float) roundDistance;
|
||||
} else if(distance > 3 && distance <= 10){
|
||||
shipCost = 5000 * (float) distance;
|
||||
} else {
|
||||
shipCost = 3000 * (float) roundDistance;
|
||||
shipCost = 3000 * (float) distance;
|
||||
}
|
||||
|
||||
txt_ship_cost.setText(Common.changeCurrencyUnit(shipCost));
|
||||
@@ -411,9 +450,13 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
Toast.makeText(OrderCheckOutActivity.this, "Error: " + t, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
progress_layout.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
public double CalculationByDistance(LatLng from, LatLng to) {
|
||||
}
|
||||
|
||||
public int CalculationByDistance(LatLng from, LatLng to) {
|
||||
int Radius = 6371;// radius of earth in Km
|
||||
double lat1 = from.latitude;
|
||||
double lat2 = to.latitude;
|
||||
@@ -429,11 +472,8 @@ public class OrderCheckOutActivity extends AppCompatActivity {
|
||||
double valueResult = Radius * c;
|
||||
double km = valueResult / 1;
|
||||
DecimalFormat newFormat = new DecimalFormat("####");
|
||||
int kmInDec = Integer.valueOf(newFormat.format(km));
|
||||
double meter = valueResult % 1000;
|
||||
int meterInDec = Integer.valueOf(newFormat.format(meter));
|
||||
|
||||
return Radius * c;
|
||||
return Integer.valueOf(newFormat.format(km));
|
||||
}
|
||||
|
||||
private boolean validForm(){
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.List;
|
||||
|
||||
public class BasketAdapter extends RecyclerView.Adapter<BasketAdapter.BasketViewHolder> {
|
||||
|
||||
private static int quantity = 0;
|
||||
|
||||
public List<Basket> listBasketFood;
|
||||
|
||||
@@ -58,7 +57,6 @@ public class BasketAdapter extends RecyclerView.Adapter<BasketAdapter.BasketView
|
||||
holder.foodName.setText(foodBasket.getName());
|
||||
holder.shopName.setText(foodBasket.getShopName());
|
||||
holder.value.setText(foodBasket.getQuantity());
|
||||
quantity = Integer.parseInt(foodBasket.getQuantity());
|
||||
|
||||
//Check value discountPercent
|
||||
float cost = 0;
|
||||
@@ -89,6 +87,9 @@ public class BasketAdapter extends RecyclerView.Adapter<BasketAdapter.BasketView
|
||||
holder.increment.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
int quantity = Integer.parseInt(foodBasket.getQuantity());
|
||||
|
||||
quantity++;
|
||||
foodBasket.setQuantity(String.valueOf(quantity));
|
||||
holder.value.setText(String.valueOf(quantity));
|
||||
@@ -101,9 +102,16 @@ public class BasketAdapter extends RecyclerView.Adapter<BasketAdapter.BasketView
|
||||
holder.decrement.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
int quantity = Integer.parseInt(foodBasket.getQuantity());
|
||||
|
||||
quantity--;
|
||||
if(quantity == 0){
|
||||
listBasketFood.remove(holder.getAdapterPosition());
|
||||
notifyDataSetChanged();
|
||||
|
||||
//Update total price
|
||||
calculateTotalPrice();
|
||||
} else {
|
||||
foodBasket.setQuantity(String.valueOf(quantity));
|
||||
holder.value.setText(String.valueOf(quantity));
|
||||
|
||||
@@ -11,15 +11,18 @@ public class Order {
|
||||
private String status;
|
||||
private String address;
|
||||
private List<OrderDetail> orderDetails;
|
||||
private String lat;
|
||||
private String lng;
|
||||
|
||||
public Order(String orderTrackingNumber, int shipperId, String paymentMethod, float shippingCost, String status, String address, List<OrderDetail> orderDetails) {
|
||||
public Order(String orderTrackingNumber, String paymentMethod, float shippingCost, String status, String address, List<OrderDetail> orderDetails, String lat, String lng) {
|
||||
this.orderTrackingNumber = orderTrackingNumber;
|
||||
this.shipperId = shipperId;
|
||||
this.paymentMethod = paymentMethod;
|
||||
this.shippingCost = shippingCost;
|
||||
this.status = status;
|
||||
this.address = address;
|
||||
this.orderDetails = orderDetails;
|
||||
this.lat = lat;
|
||||
this.lng = lng;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
@@ -96,4 +99,20 @@ public class Order {
|
||||
public void setListOrderDetail(List<OrderDetail> listOrderDetail) {
|
||||
this.orderDetails = listOrderDetail;
|
||||
}
|
||||
|
||||
public String getLat() {
|
||||
return lat;
|
||||
}
|
||||
|
||||
public void setLat(String lat) {
|
||||
this.lat = lat;
|
||||
}
|
||||
|
||||
public String getLng() {
|
||||
return lng;
|
||||
}
|
||||
|
||||
public void setLng(String lng) {
|
||||
this.lng = lng;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,14 @@ public class OrderDetail {
|
||||
private String quantity;
|
||||
private float subTotal;
|
||||
|
||||
public OrderDetail(int productId, String quantity) {
|
||||
this.productId = productId;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public OrderDetail() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user