Add ship to default address

This commit is contained in:
phanhuuloi27
2022-06-25 19:57:36 +07:00
parent b097091847
commit 4493374747
7 changed files with 196 additions and 91 deletions
@@ -122,16 +122,15 @@ public class Cart extends AppCompatActivity implements GoogleApiClient.Connectio
setContentView(R.layout.activity_cart); setContentView(R.layout.activity_cart);
//Runtime permission //Runtime permission
if(checkLocatePermission()) if (checkLocatePermission()) {
{
ActivityCompat.requestPermissions(this, new String[] ActivityCompat.requestPermissions(this, new String[]
{ {
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION Manifest.permission.ACCESS_FINE_LOCATION
}, LOCATION_REQUEST_CODE }, LOCATION_REQUEST_CODE
); );
} else { } else {
if(checkPlayServices()) { if (checkPlayServices()) {
buildGoogleApiClient(); buildGoogleApiClient();
createLocationRequest(); createLocationRequest();
} }
@@ -178,10 +177,9 @@ public class Cart extends AppCompatActivity implements GoogleApiClient.Connectio
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) { switch (requestCode) {
case LOCATION_REQUEST_CODE: case LOCATION_REQUEST_CODE: {
{ if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (checkPlayServices()) {
if(checkPlayServices()) {
buildGoogleApiClient(); buildGoogleApiClient();
createLocationRequest(); createLocationRequest();
} }
@@ -210,11 +208,10 @@ public class Cart extends AppCompatActivity implements GoogleApiClient.Connectio
private boolean checkPlayServices() { private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(resultCode != ConnectionResult.SUCCESS) { if (resultCode != ConnectionResult.SUCCESS) {
if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)) if (GooglePlayServicesUtil.isUserRecoverableError(resultCode))
GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_REQUEST).show(); GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_REQUEST).show();
else else {
{
Toast.makeText(this, "Thiết bị này chưa được hỗ trợ!", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Thiết bị này chưa được hỗ trợ!", Toast.LENGTH_SHORT).show();
finish(); finish();
} }
@@ -236,12 +233,27 @@ public class Cart extends AppCompatActivity implements GoogleApiClient.Connectio
//Radio //Radio
RadioButton rdiShipToAddress = (RadioButton) order_address_comment.findViewById(R.id.rdiShipToAddress); RadioButton rdiShipToAddress = order_address_comment.findViewById(R.id.rdiShipToAddress);
RadioButton rdiAddress = (RadioButton) order_address_comment.findViewById(R.id.rdiAdress); RadioButton rdiAddress = order_address_comment.findViewById(R.id.rdiAddress);
RadioButton rdiHomeAddress = order_address_comment.findViewById(R.id.rdiHomeAddress);
rdiHomeAddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if(!TextUtils.isEmpty(Common.currentUser.getHomeAddress()) || Common.currentUser.getHomeAddress() != null) {
edtAddress.setText(Common.currentUser.getHomeAddress());
} else
Toast.makeText(Cart.this, "Vui lòng điền địa chỉ mặc định của bạn!", Toast.LENGTH_SHORT).show();
}
}
});
rdiAddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { rdiAddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
edtAddress.setText(""); edtAddress.setText("");
} }
}); });
@@ -250,35 +262,33 @@ public class Cart extends AppCompatActivity implements GoogleApiClient.Connectio
rdiShipToAddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { rdiShipToAddress.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(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",
mGoogleMapService.getAddressName(String.format("https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&key=AIzaSyCqmoLlawoQzmwjZI2_Yo_V33An_nNZADs", mLastLocation.getLatitude(),
mLastLocation.getLatitude(), mLastLocation.getLongitude()))
mLastLocation.getLongitude())) .enqueue(new Callback<String>() {
.enqueue(new Callback<String>() { @Override
@Override public void onResponse(Call<String> call, Response<String> response) {
public void onResponse(Call<String> call, Response<String> response) { try {
try { JSONObject jsonObject = new JSONObject(response.body());
JSONObject jsonObject = new JSONObject(response.body());
JSONArray resultsArray = jsonObject.getJSONArray("results"); JSONArray resultsArray = jsonObject.getJSONArray("results");
JSONObject firstObject = resultsArray.getJSONObject(0); JSONObject firstObject = resultsArray.getJSONObject(0);
address = firstObject.getString("formatted_address"); address = firstObject.getString("formatted_address");
edtAddress.setText(address); edtAddress.setText(address);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
}
} }
}
@Override @Override
public void onFailure(Call<String> call, Throwable t) { public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(Cart.this, "Error to get location: " + t.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(Cart.this, "Error to get location: " + t.getMessage(), Toast.LENGTH_SHORT).show();
Log.i("LOCATION", "" + t.getMessage()); Log.i("LOCATION", "" + t.getMessage());
} }
}); });
}
} }
} }
}); });
@@ -291,7 +301,7 @@ public class Cart extends AppCompatActivity implements GoogleApiClient.Connectio
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if(TextUtils.isEmpty(address)){ if (TextUtils.isEmpty(edtAddress.getText().toString())) {
Toast.makeText(Cart.this, "Vui lòng nhập địa chỉ giao hàng!", Toast.LENGTH_SHORT).show(); Toast.makeText(Cart.this, "Vui lòng nhập địa chỉ giao hàng!", Toast.LENGTH_SHORT).show();
return; return;
} }
@@ -429,20 +439,19 @@ public class Cart extends AppCompatActivity implements GoogleApiClient.Connectio
} }
private void startLocationUpdates() { private void startLocationUpdates() {
if(checkLocatePermission()){ if (checkLocatePermission()) {
return; return;
} }
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} }
private void displayLocation() { private void displayLocation() {
if(checkLocatePermission()) if (checkLocatePermission())
return; return;
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation != null) { if (mLastLocation != null) {
Log.d("LOCATION", "Your location: " + mLastLocation.getLatitude() + ", " + mLastLocation.getLongitude()); Log.d("LOCATION", "Your location: " + mLastLocation.getLatitude() + ", " + mLastLocation.getLongitude());
} } else {
else {
Log.d("LOCATION", "Could not get your location!"); Log.d("LOCATION", "Could not get your location!");
} }
} }
@@ -234,6 +234,8 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
startActivity(orderIntent); startActivity(orderIntent);
} else if (id == R.id.nav_change_pwd) { } else if (id == R.id.nav_change_pwd) {
showChangePasswordDialog(); showChangePasswordDialog();
} else if (id == R.id.nav_home_address) {
showHomeAddressDialog();
} }
else if (id == R.id.nav_log_out) { else if (id == R.id.nav_log_out) {
@@ -253,6 +255,41 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
return true; return true;
} }
private void showHomeAddressDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Home.this);
alertDialog.setTitle("Đặt địa chỉ mặc định");
alertDialog.setMessage("Vui lòng điền đầy đủ thông tin");
LayoutInflater inflater = LayoutInflater.from(this);
View layout_home_address = inflater.inflate(R.layout.home_address_layout, null);
final MaterialEditText edtHomeAddress = layout_home_address.findViewById(R.id.edtHomeAddress);
alertDialog.setView(layout_home_address);
alertDialog.setPositiveButton("Cập nhật", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//Set new Home Address
Common.currentUser.setHomeAddress(edtHomeAddress.getText().toString());
FirebaseDatabase.getInstance().getReference("User")
.child(Common.currentUser.getPhone())
.setValue(Common.currentUser)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(Home.this, "Cập nhật địa chỉ thành công!", Toast.LENGTH_SHORT).show();
}
});
}
});
alertDialog.show();
}
private void showChangePasswordDialog() { private void showChangePasswordDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Home.this); AlertDialog.Builder alertDialog = new AlertDialog.Builder(Home.this);
alertDialog.setTitle("Đổi mật khẩu"); alertDialog.setTitle("Đổi mật khẩu");
@@ -8,6 +8,7 @@ public class User implements Serializable {
private String Password; private String Password;
private String Phone; private String Phone;
private String IsStaff; private String IsStaff;
private String homeAddress;
public User(){} public User(){}
@@ -48,4 +49,12 @@ public class User implements Serializable {
public void setPhone(String phone) { public void setPhone(String phone) {
Phone = phone; Phone = phone;
} }
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
} }
@@ -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="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z"/>
</vector>
@@ -0,0 +1,32 @@
<?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="match_parent"
app:cardElevation="10dp"
>
<LinearLayout
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/edtHomeAddress"
android:hint="Home Address"
android:inputType="textMultiLine"
android:textColor="@color/purple_500"
android:textColorHint="@color/purple_500"
android:textSize="24sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:met_baseColor="@color/purple_500"
app:met_floatingLabel="highlight"
app:met_primaryColor="@color/purple_500"
app:met_singleLineEllipsis="true"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
@@ -9,67 +9,74 @@
app:cardElevation="4dp" app:cardElevation="4dp"
> >
<LinearLayout <LinearLayout
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/edtAddress"
android:hint="Địa chỉ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorPrimary"
android:text=""
android:textColor="@color/colorPrimary"
android:textSize="34sp"
android:inputType="text"
app:met_baseColor="@color/colorPrimary"
app:met_floatingLabel="highlight"
app:met_primaryColor="@color/colorPrimary"
app:met_singleLineEllipsis="true"
/>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/edtComment"
android:hint="Ghi chú"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorPrimary"
android:text=""
android:textColor="@color/colorPrimary"
android:textSize="24sp"
android:inputType="textMultiLine"
app:met_baseColor="@color/colorPrimary"
app:met_floatingLabel="highlight"
app:met_primaryColor="@color/colorPrimary"
app:met_singleLineEllipsis="true"
/>
<RadioGroup
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="16dp"
> >
<com.rengwuxian.materialedittext.MaterialEditText <RadioButton
android:id="@+id/edtAddress" android:id="@+id/rdiAddress"
android:hint="Địa chỉ" android:text="Giao hàng đến địa chỉ trên"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textColorHint="@color/colorPrimary" android:checked="true"
android:text=""
android:textColor="@color/colorPrimary"
android:textSize="34sp"
android:inputType="text"
app:met_baseColor="@color/colorPrimary"
app:met_floatingLabel="highlight"
app:met_primaryColor="@color/colorPrimary"
app:met_singleLineEllipsis="true"
/> />
<com.rengwuxian.materialedittext.MaterialEditText <RadioButton
android:id="@+id/edtComment" android:id="@+id/rdiShipToAddress"
android:hint="Ghi chú" android:text="Giao hàng đến vị trí của bạn"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textColorHint="@color/colorPrimary"
android:text=""
android:textColor="@color/colorPrimary"
android:textSize="24sp"
android:inputType="textMultiLine"
app:met_baseColor="@color/colorPrimary"
app:met_floatingLabel="highlight"
app:met_primaryColor="@color/colorPrimary"
app:met_singleLineEllipsis="true"
/> />
<RadioGroup <RadioButton
android:id="@+id/rdiHomeAddress"
android:text="Giao hàng đến địa chỉ mặc định"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
> />
<RadioButton
android:id="@+id/rdiShipToAddress"
android:text="Giao hàng đến vị trí của bạn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/rdiAdress"
android:text="Giao hàng đến địa chỉ trên"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
/>
</RadioGroup> </RadioGroup>
</LinearLayout> </LinearLayout>
</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>
@@ -4,6 +4,12 @@
tools:showIn="navigation_view"> tools:showIn="navigation_view">
<group android:checkableBehavior="single"> <group android:checkableBehavior="single">
<item
android:id="@+id/nav_home_address"
android:icon="@drawable/ic_baseline_home_24"
android:iconTint="@color/white"
android:title="Địa chỉ mặc định"
/>
<item <item
android:id="@+id/nav_cart" android:id="@+id/nav_cart"
android:icon="@drawable/ic_baseline_shopping_cart_24" android:icon="@drawable/ic_baseline_shopping_cart_24"