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