Skip to content

Commit

Permalink
add product to cart
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenht65 committed Feb 21, 2022
1 parent 8519f25 commit 6917465
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 23 deletions.
4 changes: 3 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ public static String percentageNumber(int number) {
return result;
}

public static String numberLessThanTenFormat(int number) {
String result = "";
if (number < 10) {
result = "0" + number;
} else {
result = String.valueOf(number);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fu.prm391.sampl.project.model.order.add_to_cart;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class AddToCartRequest {

@SerializedName("productId")
@Expose
private int productId;

@SerializedName("quantity")
@Expose
private int quantity;

public AddToCartRequest() {
}

public AddToCartRequest(int productId, int quantity) {
this.productId = productId;
this.quantity = quantity;
}

public int getProductId() {
return productId;
}

public void setProductId(int productId) {
this.productId = productId;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fu.prm391.sampl.project.model.order.add_to_cart;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class AddToCartResponse {

@SerializedName("message")
@Expose
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package fu.prm391.sampl.project.remote.service;

import fu.prm391.sampl.project.model.order.OrderResponse;
import fu.prm391.sampl.project.model.order.add_to_cart.AddToCartRequest;
import fu.prm391.sampl.project.model.order.add_to_cart.AddToCartResponse;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;

public interface OrderService {

@GET("order/histories")
Call<OrderResponse> getOrdersHistory(@Header("Authorization") String token);

@POST("order/add")
Call<AddToCartResponse> addProductToCart(@Header("Authorization") String token, @Body AddToCartRequest addToCartRequest);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fu.prm391.sampl.project.view.product;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
Expand All @@ -9,35 +10,55 @@
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import fu.prm391.sampl.project.R;
import fu.prm391.sampl.project.adapter.product.ProductSimilarItemAdapter;
import fu.prm391.sampl.project.helper.PreferencesHelpers;
import fu.prm391.sampl.project.helper.StringHelpers;
import fu.prm391.sampl.project.model.category.Category;
import fu.prm391.sampl.project.model.order.add_to_cart.AddToCartRequest;
import fu.prm391.sampl.project.model.order.add_to_cart.AddToCartResponse;
import fu.prm391.sampl.project.model.product.Product;
import fu.prm391.sampl.project.model.product.get_list_product.ProductListResponse;
import fu.prm391.sampl.project.model.product.get_product_by_id.ProductResponse;
import fu.prm391.sampl.project.remote.ApiClient;
import fu.prm391.sampl.project.view.MainActivity;
import fu.prm391.sampl.project.view.account.Login;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SpecifyProduct extends AppCompatActivity {

private int productId;
private TextView productName, productPrice, productDescription, quantity, quantitySold, productCategory;
private TextView productName, productPrice, productDescription, quantity, quantitySold, productCategory, numberSelectedProduct;
private ImageView productImage;
private ImageView imageViewBack, imageViewFavorite;
private RecyclerView recyclerViewSimilarProduct;
private ConstraintLayout loadingConstraintLayout;
private Button btnAddToCart;
private ImageButton btnIncrease, btnDecrease;
private ImageView btnGoToCart;
private CardView cardNumberSelectedProduct;
private Product product;
private int numberProduct = 1;
private final int upperLimitNumberProduct = 15;
private final int lowerLimitNumberProduct = 1;
private String token;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -52,17 +73,22 @@ protected void onCreate(Bundle savedInstanceState) {
productCategory = findViewById(R.id.txtCategoryProduct);
loadingConstraintLayout = findViewById(R.id.loadingConstraintLayoutSpecifyProduct);
recyclerViewSimilarProduct = findViewById(R.id.recyclerViewSimilarProducts);
cardNumberSelectedProduct = findViewById(R.id.cardNumberSelectedProduct);

loadingConstraintLayout.setVisibility(View.VISIBLE);
cardNumberSelectedProduct.setVisibility(View.INVISIBLE);

token = PreferencesHelpers.loadStringData(SpecifyProduct.this, "token");

// load Api product
Intent intent = getIntent();
productId = intent.getIntExtra("productId", 0);
Call<ProductResponse> productResponseCall = ApiClient.getProductService().getProductByID(productId);
productResponseCall.enqueue(new Callback<ProductResponse>() {
@Override
public void onResponse(Call<ProductResponse> call, Response<ProductResponse> response) {
if (response.isSuccessful()) {
Product product = response.body().getData();
product = response.body().getData();
productName.setText(product.getName());
productPrice.setText(StringHelpers.currencyFormatterWithPercent(product.getPrice(), product.getDiscount()));
productDescription.setText(product.getDescription());
Expand Down Expand Up @@ -97,6 +123,7 @@ public void onResponse(Call<ProductListResponse> call, Response<ProductListRespo
recyclerViewSimilarProduct.setLayoutManager(layoutManager);

loadingConstraintLayout.setVisibility(View.GONE);
cardNumberSelectedProduct.setVisibility(View.VISIBLE);
}
}

Expand All @@ -113,6 +140,56 @@ public void onFailure(Call<ProductResponse> call, Throwable t) {
}
});

// adjust selected product
adjustNumberSelectedProduct();

btnAddToCart = findViewById(R.id.btnAddToCart);
btnAddToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if (token == "") {
finish();
startActivity(new Intent(SpecifyProduct.this, Login.class));
finish();
} else {
btnAddToCart.setEnabled(false);
if (product.getQuantity() <= 0) {
Toast.makeText(SpecifyProduct.this, "Out of stocks!", Toast.LENGTH_SHORT).show();
} else {
AddToCartRequest addToCartRequest = new AddToCartRequest();
addToCartRequest.setProductId(productId);
addToCartRequest.setQuantity(numberProduct);
Call<AddToCartResponse> addToCartResponseCall = ApiClient.getOrderService().addProductToCart("Bearer " + token, addToCartRequest);
addToCartResponseCall.enqueue(new Callback<AddToCartResponse>() {
@Override
public void onResponse(Call<AddToCartResponse> call, Response<AddToCartResponse> response) {
if (response.isSuccessful()) { //add to cart successful
AddToCartResponse addToCartResponse = response.body();
Toast.makeText(SpecifyProduct.this, addToCartResponse.getMessage(), Toast.LENGTH_SHORT).show();
// more action here


} else {
try {
JSONObject jsonObject = new JSONObject(response.errorBody().string());
Toast.makeText(SpecifyProduct.this, jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
} catch (JSONException | IOException e) {
Toast.makeText(SpecifyProduct.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
btnAddToCart.setEnabled(true);
}
}

@Override
public void onFailure(Call<AddToCartResponse> call, Throwable t) {
btnAddToCart.setEnabled(true);
}
});
}
}
}
});

imageViewBack = findViewById(R.id.imageViewBackProduct);
imageViewBack.setOnClickListener(new View.OnClickListener() {
Expand All @@ -128,5 +205,38 @@ public void onClick(View view) {
public void onClick(View view) {
}
});

}

private void adjustNumberSelectedProduct() {
numberSelectedProduct = findViewById(R.id.txtNumberSelectedProduct);
btnIncrease = findViewById(R.id.imageButtonIncreaseValue);
btnDecrease = findViewById(R.id.imageButtonDecreaseValue);

numberSelectedProduct.setText(StringHelpers.numberLessThanTenFormat(numberProduct));

btnIncrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (numberProduct < upperLimitNumberProduct && numberProduct < product.getQuantity()) {
numberProduct++;
numberSelectedProduct.setText(StringHelpers.numberLessThanTenFormat(numberProduct));
} else {
Toast.makeText(SpecifyProduct.this, "You can only select up to " + upperLimitNumberProduct + " products!", Toast.LENGTH_SHORT).show();
}
}
});

btnDecrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (numberProduct > lowerLimitNumberProduct) {
numberProduct--;
numberSelectedProduct.setText(StringHelpers.numberLessThanTenFormat(numberProduct));
} else {
Toast.makeText(SpecifyProduct.this, "You need to select at least " + lowerLimitNumberProduct + " product!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/icon_arrow_drop_down.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="40dp"
android:height="40dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="@color/black">
<path
android:fillColor="@android:color/white"
android:pathData="M7,10l5,5 5,-5z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/icon_arrow_drop_up.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="40dp"
android:height="40dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="@color/black">
<path
android:fillColor="@android:color/white"
android:pathData="M7,14l5,-5 5,5z"/>
</vector>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/icon_cart.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:width="28dp"
android:height="28dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="@color/black">
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/res/drawable/icon_favorite.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<vector android:height="24dp" android:tint="#FF0000"
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="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="30dp"
android:height="30dp"
android:tint="#FF0000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z" />
</vector>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/icon_favorite_border.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:width="30dp"
android:height="30dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="@color/black">
Expand Down
Loading

0 comments on commit 6917465

Please sign in to comment.