package com.waterbase.foodify; import android.Manifest; import android.content.Context; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; 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.rengwuxian.materialedittext.MaterialEditText; import com.waterbase.foodify.Common.Common; import com.waterbase.foodify.Database.Database; import com.waterbase.foodify.Model.MyResponse; import com.waterbase.foodify.Model.Notification; import com.waterbase.foodify.Model.Order; import com.waterbase.foodify.Model.Request; import com.waterbase.foodify.Model.Sender; import com.waterbase.foodify.Model.Token; import com.waterbase.foodify.Remote.APIService; import com.waterbase.foodify.Remote.IGoogleService; import com.waterbase.foodify.ViewHolder.CartAdapter; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; import java.util.Locale; import info.hoang8f.widget.FButton; import io.github.inflationx.calligraphy3.CalligraphyConfig; import io.github.inflationx.calligraphy3.CalligraphyInterceptor; import io.github.inflationx.viewpump.ViewPump; import io.github.inflationx.viewpump.ViewPumpContextWrapper; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; public class Cart extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { String address = null; RecyclerView recyclerView; RecyclerView.LayoutManager layoutManager; FirebaseDatabase database; DatabaseReference requests; public TextView txtTotalPrice; FButton btnPlace; List cart = new ArrayList<>(); CartAdapter adapter; APIService mService; //Location private LocationRequest mLocationRequest; private GoogleApiClient mGoogleApiClient; private Location mLastLocation; private static int UPDATE_INTERVAL = 5000; private static int FATEST_INTERVAL = 3000; private static int DISPLACEMENT = 10; private static final int LOCATION_REQUEST_CODE = 9999; private static final int PLAY_SERVICES_REQUEST = 9997; //Declare Google Map API Retrofit IGoogleService mGoogleMapService; @Override protected void attachBaseContext(Context newBase) { super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase)); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ViewPump.init(ViewPump.builder() .addInterceptor(new CalligraphyInterceptor( new CalligraphyConfig.Builder() .setDefaultFontPath("fonts/font.ttf") .setFontAttrId(io.github.inflationx.calligraphy3.R.attr.fontPath) .build())) .build()); setContentView(R.layout.activity_cart); //Runtime permission if(checkLocatePermission()) { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION }, LOCATION_REQUEST_CODE ); } else { if(checkPlayServices()) { buildGoogleApiClient(); createLocationRequest(); } } //Init mService = Common.getFCMService(); mGoogleMapService = Common.getGoogleMapAPI(); //Firebase database = FirebaseDatabase.getInstance(); requests = database.getReference("Requests"); //Init recyclerView = (RecyclerView) findViewById(R.id.listCart); recyclerView.setHasFixedSize(true); layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); txtTotalPrice = (TextView) findViewById(R.id.total); btnPlace = (FButton) findViewById(R.id.btnPlaceOrder); btnPlace.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cart.size() > 0) showAlertDialog(); else Toast.makeText(Cart.this, "Giỏ hàng của bạn đang trống!!!", Toast.LENGTH_SHORT).show(); } }); loadListFood(); setTitle("Giỏ hàng"); // calling the action bar ActionBar actionBar = getSupportActionBar(); // showing the back button in action bar actionBar.setDisplayHomeAsUpEnabled(true); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case LOCATION_REQUEST_CODE: { if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if(checkPlayServices()) { buildGoogleApiClient(); createLocationRequest(); } } } } } private void createLocationRequest() { mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(UPDATE_INTERVAL); mLocationRequest.setFastestInterval(FATEST_INTERVAL); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); mLocationRequest.setSmallestDisplacement(DISPLACEMENT); } private synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API).build(); mGoogleApiClient.connect(); } private boolean checkPlayServices() { int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if(resultCode != ConnectionResult.SUCCESS) { if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)) GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_REQUEST).show(); else { Toast.makeText(this, "Thiết bị này chưa được hỗ trợ!", Toast.LENGTH_SHORT).show(); finish(); } return false; } return true; } private void showAlertDialog() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this); alertDialog.setTitle("Còn 1 bước nữa!"); alertDialog.setMessage("Xin vui lòng điền thông tin dưới đây: "); LayoutInflater inflater = this.getLayoutInflater(); View order_address_comment = inflater.inflate(R.layout.order_address_comment, null); MaterialEditText edtAddress = (MaterialEditText) order_address_comment.findViewById(R.id.edtAddress); MaterialEditText edtComment = (MaterialEditText) order_address_comment.findViewById(R.id.edtComment); //Radio RadioButton rdiShipToAddress = (RadioButton) order_address_comment.findViewById(R.id.rdiShipToAddress); RadioButton rdiAddress = (RadioButton) order_address_comment.findViewById(R.id.rdiAdress); rdiAddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { edtAddress.setText(""); } }); //Add event rdiShipToAddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { if(isChecked){ mGoogleMapService.getAddressName(String.format("https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&key=AIzaSyCqmoLlawoQzmwjZI2_Yo_V33An_nNZADs", mLastLocation.getLatitude(), mLastLocation.getLongitude())) .enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { try { JSONObject jsonObject = new JSONObject(response.body()); JSONArray resultsArray = jsonObject.getJSONArray("results"); JSONObject firstObject = resultsArray.getJSONObject(0); address = firstObject.getString("formatted_address"); edtAddress.setText(address); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(Call call, Throwable t) { Toast.makeText(Cart.this, "Error to get location: " + t.getMessage(), Toast.LENGTH_SHORT).show(); Log.i("LOCATION", "" + t.getMessage()); } }); } } } }); alertDialog.setView(order_address_comment); alertDialog.setIcon(R.drawable.ic_baseline_shopping_cart_24); alertDialog.setPositiveButton("Đặt", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(TextUtils.isEmpty(address)){ Toast.makeText(Cart.this, "Vui lòng nhập địa chỉ giao hàng!", Toast.LENGTH_SHORT).show(); return; } //Create new Request Request request = new Request( Common.currentUser.getPhone(), Common.currentUser.getName(), edtAddress.getText().toString(), txtTotalPrice.getText().toString(), "0", edtComment.getText().toString(), cart ); //Summit to Firebase //We will using System.CurrentMilli to key String order_number = String.valueOf(System.currentTimeMillis()); requests.child(order_number).setValue(request); sendNotificationOrder(order_number); //Delete Cart new Database(getBaseContext()).cleanCart(); Toast.makeText(Cart.this, "Đặt hàng thành công!", Toast.LENGTH_SHORT).show(); finish(); } }); alertDialog.setNegativeButton("Thoát", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.show(); } private void sendNotificationOrder(String order_number) { DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Tokens"); Query data = tokens.orderByChild("serverToken").equalTo(true); //Get all node with isServerToken is true; data.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot postSnapShot : dataSnapshot.getChildren()) { Token serverToken = postSnapShot.getValue(Token.class); //Create raw payload to send Notification notification = new Notification("Foodify", "Bạn có 1 đơn hàng mới " + order_number); Sender content = new Sender(serverToken.getToken(), notification); mService.sendNotification(content) .enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { //Only run when get result if (response.code() == 200) { if (response.body().success == 1) { new Database(getBaseContext()).cleanCart(); Toast.makeText(Cart.this, "Đặt hàng thành công!", Toast.LENGTH_SHORT).show(); finish(); } else { Toast.makeText(Cart.this, "Hệ thống bị lỗi. Vui lòng thử lại sau!", Toast.LENGTH_SHORT).show(); } } } @Override public void onFailure(Call call, Throwable t) { Log.e("ERROR", t.getMessage()); } }); } } @Override public void onCancelled(DatabaseError databaseError) { } }); } private void loadListFood() { cart = new Database(this).getCarts(); adapter = new CartAdapter(cart, this); adapter.notifyDataSetChanged(); recyclerView.setAdapter(adapter); //Calculate total price float total = 0; for (Order order : cart) total += (Float.parseFloat(order.getPrice())) * (Float.parseFloat(order.getQuantity())); Locale locale = new Locale("vi", "VN"); NumberFormat fmt = NumberFormat.getCurrencyInstance(locale); txtTotalPrice.setText(fmt.format(total)); } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case android.R.id.home: this.finish(); return true; } 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 by position cart.remove(position); //Delete all old data from SQLite new Database(this).cleanCart(); //Update new data from List to SQLite for (Order item : cart) new Database(this).addToCart(item); //Refresh loadListFood(); } @Override public void onConnected(@Nullable Bundle bundle) { displayLocation(); startLocationUpdates(); } private void startLocationUpdates() { if(checkLocatePermission()){ return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } private void displayLocation() { if(checkLocatePermission()) return; mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLastLocation != null) { Log.d("LOCATION", "Your location: " + mLastLocation.getLatitude() + ", " + mLastLocation.getLongitude()); } else { Log.d("LOCATION", "Could not get your location!"); } } @Override public void onConnectionSuspended(int i) { mGoogleApiClient.connect(); } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { } private boolean checkLocatePermission() { return ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED; } @Override public void onLocationChanged(Location location) { mLastLocation = location; displayLocation(); } }