mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Check Internet status and delete Cart item
This commit is contained in:
@@ -66,7 +66,10 @@ public class Cart extends AppCompatActivity {
|
||||
btnPlace.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
showAlertDialog();
|
||||
if(cart.size() > 0)
|
||||
showAlertDialog();
|
||||
else
|
||||
Toast.makeText(Cart.this, "Giỏ hàng của bạn đang trống!!!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -130,6 +133,7 @@ public class Cart extends AppCompatActivity {
|
||||
private void loadListFood() {
|
||||
cart = new Database(this).getCarts();
|
||||
adapter = new CartAdapter(cart, this);
|
||||
adapter.notifyDataSetChanged();
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
//Calculate total price
|
||||
@@ -151,4 +155,23 @@ public class Cart extends AppCompatActivity {
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onContextItemSelected(@NonNull MenuItem item) {
|
||||
if(item.getTitle().equals(Common.DELETE))
|
||||
deleteCart(item.getOrder());
|
||||
return true;
|
||||
}
|
||||
|
||||
private void deleteCart(int position) {
|
||||
//Remove item at List<Order> by position
|
||||
cart.remove(position);
|
||||
//Delete all old data from SQLite
|
||||
new Database(this).cleanCart();
|
||||
//Update new data from List<Order> to SQLite
|
||||
for(Order item: cart)
|
||||
new Database(this).addToCart(item);
|
||||
//Refresh
|
||||
loadListFood();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,16 @@
|
||||
package com.waterbase.foodify.Common;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
import com.waterbase.foodify.Model.User;
|
||||
|
||||
public class Common {
|
||||
public static User currentUser;
|
||||
|
||||
public static final String DELETE = "Xoá";
|
||||
|
||||
public static String convertCodeToStatus(String status) {
|
||||
if(status.equals("0"))
|
||||
return "Placed";
|
||||
@@ -13,4 +19,20 @@ public class Common {
|
||||
else
|
||||
return "Shipped";
|
||||
}
|
||||
|
||||
public static boolean isConnectedToInternet(Context context) {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
if(connectivityManager != null) {
|
||||
NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
|
||||
if(info != null){
|
||||
for (int i = 0; i < info.length; i++) {
|
||||
if(info[i].getState() == NetworkInfo.State.CONNECTED)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.google.firebase.database.DatabaseReference;
|
||||
import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.google.firebase.database.ValueEventListener;
|
||||
import com.squareup.picasso.Picasso;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Database.Database;
|
||||
import com.waterbase.foodify.Model.Food;
|
||||
import com.waterbase.foodify.Model.Order;
|
||||
@@ -81,7 +82,12 @@ public class FoodDetail extends AppCompatActivity {
|
||||
if(getIntent() != null)
|
||||
foodId = getIntent().getStringExtra("FoodId");
|
||||
if(!foodId.isEmpty()) {
|
||||
getDetailFood(foodId);
|
||||
if(Common.isConnectedToInternet(getBaseContext()))
|
||||
getDetailFood(foodId);
|
||||
else {
|
||||
Toast.makeText(FoodDetail.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setTitle("Chi tiết");
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.google.firebase.database.ValueEventListener;
|
||||
import com.mancj.materialsearchbar.MaterialSearchBar;
|
||||
import com.squareup.picasso.Picasso;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Interface.ItemClickListener;
|
||||
import com.waterbase.foodify.Model.Food;
|
||||
import com.waterbase.foodify.ViewHolder.FoodViewHolder;
|
||||
@@ -65,7 +66,12 @@ public class FoodList extends AppCompatActivity {
|
||||
categoryId = getIntent().getStringExtra("CategoryId");
|
||||
categoryName = getIntent().getStringExtra("CategoryName");
|
||||
if(!categoryId.isEmpty() && categoryId != null) {
|
||||
loadListFood(categoryId);
|
||||
if(Common.isConnectedToInternet(getBaseContext()))
|
||||
loadListFood(categoryId);
|
||||
else {
|
||||
Toast.makeText(FoodList.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//Search
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.ActionBarDrawerToggle;
|
||||
@@ -68,7 +69,13 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
layoutManager = new LinearLayoutManager(this);
|
||||
recyler_menu.setLayoutManager(layoutManager);
|
||||
|
||||
loadMenu();
|
||||
if(Common.isConnectedToInternet(getBaseContext()))
|
||||
loadMenu();
|
||||
else {
|
||||
Toast.makeText(this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//Set name for user
|
||||
View headerView = navigationView.getHeaderView(0);
|
||||
@@ -109,6 +116,14 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
if(item.getItemId() == R.id.refresh)
|
||||
loadMenu();
|
||||
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
||||
int id = item.getItemId();
|
||||
|
||||
@@ -48,46 +48,51 @@ public class SignIn extends AppCompatActivity {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
final ProgressDialog mDialog = new ProgressDialog(SignIn.this);
|
||||
mDialog.setMessage("Please waiting...");
|
||||
mDialog.show();
|
||||
if (Common.isConnectedToInternet(getBaseContext())) {
|
||||
final ProgressDialog mDialog = new ProgressDialog(SignIn.this);
|
||||
mDialog.setMessage("Please waiting...");
|
||||
mDialog.show();
|
||||
|
||||
table_user.addListenerForSingleValueEvent(new ValueEventListener() {
|
||||
table_user.addListenerForSingleValueEvent(new ValueEventListener() {
|
||||
|
||||
|
||||
@Override
|
||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||
//Check phone and password is null or not
|
||||
if(!edtPhone.getText().toString().isEmpty() && !edtPassword.getText().toString().isEmpty()){
|
||||
//Check if user not in database
|
||||
if(dataSnapshot.child(edtPhone.getText().toString()).exists()){
|
||||
//Get User information
|
||||
mDialog.dismiss();
|
||||
User user = dataSnapshot.child(edtPhone.getText().toString()).getValue(User.class);
|
||||
user.setPhone(edtPhone.getText().toString());
|
||||
if(user.getPassword().equals(edtPassword.getText().toString())){
|
||||
Intent homeIntent = new Intent(SignIn.this, Home.class);
|
||||
Common.currentUser = user;
|
||||
startActivity(homeIntent);
|
||||
finish();
|
||||
@Override
|
||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||
//Check phone and password is null or not
|
||||
if (!edtPhone.getText().toString().isEmpty() && !edtPassword.getText().toString().isEmpty()) {
|
||||
//Check if user not in database
|
||||
if (dataSnapshot.child(edtPhone.getText().toString()).exists()) {
|
||||
//Get User information
|
||||
mDialog.dismiss();
|
||||
User user = dataSnapshot.child(edtPhone.getText().toString()).getValue(User.class);
|
||||
user.setPhone(edtPhone.getText().toString());
|
||||
if (user.getPassword().equals(edtPassword.getText().toString())) {
|
||||
Intent homeIntent = new Intent(SignIn.this, Home.class);
|
||||
Common.currentUser = user;
|
||||
startActivity(homeIntent);
|
||||
finish();
|
||||
} else {
|
||||
Toast.makeText(SignIn.this, "Số điện thoại hoặc mật khẩu không đúng. Xin vui lòng thử lại!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(SignIn.this, "Số điện thoại hoặc mật khẩu không đúng. Xin vui lòng thử lại!", Toast.LENGTH_SHORT).show();
|
||||
mDialog.dismiss();
|
||||
Toast.makeText(SignIn.this, "Số điện thoại chưa được đăng ký. Vui lòng đăng ký để sử dụng!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
mDialog.dismiss();
|
||||
Toast.makeText(SignIn.this, "Số điện thoại chưa được đăng ký. Vui lòng đăng ký để sử dụng!", Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(SignIn.this, "Số điện thoại và mật khẩu không được để trống. Vui lòng thử lại!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else{
|
||||
mDialog.dismiss();
|
||||
Toast.makeText(SignIn.this, "Số điện thoại và mật khẩu không được để trống. Vui lòng thử lại!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Toast.makeText(SignIn.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import com.google.firebase.database.DatabaseError;
|
||||
import com.google.firebase.database.DatabaseReference;
|
||||
import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.google.firebase.database.ValueEventListener;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Model.User;
|
||||
|
||||
public class SignUp extends AppCompatActivity {
|
||||
@@ -48,35 +49,40 @@ public class SignUp extends AppCompatActivity {
|
||||
btnSignUp.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final ProgressDialog mDialog = new ProgressDialog(SignUp.this);
|
||||
mDialog.setMessage("Please waiting...");
|
||||
mDialog.show();
|
||||
if (Common.isConnectedToInternet(getBaseContext())) {
|
||||
final ProgressDialog mDialog = new ProgressDialog(SignUp.this);
|
||||
mDialog.setMessage("Please waiting...");
|
||||
mDialog.show();
|
||||
|
||||
table_user.addListenerForSingleValueEvent(new ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||
//Check if already user phone
|
||||
if(dataSnapshot.child(edtPhone.getText().toString()).exists()) {
|
||||
mDialog.dismiss();
|
||||
Toast.makeText(SignUp.this, "Phone Number is already register!", Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
table_user.addListenerForSingleValueEvent(new ValueEventListener() {
|
||||
@Override
|
||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||
//Check if already user phone
|
||||
if (dataSnapshot.child(edtPhone.getText().toString()).exists()) {
|
||||
mDialog.dismiss();
|
||||
Toast.makeText(SignUp.this, "Phone Number is already register!", Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
} else {
|
||||
mDialog.dismiss();
|
||||
User user = new User(edtName.getText().toString(), edtPassword.getText().toString());
|
||||
table_user.child(edtPhone.getText().toString()).setValue(user);
|
||||
Toast.makeText(SignUp.this, "Sign up successfully!", Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
}
|
||||
}
|
||||
else {
|
||||
mDialog.dismiss();
|
||||
User user = new User(edtName.getText().toString(), edtPassword.getText().toString());
|
||||
table_user.child(edtPhone.getText().toString()).setValue(user);
|
||||
Toast.makeText(SignUp.this, "Sign up successfully!", Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
Toast.makeText(SignUp.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void login(View view) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.waterbase.foodify.ViewHolder;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -12,6 +13,7 @@ import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.amulyakhare.textdrawable.TextDrawable;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Interface.ItemClickListener;
|
||||
import com.waterbase.foodify.Model.Order;
|
||||
import com.waterbase.foodify.R;
|
||||
@@ -23,7 +25,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener {
|
||||
|
||||
public TextView txt_card_item, txt_price;
|
||||
public ImageView img_cart_count;
|
||||
@@ -39,12 +41,19 @@ class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickList
|
||||
txt_card_item = (TextView) itemView.findViewById(R.id.cart_item_name);
|
||||
txt_price = (TextView) itemView.findViewById(R.id.cart_item_Price);
|
||||
img_cart_count = (ImageView) itemView.findViewById(R.id.cart_item_count);
|
||||
itemView.setOnCreateContextMenuListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateContextMenu(ContextMenu contextMenu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
||||
contextMenu.setHeaderTitle("Tuỳ chọn:");
|
||||
contextMenu.add(0, 0, getAdapterPosition(), Common.DELETE);
|
||||
}
|
||||
}
|
||||
|
||||
public class CartAdapter extends RecyclerView.Adapter<CartViewHolder>{
|
||||
|
||||
@@ -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="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
|
||||
</vector>
|
||||
@@ -1,4 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item
|
||||
android:id="@+id/refresh"
|
||||
android:icon="@drawable/ic_baseline_refresh_24"
|
||||
android:iconTint="@color/white"
|
||||
android:title="Refresh"
|
||||
/>
|
||||
</menu>
|
||||
@@ -33,7 +33,7 @@ public class Common {
|
||||
return RetrofitClient.getClient(baseUrl).create(IGeoCoordinates.class);
|
||||
}
|
||||
|
||||
public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
|
||||
public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
|
||||
Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
|
||||
|
||||
float scaleX = newWidth / (float)bitmap.getWidth();
|
||||
|
||||
Reference in New Issue
Block a user