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 | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * Adapter for RecyclerView of HistoryItems. |
| 30 | */ |
| 31 | public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> { |
| 32 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 33 | private static final int EMPTY_VIEW_TYPE = 0; |
| 34 | private static final int HISTORY_VIEW_TYPE = 1; |
| 35 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 36 | private final Evaluator mEvaluator; |
| 37 | /* Text/accessibility descriptor for the current expression item. */ |
| 38 | private final String mCurrentExpressionDescription; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 39 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 40 | private List<HistoryItem> mDataSet; |
| 41 | |
| 42 | public HistoryAdapter(Calculator calculator, ArrayList<HistoryItem> dataSet, |
| 43 | String currentExpressionDescription) { |
| 44 | mEvaluator = Evaluator.getInstance(calculator); |
| 45 | mDataSet = dataSet; |
| 46 | mCurrentExpressionDescription = currentExpressionDescription; |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame^] | 47 | setHasStableIds(true); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public HistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 52 | final View v; |
| 53 | if (viewType == HISTORY_VIEW_TYPE) { |
| 54 | v = LayoutInflater.from(parent.getContext()) |
| 55 | .inflate(R.layout.history_item, parent, false); |
| 56 | } else { |
| 57 | v = LayoutInflater.from(parent.getContext()) |
| 58 | .inflate(R.layout.empty_history_view, parent, false); |
| 59 | } |
| 60 | return new ViewHolder(v, viewType); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 64 | public void onBindViewHolder(final HistoryAdapter.ViewHolder holder, int position) { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 65 | final HistoryItem item = mDataSet.get(position); |
| 66 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 67 | if (item.isEmptyView()) { |
| 68 | return; |
| 69 | } |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 70 | |
| 71 | holder.mFormula.setText(item.getFormula()); |
| 72 | // 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^] | 73 | holder.mResult.setEvaluator(mEvaluator, item.getEvaluatorIndex()); |
| 74 | if (item.getEvaluatorIndex() == Evaluator.MAIN_INDEX) { |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 75 | holder.mDate.setText(mCurrentExpressionDescription); |
| 76 | holder.mDate.setContentDescription(mCurrentExpressionDescription); |
| 77 | } else { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 78 | holder.mDate.setText(item.getDateString()); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 79 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onViewRecycled(ViewHolder holder) { |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame^] | 84 | if (holder.getItemViewType() == EMPTY_VIEW_TYPE) { |
| 85 | return; |
| 86 | } |
| 87 | mEvaluator.cancel(holder.getItemId(), true); |
| 88 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 89 | holder.mDate.setContentDescription(null); |
| 90 | holder.mDate.setText(null); |
| 91 | holder.mFormula.setText(null); |
| 92 | holder.mResult.setText(null); |
| 93 | |
| 94 | super.onViewRecycled(holder); |
| 95 | } |
| 96 | |
| 97 | @Override |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame^] | 98 | public long getItemId(int position) { |
| 99 | return mDataSet.get(position).getEvaluatorIndex(); |
| 100 | } |
| 101 | |
| 102 | @Override |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 103 | public int getItemViewType(int position) { |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 104 | HistoryItem item = mDataSet.get(position); |
| 105 | |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 106 | // Continue to lazy-fill the data set |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 107 | if (item == null) { |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame^] | 108 | final int evaluatorIndex = getEvaluatorIndex(position); |
| 109 | item = new HistoryItem(evaluatorIndex, mEvaluator.getTimeStamp(evaluatorIndex), |
| 110 | mEvaluator.getExprAsSpannable(evaluatorIndex)); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 111 | mDataSet.set(position, item); |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 112 | } |
| 113 | return item.isEmptyView() ? EMPTY_VIEW_TYPE : HISTORY_VIEW_TYPE; |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 117 | public int getItemCount() { |
| 118 | return mDataSet.size(); |
| 119 | } |
| 120 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 121 | public void setDataSet(ArrayList<HistoryItem> dataSet) { |
| 122 | mDataSet = dataSet; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame^] | 125 | private int getEvaluatorIndex(int position) { |
| 126 | if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) { |
| 127 | return (int) mEvaluator.getMaxIndex() - position; |
| 128 | } else { |
| 129 | // Account for the additional "Current Expression" with the +1. |
| 130 | return (int) mEvaluator.getMaxIndex() - position + 1; |
| 131 | } |
| 132 | } |
| 133 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 134 | public static class ViewHolder extends RecyclerView.ViewHolder { |
| 135 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 136 | private TextView mDate; |
| 137 | private CalculatorFormula mFormula; |
| 138 | private CalculatorResult mResult; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 139 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 140 | public ViewHolder(View v, int viewType) { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 141 | super(v); |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame] | 142 | if (viewType == EMPTY_VIEW_TYPE) { |
| 143 | return; |
| 144 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 145 | mDate = (TextView) v.findViewById(R.id.history_date); |
| 146 | mFormula = (CalculatorFormula) v.findViewById(R.id.history_formula); |
| 147 | mResult = (CalculatorResult) v.findViewById(R.id.history_result); |
| 148 | } |
| 149 | |
| 150 | public CalculatorFormula getFormula() { |
| 151 | return mFormula; |
| 152 | } |
| 153 | |
| 154 | public CalculatorResult getResult() { |
| 155 | return mResult; |
| 156 | } |
| 157 | |
| 158 | public TextView getDate() { |
| 159 | return mDate; |
| 160 | } |
| 161 | } |
| 162 | } |