Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.calculator2; |
| 18 | |
| 19 | import android.support.v7.widget.RecyclerView; |
| 20 | import android.view.LayoutInflater; |
| 21 | import android.view.View; |
| 22 | import android.view.ViewGroup; |
| 23 | import android.widget.TextView; |
| 24 | |
| 25 | import java.util.ArrayList; |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 26 | import java.util.Calendar; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * Adapter for RecyclerView of HistoryItems. |
| 31 | */ |
| 32 | public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> { |
| 33 | |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 34 | private static final String TAG = "HistoryAdapter"; |
| 35 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 36 | private static final int EMPTY_VIEW_TYPE = 0; |
| 37 | private static final int HISTORY_VIEW_TYPE = 1; |
| 38 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 39 | private final Evaluator mEvaluator; |
| 40 | /* Text/accessibility descriptor for the current expression item. */ |
| 41 | private final String mCurrentExpressionDescription; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 42 | |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 43 | private final Calendar mCalendar = Calendar.getInstance(); |
| 44 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 45 | private List<HistoryItem> mDataSet; |
| 46 | |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 47 | private boolean mIsResultLayout; |
Annie Chin | 70ac8ea | 2016-11-18 14:43:56 -0800 | [diff] [blame] | 48 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 49 | public HistoryAdapter(Calculator calculator, ArrayList<HistoryItem> dataSet, |
| 50 | String currentExpressionDescription) { |
| 51 | mEvaluator = Evaluator.getInstance(calculator); |
| 52 | mDataSet = dataSet; |
| 53 | mCurrentExpressionDescription = currentExpressionDescription; |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 54 | setHasStableIds(true); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public HistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 59 | final View v; |
| 60 | if (viewType == HISTORY_VIEW_TYPE) { |
| 61 | v = LayoutInflater.from(parent.getContext()) |
| 62 | .inflate(R.layout.history_item, parent, false); |
| 63 | } else { |
| 64 | v = LayoutInflater.from(parent.getContext()) |
| 65 | .inflate(R.layout.empty_history_view, parent, false); |
| 66 | } |
| 67 | return new ViewHolder(v, viewType); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 71 | public void onBindViewHolder(final HistoryAdapter.ViewHolder holder, int position) { |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 72 | final HistoryItem item = getItem(position); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 73 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 74 | if (item.isEmptyView()) { |
| 75 | return; |
| 76 | } |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 77 | |
| 78 | holder.mFormula.setText(item.getFormula()); |
| 79 | // Note: HistoryItems that are not the current expression will always have interesting ops. |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 80 | holder.mResult.setEvaluator(mEvaluator, item.getEvaluatorIndex()); |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 81 | if (item.getEvaluatorIndex() == Evaluator.HISTORY_MAIN_INDEX) { |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 82 | holder.mDate.setText(mCurrentExpressionDescription); |
| 83 | holder.mDate.setContentDescription(mCurrentExpressionDescription); |
| 84 | } else { |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 85 | // If the previous item occurred on the same date, the current item does not need |
| 86 | // a date header. |
| 87 | if (shouldShowHeader(position, item)) { |
| 88 | holder.mDate.setText(item.getDateString()); |
| 89 | // Special case -- very first item should not have a divider above it. |
| 90 | holder.mDivider.setVisibility(position == getItemCount() - 1 |
| 91 | ? View.GONE : View.VISIBLE); |
| 92 | } else { |
| 93 | holder.mDate.setVisibility(View.GONE); |
| 94 | holder.mDivider.setVisibility(View.INVISIBLE); |
| 95 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 96 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void onViewRecycled(ViewHolder holder) { |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 101 | if (holder.getItemViewType() == EMPTY_VIEW_TYPE) { |
| 102 | return; |
| 103 | } |
| 104 | mEvaluator.cancel(holder.getItemId(), true); |
| 105 | |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 106 | holder.mDate.setVisibility(View.VISIBLE); |
| 107 | holder.mDivider.setVisibility(View.VISIBLE); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 108 | holder.mDate.setContentDescription(null); |
| 109 | holder.mDate.setText(null); |
| 110 | holder.mFormula.setText(null); |
| 111 | holder.mResult.setText(null); |
| 112 | |
| 113 | super.onViewRecycled(holder); |
| 114 | } |
| 115 | |
| 116 | @Override |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 117 | public long getItemId(int position) { |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 118 | return getItem(position).getEvaluatorIndex(); |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 122 | public int getItemViewType(int position) { |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 123 | return getItem(position).isEmptyView() ? EMPTY_VIEW_TYPE : HISTORY_VIEW_TYPE; |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 127 | public int getItemCount() { |
| 128 | return mDataSet.size(); |
| 129 | } |
| 130 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 131 | public void setDataSet(ArrayList<HistoryItem> dataSet) { |
| 132 | mDataSet = dataSet; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 135 | public void setIsResultLayout(boolean isResult) { |
| 136 | mIsResultLayout = isResult; |
| 137 | } |
| 138 | |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 139 | private int getEvaluatorIndex(int position) { |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 140 | if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator) || mIsResultLayout) { |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 141 | return (int) (mEvaluator.getMaxIndex() - position); |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 142 | } else { |
| 143 | // Account for the additional "Current Expression" with the +1. |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 144 | return (int) (mEvaluator.getMaxIndex() - position + 1); |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 148 | private boolean shouldShowHeader(int position, HistoryItem item) { |
| 149 | if (position == getItemCount() - 1) { |
| 150 | // First/oldest element should always show the header. |
| 151 | return true; |
| 152 | } |
| 153 | final HistoryItem prevItem = getItem(position + 1); |
| 154 | // We need to use Calendars to determine this because of Daylight Savings. |
| 155 | mCalendar.setTimeInMillis(item.getTimeInMillis()); |
| 156 | final int year = mCalendar.get(Calendar.YEAR); |
| 157 | final int day = mCalendar.get(Calendar.DAY_OF_YEAR); |
| 158 | mCalendar.setTimeInMillis(prevItem.getTimeInMillis()); |
| 159 | final int prevYear = mCalendar.get(Calendar.YEAR); |
| 160 | final int prevDay = mCalendar.get(Calendar.DAY_OF_YEAR); |
| 161 | return year != prevYear || day != prevDay; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Gets the HistoryItem from mDataSet, lazy-filling the dataSet if necessary. |
| 166 | */ |
| 167 | private HistoryItem getItem(int position) { |
| 168 | HistoryItem item = mDataSet.get(position); |
| 169 | // Lazy-fill the data set. |
| 170 | if (item == null) { |
| 171 | final int evaluatorIndex = getEvaluatorIndex(position); |
| 172 | item = new HistoryItem(evaluatorIndex, |
| 173 | mEvaluator.getTimeStamp(evaluatorIndex), |
| 174 | mEvaluator.getExprAsSpannable(evaluatorIndex)); |
| 175 | mDataSet.set(position, item); |
| 176 | } |
| 177 | return item; |
Annie Chin | 70ac8ea | 2016-11-18 14:43:56 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 180 | public static class ViewHolder extends RecyclerView.ViewHolder { |
| 181 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 182 | private TextView mDate; |
Annie Chin | 8149c8c | 2016-11-28 13:44:09 -0800 | [diff] [blame] | 183 | private AlignedTextView mFormula; |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 184 | private CalculatorResult mResult; |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 185 | private View mDivider; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 186 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 187 | public ViewHolder(View v, int viewType) { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 188 | super(v); |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 189 | if (viewType == EMPTY_VIEW_TYPE) { |
| 190 | return; |
| 191 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 192 | mDate = (TextView) v.findViewById(R.id.history_date); |
Annie Chin | 8149c8c | 2016-11-28 13:44:09 -0800 | [diff] [blame] | 193 | mFormula = (AlignedTextView) v.findViewById(R.id.history_formula); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 194 | mResult = (CalculatorResult) v.findViewById(R.id.history_result); |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 195 | mDivider = v.findViewById(R.id.history_divider); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Annie Chin | 8149c8c | 2016-11-28 13:44:09 -0800 | [diff] [blame] | 198 | public AlignedTextView getFormula() { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 199 | return mFormula; |
| 200 | } |
| 201 | |
| 202 | public CalculatorResult getResult() { |
| 203 | return mResult; |
| 204 | } |
| 205 | |
| 206 | public TextView getDate() { |
| 207 | return mDate; |
| 208 | } |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 209 | |
| 210 | public View getDivider() { |
| 211 | return mDivider; |
| 212 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 213 | } |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame^] | 214 | } |