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; |
| 26 | import java.util.Calendar; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * Adapter for RecyclerView of HistoryItems. |
| 31 | */ |
| 32 | public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> { |
| 33 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame^] | 34 | private static final int EMPTY_VIEW_TYPE = 0; |
| 35 | private static final int HISTORY_VIEW_TYPE = 1; |
| 36 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 37 | 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 Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame^] | 49 | // Temporary: just testing the empty view placeholder |
| 50 | mDataSet.add(new HistoryItem()); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public HistoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame^] | 55 | 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 Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void onBindViewHolder(HistoryAdapter.ViewHolder holder, int position) { |
| 68 | final HistoryItem item = mDataSet.get(position); |
| 69 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame^] | 70 | if (item.isEmptyView()) { |
| 71 | return; |
| 72 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 73 | 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 Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame^] | 95 | public int getItemViewType(int position) { |
| 96 | return mDataSet.get(position).isEmptyView() ? EMPTY_VIEW_TYPE : HISTORY_VIEW_TYPE; |
| 97 | } |
| 98 | |
| 99 | @Override |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 100 | 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 Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame^] | 110 | private TextView mDate; |
| 111 | private CalculatorFormula mFormula; |
| 112 | private CalculatorResult mResult; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 113 | |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame^] | 114 | public ViewHolder(View v, int viewType) { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 115 | super(v); |
Annie Chin | ab657d4 | 2016-11-03 16:43:59 -0700 | [diff] [blame^] | 116 | if (viewType == EMPTY_VIEW_TYPE) { |
| 117 | return; |
| 118 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 119 | 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 | } |