Skip to content

Instantly share code, notes, and snippets.

@Gkemon
Created May 1, 2020 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gkemon/2b070da4f85d431ebf92d04a638a43b6 to your computer and use it in GitHub Desktop.
Save Gkemon/2b070da4f85d431ebf92d04a638a43b6 to your computer and use it in GitHub Desktop.
package com.ishtiaq.techlogicians.offergenie.list;
import android.content.Context;
import android.content.Intent;
import android.os.StrictMode;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.ishtiaq.techlogicians.offergenie.Activity.LoginActivity;
import com.ishtiaq.techlogicians.offergenie.Activity.OfferDetailsActivity;
import com.ishtiaq.techlogicians.offergenie.HelperClasses.GlobalAppContext;
import com.ishtiaq.techlogicians.offergenie.HelperClasses.Constants;
import com.ishtiaq.techlogicians.offergenie.HelperClasses.GlobalDataStorage;
import com.ishtiaq.techlogicians.offergenie.HelperClasses.NetworkCheckingClass;
import com.ishtiaq.techlogicians.offergenie.HelperClasses.SharedPreferenceUtils;
import com.ishtiaq.techlogicians.offergenie.HelperClasses.Utils;
import com.ishtiaq.techlogicians.offergenie.model.Offer;
import com.ishtiaq.techlogicians.offergenie.model.ResponseData.IsInterested;
import com.ishtiaq.techlogicians.offergenie.R;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import org.jsoup.Jsoup;
import java.util.ArrayList;
import java.util.List;
import es.dmoral.toasty.Toasty;
import static com.facebook.FacebookSdk.getApplicationContext;
import static com.ishtiaq.techlogicians.offergenie.HelperClasses.NetworkCaller.doAddOrRemoveInterest;
public class OfferFeedAdapterBACKUPwithDynamicOfferTag extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static Context context;
private List<Offer> offers;
private String comingFrom;
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
public CustomViewHolder customHolder;
public ArrayList<Boolean> isViewCreate;
public
OfferFeedAdapterBACKUPwithDynamicOfferTag(Context context, List<Offer> offers, String comingFrom){
this.context = context;
this.offers = offers;
this.comingFrom=comingFrom;
isViewCreate=new ArrayList<>();
for(int i=0;i<offers.size();i++){
isViewCreate.add(false);
}
}
public void refreshWhenSwipe( List<Offer> offers){
this.offers.clear();
this.offers=offers;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder=null;
if (viewType == VIEW_TYPE_ITEM) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_offer_feed, null);
viewHolder = new CustomViewHolder(view);
}
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof OfferFeedAdapterBACKUPwithDynamicOfferTag.CustomViewHolder) {
final Offer offer = offers.get(position);
String offerTitle = offer.name;
customHolder = (CustomViewHolder) holder;
if(offerTitle.length()>=75){
offerTitle=offerTitle.subSequence(0,75)+"........";
}
customHolder.offerTitle.setText(offerTitle);
String offerTag=offer.offerTag.trim();
if (offerTag.length() >= 16 && offerTag.length() <=20) {
offerTag = offer.offerTag.substring(0, 7) + '\n' + offer.offerTag.substring(8, offer.offerTag.length());
ViewGroup.LayoutParams layoutParams =customHolder.offerTag.getLayoutParams();
layoutParams.height=200;
layoutParams.width=200;
customHolder.offerTag.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(customHolder.offerTag.getLayoutParams().height - 20, customHolder.offerTag.getLayoutParams().width - 20);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_END);
customHolder.offerTag.setLayoutParams(params);
} else if (offerTag.length() <= 10) {
ViewGroup.LayoutParams layoutParams =customHolder.offerTag.getLayoutParams();
layoutParams.height=200;
layoutParams.width=200;
customHolder.offerTag.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(customHolder.offerTag.getLayoutParams().height - 60, customHolder.offerTag.getLayoutParams().width - 60);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_END);
customHolder.offerTag.setLayoutParams(params);
} else if (offerTag.length() >= 24) {
offerTag = offer.offerTag.substring(0, 10) + '\n' + offer.offerTag.substring(11);
//this is for get the orgitial view's height width
ViewGroup.LayoutParams layoutParams =customHolder.offerTag.getLayoutParams();
layoutParams.height=250;
layoutParams.width=250;
customHolder.offerTag.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(customHolder.offerTag.getLayoutParams().height + 30, customHolder.offerTag.getLayoutParams().width + 30);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_END);
customHolder.offerTag.setLayoutParams(params);
}
customHolder.offerTag.setText(offerTag);
if(offer.isInterested==null)
customHolder.interest.setImageResource(R.drawable.ic_unsave_2);
else customHolder.interest.setImageResource(R.drawable.ic_save_2);
customHolder.daysLeft.setText(Utils.dateStringToDaysLeft(offer.endDate));
customHolder.companyName.setText(offer.company.name);
customHolder.startDate.setText(String.format("start on %s", offer.startDate));
//TODO remove html tag with comment
String details =Jsoup.parse(offer.offerDetails).text();
String string=details;
if(details.length()>120){
string = details.substring(0,120)+"..... <b>" + "READ MORE" + "</b> ";
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
customHolder.shortDescription.setText(Html.fromHtml(details, Html.FROM_HTML_MODE_LEGACY));
}else{
customHolder.shortDescription.setText(Html.fromHtml(string));
}
GlobalAppContext globalAppContext=(GlobalAppContext)context.getApplicationContext();
if(customHolder.companyLogo.getDrawable()== null)
Picasso.get()
.load(offer.company.logo)
.fit()
.centerCrop()
.networkPolicy(NetworkPolicy.OFFLINE)
.into(customHolder.companyLogo, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
}
});
if(customHolder.offerImage.getDrawable()== null)
Picasso.get()
.load(offer.imageOriginal)
.fit()
.centerCrop()
.networkPolicy(NetworkPolicy.OFFLINE)
.into(customHolder.offerImage, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Picasso.get()
.load(offer.imageOriginal)
.fit()
.centerCrop()
.fit()
.centerCrop()
.into(((CustomViewHolder) holder).offerImage);
}
});
String view;
try
{
view=""+offer.viewCount.count;
customHolder.viewCount.setText(String.format("%s views", view));
}
catch (NullPointerException nullPointer)
{
view="0 views";
customHolder.viewCount.setText(view);
}
}
}
@Override
public int getItemCount() {
return (offers!=null ? offers.size() : 0);
}
@Override
public int getItemViewType(int position) {
return offers.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// Your holder should contain a member variable
// for any view that will be set as you render a row
View report;
TextView shortDescription;
TextView companyName;
TextView startDate;
ImageView companyLogo;
ImageView offerImage;
ImageView interest;
View shareButton;
TextView offerTitle;
TextView viewCount ;
TextView daysLeft;
TextView offerTag;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
CustomViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
companyLogo = itemView.findViewById(R.id.company_imageview);
offerImage = itemView.findViewById(R.id.offer_imageview);
offerTitle = itemView.findViewById(R.id.offer_title);
interest = itemView.findViewById(R.id.interest_fab);
companyName = itemView.findViewById(R.id.offer_company);
startDate = itemView.findViewById(R.id.start_date);
shortDescription = itemView.findViewById(R.id.offer_short_details);
daysLeft = itemView.findViewById(R.id.days_left);
offerTag = itemView.findViewById(R.id.offer_tag);
offerImage.setOnClickListener(this);
shortDescription.setOnClickListener(this);
report.setOnClickListener(this);
interest.setOnClickListener(this);
shareButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int clickedPosition = getAdapterPosition();
switch(v.getId())
{
case R.id.offer_short_details:
case R.id.offer_imageview:
Intent intent = new Intent(context, OfferDetailsActivity.class);
intent.putExtra(Constants.SELECTED_ITEM_POSITION,clickedPosition);
intent.putExtra(Constants.SELECTED_ITEM_POSITION,clickedPosition);
intent.putExtra(Constants.OFFER_COMING_FROM,comingFrom);
GlobalDataStorage.SELECTED_OFFER_FOR_DETAILS =offers.get(clickedPosition);
context.startActivity(intent);
break;
case R.id.interest_fab:
//For avoiding network exception
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
if(NetworkCheckingClass.hasActiveInternetConnection(context)) {
Offer offer = offers.get(clickedPosition);
if (SharedPreferenceUtils.getStringPreference(getApplicationContext(), Constants.ACCESS_TOKEN) != null) {
doAddOrRemoveInterest(offers.get(clickedPosition).id);
if (offers.get(clickedPosition).isInterested == null) {
Toasty.info(context, "Saved", Toast.LENGTH_LONG, true).show();
ImageView interestedButton = v.findViewById(R.id.interest_fab);
interestedButton.setImageResource(R.drawable.ic_save_2);
Utils.setSavedUnsavedInGlobalStorage(comingFrom,clickedPosition,offer.id,new IsInterested());
offers.get(clickedPosition).isInterested = new IsInterested();
notifyDataSetChanged();
} else {
//this is for the sitaution if it is in interested offer activity then it should remove the item beside do unsaved
Toasty.info(context, "Unsaved", Toast.LENGTH_LONG, true).show();
Utils.setSavedUnsavedInGlobalStorage(comingFrom,clickedPosition,offer.id,null);
if(comingFrom.equals(Constants.IS_COMING_FROM_INTERESTED_OFFERS)){
GlobalDataStorage.INTERESTED_OFFERS.remove(clickedPosition);
notifyDataSetChanged();
return;
}
ImageView interestedButton = v.findViewById(R.id.interest_fab);
interestedButton.setImageResource(R.drawable.ic_unsave_2);
offers.get(clickedPosition).isInterested = null;
notifyDataSetChanged();
}
} else {
context.startActivity(new Intent(context, LoginActivity.class));
}
}
else {
Toasty.error(context, context.getString(R.string.no_internet), Toast.LENGTH_LONG, true).show();
}
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment