Annie Chin | abd202f | 2016-10-14 14:23:45 -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 | |
Justin Klaassen | 3929778 | 2016-12-19 09:11:38 -0800 | [diff] [blame] | 19 | import android.animation.Animator; |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 20 | import android.app.Fragment; |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 21 | import android.os.Bundle; |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame] | 22 | import android.support.v4.content.ContextCompat; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 23 | import android.support.v7.widget.RecyclerView; |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
| 25 | import android.view.MenuItem; |
| 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
| 28 | import android.widget.Toolbar; |
| 29 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 32 | import static android.support.v7.widget.RecyclerView.SCROLL_STATE_DRAGGING; |
| 33 | |
Annie Chin | 52eed7a | 2017-01-04 15:06:05 -0800 | [diff] [blame] | 34 | public class HistoryFragment extends Fragment implements DragLayout.DragCallback { |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 35 | |
| 36 | public static final String TAG = "HistoryFragment"; |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame] | 37 | public static final String CLEAR_DIALOG_TAG = "clear"; |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 38 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 39 | private final DragController mDragController = new DragController(); |
| 40 | |
| 41 | private RecyclerView mRecyclerView; |
| 42 | private HistoryAdapter mAdapter; |
Annie Chin | b2e9618 | 2016-11-28 13:14:54 -0800 | [diff] [blame] | 43 | private DragLayout mDragLayout; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 44 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 45 | private Evaluator mEvaluator; |
| 46 | |
| 47 | private ArrayList<HistoryItem> mDataSet = new ArrayList<>(); |
| 48 | |
Annie Chin | bfffafd | 2017-01-25 15:51:11 -0800 | [diff] [blame] | 49 | private boolean mIsDisplayEmpty; |
| 50 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 51 | @Override |
| 52 | public void onCreate(Bundle savedInstanceState) { |
| 53 | super.onCreate(savedInstanceState); |
Annie Chin | 7d03963 | 2016-12-08 15:23:39 -0800 | [diff] [blame] | 54 | mAdapter = new HistoryAdapter(mDataSet); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 57 | @Override |
| 58 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 59 | Bundle savedInstanceState) { |
| 60 | final View view = inflater.inflate( |
| 61 | R.layout.fragment_history, container, false /* attachToRoot */); |
| 62 | |
Justin Klaassen | 3929778 | 2016-12-19 09:11:38 -0800 | [diff] [blame] | 63 | mDragLayout = (DragLayout) container.getRootView().findViewById(R.id.drag_layout); |
| 64 | mDragLayout.addDragCallback(this); |
| 65 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 66 | mRecyclerView = (RecyclerView) view.findViewById(R.id.history_recycler_view); |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 67 | mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { |
| 68 | @Override |
| 69 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) { |
| 70 | if (newState == SCROLL_STATE_DRAGGING) { |
| 71 | stopActionModeOrContextMenu(); |
| 72 | } |
| 73 | super.onScrollStateChanged(recyclerView, newState); |
| 74 | } |
| 75 | }); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 76 | |
| 77 | // The size of the RecyclerView is not affected by the adapter's contents. |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame] | 78 | mRecyclerView.setHasFixedSize(true); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 79 | mRecyclerView.setAdapter(mAdapter); |
| 80 | |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 81 | final Toolbar toolbar = (Toolbar) view.findViewById(R.id.history_toolbar); |
| 82 | toolbar.inflateMenu(R.menu.fragment_history); |
| 83 | toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { |
| 84 | @Override |
| 85 | public boolean onMenuItemClick(MenuItem item) { |
| 86 | if (item.getItemId() == R.id.menu_clear_history) { |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame] | 87 | final Calculator calculator = (Calculator) getActivity(); |
| 88 | AlertDialogFragment.showMessageDialog(calculator, "" /* title */, |
| 89 | getString(R.string.dialog_clear), |
| 90 | getString(R.string.menu_clear_history), |
| 91 | CLEAR_DIALOG_TAG); |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | return onOptionsItemSelected(item); |
| 95 | } |
| 96 | }); |
| 97 | toolbar.setNavigationOnClickListener(new View.OnClickListener() { |
| 98 | @Override |
| 99 | public void onClick(View v) { |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 100 | getActivity().onBackPressed(); |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 101 | } |
| 102 | }); |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 103 | return view; |
| 104 | } |
| 105 | |
| 106 | @Override |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 107 | public void onActivityCreated(Bundle savedInstanceState) { |
| 108 | super.onActivityCreated(savedInstanceState); |
| 109 | |
Annie Chin | 70ac8ea | 2016-11-18 14:43:56 -0800 | [diff] [blame] | 110 | final Calculator activity = (Calculator) getActivity(); |
Christine Franks | cbc51fa | 2017-01-04 21:00:36 -0800 | [diff] [blame] | 111 | mEvaluator = Evaluator.getInstance(activity); |
| 112 | mAdapter.setEvaluator(mEvaluator); |
Annie Chin | 7d03963 | 2016-12-08 15:23:39 -0800 | [diff] [blame] | 113 | |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 114 | final boolean isResultLayout = activity.isResultLayout(); |
Annie Chin | 8861323 | 2017-01-04 12:21:52 -0800 | [diff] [blame] | 115 | final boolean isOneLine = activity.isOneLine(); |
Annie Chin | 70ac8ea | 2016-11-18 14:43:56 -0800 | [diff] [blame] | 116 | |
Annie Chin | bfffafd | 2017-01-25 15:51:11 -0800 | [diff] [blame] | 117 | // Snapshot display state here. For the rest of the lifecycle of this current |
| 118 | // HistoryFragment, this is what we will consider the display state. |
| 119 | // In rare cases, the display state can change after our adapter is initialized. |
| 120 | final CalculatorExpr mainExpr = mEvaluator.getExpr(Evaluator.MAIN_INDEX); |
| 121 | mIsDisplayEmpty = mainExpr == null || mainExpr.isEmpty(); |
| 122 | |
| 123 | initializeController(isResultLayout, isOneLine, mIsDisplayEmpty); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 124 | |
Annie Chin | e7c3232 | 2017-01-30 18:08:56 -0800 | [diff] [blame] | 125 | final long maxIndex = mEvaluator.getMaxIndex(); |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 126 | |
Annie Chin | e7c3232 | 2017-01-30 18:08:56 -0800 | [diff] [blame] | 127 | final ArrayList<HistoryItem> newDataSet = new ArrayList<>(); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 128 | |
Annie Chin | bfffafd | 2017-01-25 15:51:11 -0800 | [diff] [blame] | 129 | if (!mIsDisplayEmpty && !isResultLayout) { |
Annie Chin | e7c3232 | 2017-01-30 18:08:56 -0800 | [diff] [blame] | 130 | // Add the current expression as the first element in the list (the layout is |
| 131 | // reversed and we want the current expression to be the last one in the |
| 132 | // RecyclerView). |
| 133 | // If we are in the result state, the result will animate to the last history |
| 134 | // element in the list and there will be no "Current Expression." |
| 135 | mEvaluator.copyMainToHistory(); |
| 136 | newDataSet.add(new HistoryItem(Evaluator.HISTORY_MAIN_INDEX, |
| 137 | System.currentTimeMillis(), mEvaluator.getExprAsSpannable(0))); |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 138 | } |
Annie Chin | e7c3232 | 2017-01-30 18:08:56 -0800 | [diff] [blame] | 139 | for (long i = 0; i < maxIndex; ++i) { |
| 140 | newDataSet.add(null); |
| 141 | } |
| 142 | final boolean isEmpty = newDataSet.isEmpty(); |
| 143 | mRecyclerView.setBackgroundColor(ContextCompat.getColor(activity, |
| 144 | isEmpty ? R.color.empty_history_color : R.color.display_background_color)); |
| 145 | if (isEmpty) { |
| 146 | newDataSet.add(new HistoryItem()); |
| 147 | } |
| 148 | mDataSet = newDataSet; |
| 149 | mAdapter.setDataSet(mDataSet); |
| 150 | mAdapter.setIsResultLayout(isResultLayout); |
| 151 | mAdapter.setIsOneLine(activity.isOneLine()); |
Annie Chin | bfffafd | 2017-01-25 15:51:11 -0800 | [diff] [blame] | 152 | mAdapter.setIsDisplayEmpty(mIsDisplayEmpty); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 153 | mAdapter.notifyDataSetChanged(); |
Annie Chin | efa259a | 2016-11-23 14:15:15 -0800 | [diff] [blame] | 154 | } |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 155 | |
Annie Chin | efa259a | 2016-11-23 14:15:15 -0800 | [diff] [blame] | 156 | @Override |
| 157 | public void onStart() { |
| 158 | super.onStart(); |
Justin Klaassen | 3929778 | 2016-12-19 09:11:38 -0800 | [diff] [blame] | 159 | |
Annie Chin | 8861323 | 2017-01-04 12:21:52 -0800 | [diff] [blame] | 160 | final Calculator activity = (Calculator) getActivity(); |
Annie Chin | b61d00b | 2017-01-31 12:33:25 -0800 | [diff] [blame] | 161 | mDragController.initializeAnimation(activity.isResultLayout(), activity.isOneLine(), |
Annie Chin | bfffafd | 2017-01-25 15:51:11 -0800 | [diff] [blame] | 162 | mIsDisplayEmpty); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
Justin Klaassen | 3929778 | 2016-12-19 09:11:38 -0800 | [diff] [blame] | 166 | public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { |
Annie Chin | a8b31db | 2017-02-09 08:11:22 -0800 | [diff] [blame^] | 167 | return mDragLayout.createAnimator(enter); |
Justin Klaassen | 3929778 | 2016-12-19 09:11:38 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void onDestroy() { |
| 172 | super.onDestroy(); |
| 173 | |
| 174 | if (mDragLayout != null) { |
| 175 | mDragLayout.removeDragCallback(this); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 176 | } |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 177 | |
Annie Chin | 1838cf7 | 2017-01-30 08:46:47 -0800 | [diff] [blame] | 178 | if (mEvaluator != null) { |
| 179 | // Note that the view is destroyed when the fragment backstack is popped, so |
| 180 | // these are essentially called when the DragLayout is closed. |
| 181 | mEvaluator.cancelNonMain(); |
| 182 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Annie Chin | bfffafd | 2017-01-25 15:51:11 -0800 | [diff] [blame] | 185 | private void initializeController(boolean isResult, boolean isOneLine, boolean isDisplayEmpty) { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 186 | mDragController.setDisplayFormula( |
| 187 | (CalculatorFormula) getActivity().findViewById(R.id.formula)); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 188 | mDragController.setDisplayResult( |
| 189 | (CalculatorResult) getActivity().findViewById(R.id.result)); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 190 | mDragController.setToolbar(getActivity().findViewById(R.id.toolbar)); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 191 | mDragController.setEvaluator(mEvaluator); |
Annie Chin | bfffafd | 2017-01-25 15:51:11 -0800 | [diff] [blame] | 192 | mDragController.initializeController(isResult, isOneLine, isDisplayEmpty); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 195 | public boolean stopActionModeOrContextMenu() { |
Annie Chin | 1fcfc31 | 2016-12-06 09:36:36 -0800 | [diff] [blame] | 196 | if (mRecyclerView == null) { |
| 197 | return false; |
| 198 | } |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 199 | for (int i = 0; i < mRecyclerView.getChildCount(); i++) { |
| 200 | final View view = mRecyclerView.getChildAt(i); |
| 201 | final HistoryAdapter.ViewHolder viewHolder = |
| 202 | (HistoryAdapter.ViewHolder) mRecyclerView.getChildViewHolder(view); |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame] | 203 | if (viewHolder != null && viewHolder.getResult() != null |
| 204 | && viewHolder.getResult().stopActionModeOrContextMenu()) { |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | } |
| 208 | return false; |
| 209 | } |
Annie Chin | 52eed7a | 2017-01-04 15:06:05 -0800 | [diff] [blame] | 210 | |
| 211 | /* Begin override DragCallback methods. */ |
| 212 | |
| 213 | @Override |
| 214 | public void onStartDraggingOpen() { |
| 215 | // no-op |
| 216 | } |
| 217 | |
| 218 | @Override |
Justin Klaassen | 3929778 | 2016-12-19 09:11:38 -0800 | [diff] [blame] | 219 | public void onInstanceStateRestored(boolean isOpen) { |
| 220 | if (isOpen) { |
| 221 | mRecyclerView.setVisibility(View.VISIBLE); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | @Override |
Annie Chin | 52eed7a | 2017-01-04 15:06:05 -0800 | [diff] [blame] | 226 | public void whileDragging(float yFraction) { |
Justin Klaassen | 3929778 | 2016-12-19 09:11:38 -0800 | [diff] [blame] | 227 | if (isVisible() || isRemoving()) { |
| 228 | mDragController.animateViews(yFraction, mRecyclerView); |
| 229 | } |
Annie Chin | 52eed7a | 2017-01-04 15:06:05 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public boolean shouldCaptureView(View view, int x, int y) { |
Justin Klaassen | 3929778 | 2016-12-19 09:11:38 -0800 | [diff] [blame] | 234 | return !mRecyclerView.canScrollVertically(1 /* scrolling down */); |
Annie Chin | 52eed7a | 2017-01-04 15:06:05 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public int getDisplayHeight() { |
| 239 | return 0; |
| 240 | } |
| 241 | |
Annie Chin | 52eed7a | 2017-01-04 15:06:05 -0800 | [diff] [blame] | 242 | /* End override DragCallback methods. */ |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 243 | } |