Skip to content

Instantly share code, notes, and snippets.

@moderateepheezy
Created April 12, 2017 10:35
Show Gist options
  • Save moderateepheezy/d6f584d20c4a0217b88bc3f57873a481 to your computer and use it in GitHub Desktop.
Save moderateepheezy/d6f584d20c4a0217b88bc3f57873a481 to your computer and use it in GitHub Desktop.
public class VendorNewAdapter extends FirebaseRecyclerAdapter<NewsPaper, NewsPaperHolder> implements SimpleAlertDialog.SingleChoiceArrayItemProvider{
private static final int REQUEST_CODE_SINGLE_CHOICE_LIST = 34005;
private static final int REQUEST_CODE_SINGLE_Any_CHOICE_LIST = 340005;
private static final java.lang.String CHOICE_DIALOG = "dialogTagChoice";
private boolean isColorsInverted = false;
private Context context;
private AppCompatActivity activity;
private DatabaseReference mDatabaseRef;
private DatabaseReference mDatabase;
private DatabaseReference childRef;
private DatabaseReference mDataRef;
private StorageReference mStorage;
private static boolean isUserSubscribed = false;
FirebaseAuth mAuth;
/**
* @param modelClass Firebase will marshall the data at a location into
* an instance of a class that you provide
* @param modelLayout This is the layout used to represent a single item in the list.
* You will be responsible for populating an instance of the corresponding
* view with the data from an instance of modelClass.
* @param viewHolderClass The class that hold references to all sub-views in an instance modelLayout.
* @param ref The Firebase location to watch for data changes. Can also be a slice of a location,
* using some combination of {@code limit()}, {@code startAt()}, and {@code endAt()}.
*/
public VendorNewAdapter(Class<NewsPaper> modelClass, int modelLayout,
Class<NewsPaperHolder> viewHolderClass, Query ref, AppCompatActivity activity,
boolean isViewWithList, Context context) {
super(modelClass, modelLayout, viewHolderClass, ref);
this.context = context;
this. activity = activity;
this.isColorsInverted = isViewWithList;
}
@Override
protected void populateViewHolder(final NewsPaperHolder viewHolder, final NewsPaper model, final int position) {
viewHolder.vendorName.setText(model.getPaper_name());
viewHolder.subscribe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isColorsInverted = !isColorsInverted;
if(isUserSubscribed){
isUserSubscribed = false;
TransitionManager.beginDelayedTransition(viewHolder.transitionsContainer, new AutoTransition());
viewHolder.subscribe.setBackgroundTintList(context.getResources().getColorStateList(R.color.colorAccent));
viewHolder.subscribe.setText("Subscribe");
unSubscribeVendor(getRef(position).getKey());
}else {
new SimpleAlertDialogFragment.Builder()
.setTitle("Choose one")
.setSingleChoiceCheckedItem(0) // This enables a single choice list
.setRequestCode(REQUEST_CODE_SINGLE_CHOICE_LIST)
.setNegativeButton("Cancel")
.setCancelable(true)
.create().show(activity.getFragmentManager(), "dialogTagChoice");
isUserSubscribed = true;
// SubscribeChoiceFragment dialog = new SubscribeChoiceFragment();
// dialog.show(activity.getFragmentManager(), CHOICE_DIALOG);
// TransitionManager.beginDelayedTransition(viewHolder.transitionsContainer, new AutoTransition());
// viewHolder.subscribe.setBackgroundTintList(context.getResources().getColorStateList(R.color.button_back_color));
// viewHolder.subscribe.setText("Unsubscribe");
// subscribeVendor(getRef(position).getKey());
}
}
});
}
public void updateUI(NewsPaperHolder holder, News news){
String latest = "Latest: ";
String caption = news.getCaption();
String newsTitle = latest + caption;
Spannable sb = new SpannableString( newsTitle );
//sb.setSpan(new StyleSpan(Typeface.BOLD), newsTitle.indexOf(latest) + latest.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold
//sb.setSpan(new AbsoluteSizeSpan(14), newsTitle.indexOf(latest)+ latest.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.firstNewsTitle.setText(latest + news.getCaption());
}
@Override
public void onSingleChoiceArrayItemClick(SimpleAlertDialog dialog, int requestCode, int position) {
if (requestCode == REQUEST_CODE_SINGLE_CHOICE_LIST) {
// Do something
new SimpleAlertDialogFragment.Builder()
.setTitle("Choose one")
.setSingleChoiceCheckedItem(0) // This enables a single choice list
.setRequestCode(REQUEST_CODE_SINGLE_Any_CHOICE_LIST)
.setCancelable(true)
.setNegativeButton("Cancel")
.create().show(activity.getFragmentManager(), "dialog2");
}else if (requestCode == REQUEST_CODE_SINGLE_Any_CHOICE_LIST){
isUserSubscribed = true;
//
}
}
@Override
public CharSequence[] onCreateSingleChoiceArray(SimpleAlertDialog dialog, int requestCode) {
if (requestCode == REQUEST_CODE_SINGLE_CHOICE_LIST) {
final CharSequence items [] = {"Daily","Weekly","Monthly"};
return items;
}else if(requestCode == REQUEST_CODE_SINGLE_Any_CHOICE_LIST){
final CharSequence items [] = {"Direct Billing","Mobile Money"};
return items;
}
return null;
}
}
@LordRahl90
Copy link

So which point is giving u issue???!

@tofbabs
Copy link

tofbabs commented Apr 12, 2017

Are we now good with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment