blob: be500502402a3f57276bbb98c9a6b5a1e2caa619 [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
Aurimas Liutikas8c43f062018-03-28 08:10:28 -070019import androidx.recyclerview.widget.RecyclerView;
Annie Chind0f87d22016-10-24 09:04:12 -070020import 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;
Annie Chinbfffafd2017-01-25 15:51:11 -080037 public static final int HISTORY_VIEW_TYPE = 1;
Annie Chinab657d42016-11-03 16:43:59 -070038
Annie Chin7d039632016-12-08 15:23:39 -080039 private Evaluator mEvaluator;
Annie Chind0f87d22016-10-24 09:04:12 -070040
Annie Chin36147982016-12-01 15:07:34 -080041 private final Calendar mCalendar = Calendar.getInstance();
42
Annie Chin06fd3cf2016-11-07 16:04:33 -080043 private List<HistoryItem> mDataSet;
44
Annie Chin94c1bd92016-11-23 13:39:56 -080045 private boolean mIsResultLayout;
Annie Chin88613232017-01-04 12:21:52 -080046 private boolean mIsOneLine;
Annie Chinbfffafd2017-01-25 15:51:11 -080047 private boolean mIsDisplayEmpty;
Annie Chin70ac8ea2016-11-18 14:43:56 -080048
Annie Chin7d039632016-12-08 15:23:39 -080049 public HistoryAdapter(ArrayList<HistoryItem> dataSet) {
Annie Chin06fd3cf2016-11-07 16:04:33 -080050 mDataSet = dataSet;
Annie Chin91796232016-11-16 17:27:36 -080051 setHasStableIds(true);
Annie Chind0f87d22016-10-24 09:04:12 -070052 }
53
54 @Override
55 public HistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Annie Chinab657d42016-11-03 16:43:59 -070056 final View v;
57 if (viewType == HISTORY_VIEW_TYPE) {
58 v = LayoutInflater.from(parent.getContext())
59 .inflate(R.layout.history_item, parent, false);
60 } else {
61 v = LayoutInflater.from(parent.getContext())
62 .inflate(R.layout.empty_history_view, parent, false);
63 }
64 return new ViewHolder(v, viewType);
Annie Chind0f87d22016-10-24 09:04:12 -070065 }
66
67 @Override
Annie Chin06fd3cf2016-11-07 16:04:33 -080068 public void onBindViewHolder(final HistoryAdapter.ViewHolder holder, int position) {
Annie Chin36147982016-12-01 15:07:34 -080069 final HistoryItem item = getItem(position);
Annie Chind0f87d22016-10-24 09:04:12 -070070
Annie Chinab657d42016-11-03 16:43:59 -070071 if (item.isEmptyView()) {
72 return;
73 }
Annie Chin06fd3cf2016-11-07 16:04:33 -080074
75 holder.mFormula.setText(item.getFormula());
76 // Note: HistoryItems that are not the current expression will always have interesting ops.
Annie Chin91796232016-11-16 17:27:36 -080077 holder.mResult.setEvaluator(mEvaluator, item.getEvaluatorIndex());
Hans Boehm31ea2522016-11-23 17:47:02 -080078 if (item.getEvaluatorIndex() == Evaluator.HISTORY_MAIN_INDEX) {
Annie Chin7d039632016-12-08 15:23:39 -080079 holder.mDate.setText(R.string.title_current_expression);
Annie Chin88613232017-01-04 12:21:52 -080080 holder.mResult.setVisibility(mIsOneLine ? View.GONE : View.VISIBLE);
Annie Chin06fd3cf2016-11-07 16:04:33 -080081 } else {
Annie Chin36147982016-12-01 15:07:34 -080082 // If the previous item occurred on the same date, the current item does not need
83 // a date header.
84 if (shouldShowHeader(position, item)) {
85 holder.mDate.setText(item.getDateString());
86 // Special case -- very first item should not have a divider above it.
87 holder.mDivider.setVisibility(position == getItemCount() - 1
88 ? View.GONE : View.VISIBLE);
89 } else {
90 holder.mDate.setVisibility(View.GONE);
91 holder.mDivider.setVisibility(View.INVISIBLE);
92 }
Annie Chind0f87d22016-10-24 09:04:12 -070093 }
Annie Chind0f87d22016-10-24 09:04:12 -070094 }
95
96 @Override
97 public void onViewRecycled(ViewHolder holder) {
Annie Chin91796232016-11-16 17:27:36 -080098 if (holder.getItemViewType() == EMPTY_VIEW_TYPE) {
99 return;
100 }
101 mEvaluator.cancel(holder.getItemId(), true);
102
Annie Chin36147982016-12-01 15:07:34 -0800103 holder.mDate.setVisibility(View.VISIBLE);
104 holder.mDivider.setVisibility(View.VISIBLE);
Annie Chind0f87d22016-10-24 09:04:12 -0700105 holder.mDate.setText(null);
106 holder.mFormula.setText(null);
107 holder.mResult.setText(null);
108
109 super.onViewRecycled(holder);
110 }
111
112 @Override
Annie Chin91796232016-11-16 17:27:36 -0800113 public long getItemId(int position) {
Annie Chin36147982016-12-01 15:07:34 -0800114 return getItem(position).getEvaluatorIndex();
Annie Chin91796232016-11-16 17:27:36 -0800115 }
116
117 @Override
Annie Chinab657d42016-11-03 16:43:59 -0700118 public int getItemViewType(int position) {
Annie Chin36147982016-12-01 15:07:34 -0800119 return getItem(position).isEmptyView() ? EMPTY_VIEW_TYPE : HISTORY_VIEW_TYPE;
Annie Chinab657d42016-11-03 16:43:59 -0700120 }
121
122 @Override
Annie Chind0f87d22016-10-24 09:04:12 -0700123 public int getItemCount() {
124 return mDataSet.size();
125 }
126
Annie Chin06fd3cf2016-11-07 16:04:33 -0800127 public void setDataSet(ArrayList<HistoryItem> dataSet) {
128 mDataSet = dataSet;
Annie Chind0f87d22016-10-24 09:04:12 -0700129 }
130
Annie Chin36147982016-12-01 15:07:34 -0800131 public void setIsResultLayout(boolean isResult) {
132 mIsResultLayout = isResult;
133 }
134
Annie Chin88613232017-01-04 12:21:52 -0800135 public void setIsOneLine(boolean isOneLine) {
136 mIsOneLine = isOneLine;
137 }
138
Annie Chinbfffafd2017-01-25 15:51:11 -0800139 public void setIsDisplayEmpty(boolean isDisplayEmpty) {
140 mIsDisplayEmpty = isDisplayEmpty;
141 }
142
Annie Chin7d039632016-12-08 15:23:39 -0800143 public void setEvaluator(Evaluator evaluator) {
144 mEvaluator = evaluator;
145 }
146
Annie Chin91796232016-11-16 17:27:36 -0800147 private int getEvaluatorIndex(int position) {
Annie Chinbfffafd2017-01-25 15:51:11 -0800148 if (mIsDisplayEmpty || mIsResultLayout) {
Annie Chin36147982016-12-01 15:07:34 -0800149 return (int) (mEvaluator.getMaxIndex() - position);
Annie Chin91796232016-11-16 17:27:36 -0800150 } else {
151 // Account for the additional "Current Expression" with the +1.
Annie Chin36147982016-12-01 15:07:34 -0800152 return (int) (mEvaluator.getMaxIndex() - position + 1);
Annie Chin91796232016-11-16 17:27:36 -0800153 }
154 }
155
Annie Chin36147982016-12-01 15:07:34 -0800156 private boolean shouldShowHeader(int position, HistoryItem item) {
157 if (position == getItemCount() - 1) {
158 // First/oldest element should always show the header.
159 return true;
160 }
161 final HistoryItem prevItem = getItem(position + 1);
162 // We need to use Calendars to determine this because of Daylight Savings.
163 mCalendar.setTimeInMillis(item.getTimeInMillis());
164 final int year = mCalendar.get(Calendar.YEAR);
165 final int day = mCalendar.get(Calendar.DAY_OF_YEAR);
166 mCalendar.setTimeInMillis(prevItem.getTimeInMillis());
167 final int prevYear = mCalendar.get(Calendar.YEAR);
168 final int prevDay = mCalendar.get(Calendar.DAY_OF_YEAR);
169 return year != prevYear || day != prevDay;
170 }
171
172 /**
173 * Gets the HistoryItem from mDataSet, lazy-filling the dataSet if necessary.
174 */
175 private HistoryItem getItem(int position) {
176 HistoryItem item = mDataSet.get(position);
177 // Lazy-fill the data set.
178 if (item == null) {
179 final int evaluatorIndex = getEvaluatorIndex(position);
180 item = new HistoryItem(evaluatorIndex,
181 mEvaluator.getTimeStamp(evaluatorIndex),
182 mEvaluator.getExprAsSpannable(evaluatorIndex));
183 mDataSet.set(position, item);
184 }
185 return item;
Annie Chin70ac8ea2016-11-18 14:43:56 -0800186 }
187
Annie Chind0f87d22016-10-24 09:04:12 -0700188 public static class ViewHolder extends RecyclerView.ViewHolder {
189
Annie Chinab657d42016-11-03 16:43:59 -0700190 private TextView mDate;
Annie Chin8149c8c2016-11-28 13:44:09 -0800191 private AlignedTextView mFormula;
Annie Chinab657d42016-11-03 16:43:59 -0700192 private CalculatorResult mResult;
Annie Chin36147982016-12-01 15:07:34 -0800193 private View mDivider;
Annie Chind0f87d22016-10-24 09:04:12 -0700194
Annie Chinab657d42016-11-03 16:43:59 -0700195 public ViewHolder(View v, int viewType) {
Annie Chind0f87d22016-10-24 09:04:12 -0700196 super(v);
Annie Chinab657d42016-11-03 16:43:59 -0700197 if (viewType == EMPTY_VIEW_TYPE) {
198 return;
199 }
Annie Chind0f87d22016-10-24 09:04:12 -0700200 mDate = (TextView) v.findViewById(R.id.history_date);
Annie Chin8149c8c2016-11-28 13:44:09 -0800201 mFormula = (AlignedTextView) v.findViewById(R.id.history_formula);
Annie Chind0f87d22016-10-24 09:04:12 -0700202 mResult = (CalculatorResult) v.findViewById(R.id.history_result);
Annie Chin36147982016-12-01 15:07:34 -0800203 mDivider = v.findViewById(R.id.history_divider);
Annie Chind0f87d22016-10-24 09:04:12 -0700204 }
205
Annie Chin8149c8c2016-11-28 13:44:09 -0800206 public AlignedTextView getFormula() {
Annie Chind0f87d22016-10-24 09:04:12 -0700207 return mFormula;
208 }
209
210 public CalculatorResult getResult() {
211 return mResult;
212 }
213
214 public TextView getDate() {
215 return mDate;
216 }
Annie Chin36147982016-12-01 15:07:34 -0800217
218 public View getDivider() {
219 return mDivider;
220 }
Annie Chind0f87d22016-10-24 09:04:12 -0700221 }
Annie Chin36147982016-12-01 15:07:34 -0800222}