Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.grejtCZ.ukolnicek.android.headeredList;
- import android.arch.paging.PagedListAdapter;
- import android.content.Context;
- import android.support.annotation.NonNull;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.util.DiffUtil;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.CheckBox;
- import android.widget.TextView;
- import org.joda.time.LocalDate;
- import org.joda.time.format.DateTimeFormat;
- import org.joda.time.format.DateTimeFormatter;
- import cz.grejtCZ.ukolnicek.android.R;
- import cz.grejtCZ.ukolnicek.android.activity.MainActivity;
- import cz.grejtCZ.ukolnicek.android.sql.SQLUkol; //I had old system without database, so most database-based object are colled SQL-something
- public class UkolListAdapter extends PagedListAdapter<UkolOrHeader, UkolListAdapter.Holder> {
- public static final int TYPE_HEADER = 1;
- public static final int TYPE_ITEM = 0;
- Context context;
- MainActivity.ClickCallback callback;
- public UkolListAdapter(Context context, MainActivity.ClickCallback callback) {
- super(DIFF_CALLBACK);
- this.context = context;
- this.callback = callback;
- }
- /**
- * Reutrns {@link #TYPE_HEADER} or {@link #TYPE_ITEM} whether item on given position is {@link Header} or {@link UkolInList}
- * @param position position of the item in list
- * @return {@link #TYPE_HEADER} or {@link #TYPE_ITEM} whether item on given position is {@link Header} or {@link UkolInList}
- */
- @Override
- public int getItemViewType(int position) {
- if (getItem(position) instanceof UkolInList)
- return TYPE_ITEM;
- return TYPE_HEADER;
- }
- @Override
- public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
- LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- if (viewType == TYPE_ITEM)
- return new UkolHolder(layoutInflater.inflate(R.layout.ukol_list_item, parent, false), callback);
- else
- return new HeaderHolder(layoutInflater.inflate(R.layout.ukol_list_header,parent,false));
- }
- @Override
- public void onBindViewHolder(Holder holder, int position) {
- UkolOrHeader ukol = getItem(position);
- if (ukol != null) {
- holder.bindTo(ukol);
- } else {
- holder.clear();
- }
- }
- /**
- * Base Holder class
- */
- public static abstract class Holder extends RecyclerView.ViewHolder {
- public Holder(View itemView) {
- super(itemView);
- }
- /**
- * Binds the holder to the give {@link UkolOrHeader}
- * @param item item to bind to
- */
- public abstract void bindTo(UkolOrHeader item);
- /**
- * Unbinds the holder
- */
- public abstract void clear();
- }
- /**
- * Holder for Ukols
- */
- public static class UkolHolder extends Holder {
- public TextView textView;
- public CheckBox checkBox;
- public View view;
- public MainActivity.ClickCallback callBack;
- public UkolHolder(View itemView, MainActivity.ClickCallback callBack) {
- super(itemView);
- view = itemView;
- textView = itemView.findViewById(R.id.textView);
- checkBox = itemView.findViewById(R.id.checkBox);
- this.callBack = callBack;
- }
- public void bindTo(UkolOrHeader ukolInList) {
- if (!(ukolInList instanceof UkolInList))
- throw new IllegalArgumentException("This is holder for UkolInList a given argument is not UkolInList");
- final SQLUkol ukol = ((UkolInList) ukolInList).getUkol();
- checkBox.setChecked(ukol.isCompleted());
- checkBox.setVisibility(View.VISIBLE);
- textView.setText(ukol.getTitle());
- final long fid = ukol.getId();
- view.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- callBack.edit(fid);
- }
- });
- checkBox.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //here is the "checked" state of Ukol updated and written to the Room database
- ukol.setCompleted(checkBox.isChecked());
- ukol.save();
- }
- });
- }
- public void clear() {
- textView.setText("");
- checkBox.setChecked(false);
- checkBox.setVisibility(View.INVISIBLE);
- view.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- }
- });
- //id se nemeni
- }
- }
- public static class HeaderHolder extends Holder {
- TextView textView;
- LocalDate date;
- private final DateTimeFormatter DTF = DateTimeFormat.forPattern("EEE dd. MMMM YYYY");
- public HeaderHolder(View itemView) {
- super(itemView);
- textView = itemView.findViewById(R.id.textView);
- }
- @Override
- public void bindTo(UkolOrHeader item) {
- if (!(item instanceof Header))
- throw new IllegalArgumentException("This is holder for Header a given argument is not Header");
- date = ((Header) item).getDate();
- Context context = textView.getContext();
- if (date == null) {
- textView.setText(R.string.no_date);
- textView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
- } else {
- if (date.isBefore(LocalDate.now())) {
- textView.setTextColor(ContextCompat.getColor(context, R.color.colorMinimal));
- } else {
- textView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
- }
- LocalDate now = LocalDate.now();
- if (date.isEqual(now.minusDays(1)))
- textView.setText(R.string.yesterday);
- else if (date.isEqual(now))
- textView.setText(R.string.today);
- else if (date.isEqual(now.plusDays(1)))
- textView.setText(R.string.tommorow);
- else {
- textView.setText(DTF.print(date));
- }
- }
- }
- @Override
- public void clear() {
- textView.setText("");
- }
- }
- /**
- * Calculates Differences between old and new items
- */
- public static final DiffUtil.ItemCallback<UkolOrHeader> DIFF_CALLBACK = new DiffUtil.ItemCallback<UkolOrHeader>() {
- @Override
- public boolean areItemsTheSame(@NonNull UkolOrHeader old, @NonNull UkolOrHeader young) {
- boolean oldUkol = old instanceof UkolInList;
- boolean youngUkol = young instanceof UkolInList;
- if (oldUkol != youngUkol)
- return false;
- if (!youngUkol) {
- return old.equals(young);
- }
- return ((UkolInList) old).getUkol().getId() == ((UkolInList) young).getUkol().getId();
- }
- @Override
- public boolean areContentsTheSame(@NonNull UkolOrHeader old, @NonNull UkolOrHeader young) {
- boolean oldUkol = old instanceof UkolInList;
- boolean youngUkol = young instanceof UkolInList;
- if (oldUkol != youngUkol)
- return false;
- if (!youngUkol) {
- return old.equals(young);
- }
- return ((UkolInList) old).getUkol().getTitle().equals(((UkolInList) young).getUkol().getTitle());
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement