blob: 68ad7bf3620f704f454361988951c5a6e0c9a614 [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 Chin36147982016-12-01 15:07:34 -080026import java.util.Calendar;
Annie Chind0f87d22016-10-24 09:04:12 -070027import java.util.List;
28
29/**
30 * Adapter for RecyclerView of HistoryItems.
31 */
32public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {
33
Annie Chin36147982016-12-01 15:07:34 -080034 private static final String TAG = "HistoryAdapter";
35
Annie Chinab657d42016-11-03 16:43:59 -070036 private static final int EMPTY_VIEW_TYPE = 0;
37 private static final int HISTORY_VIEW_TYPE = 1;
38
Annie Chin06fd3cf2016-11-07 16:04:33 -080039 private final Evaluator mEvaluator;
40 /* Text/accessibility descriptor for the current expression item. */
41 private final String mCurrentExpressionDescription;
Annie Chind0f87d22016-10-24 09:04:12 -070042
Annie Chin36147982016-12-01 15:07:34 -080043 private final Calendar mCalendar = Calendar.getInstance();
44
Annie Chin06fd3cf2016-11-07 16:04:33 -080045 private List<HistoryItem> mDataSet;
46
Annie Chin94c1bd92016-11-23 13:39:56 -080047 private boolean mIsResultLayout;
Annie Chin70ac8ea2016-11-18 14:43:56 -080048
Annie Chin06fd3cf2016-11-07 16:04:33 -080049 public HistoryAdapter(Calculator calculator, ArrayList<HistoryItem> dataSet,
50 String currentExpressionDescription) {
51 mEvaluator = Evaluator.getInstance(calculator);
52 mDataSet = dataSet;
53 mCurrentExpressionDescription = currentExpressionDescription;
Annie Chin91796232016-11-16 17:27:36 -080054 setHasStableIds(true);
Annie Chind0f87d22016-10-24 09:04:12 -070055 }
56
57 @Override
58 public HistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Annie Chinab657d42016-11-03 16:43:59 -070059 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 Chind0f87d22016-10-24 09:04:12 -070068 }
69
70 @Override
Annie Chin06fd3cf2016-11-07 16:04:33 -080071 public void onBindViewHolder(final HistoryAdapter.ViewHolder holder, int position) {
Annie Chin36147982016-12-01 15:07:34 -080072 final HistoryItem item = getItem(position);
Annie Chind0f87d22016-10-24 09:04:12 -070073
Annie Chinab657d42016-11-03 16:43:59 -070074 if (item.isEmptyView()) {
75 return;
76 }
Annie Chin06fd3cf2016-11-07 16:04:33 -080077
78 holder.mFormula.setText(item.getFormula());
79 // Note: HistoryItems that are not the current expression will always have interesting ops.
Annie Chin91796232016-11-16 17:27:36 -080080 holder.mResult.setEvaluator(mEvaluator, item.getEvaluatorIndex());
Hans Boehm31ea2522016-11-23 17:47:02 -080081 if (item.getEvaluatorIndex() == Evaluator.HISTORY_MAIN_INDEX) {
Annie Chin06fd3cf2016-11-07 16:04:33 -080082 holder.mDate.setText(mCurrentExpressionDescription);
83 holder.mDate.setContentDescription(mCurrentExpressionDescription);
84 } else {
Annie Chin36147982016-12-01 15:07:34 -080085 // 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 Chind0f87d22016-10-24 09:04:12 -070096 }
Annie Chind0f87d22016-10-24 09:04:12 -070097 }
98
99 @Override
100 public void onViewRecycled(ViewHolder holder) {
Annie Chin91796232016-11-16 17:27:36 -0800101 if (holder.getItemViewType() == EMPTY_VIEW_TYPE) {
102 return;
103 }
104 mEvaluator.cancel(holder.getItemId(), true);
105
Annie Chin36147982016-12-01 15:07:34 -0800106 holder.mDate.setVisibility(View.VISIBLE);
107 holder.mDivider.setVisibility(View.VISIBLE);
Annie Chind0f87d22016-10-24 09:04:12 -0700108 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 Chin91796232016-11-16 17:27:36 -0800117 public long getItemId(int position) {
Annie Chin36147982016-12-01 15:07:34 -0800118 return getItem(position).getEvaluatorIndex();
Annie Chin91796232016-11-16 17:27:36 -0800119 }
120
121 @Override
Annie Chinab657d42016-11-03 16:43:59 -0700122 public int getItemViewType(int position) {
Annie Chin36147982016-12-01 15:07:34 -0800123 return getItem(position).isEmptyView() ? EMPTY_VIEW_TYPE : HISTORY_VIEW_TYPE;
Annie Chinab657d42016-11-03 16:43:59 -0700124 }
125
126 @Override
Annie Chind0f87d22016-10-24 09:04:12 -0700127 public int getItemCount() {
128 return mDataSet.size();
129 }
130
Annie Chin06fd3cf2016-11-07 16:04:33 -0800131 public void setDataSet(ArrayList<HistoryItem> dataSet) {
132 mDataSet = dataSet;
Annie Chind0f87d22016-10-24 09:04:12 -0700133 }
134
Annie Chin36147982016-12-01 15:07:34 -0800135 public void setIsResultLayout(boolean isResult) {
136 mIsResultLayout = isResult;
137 }
138
Annie Chin91796232016-11-16 17:27:36 -0800139 private int getEvaluatorIndex(int position) {
Annie Chin94c1bd92016-11-23 13:39:56 -0800140 if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator) || mIsResultLayout) {
Annie Chin36147982016-12-01 15:07:34 -0800141 return (int) (mEvaluator.getMaxIndex() - position);
Annie Chin91796232016-11-16 17:27:36 -0800142 } else {
143 // Account for the additional "Current Expression" with the +1.
Annie Chin36147982016-12-01 15:07:34 -0800144 return (int) (mEvaluator.getMaxIndex() - position + 1);
Annie Chin91796232016-11-16 17:27:36 -0800145 }
146 }
147
Annie Chin36147982016-12-01 15:07:34 -0800148 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 Chin70ac8ea2016-11-18 14:43:56 -0800178 }
179
Annie Chind0f87d22016-10-24 09:04:12 -0700180 public static class ViewHolder extends RecyclerView.ViewHolder {
181
Annie Chinab657d42016-11-03 16:43:59 -0700182 private TextView mDate;
Annie Chin8149c8c2016-11-28 13:44:09 -0800183 private AlignedTextView mFormula;
Annie Chinab657d42016-11-03 16:43:59 -0700184 private CalculatorResult mResult;
Annie Chin36147982016-12-01 15:07:34 -0800185 private View mDivider;
Annie Chind0f87d22016-10-24 09:04:12 -0700186
Annie Chinab657d42016-11-03 16:43:59 -0700187 public ViewHolder(View v, int viewType) {
Annie Chind0f87d22016-10-24 09:04:12 -0700188 super(v);
Annie Chinab657d42016-11-03 16:43:59 -0700189 if (viewType == EMPTY_VIEW_TYPE) {
190 return;
191 }
Annie Chind0f87d22016-10-24 09:04:12 -0700192 mDate = (TextView) v.findViewById(R.id.history_date);
Annie Chin8149c8c2016-11-28 13:44:09 -0800193 mFormula = (AlignedTextView) v.findViewById(R.id.history_formula);
Annie Chind0f87d22016-10-24 09:04:12 -0700194 mResult = (CalculatorResult) v.findViewById(R.id.history_result);
Annie Chin36147982016-12-01 15:07:34 -0800195 mDivider = v.findViewById(R.id.history_divider);
Annie Chind0f87d22016-10-24 09:04:12 -0700196 }
197
Annie Chin8149c8c2016-11-28 13:44:09 -0800198 public AlignedTextView getFormula() {
Annie Chind0f87d22016-10-24 09:04:12 -0700199 return mFormula;
200 }
201
202 public CalculatorResult getResult() {
203 return mResult;
204 }
205
206 public TextView getDate() {
207 return mDate;
208 }
Annie Chin36147982016-12-01 15:07:34 -0800209
210 public View getDivider() {
211 return mDivider;
212 }
Annie Chind0f87d22016-10-24 09:04:12 -0700213 }
Annie Chin36147982016-12-01 15:07:34 -0800214}