Skip to content

Instantly share code, notes, and snippets.

@MAnfal
Last active February 4, 2016 07:37
Show Gist options
  • Save MAnfal/dc581234ce597a8ff062 to your computer and use it in GitHub Desktop.
Save MAnfal/dc581234ce597a8ff062 to your computer and use it in GitHub Desktop.
//Custom class used as key for HashMap
public class CustomHashMapIntegerKey {
private Integer key = 0;
public void setKey(Integer key) {
this.key = key;
}
public Integer getKey() {
return this.key;
}
@Override
public int hashCode() {
return this.key.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (null == obj) return false;
if (this.getClass() != obj.getClass()) return false;
final CustomHashMapIntegerKey customHashMapIntegerKey = (CustomHashMapIntegerKey) obj;
if (null == this.key && null != customHashMapIntegerKey.getKey()) return false;
if (null != this.key) {
if (!this.key.equals(customHashMapIntegerKey.getKey())) return false;
}
return true;
}
}
//DepartmentItemDataObject
public class DepartmentItemDataObject {
private Integer itemPrice, itemDiscount, itemId, orderedQuantity = 0;
private String itemImageUrl, itemImageThumbnailUrl, itemWeightOrQuantity, nutritionFacts, itemDescription, itemName = null;
private CustomHashMapIntegerKey customHashMapIntegerKey = null;
public DepartmentItemDataObject() {
this.customHashMapIntegerKey = new CustomHashMapIntegerKey();
}
public int getItemPrice() {
return itemPrice;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getItemDiscount() {
return itemDiscount;
}
public void setItemDiscount(int itemDiscount) {
this.itemDiscount = itemDiscount;
}
public String getItemImageUrl() {
return itemImageUrl;
}
public void setItemImageUrl(String itemImageUrl) {
this.itemImageUrl = itemImageUrl;
}
public String getItemImageThumbnailUrl() {
return itemImageThumbnailUrl;
}
public void setItemImageThumbnailUrl(String itemImageThumbnailUrl) {
this.itemImageThumbnailUrl = itemImageThumbnailUrl;
}
public String getItemWeightOrQuantity() {
return itemWeightOrQuantity;
}
public void setItemWeightOrQuantity(String itemWeightOrQuantity) {
this.itemWeightOrQuantity = itemWeightOrQuantity;
}
public String getNutritionFacts() {
return nutritionFacts;
}
public void setNutritionFacts(String nutritionFacts) {
this.nutritionFacts = nutritionFacts;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
this.customHashMapIntegerKey.setKey(this.itemId);
}
public int getOrderedQuantity() {
return orderedQuantity;
}
public void setOrderedQuantity(int orderedQuantity) {
this.orderedQuantity = orderedQuantity;
}
public CustomHashMapIntegerKey getCustomHashMapIntegerKey() {
return this.customHashMapIntegerKey;
}
}
//Department Item Holder
public class DepartmentItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView imageView = null;
private TextView itemPriceTexView, itemNameTextView, itemWeightTextView, itemDiscountTextView, itemDiscountedPrice = null;
private Spinner addToCartSpinner = null;
private DepartmentItemDataObject departmentItemDataObject = null;
public DepartmentItemHolder(View itemView) {
super(itemView);
this.imageView = (ImageView) itemView.findViewById(R.id.department_item_image);
this.itemPriceTexView = (TextView) itemView.findViewById(R.id.department_item_price);
this.itemNameTextView = (TextView) itemView.findViewById(R.id.department_item_name);
this.itemWeightTextView = (TextView) itemView.findViewById(R.id.department_item_weight);
this.itemDiscountTextView = (TextView) itemView.findViewById(R.id.department_item_discount);
this.itemDiscountedPrice = (TextView) itemView.findViewById(R.id.department_item_discounted_price);
this.addToCartSpinner = (Spinner) itemView.findViewById(R.id.department_add_to_cart_quantity);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
DepartmentItemClickProxy departmentItemClickProxy = new DepartmentItemClickProxy();
departmentItemClickProxy.itemClicked(v, this.departmentItemDataObject);
}
public ImageView getImageView() {
return this.imageView;
}
public TextView getItemPriceTexView() {
return itemPriceTexView;
}
public TextView getItemNameTextView() {
return itemNameTextView;
}
public TextView getItemWeightTextView() {
return itemWeightTextView;
}
public TextView getItemDiscountTextView() {
return itemDiscountTextView;
}
public TextView getItemDiscountedPrice() {
return itemDiscountedPrice;
}
public void setViewListeners() {
ItemAddToCartListener itemAddToCartListener = new ItemAddToCartListener(this.departmentItemDataObject);
this.addToCartSpinner.setOnTouchListener(itemAddToCartListener);
this.addToCartSpinner.setOnItemSelectedListener(itemAddToCartListener);
}
public Spinner getAddToCartSpinner() {
return this.addToCartSpinner;
}
public DepartmentItemDataObject getDepartmentItemDataObject() {
return this.departmentItemDataObject;
}
public void setDepartmentItemDataObject(DepartmentItemDataObject departmentItemDataObject) {
this.departmentItemDataObject = departmentItemDataObject;
}
}
//Function where Department Item Holder is added to map
@Override
public void onBindViewHolder(DepartmentItemHolder departmentItemHolder, int position) {
departmentItemHolder.getItemNameTextView().setText(this.departmentItemDataObjects.get(position).getItemName());
departmentItemHolder.getItemPriceTexView().setText(
departmentItemHolder.getItemPriceTexView().getContext().getString(
R.string.currency,
this.departmentItemDataObjects.get(position).getItemPrice()
)
);
if (0 < this.departmentItemDataObjects.get(position).getItemDiscount()) {
departmentItemHolder.getItemDiscountTextView().setText(
departmentItemHolder.getItemDiscountTextView().getContext().getString(
R.string.discount_on_item,
this.departmentItemDataObjects.get(position).getItemDiscount()
)
);
departmentItemHolder.getItemDiscountedPrice().setText(
departmentItemHolder.getItemDiscountedPrice().getContext().getString(
R.string.currency,
ItemCheckoutUtility.getInstance().getDiscountedPrice(
this.departmentItemDataObjects.get(position).getItemPrice(),
this.departmentItemDataObjects.get(position).getItemDiscount()
)
)
);
Utilities.getInstance().strikeThroughText(departmentItemHolder.getItemPriceTexView());
} else {
departmentItemHolder.getItemDiscountTextView().setText(null);
departmentItemHolder.getItemDiscountedPrice().setText(null);
Utilities.getInstance().removeStrikeThroughFromText(departmentItemHolder.getItemPriceTexView());
}
departmentItemHolder.getItemWeightTextView().setText(this.departmentItemDataObjects.get(position).getItemWeightOrQuantity());
departmentItemHolder.setDepartmentItemDataObject(this.departmentItemDataObjects.get(position));
Glide.with(departmentItemHolder.getImageView().getContext())
.load(Config.API_HOST + this.departmentItemDataObjects.get(position).getItemImageThumbnailUrl())
.fitCenter()
.crossFade()
.into(departmentItemHolder.getImageView());
departmentItemHolder.getAddToCartSpinner().setSelection(
CartObserver.getCartObserverInstance().getQuantityToCheckoutFromMap(this.departmentItemDataObjects.get(position).getItemId()),
false
);
departmentItemHolder.setViewListeners();
CartObserver.getCartObserverInstance().addToDepartmentItemMap(departmentItemHolder);
}
//HashMap Instantiation
private Map<CustomHashMapIntegerKey, List<DepartmentItemHolder>> departmentItemsMap = new HashMap<>();
//Addition of items to department items map
public void addToDepartmentItemMap(DepartmentItemHolder departmentItemHolder) {
if (this.appCompatActivity instanceof DepartmentPage || this.appCompatActivity instanceof SearchPage) {
CustomHashMapIntegerKey customHashMapIntegerKey = departmentItemHolder.getDepartmentItemDataObject().getCustomHashMapIntegerKey();
if (this.departmentItemsMap.containsKey(customHashMapIntegerKey)) {
this.departmentItemsMap.get(customHashMapIntegerKey).add(departmentItemHolder);
} else {
List<DepartmentItemHolder> departmentItemHolders = new ArrayList<>();
departmentItemHolders.add(departmentItemHolder);
this.departmentItemsMap.put(customHashMapIntegerKey, departmentItemHolders);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment