Commit e8f445d2 authored by Daniel Cesanelli's avatar Daniel Cesanelli
Browse files

Agregado carrito con ViewModel para la entrega parcial

parent dd5511fd
......@@ -3,6 +3,7 @@ package com.cesanelli.lajusta;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import androidx.appcompat.app.AppCompatActivity;
......@@ -30,7 +31,7 @@ public class MainActivity extends AppCompatActivity {
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_quienes_somos, R.id.nav_news, R.id.nav_producers, R.id.nav_products, R.id.nav_login)
R.id.nav_quienes_somos, R.id.nav_news, R.id.nav_producers, R.id.nav_products,R.id.nav_cart, R.id.nav_login)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
......@@ -41,7 +42,7 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key == "JWT"){
Log.v("JWT CHANGED!!!",sharedPreferences.getString(key,""));
}
}
});
......
......@@ -2,11 +2,16 @@ package com.cesanelli.lajusta.io;
import com.cesanelli.lajusta.io.models.Producer;
import com.cesanelli.lajusta.io.models.Product;
import com.cesanelli.lajusta.io.models.Token;
import com.cesanelli.lajusta.io.models.TokenBody;
import com.cesanelli.lajusta.io.models.User;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
public interface ApiService {
......@@ -21,4 +26,10 @@ public interface ApiService {
@GET("producer/{id}")
Call<Producer> getProducer(@Path("id") int id);
@POST("token/generate-token")
Call<Token> login(@Body TokenBody body);
@POST("user/signup")
Call<User> signup(@Body User body);
}
package com.cesanelli.lajusta.io.models;
import java.util.ArrayList;
public class Cart {
private ArrayList<CartItem> items;
private Integer quantity;
private float total;
public Cart(){
items = new ArrayList<CartItem>();
quantity = 0;
total = 0;
}
public ArrayList<CartItem> getItems() {
return items;
}
public void setItems(ArrayList<CartItem> items) {
this.items = items;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public float getTotal() {
return total;
}
public void setTotal(float total) {
this.total = total;
}
}
package com.cesanelli.lajusta.io.models;
public class CartItem {
private Product product;
private Integer quantity;
public CartItem(Product product, Integer quantity) {
this.product = product;
this.quantity = quantity;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
}
......@@ -10,6 +10,8 @@ public class Product {
private Category[] categories;
private Image[] images;
private Producer producer;
private Unit unit;
private int unitQuantity;
public int getId() {
return id;
......@@ -82,5 +84,21 @@ public class Product {
public void setProducer(Producer producer) {
this.producer = producer;
}
public Unit getUnit() {
return unit;
}
public void setUnit(Unit unit) {
this.unit = unit;
}
public int getUnitQuantity() {
return unitQuantity;
}
public void setUnitQuantity(int unitQuantity) {
this.unitQuantity = unitQuantity;
}
}
package com.cesanelli.lajusta.io.models;
public class Token {
private String value;
private User user;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
package com.cesanelli.lajusta.io.models;
public class TokenBody {
private String userName;
private String userPassword;
public TokenBody (String userName, String userPassword){
this.userName = userName;
this.userPassword = userPassword;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
package com.cesanelli.lajusta.io.models;
public class Unit {
private int id;
private String code;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
......@@ -5,21 +5,35 @@ 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;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
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.io.models.CartItem;
import com.cesanelli.lajusta.io.models.Product;
import com.cesanelli.lajusta.ui.viewmodel.CartViewModel;
public class CarsFragment extends Fragment {
public CarsFragment() {
import java.text.DecimalFormat;
import java.util.ArrayList;
public class CartFragment extends Fragment implements CartRecyclerViewAdapter.OnCartListener{
private CartViewModel cartViewModel;
public CartFragment() {
// Required empty public constructor
}
public static CarsFragment newInstance(String param1, String param2) {
CarsFragment fragment = new CarsFragment();
public static CartFragment newInstance(String param1, String param2) {
CartFragment fragment = new CartFragment();
return fragment;
}
......@@ -32,12 +46,26 @@ public class CarsFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_cars, container, false);
return inflater.inflate(R.layout.fragment_cart_list, container, false);
}
@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 -> {
if (cart != null) {
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()));
}
});
btnComprar.setOnClickListener(new View.OnClickListener() {
@Override
......@@ -47,4 +75,11 @@ public class CarsFragment extends Fragment {
});
}
@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();
}
}
\ No newline at end of file
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.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;
private OnCartListener onCartListener;
public CartRecyclerViewAdapter(Cart cart, OnCartListener onCartListener) {
this.cart = cart;
this.onCartListener = onCartListener;
}
@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);
}
@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()));
}
@Override
public int getItemCount() {
return cart.getItems().size();
}
public interface OnCartListener {
void onProductRemove(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);
}
});
}
@Override
public String toString() {
return super.toString() + " '" + product_title.getText() + "'";
}
}
}
\ No newline at end of file
......@@ -3,10 +3,13 @@ package com.cesanelli.lajusta.ui;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
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;
import androidx.annotation.Nullable;
......@@ -14,8 +17,17 @@ import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import com.cesanelli.lajusta.R;
import com.cesanelli.lajusta.io.ApiAdapter;
import com.cesanelli.lajusta.io.models.Token;
import com.cesanelli.lajusta.io.models.TokenBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class LoginFragment extends Fragment {
private TextView login_user;
private TextView login_password;
public LoginFragment() {
// Required empty public constructor
......@@ -41,16 +53,39 @@ public class LoginFragment extends Fragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnLogin = view.findViewById(R.id.btnLogin);
Button btnRegistrar = view.findViewById(R.id.btnRegister);
Button btnLogin = view.findViewById(R.id.btn_login);
Button btnRegistrar = view.findViewById(R.id.btn_register);
login_user = (TextView) view.findViewById(R.id.login_user);
login_password = (TextView) view.findViewById(R.id.login_password);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
sharedpreferences.edit().putString("JWT", "XXX").apply();
Navigation.findNavController(v).navigate(R.id.nav_cars);
Call<Token> call = ApiAdapter.getApiService().login(new TokenBody(login_user.getText().toString(), login_password.getText().toString()));
call.enqueue(new Callback<Token>() {
@Override
public void onResponse(Call<Token> call, Response<Token> response) {
try {
if (response.isSuccessful()) {
Log.v("LOGIN", response.body().getValue());
sharedpreferences.edit().putString("JWT", response.body().getValue()).apply();
}
} catch (Exception ex) {
Toast.makeText(getContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<Token> call, Throwable t) {
Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
//Navigation.findNavController(v).navigate(R.id.nav_cars);
}
});
......
package com.cesanelli.lajusta.ui;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
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.annotation.NonNull;
......@@ -16,7 +20,10 @@ import com.cesanelli.lajusta.ui.viewmodel.ProducerViewModel;
public class ProducerFragment extends Fragment {
private ProducerViewModel viewModel;
private TextView textView;
private TextView producer_name;
private ImageView producer_image;
private TextView producer_email;
private TextView producer_description;
public ProducerFragment() {
// Required empty public constructor
......@@ -43,9 +50,20 @@ public class ProducerFragment extends Fragment {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewModel = new ViewModelProvider(getActivity()).get(ProducerViewModel.class);
textView = (TextView) view.findViewById(R.id.textProducer);
producer_name = (TextView) view.findViewById(R.id.producer_name);
producer_image = (ImageView) view.findViewById(R.id.producer_image);
producer_description = (TextView) view.findViewById(R.id.producer_description);
producer_email = (TextView) view.findViewById(R.id.producer_email);
viewModel.getProducer().observe(getViewLifecycleOwner(), producer -> {
textView.setText(producer.getName());
producer_name.setText(producer.getName());
producer_email.setText(producer.getEmail());
if (producer.getImages().length > 0) {
String cleanImage = producer.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);
producer_image.setImageBitmap(decodedByte);
}
producer_description.setText(producer.getDescription());
});
}
}
\ No newline at end of file
......@@ -31,8 +31,8 @@ import retrofit2.Response;
* A fragment representing a list of Items.
*/
public class ProducersFragment extends Fragment implements ProducersRecyclerViewAdapter.OnProducerListener {
private ProducerViewModel viewModel;
private ProducerListViewModel listViewModel;
private ProducerViewModel producerViewModel;
private ProducerListViewModel producerListViewModel;
public ProducersFragment() {
}
......@@ -62,7 +62,7 @@ public class ProducersFragment extends Fragment implements ProducersRecyclerView
public void onResponse(Call<ArrayList<Producer>> call, Response<ArrayList<Producer>> response) {
try {
if(response.isSuccessful()) {
listViewModel.setProducerList(response.body());
producerListViewModel.setProducerList(response.body());
}
} catch (Exception ex){
Toast.makeText(context,ex.getMessage(),Toast.LENGTH_LONG);
......@@ -81,10 +81,10 @@ public class ProducersFragment extends Fragment implements ProducersRecyclerView
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewModel = new ViewModelProvider(getActivity()).get(ProducerViewModel.class);
listViewModel = new ViewModelProvider(getActivity()).get(ProducerListViewModel.class);
listViewModel.setProducerList(new ArrayList<Producer>());
listViewModel.getProducerList().observe(getViewLifecycleOwner(),list ->{
producerViewModel = new ViewModelProvider(getActivity()).get(ProducerViewModel.class);
producerListViewModel = new ViewModelProvider(getActivity()).get(ProducerListViewModel.class);
producerListViewModel.setProducerList(new ArrayList<Producer>());
producerListViewModel.getProducerList().observe(getViewLifecycleOwner(), list ->{
if (list != null) {
RecyclerView recyclerView = (RecyclerView) view;
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
......@@ -95,7 +95,7 @@ public class ProducersFragment extends Fragment implements ProducersRecyclerView
@Override