Advertisement
Grejt_cz

UkolListAdapter.java

May 13th, 2018
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.95 KB | None | 0 0
  1. package cz.grejtCZ.ukolnicek.android.headeredList;
  2.  
  3. import android.arch.paging.PagedListAdapter;
  4. import android.content.Context;
  5. import android.support.annotation.NonNull;
  6. import android.support.v4.content.ContextCompat;
  7. import android.support.v7.util.DiffUtil;
  8. import android.support.v7.widget.RecyclerView;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.CheckBox;
  13. import android.widget.TextView;
  14.  
  15. import org.joda.time.LocalDate;
  16. import org.joda.time.format.DateTimeFormat;
  17. import org.joda.time.format.DateTimeFormatter;
  18.  
  19. import cz.grejtCZ.ukolnicek.android.R;
  20. import cz.grejtCZ.ukolnicek.android.activity.MainActivity;
  21. import cz.grejtCZ.ukolnicek.android.sql.SQLUkol; //I had old system without database, so most database-based object are colled SQL-something
  22.  
  23.  
  24. public class UkolListAdapter extends PagedListAdapter<UkolOrHeader, UkolListAdapter.Holder> {
  25.     public static final int TYPE_HEADER = 1;
  26.     public static final int TYPE_ITEM = 0;
  27.  
  28.  
  29.     Context context;
  30.     MainActivity.ClickCallback callback;
  31.  
  32.     public UkolListAdapter(Context context, MainActivity.ClickCallback callback) {
  33.         super(DIFF_CALLBACK);
  34.         this.context = context;
  35.         this.callback = callback;
  36.     }
  37.  
  38.     /**
  39.      * Reutrns {@link #TYPE_HEADER} or {@link #TYPE_ITEM} whether item on given position  is {@link Header} or {@link UkolInList}
  40.      * @param position position of the item in list
  41.      * @return {@link #TYPE_HEADER} or {@link #TYPE_ITEM} whether item on given position  is {@link Header} or {@link UkolInList}
  42.      */
  43.     @Override
  44.     public int getItemViewType(int position) {
  45.         if (getItem(position) instanceof UkolInList)
  46.             return TYPE_ITEM;
  47.         return TYPE_HEADER;
  48.     }
  49.  
  50.     @Override
  51.     public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
  52.         LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  53.         if (viewType == TYPE_ITEM)
  54.         return new UkolHolder(layoutInflater.inflate(R.layout.ukol_list_item, parent, false), callback);
  55.         else
  56.             return new HeaderHolder(layoutInflater.inflate(R.layout.ukol_list_header,parent,false));
  57.     }
  58.  
  59.     @Override
  60.     public void onBindViewHolder(Holder holder, int position) {
  61.         UkolOrHeader ukol = getItem(position);
  62.         if (ukol != null) {
  63.             holder.bindTo(ukol);
  64.         } else {
  65.             holder.clear();
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * Base Holder class
  71.      */
  72.     public static abstract class Holder extends RecyclerView.ViewHolder {
  73.         public Holder(View itemView) {
  74.             super(itemView);
  75.         }
  76.  
  77.         /**
  78.          * Binds the holder to the give {@link UkolOrHeader}
  79.          * @param item item to bind to
  80.          */
  81.         public abstract void bindTo(UkolOrHeader item);
  82.  
  83.         /**
  84.          * Unbinds the holder
  85.          */
  86.         public abstract void clear();
  87.     }
  88.  
  89.     /**
  90.      * Holder for Ukols
  91.      */
  92.     public static class UkolHolder extends Holder {
  93.         public TextView textView;
  94.         public CheckBox checkBox;
  95.         public View view;
  96.         public MainActivity.ClickCallback callBack;
  97.  
  98.         public UkolHolder(View itemView, MainActivity.ClickCallback callBack) {
  99.             super(itemView);
  100.             view = itemView;
  101.             textView = itemView.findViewById(R.id.textView);
  102.             checkBox = itemView.findViewById(R.id.checkBox);
  103.             this.callBack = callBack;
  104.         }
  105.  
  106.         public void bindTo(UkolOrHeader ukolInList) {
  107.             if (!(ukolInList instanceof UkolInList))
  108.                 throw new IllegalArgumentException("This is holder for UkolInList a given argument is not UkolInList");
  109.  
  110.             final SQLUkol ukol = ((UkolInList) ukolInList).getUkol();
  111.             checkBox.setChecked(ukol.isCompleted());
  112.             checkBox.setVisibility(View.VISIBLE);
  113.             textView.setText(ukol.getTitle());
  114.             final long fid = ukol.getId();
  115.             view.setOnClickListener(new View.OnClickListener() {
  116.                 @Override
  117.                 public void onClick(View v) {
  118.                     callBack.edit(fid);
  119.                 }
  120.             });
  121.             checkBox.setOnClickListener(new View.OnClickListener() {
  122.                 @Override
  123.                 public void onClick(View v) {
  124.                     //here is the "checked" state of Ukol updated and written to the Room database
  125.                     ukol.setCompleted(checkBox.isChecked());
  126.                     ukol.save();
  127.                 }
  128.             });
  129.  
  130.         }
  131.  
  132.         public void clear() {
  133.             textView.setText("");
  134.             checkBox.setChecked(false);
  135.             checkBox.setVisibility(View.INVISIBLE);
  136.             view.setOnClickListener(new View.OnClickListener() {
  137.                 @Override
  138.                 public void onClick(View v) {
  139.  
  140.                 }
  141.             });
  142.             //id se nemeni
  143.         }
  144.     }
  145.  
  146.     public static class HeaderHolder extends Holder {
  147.         TextView textView;
  148.         LocalDate date;
  149.  
  150.         private final DateTimeFormatter DTF = DateTimeFormat.forPattern("EEE dd. MMMM YYYY");
  151.  
  152.  
  153.         public HeaderHolder(View itemView) {
  154.             super(itemView);
  155.             textView = itemView.findViewById(R.id.textView);
  156.         }
  157.  
  158.         @Override
  159.         public void bindTo(UkolOrHeader item) {
  160.             if (!(item instanceof Header))
  161.                 throw new IllegalArgumentException("This is holder for Header a given argument is not Header");
  162.             date = ((Header) item).getDate();
  163.  
  164.             Context context = textView.getContext();
  165.  
  166.             if (date == null) {
  167.                 textView.setText(R.string.no_date);
  168.                 textView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
  169.             } else {
  170.                 if (date.isBefore(LocalDate.now())) {
  171.                     textView.setTextColor(ContextCompat.getColor(context, R.color.colorMinimal));
  172.                 } else {
  173.                     textView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
  174.                 }
  175.  
  176.                 LocalDate now = LocalDate.now();
  177.  
  178.                 if (date.isEqual(now.minusDays(1)))
  179.                     textView.setText(R.string.yesterday);
  180.                 else if (date.isEqual(now))
  181.                     textView.setText(R.string.today);
  182.                 else if (date.isEqual(now.plusDays(1)))
  183.                     textView.setText(R.string.tommorow);
  184.                 else {
  185.                     textView.setText(DTF.print(date));
  186.                 }
  187.             }
  188.         }
  189.  
  190.         @Override
  191.         public void clear() {
  192.             textView.setText("");
  193.         }
  194.     }
  195.  
  196.     /**
  197.      * Calculates Differences between old and new items
  198.      */
  199.     public static final DiffUtil.ItemCallback<UkolOrHeader> DIFF_CALLBACK = new DiffUtil.ItemCallback<UkolOrHeader>() {
  200.         @Override
  201.         public boolean areItemsTheSame(@NonNull UkolOrHeader old, @NonNull UkolOrHeader young) {
  202.             boolean oldUkol = old instanceof UkolInList;
  203.             boolean youngUkol = young instanceof UkolInList;
  204.             if (oldUkol != youngUkol)
  205.                 return false;
  206.             if (!youngUkol) {
  207.                 return old.equals(young);
  208.             }
  209.             return ((UkolInList) old).getUkol().getId() == ((UkolInList) young).getUkol().getId();
  210.         }
  211.  
  212.         @Override
  213.         public boolean areContentsTheSame(@NonNull UkolOrHeader old, @NonNull UkolOrHeader young) {
  214.             boolean oldUkol = old instanceof UkolInList;
  215.             boolean youngUkol = young instanceof UkolInList;
  216.             if (oldUkol != youngUkol)
  217.                 return false;
  218.             if (!youngUkol) {
  219.                 return old.equals(young);
  220.             }
  221.             return ((UkolInList) old).getUkol().getTitle().equals(((UkolInList) young).getUkol().getTitle());
  222.         }
  223.     };
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement