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