blob: 25c70442a7fa7fcb59ceabb532b367f78683037a [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;
26import java.util.Calendar;
27import java.util.List;
28
29/**
30 * Adapter for RecyclerView of HistoryItems.
31 */
32public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {
33
Annie Chinab657d42016-11-03 16:43:59 -070034 private static final int EMPTY_VIEW_TYPE = 0;
35 private static final int HISTORY_VIEW_TYPE = 1;
36
Annie Chind0f87d22016-10-24 09:04:12 -070037 private final List<HistoryItem> mDataSet = new ArrayList<>();
38 private String mCurrentExpression;
39
40 public HistoryAdapter(int[] dataset, String currentExpression) {
41 mCurrentExpression = currentExpression;
42 // Temporary dataset
43 final Calendar calendar = Calendar.getInstance();
44 for (int i: dataset) {
45 calendar.set(2016, 10, i);
46 mDataSet.add(new HistoryItem(calendar.getTimeInMillis(), Integer.toString(i) + "+1",
47 Integer.toString(i+1)));
48 }
Annie Chinab657d42016-11-03 16:43:59 -070049 // Temporary: just testing the empty view placeholder
50 mDataSet.add(new HistoryItem());
Annie Chind0f87d22016-10-24 09:04:12 -070051 }
52
53 @Override
54 public HistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Annie Chinab657d42016-11-03 16:43:59 -070055 final View v;
56 if (viewType == HISTORY_VIEW_TYPE) {
57 v = LayoutInflater.from(parent.getContext())
58 .inflate(R.layout.history_item, parent, false);
59 } else {
60 v = LayoutInflater.from(parent.getContext())
61 .inflate(R.layout.empty_history_view, parent, false);
62 }
63 return new ViewHolder(v, viewType);
Annie Chind0f87d22016-10-24 09:04:12 -070064 }
65
66 @Override
67 public void onBindViewHolder(HistoryAdapter.ViewHolder holder, int position) {
68 final HistoryItem item = mDataSet.get(position);
69
Annie Chinab657d42016-11-03 16:43:59 -070070 if (item.isEmptyView()) {
71 return;
72 }
Annie Chind0f87d22016-10-24 09:04:12 -070073 if (!isCurrentExpressionItem(position)) {
74 holder.mDate.setText(item.getDateString());
75 holder.mDate.setContentDescription(item.getDateDescription());
76 } else {
77 holder.mDate.setText(mCurrentExpression);
78 holder.mDate.setContentDescription(mCurrentExpression);
79 }
80 holder.mFormula.setText(item.getFormula());
81 holder.mResult.setText(item.getResult());
82 }
83
84 @Override
85 public void onViewRecycled(ViewHolder holder) {
86 holder.mDate.setContentDescription(null);
87 holder.mDate.setText(null);
88 holder.mFormula.setText(null);
89 holder.mResult.setText(null);
90
91 super.onViewRecycled(holder);
92 }
93
94 @Override
Annie Chinab657d42016-11-03 16:43:59 -070095 public int getItemViewType(int position) {
96 return mDataSet.get(position).isEmptyView() ? EMPTY_VIEW_TYPE : HISTORY_VIEW_TYPE;
97 }
98
99 @Override
Annie Chind0f87d22016-10-24 09:04:12 -0700100 public int getItemCount() {
101 return mDataSet.size();
102 }
103
104 private boolean isCurrentExpressionItem(int position) {
105 return position == mDataSet.size() - 1;
106 }
107
108 public static class ViewHolder extends RecyclerView.ViewHolder {
109
Annie Chinab657d42016-11-03 16:43:59 -0700110 private TextView mDate;
111 private CalculatorFormula mFormula;
112 private CalculatorResult mResult;
Annie Chind0f87d22016-10-24 09:04:12 -0700113
Annie Chinab657d42016-11-03 16:43:59 -0700114 public ViewHolder(View v, int viewType) {
Annie Chind0f87d22016-10-24 09:04:12 -0700115 super(v);
Annie Chinab657d42016-11-03 16:43:59 -0700116 if (viewType == EMPTY_VIEW_TYPE) {
117 return;
118 }
Annie Chind0f87d22016-10-24 09:04:12 -0700119 mDate = (TextView) v.findViewById(R.id.history_date);
120 mFormula = (CalculatorFormula) v.findViewById(R.id.history_formula);
121 mResult = (CalculatorResult) v.findViewById(R.id.history_result);
122 }
123
124 public CalculatorFormula getFormula() {
125 return mFormula;
126 }
127
128 public CalculatorResult getResult() {
129 return mResult;
130 }
131
132 public TextView getDate() {
133 return mDate;
134 }
135 }
136}