Add address method for user

This commit is contained in:
Nezumi
2023-03-18 12:37:15 +07:00
parent 893c1f232d
commit ac603fe941
11 changed files with 405 additions and 78 deletions
+12 -1
View File
@@ -1,6 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<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" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<targetSelectedWithDropDown>
<Target>
<type value="QUICK_BOOT_TARGET" />
@@ -12,6 +23,6 @@
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-03-13T15:02:48.647572900Z" />
<timeTargetWasSelectedWithDropDown value="2023-03-18T03:45:12.257058300Z" />
</component>
</project>
@@ -4,7 +4,9 @@ import static com.capstone.foodify.Common.TOKEN;
import androidx.annotation.NonNull;
import com.capstone.foodify.Model.Address;
import com.capstone.foodify.Model.Food;
import com.capstone.foodify.Model.Response.Addresses;
import com.capstone.foodify.Model.Response.CustomResponse;
import com.capstone.foodify.Model.Response.Foods;
import com.capstone.foodify.Model.User;
@@ -66,4 +68,7 @@ public interface FoodApiToken {
@GET("users/{userId}/loves/{productId}")
Call<CustomResponse> checkFoodIsFavorite(@Path("userId") int userId, @Path("productId") int productId);
@POST("users/{userId}/addresses")
Call<CustomResponse> createAddressUser(@Path("userId") int userId, @Body Address address);
}
@@ -1,6 +1,7 @@
package com.capstone.foodify.Activity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@@ -13,19 +14,23 @@ import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.capstone.foodify.API.FoodApi;
import com.capstone.foodify.API.FoodApiToken;
import com.capstone.foodify.Common;
import com.capstone.foodify.Model.Address;
import com.capstone.foodify.Adapter.AddressAdapter;
import com.capstone.foodify.Model.DistrictWardResponse;
import com.capstone.foodify.Model.Response.CustomResponse;
import com.capstone.foodify.R;
import com.google.android.material.textfield.TextInputLayout;
import com.sjapps.library.customdialog.CustomViewDialog;
import com.sjapps.library.customdialog.DialogButtonEvents;
import java.util.ArrayList;
import java.util.List;
@@ -36,16 +41,20 @@ import retrofit2.Response;
public class AddressManagerActivity extends AppCompatActivity {
private Address defaultAddress = null;
private RecyclerView recycler_view_address;
private AddressAdapter adapter;
private Button add_address_button;
private TextInputLayout textInput_address;
private ImageView back_image;
private TextView txt_Address, errorTextDistrictWard;
private List<Address> listAddress = new ArrayList<>();
Spinner wardSpinner, districtSpinner;
EditText edt_address;
ConstraintLayout progressLayout, progressLayoutDialog;
private static final ArrayList<String> wardList = new ArrayList<>();
private static final ArrayList<String> districtList = new ArrayList<>();
String address, ward, district;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -56,14 +65,16 @@ public class AddressManagerActivity extends AppCompatActivity {
recycler_view_address = findViewById(R.id.recycler_view_address);
add_address_button = findViewById(R.id.add_address_button);
back_image = findViewById(R.id.back_image);
txt_Address = findViewById(R.id.text_view_address);
progressLayout = findViewById(R.id.progress_layout);
//Set layout recyclerview
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recycler_view_address.setLayoutManager(linearLayoutManager);
adapter = new AddressAdapter();
adapter = new AddressAdapter(this);
getAddressList();
getDefaultAddress();
//Set event image view on click
back_image.setOnClickListener(new View.OnClickListener() {
@@ -83,25 +94,77 @@ public class AddressManagerActivity extends AppCompatActivity {
}
private void getAddressList(){
for(int i = 1; i <= 10; i++){
listAddress.add(new Address(i, "Hẻm 215, đường Phạm Như Xương", "phường Hoà Khánh Nam", "Liên Chiểu"));
}
listAddress.addAll(Common.CURRENT_USER.getAddresses());
adapter.setData(listAddress);
recycler_view_address.setAdapter(adapter);
progressLayout.setVisibility(View.GONE);
}
private void initData(){
//Create a full address string
String fullAddress = defaultAddress.getAddress() + ", " + defaultAddress.getWard() + ", " + defaultAddress.getDistrict();
//Bind data to component
txt_Address.setText(fullAddress);
}
private void getDefaultAddress(){
for(Address tempAddress: Common.CURRENT_USER.getAddresses()){
if(Common.CURRENT_USER.getDefaultAddress() == tempAddress.getId()){
defaultAddress = tempAddress;
initData();
getAddressList();
break;
}
}
if(defaultAddress == null){
progressLayout.setVisibility(View.GONE);
Toast.makeText(this, "Đã có lỗi từ hệ thống, vui lòng thử lại sau!", Toast.LENGTH_SHORT).show();
}
}
private void addAddress(String address, String ward, String district){
progressLayoutDialog.setVisibility(View.VISIBLE);
//Create object address
Address newAddress = new Address(address, ward, district);
FoodApiToken.apiService.createAddressUser(Common.CURRENT_USER.getId(), newAddress).enqueue(new Callback<CustomResponse>() {
@Override
public void onResponse(Call<CustomResponse> call, Response<CustomResponse> response) {
Toast.makeText(AddressManagerActivity.this, "Đã thêm địa chỉ thành công!", Toast.LENGTH_SHORT).show();
//Add address to list address at local storage
listAddress.add(newAddress);
adapter.notifyDataSetChanged();
progressLayoutDialog.setVisibility(View.GONE);
}
@Override
public void onFailure(Call<CustomResponse> call, Throwable t) {
Toast.makeText(AddressManagerActivity.this, "Đã có lỗi từ hệ thống, vui lòng thử lại sau!", Toast.LENGTH_SHORT).show();
}
});
}
private void showAddAddressDialog(){
View view = LayoutInflater.from(this).inflate(R.layout.add_address_dialog,null);
//Init component
TextInputLayout textInputLayout = view.findViewById(R.id.textInput_address);
textInput_address = view.findViewById(R.id.textInput_address);
wardSpinner = view.findViewById(R.id.ward);
districtSpinner = view.findViewById(R.id.district);
edt_address = view.findViewById(R.id.edt_address);
errorTextDistrictWard = view.findViewById(R.id.errorTextDistrictWard);
progressLayoutDialog = view.findViewById(R.id.progress_layout);
//Set font
textInputLayout.setTypeface(Common.setFontOpenSans(getAssets()));
textInput_address.setTypeface(Common.setFontOpenSans(getAssets()));
//Load district and ward
if(districtList.size() == 0){
@@ -135,18 +198,63 @@ public class AddressManagerActivity extends AppCompatActivity {
//Add custom view
customViewDialog.addCustomView(view);
//Set left button text
customViewDialog.setLeftButtonText("Trở về");
customViewDialog.setLeftButtonText("Thêm");
//Set right button text
customViewDialog.setRightButtonText("Thêm");
customViewDialog.setRightButtonText("Trở về");
//Set color for left button
customViewDialog.setLeftButtonColor(getResources().getColor(R.color.gray, null));
customViewDialog.setLeftButtonColor(getResources().getColor(R.color.primaryColor, null));
//Set color for right button
customViewDialog.setRightButtonColor(getResources().getColor(R.color.primaryColor, null));
customViewDialog.setRightButtonColor(getResources().getColor(R.color.gray, null));
//Set event button
customViewDialog.onButtonClick(new DialogButtonEvents() {
@Override
public void onLeftButtonClick() {
//Get data
address = edt_address.getText().toString();
ward = wardSpinner.getSelectedItem().toString();
district = districtSpinner.getSelectedItem().toString();
if(validData(address, ward, district)){
addAddress(address, ward, district);
customViewDialog.dismiss();
}
}
@Override
public void onRightButtonClick() {
customViewDialog.dismiss();
}
});
customViewDialog.show();
}
private boolean validData(String address, String ward, String district){
//Valid data
boolean dataHasValidate = true;
//Check address
if(address.length() < 8) {
textInput_address.setError("Trường địa chỉ của bạn tối thiếu từ 8 ký tự trở lên");
dataHasValidate = false;
} else {
textInput_address.setErrorEnabled(false);
}
//Check district and ward
if(district.equals("---Quận") || ward.equals("---Phường")){
errorTextDistrictWard.setVisibility(View.VISIBLE);
dataHasValidate = false;
} else {
errorTextDistrictWard.setVisibility(View.GONE);
}
return dataHasValidate;
}
private void getListDistrict() {
districtList.add("---Quận");
@@ -264,7 +264,7 @@ public class SignUpActivity extends AppCompatActivity {
textInput_address.setErrorEnabled(false);
}
//Check district
//Check district and ward
if(district.equals("---Quận") || ward.equals("---Phường")){
errorTextDistrictWard.setVisibility(View.VISIBLE);
@@ -1,25 +1,36 @@
package com.capstone.foodify.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import com.capstone.foodify.Common;
import com.capstone.foodify.Model.Address;
import com.capstone.foodify.R;
import com.google.android.material.textfield.TextInputLayout;
import com.sjapps.library.customdialog.CustomViewDialog;
import com.sjapps.library.customdialog.DialogButtonEvents;
import java.util.List;
public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressViewHolder>{
private List<Address> listAddress;
private Context context;
public AddressAdapter() {
public AddressAdapter(Context context) {
this.context = context;
}
public void setData(List<Address> listAddress){
@@ -46,6 +57,12 @@ public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressV
//Bind data to component
holder.text_view_address.setText(fullAddress);
holder.edit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAddAddressDialog();
}
});
}
@@ -56,6 +73,85 @@ public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressV
return 0;
}
private void showAddAddressDialog(){
View view = LayoutInflater.from(context).inflate(R.layout.add_address_dialog,null);
//Init component
TextInputLayout textInput_address = view.findViewById(R.id.textInput_address);
Spinner wardSpinner = view.findViewById(R.id.ward);
Spinner districtSpinner = view.findViewById(R.id.district);
EditText edt_address = view.findViewById(R.id.edt_address);
TextView errorTextDistrictWard = view.findViewById(R.id.errorTextDistrictWard);
ConstraintLayout progressLayoutDialog = view.findViewById(R.id.progress_layout);
//Set font
// textInput_address.setTypeface(Common.setFontOpenSans(getAssets()));
//Load district and ward
// if(districtList.size() == 0){
// getListDistrict();
// } else {
// setAdapter(districtList, "---Quận", districtSpinner);
// }
// getListWard(0);
//
// //Set event when click on district spinner
// districtSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
// @Override
// public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// getListWard(i);
// }
//
// @Override
// public void onNothingSelected(AdapterView<?> adapterView) {
//
// }
// });
CustomViewDialog customViewDialog = new CustomViewDialog();
//Create dialog
customViewDialog.Builder(context);
//Set title
customViewDialog.setTitle("Thêm địa chỉ");
//Create dialog width two buttons
customViewDialog.dialogWithTwoButtons();
//Add custom view
customViewDialog.addCustomView(view);
//Set left button text
customViewDialog.setLeftButtonText("Thêm");
//Set right button text
customViewDialog.setRightButtonText("Trở về");
//Set color for left button
// customViewDialog.setLeftButtonColor(getResources().getColor(R.color.primaryColor, null));
// //Set color for right button
// customViewDialog.setRightButtonColor(getResources().getColor(R.color.gray, null));
//Set event button
customViewDialog.onButtonClick(new DialogButtonEvents() {
@Override
public void onLeftButtonClick() {
//Get data
// address = edt_address.getText().toString();
// ward = wardSpinner.getSelectedItem().toString();
// district = districtSpinner.getSelectedItem().toString();
//
// if(validData(address, ward, district)){
// addAddress(address, ward, district);
// customViewDialog.dismiss();
// }
}
@Override
public void onRightButtonClick() {
customViewDialog.dismiss();
}
});
customViewDialog.show();
}
public class AddressViewHolder extends RecyclerView.ViewHolder{
private TextView text_view_address, default_text_view, blank_text_view;
@@ -3,6 +3,7 @@ package com.capstone.foodify;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
@@ -13,9 +14,11 @@ import android.os.Build;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import com.capstone.foodify.Activity.MainActivity;
import com.capstone.foodify.Model.Basket;
import com.capstone.foodify.Model.Food;
import com.capstone.foodify.Model.User;
import com.google.firebase.auth.FirebaseAuth;
import com.saadahmedsoft.popupdialog.PopupDialog;
import com.saadahmedsoft.popupdialog.Styles;
import com.saadahmedsoft.popupdialog.listener.OnDialogButtonClickListener;
@@ -29,6 +32,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import io.paperdb.Paper;
public class Common {
public static final String FORMAT_DATE="dd-MM-yyyy";
public static final String VALID_EMAIL_ADDRESS_REGEX = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$";
@@ -14,13 +14,6 @@ public class Address implements Serializable {
this.district = district;
}
public Address(int id, String address, String ward, String district) {
this.id = id;
this.address = address;
this.ward = ward;
this.district = district;
}
public int getId() {
return id;
}
@@ -0,0 +1,27 @@
package com.capstone.foodify.Model.Response;
import com.capstone.foodify.Model.Address;
import com.capstone.foodify.Model.Page;
import java.util.List;
public class Addresses {
private List<Address> addresses;
private Page page;
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
@@ -8,7 +8,12 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<ImageView
android:id="@+id/back_image"
@@ -67,7 +72,9 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<TextView
android:id="@+id/default_text_view"
@@ -145,9 +152,15 @@
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constrainedHeight="true"
app:layout_constraintHeight_max="wrap"
app:layout_constraintHeight_percent="0.6"
app:layout_constraintVertical_bias="0"
app:layout_constraintTop_toBottomOf="@id/address_default"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/add_address_button"
/>
<Button
@@ -157,15 +170,32 @@
android:layout_height="wrap_content"
android:layout_marginStart="70dp"
android:layout_marginEnd="70dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
app:layout_constraintTop_toBottomOf="@id/recycler_view_address"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:id="@+id/progress_layout"
>
<ProgressBar
android:id="@+id/progressBarLoadData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="@color/primaryColor"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
+51 -1
View File
@@ -6,10 +6,18 @@
android:background="@drawable/bg_white_corner_16"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInput_address"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Địa chỉ"
app:boxBackgroundColor="@color/white"
@@ -62,4 +70,46 @@
</LinearLayout>
<TextView
android:id="@+id/errorTextDistrictWard"
android:text="Bạn chưa chọn quận huyện!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:textSize="12sp"
android:textColor="#b00020"
app:layout_constraintTop_toBottomOf="@id/linearLayout_district_ward"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:visibility="gone"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#80000000"
android:id="@+id/progress_layout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:clickable="true"
android:visibility="gone"
>
<ProgressBar
android:id="@+id/progressBarLoadData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="@color/primaryColor"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
+3 -1
View File
@@ -35,7 +35,9 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<TextView
android:id="@+id/blank_text_view"