147 lines
5.9 KiB
Java
147 lines
5.9 KiB
Java
package com.example.lookifyv2;
|
|
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.os.Bundle;
|
|
import android.content.Context;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
import android.view.MenuInflater;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.ImageView;
|
|
import android.widget.PopupMenu;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import com.bumptech.glide.Glide;
|
|
import com.google.common.collect.Lists;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
//Klasa napisana z pomocą tutorialu z http://androidbash.com/connecting-android-app-to-a-database-using-php-and-mysql/.
|
|
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ViewHolder>{
|
|
|
|
private Context context;
|
|
private List<Product> products;
|
|
|
|
ProductsAdapter(Context context, List<Product> products) {
|
|
this.context = context;
|
|
this.products = products;
|
|
}
|
|
|
|
//Viewholder przechowuje dane viewów, by nie musieć za każdym razem ich pojedynczo wypełniać.
|
|
@Override
|
|
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);
|
|
|
|
return new ViewHolder(itemView);
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(final ViewHolder holder, int position) {
|
|
|
|
//Do załadowania obrazka używana jest zewnętrzna biblioteka Glide.
|
|
Glide.with(context).load(products.get(position).getImageLink()).into(holder.imageView);
|
|
|
|
holder.productName.setText(products.get(position).getproductName());
|
|
holder.productPrice.setText(products.get(position).getproductPrice());
|
|
holder.productRetailer.setText(products.get(position).getproductRetailer());
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return products.size();
|
|
}
|
|
|
|
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
|
|
|
|
ImageView imageView;
|
|
TextView productName;
|
|
TextView productPrice;
|
|
TextView productRetailer;
|
|
|
|
ViewHolder(View itemView) {
|
|
super(itemView);
|
|
imageView = itemView.findViewById(R.id.productimage);
|
|
imageView.setOnClickListener(this);
|
|
productName = itemView.findViewById(R.id.productname);
|
|
productPrice = itemView.findViewById(R.id.productprice);
|
|
productRetailer = itemView.findViewById(R.id.productretailer);
|
|
}
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
int position = getAdapterPosition();
|
|
showPopupMenu(v,position);
|
|
}
|
|
}
|
|
|
|
private void showPopupMenu(View view, int position) {
|
|
PopupMenu popup = new PopupMenu(context, view);
|
|
MenuInflater inflater = popup.getMenuInflater();
|
|
inflater.inflate(R.menu.menu_context, popup.getMenu());
|
|
popup.setOnMenuItemClickListener(new MenuClickListener(position));
|
|
popup.show();
|
|
}
|
|
|
|
public class MenuClickListener implements PopupMenu.OnMenuItemClickListener {
|
|
Integer pos;
|
|
MenuClickListener(int pos) {
|
|
this.pos=pos;
|
|
}
|
|
|
|
//Zasadniczo dzieje się tutaj to samo, co w przypadku przycisków w Results.
|
|
@Override
|
|
public boolean onMenuItemClick(MenuItem menuItem) {
|
|
switch (menuItem.getItemId()) {
|
|
case R.id.action_favourite:
|
|
SharedPreferences pref = MyApplication.getInstance().getSharedPreferences("Favourites", 0);
|
|
SharedPreferences.Editor editor = pref.edit();
|
|
Map<String, ?> codes = pref.getAll();
|
|
ArrayList<String> codeslist = Lists.newArrayList(codes.keySet());
|
|
int checker = 0;
|
|
for(int i = 0; i < codeslist.size(); i++){
|
|
if(products.get(pos).getproductCode().equals(codeslist.get(i))){
|
|
checker = -1;
|
|
Toast.makeText(context, "Ten produkt jest już obserwowany!",
|
|
Toast.LENGTH_SHORT).show();
|
|
break;
|
|
}
|
|
}
|
|
if(checker == 0){
|
|
editor.putBoolean(products.get(pos).getproductCode(), true);
|
|
editor.apply(); //commit?
|
|
Toast.makeText(context, "Produkt \"" + products.get(pos).getproductName() + "\" został dodany do Obserwowanych.",
|
|
Toast.LENGTH_SHORT).show();
|
|
}
|
|
return true;
|
|
case R.id.action_details:
|
|
Intent intent_Popup = new Intent(context, Popup.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
bundle.putSerializable("productimage", products.get(pos).getImageLink());
|
|
bundle.putSerializable("productcode", products.get(pos).getproductCode());
|
|
bundle.putSerializable("productname", products.get(pos).getproductName());
|
|
bundle.putSerializable("productprice", products.get(pos).getproductPrice());
|
|
bundle.putSerializable("productretailer", products.get(pos).getproductRetailer());
|
|
bundle.putSerializable("productcolour", products.get(pos).getproductColour());
|
|
bundle.putSerializable("productstyle", products.get(pos).getproductStyle());
|
|
bundle.putSerializable("productcollection", products.get(pos).getproductCollection());
|
|
|
|
intent_Popup.putExtras(bundle);
|
|
|
|
context.startActivity(intent_Popup);
|
|
return true;
|
|
default:
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|