blob: ca5509c2a6fc1321e97d77db2a045b48de720309 [file] [log] [blame]
Annie Chind0f87d22016-10-24 09:04:12 -07001/*
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
17package com.android.calculator2;
18
19import android.support.v7.widget.RecyclerView;
20import android.view.LayoutInflater;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.TextView;
24
25import java.util.ArrayList;
Annie Chind0f87d22016-10-24 09:04:12 -070026import java.util.List;
27
28/**
29 * Adapter for RecyclerView of HistoryItems.
30 */
31public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {
32
Annie Chinab657d42016-11-03 16:43:59 -070033 private static final int EMPTY_VIEW_TYPE = 0;
34 private static final int HISTORY_VIEW_TYPE = 1;
35
Annie Chin06fd3cf2016-11-07 16:04:33 -080036 private final Evaluator mEvaluator;
37 /* Text/accessibility descriptor for the current expression item. */
38 private final String mCurrentExpressionDescription;
Annie Chind0f87d22016-10-24 09:04:12 -070039
Annie Chin06fd3cf2016-11-07 16:04:33 -080040 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 Chin91796232016-11-16 17:27:36 -080047 setHasStableIds(true);
Annie Chind0f87d22016-10-24 09:04:12 -070048 }
49
50 @Override
51 public HistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Annie Chinab657d42016-11-03 16:43:59 -070052 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 Chind0f87d22016-10-24 09:04:12 -070061 }
62
63 @Override
Annie Chin06fd3cf2016-11-07 16:04:33 -080064 public void onBindViewHolder(final HistoryAdapter.ViewHolder holder, int position) {
Annie Chind0f87d22016-10-24 09:04:12 -070065 final HistoryItem item = mDataSet.get(position);
66
Annie Chinab657d42016-11-03 16:43:59 -070067 if (item.isEmptyView()) {
68 return;
69 }
Annie Chin06fd3cf2016-11-07 16:04:33 -080070
71 holder.mFormula.setText(item.getFormula());
72 // Note: HistoryItems that are not the current expression will always have interesting ops.
Annie Chin91796232016-11-16 17:27:36 -080073 holder.mResult.setEvaluator(mEvaluator, item.getEvaluatorIndex());
74 if (item.getEvaluatorIndex() == Evaluator.MAIN_INDEX) {
Annie Chin06fd3cf2016-11-07 16:04:33 -080075 holder.mDate.setText(mCurrentExpressionDescription);
76 holder.mDate.setContentDescription(mCurrentExpressionDescription);
77 } else {
Annie Chind0f87d22016-10-24 09:04:12 -070078 holder.mDate.setText(item.getDateString());
Annie Chind0f87d22016-10-24 09:04:12 -070079 }
Annie Chind0f87d22016-10-24 09:04:12 -070080 }
81
82 @Override
83 public void onViewRecycled(ViewHolder holder) {
Annie Chin91796232016-11-16 17:27:36 -080084 if (holder.getItemViewType() == EMPTY_VIEW_TYPE) {
85 return;
86 }
87 mEvaluator.cancel(holder.getItemId(), true);
88
Annie Chind0f87d22016-10-24 09:04:12 -070089 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 Chin91796232016-11-16 17:27:36 -080098 public long getItemId(int position) {
99 return mDataSet.get(position).getEvaluatorIndex();
100 }
101
102 @Override
Annie Chinab657d42016-11-03 16:43:59 -0700103 public int getItemViewType(int position) {
Annie Chin06fd3cf2016-11-07 16:04:33 -0800104 HistoryItem item = mDataSet.get(position);
105
Annie Chinbc001882016-11-09 19:41:21 -0800106 // Continue to lazy-fill the data set
Annie Chin06fd3cf2016-11-07 16:04:33 -0800107 if (item == null) {
Annie Chin91796232016-11-16 17:27:36 -0800108 final int evaluatorIndex = getEvaluatorIndex(position);
109 item = new HistoryItem(evaluatorIndex, mEvaluator.getTimeStamp(evaluatorIndex),
110 mEvaluator.getExprAsSpannable(evaluatorIndex));
Annie Chinbc001882016-11-09 19:41:21 -0800111 mDataSet.set(position, item);
Annie Chin06fd3cf2016-11-07 16:04:33 -0800112 }
113 return item.isEmptyView() ? EMPTY_VIEW_TYPE : HISTORY_VIEW_TYPE;
Annie Chinab657d42016-11-03 16:43:59 -0700114 }
115
116 @Override
Annie Chind0f87d22016-10-24 09:04:12 -0700117 public int getItemCount() {
118 return mDataSet.size();
119 }
120
Annie Chin06fd3cf2016-11-07 16:04:33 -0800121 public void setDataSet(ArrayList<HistoryItem> dataSet) {
122 mDataSet = dataSet;
Annie Chind0f87d22016-10-24 09:04:12 -0700123 }
124
Annie Chin91796232016-11-16 17:27:36 -0800125 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 Chind0f87d22016-10-24 09:04:12 -0700134 public static class ViewHolder extends RecyclerView.ViewHolder {
135
Annie Chinab657d42016-11-03 16:43:59 -0700136 private TextView mDate;
137 private CalculatorFormula mFormula;
138 private CalculatorResult mResult;
Annie Chind0f87d22016-10-24 09:04:12 -0700139
Annie Chinab657d42016-11-03 16:43:59 -0700140 public ViewHolder(View v, int viewType) {
Annie Chind0f87d22016-10-24 09:04:12 -0700141 super(v);
Annie Chinab657d42016-11-03 16:43:59 -0700142 if (viewType == EMPTY_VIEW_TYPE) {
143 return;
144 }
Annie Chind0f87d22016-10-24 09:04:12 -0700145 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}