Commit 702a1f58 authored by Daniel Cesanelli's avatar Daniel Cesanelli
Browse files

Modificadas vistas para usar variables

Agregado incrementar/decrementar cantidad en carrito
parent e54a0e15
......@@ -7,6 +7,7 @@ import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
......@@ -31,6 +32,7 @@ public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private SharedPreferences sharedpreferences;
private NavigationView navigationView;
private NavController navController;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -68,7 +70,7 @@ public class MainActivity extends AppCompatActivity {
}
});
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
checkToken();
......@@ -114,9 +116,13 @@ public class MainActivity extends AppCompatActivity {
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return NavigationUI.onNavDestinationSelected(item, navController) || super.onOptionsItemSelected(item);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
......
package com.cesanelli.lajusta.io.models;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.widget.ImageView;
import androidx.databinding.BindingAdapter;
public class Product {
private int id;
private String title;
......@@ -100,5 +107,16 @@ public class Product {
public void setUnitQuantity(int unitQuantity) {
this.unitQuantity = unitQuantity;
}
@BindingAdapter("android:productImage")
public static void loadImage(ImageView imageView, Image[] images){
if (images.length > 0) {
String cleanImage = images[0].getValue().replace("data:image/png;base64,", "").replace("data:image/jpeg;base64,", "");
byte[] decodedString = Base64.decode(cleanImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
imageView.setImageBitmap(decodedByte);
}
}
}
......@@ -5,7 +5,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
......@@ -17,15 +16,12 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.cesanelli.lajusta.R;
import com.cesanelli.lajusta.io.models.Cart;
import com.cesanelli.lajusta.databinding.FragmentCartListBinding;
import com.cesanelli.lajusta.io.models.CartItem;
import com.cesanelli.lajusta.io.models.Product;
import com.cesanelli.lajusta.ui.viewmodel.CartViewModel;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class CartFragment extends Fragment implements CartRecyclerViewAdapter.OnCartListener{
public class CartFragment extends Fragment implements CartRecyclerViewAdapter.OnCartListener {
private FragmentCartListBinding fragmentCartListBinding;
private CartViewModel cartViewModel;
public CartFragment() {
......@@ -45,15 +41,15 @@ public class CartFragment extends Fragment implements CartRecyclerViewAdapter.On
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_cart_list, container, false);
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
fragmentCartListBinding = FragmentCartListBinding.inflate(layoutInflater, container, false);
return fragmentCartListBinding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnComprar = view.findViewById(R.id.btnBuy);
TextView txtQuantity = view.findViewById(R.id.txtQuantity);
TextView txtTotal = view.findViewById(R.id.txtTotal);
cartViewModel = new ViewModelProvider(getActivity()).get(CartViewModel.class);
cartViewModel.getCart().observe(getActivity(), cart -> {
......@@ -61,9 +57,7 @@ public class CartFragment extends Fragment implements CartRecyclerViewAdapter.On
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.cartList);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(new CartRecyclerViewAdapter(cart, this));
txtQuantity.setText(cart.getQuantity().toString());
DecimalFormat decimalFormat = new DecimalFormat("#.00");
txtTotal.setText(decimalFormat.format(cart.getTotal()));
fragmentCartListBinding.setCart(cart);
}
});
......@@ -75,11 +69,20 @@ public class CartFragment extends Fragment implements CartRecyclerViewAdapter.On
});
}
@Override
public void onProductRemove(CartItem cartItem) {
cartViewModel.deleteProduct(cartItem.getProduct().getId());
Toast.makeText(getContext(),"Se eliminó " + cartItem.getProduct().getTitle() + " al carrito.",Toast.LENGTH_SHORT).show();
Toast.makeText(getContext(), "Se eliminó " + cartItem.getProduct().getTitle() + " al carrito.", Toast.LENGTH_SHORT).show();
}
@Override
public void onAddQuantity(CartItem cartItem) {
cartViewModel.addQuantity(cartItem.getProduct().getId());
}
@Override
public void onRemoveQuantity(CartItem cartItem) {
cartViewModel.removeQuantity(cartItem.getProduct().getId());
}
}
\ No newline at end of file
......@@ -12,16 +12,15 @@ import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.cesanelli.lajusta.R;
import com.cesanelli.lajusta.databinding.FragmentCartBinding;
import com.cesanelli.lajusta.databinding.FragmentProductsBinding;
import com.cesanelli.lajusta.io.models.Cart;
import com.cesanelli.lajusta.io.models.CartItem;
import com.cesanelli.lajusta.io.models.Product;
import java.util.ArrayList;
/**
* {@link RecyclerView.Adapter} that can display a {@link Product}.
* TODO: Replace the implementation with code for your data type.
*/
public class CartRecyclerViewAdapter extends RecyclerView.Adapter<CartRecyclerViewAdapter.ViewHolder> {
private final Cart cart;
......@@ -35,23 +34,16 @@ public class CartRecyclerViewAdapter extends RecyclerView.Adapter<CartRecyclerVi
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_cart, parent, false);
return new ViewHolder(view, onCartListener);
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
FragmentCartBinding fragmentCartBinding = FragmentCartBinding.inflate(layoutInflater, parent, false);
return new CartRecyclerViewAdapter.ViewHolder(fragmentCartBinding);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.cartItem = cart.getItems().get(position);
if (holder.cartItem.getProduct().getImages().length > 0) {
String cleanImage = holder.cartItem.getProduct().getImages()[0].getValue().replace("data:image/png;base64,", "").replace("data:image/jpeg;base64,", "");
byte[] decodedString = Base64.decode(cleanImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.product_image.setImageBitmap(decodedByte);
}
holder.product_title.setText(holder.cartItem.getProduct().getTitle());
holder.product_unit.setText(holder.cartItem.getQuantity().toString());
holder.product_price.setText(String.format("$ %.2f", holder.cartItem.getProduct().getBuyPrice()));
CartItem cartItem = cart.getItems().get(position);
holder.fragmentCartBinding.setCartItem(cartItem);
holder.fragmentCartBinding.setOnCartListener(onCartListener);;
}
@Override
......@@ -61,40 +53,15 @@ public class CartRecyclerViewAdapter extends RecyclerView.Adapter<CartRecyclerVi
public interface OnCartListener {
void onProductRemove(CartItem cartItem);
void onAddQuantity(CartItem cartItem);
void onRemoveQuantity(CartItem cartItem);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final ImageView product_image;
public final TextView product_title;
public final TextView product_price;
public final TextView product_unit;
public final ImageView product_delete;
public CartItem cartItem;
OnCartListener onCartListener;
public ViewHolder(View view, OnCartListener onCartListener) {
super(view);
mView = view;
product_image = (ImageView) view.findViewById(R.id.product_image);
product_title = (TextView) view.findViewById(R.id.product_title);
product_price = (TextView) view.findViewById(R.id.product_price);
product_unit = (TextView) view.findViewById(R.id.product_unit);
product_delete = (ImageView) view.findViewById(R.id.product_delete);
product_delete.bringToFront();
this.onCartListener = onCartListener;
product_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onCartListener.onProductRemove(cartItem);
}
});
public FragmentCartBinding fragmentCartBinding;
public ViewHolder(FragmentCartBinding binding) {
super(binding.getRoot());
this.fragmentCartBinding = binding;
}
@Override
public String toString() {
return super.toString() + " '" + product_title.getText() + "'";
}
}
}
\ No newline at end of file
......@@ -16,59 +16,29 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.cesanelli.lajusta.R;
import com.cesanelli.lajusta.databinding.FragmentProductBinding;
import com.cesanelli.lajusta.ui.viewmodel.ProductViewModel;
public class ProductFragment extends Fragment {
private ProductViewModel viewModel;
private TextView product_title;
private ImageView product_image;
private TextView product_description;
private TextView product_price;
private TextView product_unit;
private ProductViewModel productViewModel;
FragmentProductBinding fragmentProductBinding;
public ProductFragment() {
// Required empty public constructor
}
public static ProductFragment newInstance() {
ProductFragment fragment = new ProductFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_product, container, false);
fragmentProductBinding = FragmentProductBinding.inflate(inflater,container,false);
return fragmentProductBinding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewModel = new ViewModelProvider(getActivity()).get(ProductViewModel.class);
product_title = (TextView) view.findViewById(R.id.product_title);
product_image = (ImageView) view.findViewById(R.id.product_image);
product_description = (TextView) view.findViewById(R.id.product_description);
product_price = (TextView) view.findViewById(R.id.product_price);
product_unit = (TextView) view.findViewById(R.id.product_unit);
viewModel.getProduct().observe(getViewLifecycleOwner(), product -> {
product_title.setText(product.getTitle());
if (product.getImages().length > 0) {
String cleanImage = product.getImages()[0].getValue().replace("data:image/png;base64,", "").replace("data:image/jpeg;base64,", "");
byte[] decodedString = Base64.decode(cleanImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
product_image.setImageBitmap(decodedByte);
}
product_description.setText(product.getDescription());
product_price.setText(String.format("$ %.2f",product.getBuyPrice()));
product_unit.setText(product.getUnitQuantity() + " " + product.getUnit().getCode());
});
productViewModel = new ViewModelProvider(requireActivity()).get(ProductViewModel.class);
fragmentProductBinding.setProductViewModel(productViewModel);
}
}
\ No newline at end of file
......@@ -50,30 +50,6 @@ public class ProductsFragment extends Fragment implements ProductsRecyclerViewAd
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_products_list, container, false);
Context context = view.getContext();
// Set the adapter
if (view instanceof RecyclerView) {
Call<ArrayList<Product>> call = ApiAdapter.getApiService().getProducts();
call.enqueue(new Callback<ArrayList<Product>>() {
@Override
public void onResponse(Call<ArrayList<Product>> call, Response<ArrayList<Product>> response) {
try {
if (response.isSuccessful()) {
productListViewModel.setProductList(response.body());
}
} catch (Exception ex) {
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG);
}
}
@Override
public void onFailure(Call<ArrayList<Product>> call, Throwable t) {
Toast.makeText(context, t.getMessage(), Toast.LENGTH_LONG);
}
});
}
return view;
}
......
package com.cesanelli.lajusta.ui;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.cesanelli.lajusta.R;
import com.cesanelli.lajusta.databinding.FragmentProductsBinding;
import com.cesanelli.lajusta.io.models.Product;
import java.util.ArrayList;
......@@ -33,23 +27,16 @@ public class ProductsRecyclerViewAdapter extends RecyclerView.Adapter<ProductsRe
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_products, parent, false);
return new ViewHolder(view, onProductListener);
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
FragmentProductsBinding fragmentProductsBinding = FragmentProductsBinding.inflate(layoutInflater, parent, false);
return new ViewHolder(fragmentProductsBinding);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.product = products.get(position);
if (holder.product.getImages().length > 0) {
String cleanImage = holder.product.getImages()[0].getValue().replace("data:image/png;base64,", "").replace("data:image/jpeg;base64,", "");
byte[] decodedString = Base64.decode(cleanImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.product_image.setImageBitmap(decodedByte);
}
holder.product_title.setText(holder.product.getTitle());
holder.product_unit.setText(holder.product.getUnitQuantity() + " " + holder.product.getUnit().getCode());
holder.product_price.setText(String.format("$ %.2f", holder.product.getBuyPrice()));
Product product = products.get(position);
holder.fragmentProductsBinding.setProduct(product);
holder.fragmentProductsBinding.setOnProductListener(onProductListener);
}
@Override
......@@ -64,43 +51,11 @@ public class ProductsRecyclerViewAdapter extends RecyclerView.Adapter<ProductsRe
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final ImageView product_image;
public final TextView product_title;
public final TextView product_price;
public final TextView product_unit;
public final ImageView product_add;
public Product product;
OnProductListener onProductListener;
public ViewHolder(View view, OnProductListener onProductListener) {
super(view);
mView = view;
product_image = (ImageView) view.findViewById(R.id.product_image);
product_title = (TextView) view.findViewById(R.id.product_title);
product_price = (TextView) view.findViewById(R.id.product_price);
product_unit = (TextView) view.findViewById(R.id.product_unit);
product_add = (ImageView) view.findViewById(R.id.product_add);
product_add.bringToFront();
this.onProductListener = onProductListener;
product_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onProductListener.onProductAdd(product);
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onProductListener.onProductClick(product);
}
});
}
FragmentProductsBinding fragmentProductsBinding;
@Override
public String toString() {
return super.toString() + " '" + product_title.getText() + "'";
public ViewHolder(FragmentProductsBinding binding) {
super(binding.getRoot());
fragmentProductsBinding = binding;
}
}
}
\ No newline at end of file
......@@ -57,4 +57,40 @@ public class CartViewModel extends ViewModel {
this.cart.setValue(this.cart.getValue());
}
public void addQuantity(Integer id) {
CartItem cartItem = null;
Cart cart = this.cart.getValue();
List<CartItem> items = cart.getItems();
for (CartItem item : items) {
if (item.getProduct().getId() == id) {
item.setQuantity(item.getQuantity() + 1);
cartItem = item;
}
}
cart.setQuantity(cart.getQuantity() + 1);
cart.setTotal(cart.getTotal() + cartItem.getProduct().getBuyPrice());
this.cart.setValue(cart);
}
public void removeQuantity(Integer id) {
CartItem cartItem = null;
Cart cart = this.cart.getValue();
List<CartItem> items = cart.getItems();
for (CartItem item : items) {
if (item.getProduct().getId() == id) {
item.setQuantity(item.getQuantity() - 1);
cartItem = item;
}
}
if (cartItem.getQuantity() == 0) {
items.remove(cartItem);
}
cart.setQuantity(cart.getQuantity() - 1);
cart.setTotal(cart.getTotal() - cartItem.getProduct().getBuyPrice());
this.cart.setValue(cart);
}
}
......@@ -4,18 +4,50 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.cesanelli.lajusta.io.ApiAdapter;
import com.cesanelli.lajusta.io.models.Product;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class ProductListViewModel extends ViewModel {
final private MutableLiveData<List<Product>> products = new MutableLiveData<List<Product>>();
private MutableLiveData<List<Product>> products;
public LiveData<List<Product>> getProductList() {
if (products == null) {
products = new MutableLiveData<List<Product>>();
loadProducts();
}
return products;
}
public void setProductList(List<Product> products) {
this.products.setValue(products);
}
private void loadProducts() {
List<Product> products = new ArrayList<Product>();
Call<ArrayList<Product>> call = ApiAdapter.getApiService().getProducts();
call.enqueue(new Callback<ArrayList<Product>>() {
@Override
public void onResponse(Call<ArrayList<Product>> call, Response<ArrayList<Product>> response) {
try {
if (response.isSuccessful()) {
setProductList(response.body());
}
} catch (Exception ex) {
}
}
@Override
public void onFailure(Call<ArrayList<Product>> call, Throwable t) {
}
});
}
}
\ No newline at end of file
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="85.84757"
android:endY="92.4963"
android:startX="42.9492"
android:startY="49.59793"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
\ No newline at end of file