Skip to content

Instantly share code, notes, and snippets.

@styliii
Created April 4, 2015 00:30
Show Gist options
  • Save styliii/0f4440476160b35e9d02 to your computer and use it in GitHub Desktop.
Save styliii/0f4440476160b35e9d02 to your computer and use it in GitHub Desktop.
package com.finnerds.wealthnow.adapters;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.finnerds.wealthnow.R;
import com.finnerds.wealthnow.models.Alert;
import com.finnerds.wealthnow.models.AlertTypes;
import com.finnerds.wealthnow.models.PriceAlert;
import com.finnerds.wealthnow.models.PriceGreaterThanAlert;
import com.finnerds.wealthnow.models.PriceLessThanAlert;
import java.util.List;
public class AlertRecyclerAdapter extends RecyclerView.Adapter<AlertRecyclerAdapter.PriceAlertItemViewHolder> {
private List<Alert> alerts;
private AlertRecyclerAdapterListener listener;
public interface AlertRecyclerAdapterListener {
public void onAlertDelete(Alert alertToDelete);
}
public AlertRecyclerAdapter(List<Alert> alerts, AlertRecyclerAdapterListener listener) {
this.alerts = alerts;
this.listener = listener;
}
@Override
public PriceAlertItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View alertView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.price_alert_list_item, viewGroup, false);
return new PriceAlertItemViewHolder(alertView, new BaseAlertViewHolder.OnDeleteClickListener() {
@Override
public void onAlertDelete(Button deleteButton) {
int position = (int) deleteButton.getTag();
Alert alertToDelete = getItem(position);
if (listener != null) {
listener.onAlertDelete(alertToDelete);
}
}
@Override
public void onAlertEdit(View view) {
Toast.makeText(view.getContext(), "ok you are clicking on item", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onBindViewHolder(PriceAlertItemViewHolder alertViewHolder, int position) {
Alert alert = alerts.get(position);
int type = getItemViewType(position);
alertViewHolder.symbol.setText(alert.symbol);
alertViewHolder.alertIcon.setVisibility(alert.hasTriggered() ? View.VISIBLE : View.INVISIBLE);
if (type == AlertTypes.PRICE_GREATER_THAN_ALERT.ordinal() ||
type == AlertTypes.PRICE_LESS_THAN_ALERT.ordinal()) {
alertViewHolder.alertType.setText("Price Alert");
alertViewHolder.property.setText("Price");
String operator = type == AlertTypes.PRICE_GREATER_THAN_ALERT.ordinal() ? "Greater than" : "Less than";
alertViewHolder.operator.setText(operator);
alertViewHolder.target.setText(String.valueOf(((PriceAlert) alert).priceTarget));
alertViewHolder.currentPrice.setText(String.valueOf(((PriceAlert) alert).currentPrice));
}
alertViewHolder.deleteAlert.setTag(position);
}
@Override
public int getItemCount() {
return this.alerts.size();
}
@Override
public int getItemViewType(int position) {
Alert alert = getItem(position);
if (alert instanceof PriceGreaterThanAlert) {
return AlertTypes.PRICE_GREATER_THAN_ALERT.ordinal();
} else if (alert instanceof PriceLessThanAlert) {
return AlertTypes.PRICE_LESS_THAN_ALERT.ordinal();
}
return -1;
}
private Alert getItem(int position) {
return alerts.get(position);
}
public void addToList(Alert alert, int position) {
alerts.add(position, alert);
notifyItemInserted(position);
}
public void removeFromList(int position) {
alerts.remove(position);
notifyItemRemoved(position);
}
public void remove(Alert alert) {
int position = alerts.indexOf(alert);
removeFromList(position);
}
public void clear() {
alerts.clear();
notifyDataSetChanged();
}
public void addAll(List<Alert> alerts) {
this.alerts = alerts;
notifyDataSetChanged();
}
public static abstract class BaseAlertViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView alertType;
TextView symbol;
Button deleteAlert;
ImageView alertIcon;
OnDeleteClickListener mListener;
public BaseAlertViewHolder(View itemView, OnDeleteClickListener listener) {
super(itemView);
mListener = listener;
symbol = (TextView) itemView.findViewById(R.id.tvSymbol);
alertType = (TextView) itemView.findViewById(R.id.tvAlertType);
deleteAlert = (Button) itemView.findViewById(R.id.btnDeleteAlert);
deleteAlert.setOnClickListener(this);
alertIcon = (ImageView) itemView.findViewById(R.id.ivAlertIcon);
}
@Override
public void onClick(View v) {
if (v instanceof Button){
mListener.onAlertDelete((Button) v);
} else {
mListener.onAlertEdit(v);
}
}
public static interface OnDeleteClickListener {
public void onAlertDelete(Button deleteButton);
public void onAlertEdit(View view);
}
}
public final static class PriceAlertItemViewHolder extends BaseAlertViewHolder {
TextView property;
TextView operator;
TextView target;
TextView currentPrice;
public PriceAlertItemViewHolder (View alertView, OnDeleteClickListener listener) {
super(alertView, listener);
property = (TextView) alertView.findViewById(R.id.tvProperty);
operator = (TextView) alertView.findViewById(R.id.tvOperator);
target = (TextView) alertView.findViewById(R.id.tvTarget);
currentPrice = (TextView) alertView.findViewById(R.id.tvCurrentPrice);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment