|
| 1 | +package apps.jizzu.simpletodo.widget; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.support.v4.content.ContextCompat; |
| 5 | +import android.text.format.DateUtils; |
| 6 | +import android.util.Log; |
| 7 | +import android.view.Display; |
| 8 | +import android.view.View; |
| 9 | +import android.view.WindowManager; |
| 10 | +import android.widget.RemoteViews; |
| 11 | +import android.widget.RemoteViewsService; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Calendar; |
| 15 | + |
| 16 | +import apps.jizzu.simpletodo.R; |
| 17 | +import apps.jizzu.simpletodo.adapter.RecyclerViewAdapter; |
| 18 | +import apps.jizzu.simpletodo.model.ModelTask; |
| 19 | +import apps.jizzu.simpletodo.utils.Utils; |
| 20 | + |
| 21 | +import static android.content.ContentValues.TAG; |
| 22 | + |
| 23 | +/** |
| 24 | + * Class that will fill the list with values. |
| 25 | + * It's methods are very similar to the standard adapter methods. |
| 26 | + */ |
| 27 | +public class ViewFactory implements RemoteViewsService.RemoteViewsFactory { |
| 28 | + |
| 29 | + private ArrayList<ModelTask> mListData; |
| 30 | + private Context mContext; |
| 31 | + |
| 32 | + ViewFactory(Context context) { |
| 33 | + mContext = context; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Called when factory is first constructed. |
| 38 | + */ |
| 39 | + @Override |
| 40 | + public void onCreate() { |
| 41 | + mListData = new ArrayList<>(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Called when notifyDataSetChanged() is triggered on the remote adapter. |
| 46 | + */ |
| 47 | + @Override |
| 48 | + public void onDataSetChanged() { |
| 49 | + mListData.clear(); |
| 50 | + mListData.addAll(RecyclerViewAdapter.mItems); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Called when the last RemoteViewsAdapter that is associated with this factory is unbound. |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public void onDestroy() { |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public int getCount() { |
| 63 | + return mListData.size(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get a View that displays the data at the specified position in the data set. |
| 68 | + */ |
| 69 | + @Override |
| 70 | + public RemoteViews getViewAt(int position) { |
| 71 | + RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.widget_item); |
| 72 | + remoteViews.setTextViewText(R.id.item_title, mListData.get(position).getTitle()); |
| 73 | + |
| 74 | + if (mListData.get(position).getDate() != 0) { |
| 75 | + remoteViews.setViewPadding(R.id.item_title, 0, 0, 0, 0); |
| 76 | + remoteViews.setViewVisibility(R.id.item_date, View.VISIBLE); |
| 77 | + |
| 78 | + if (DateUtils.isToday(mListData.get(position).getDate())) { |
| 79 | + remoteViews.setTextViewText(R.id.item_date, mContext.getString(R.string.reminder_today) + " " + Utils.getTime(mListData.get(position).getDate())); |
| 80 | + } else if (DateUtils.isToday(mListData.get(position).getDate() + DateUtils.DAY_IN_MILLIS)) { |
| 81 | + remoteViews.setTextColor(R.id.item_date, ContextCompat.getColor(mContext, R.color.red)); |
| 82 | + remoteViews.setTextViewText(R.id.item_date, mContext.getString(R.string.reminder_yesterday) + " " + Utils.getTime(mListData.get(position).getDate())); |
| 83 | + } else if (DateUtils.isToday(mListData.get(position).getDate() - DateUtils.DAY_IN_MILLIS)) { |
| 84 | + remoteViews.setTextViewText(R.id.item_date, mContext.getString(R.string.reminder_tomorrow) + " " + Utils.getTime(mListData.get(position).getDate())); |
| 85 | + } else if (mListData.get(position).getDate() < Calendar.getInstance().getTimeInMillis()) { |
| 86 | + remoteViews.setTextColor(R.id.item_date, ContextCompat.getColor(mContext, R.color.red)); |
| 87 | + remoteViews.setTextViewText(R.id.item_date, Utils.getFullDate(mListData.get(position).getDate())); |
| 88 | + } else { |
| 89 | + remoteViews.setTextViewText(R.id.item_date, Utils.getFullDate(mListData.get(position).getDate())); |
| 90 | + } |
| 91 | + } else { |
| 92 | + Display d = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); |
| 93 | + int width = d.getWidth(); |
| 94 | + int height = d.getHeight(); |
| 95 | + Log.d(TAG, "width = " + width + ", height = " + height); |
| 96 | + |
| 97 | + remoteViews.setViewVisibility(R.id.item_date, View.GONE); |
| 98 | + if (width >= 1080 || height >= 1776) { |
| 99 | + remoteViews.setViewPadding(R.id.item_title, 0, 27, 0, 27); |
| 100 | + } else if (width >= 720 || height >= 1184) { |
| 101 | + remoteViews.setViewPadding(R.id.item_title, 0, 20, 0, 20); |
| 102 | + } else if (width >= 480 || height >= 800) { |
| 103 | + remoteViews.setViewPadding(R.id.item_title, 0, 15, 0, 15); |
| 104 | + } |
| 105 | + } |
| 106 | + return remoteViews; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * This allows for the use of a custom loading view which appears between the time that getViewAt(int) is called and returns. |
| 111 | + */ |
| 112 | + @Override |
| 113 | + public RemoteViews getLoadingView() { |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Returns the number of types of Views that will be created by getView(). |
| 119 | + * This adapter always returns the same type of View for all items. |
| 120 | + */ |
| 121 | + @Override |
| 122 | + public int getViewTypeCount() { |
| 123 | + return 1; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public long getItemId(int position) { |
| 128 | + return position; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Indicates whether the item ids are stable across changes to the underlying data.. |
| 133 | + * Returns true if the same id always refers to the same object. |
| 134 | + */ |
| 135 | + @Override |
| 136 | + public boolean hasStableIds() { |
| 137 | + return true; |
| 138 | + } |
| 139 | +} |
0 commit comments