package com.example.lookifyv2; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; 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 androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import java.util.List; //klasa napisana z pomocą tutorialu z http://androidbash.com/connecting-android-app-to-a-database-using-php-and-mysql/ public class FavouritesAdapter extends RecyclerView.Adapter{ private Context context; private List products; FavouritesAdapter(Context context, List products) { this.context = context; this.products = products; } @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) { 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; private 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_favourites, popup.getMenu()); popup.setOnMenuItemClickListener(new MenuClickListener(position)); popup.show(); } public class MenuClickListener implements PopupMenu.OnMenuItemClickListener { Integer pos; private MenuClickListener(int pos) { this.pos=pos; } //Pierwszy case to usuwanie produktu z obserwowanych. By odświeżyć widok po usunięciu zamykane są ona i powiązane z nią activity, //a ona sama uruchamiana ponownie. @Override public boolean onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.action_remove: SharedPreferences pref = MyApplication.getInstance().getSharedPreferences("Favourites", 0); SharedPreferences.Editor editor = pref.edit(); editor.remove(products.get(pos).getproductCode()); editor.apply(); Intent intent_Favourites = new Intent(context, Favourites.class); intent_Favourites.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //Niszczy tą i powiązane activity, dzięki czemu przy kolejnym //uruchomieniu favourites nie widać już usuniętych kafelków. context.startActivity(intent_Favourites); Toast.makeText(context, "Produkt \"" + products.get(pos).getproductName() + "\" został usunięty z Obserwowanych.", Toast.LENGTH_SHORT).show(); return true; case R.id.action_favourite_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; } } }