blob: 7da375dc7df781f22c6e6fc8f9dc59b477759763 [file] [log] [blame]
Annie Chinabd202f2016-10-14 14:23:45 -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
Justin Klaassen39297782016-12-19 09:11:38 -080019import android.animation.Animator;
Annie Chinabd202f2016-10-14 14:23:45 -070020import android.app.Fragment;
Justin Klaassen39297782016-12-19 09:11:38 -080021import android.app.FragmentTransaction;
Annie Chinabd202f2016-10-14 14:23:45 -070022import android.os.Bundle;
Annie Chin36147982016-12-01 15:07:34 -080023import android.support.v4.content.ContextCompat;
Annie Chind0f87d22016-10-24 09:04:12 -070024import android.support.v7.widget.RecyclerView;
Annie Chinabd202f2016-10-14 14:23:45 -070025import android.view.LayoutInflater;
26import android.view.MenuItem;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.Toolbar;
30
Annie Chin06fd3cf2016-11-07 16:04:33 -080031import java.util.ArrayList;
32
Christine Franks7485df52016-12-01 13:18:45 -080033import static android.support.v7.widget.RecyclerView.SCROLL_STATE_DRAGGING;
34
Annie Chin52eed7a2017-01-04 15:06:05 -080035public class HistoryFragment extends Fragment implements DragLayout.DragCallback {
Annie Chinabd202f2016-10-14 14:23:45 -070036
37 public static final String TAG = "HistoryFragment";
Annie Chin532b77e2016-12-06 13:30:35 -080038 public static final String CLEAR_DIALOG_TAG = "clear";
Annie Chinabd202f2016-10-14 14:23:45 -070039
Annie Chind0f87d22016-10-24 09:04:12 -070040 private final DragController mDragController = new DragController();
41
42 private RecyclerView mRecyclerView;
43 private HistoryAdapter mAdapter;
Annie Chinb2e96182016-11-28 13:14:54 -080044 private DragLayout mDragLayout;
Annie Chind0f87d22016-10-24 09:04:12 -070045
Annie Chin06fd3cf2016-11-07 16:04:33 -080046 private Evaluator mEvaluator;
47
48 private ArrayList<HistoryItem> mDataSet = new ArrayList<>();
49
Annie Chind0f87d22016-10-24 09:04:12 -070050 @Override
51 public void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
Annie Chin7d039632016-12-08 15:23:39 -080053 mAdapter = new HistoryAdapter(mDataSet);
Annie Chind0f87d22016-10-24 09:04:12 -070054 }
55
Annie Chinabd202f2016-10-14 14:23:45 -070056 @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 Klaassen39297782016-12-19 09:11:38 -080062 mDragLayout = (DragLayout) container.getRootView().findViewById(R.id.drag_layout);
63 mDragLayout.addDragCallback(this);
64
Annie Chind0f87d22016-10-24 09:04:12 -070065 mRecyclerView = (RecyclerView) view.findViewById(R.id.history_recycler_view);
Christine Franks7485df52016-12-01 13:18:45 -080066 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 Chind0f87d22016-10-24 09:04:12 -070075
76 // The size of the RecyclerView is not affected by the adapter's contents.
Annie Chin36147982016-12-01 15:07:34 -080077 mRecyclerView.setHasFixedSize(true);
Annie Chind0f87d22016-10-24 09:04:12 -070078 mRecyclerView.setAdapter(mAdapter);
79
Annie Chinabd202f2016-10-14 14:23:45 -070080 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 Chin532b77e2016-12-06 13:30:35 -080086 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 Chinabd202f2016-10-14 14:23:45 -070091 return true;
92 }
93 return onOptionsItemSelected(item);
94 }
95 });
96 toolbar.setNavigationOnClickListener(new View.OnClickListener() {
97 @Override
98 public void onClick(View v) {
Annie Chin09547532016-10-14 10:59:07 -070099 getActivity().onBackPressed();
Annie Chinabd202f2016-10-14 14:23:45 -0700100 }
101 });
Annie Chinabd202f2016-10-14 14:23:45 -0700102 return view;
103 }
104
105 @Override
Annie Chind0f87d22016-10-24 09:04:12 -0700106 public void onActivityCreated(Bundle savedInstanceState) {
107 super.onActivityCreated(savedInstanceState);
108
Annie Chin70ac8ea2016-11-18 14:43:56 -0800109 final Calculator activity = (Calculator) getActivity();
Christine Frankscbc51fa2017-01-04 21:00:36 -0800110 mEvaluator = Evaluator.getInstance(activity);
111 mAdapter.setEvaluator(mEvaluator);
Annie Chin7d039632016-12-08 15:23:39 -0800112
Annie Chin94c1bd92016-11-23 13:39:56 -0800113 final boolean isResultLayout = activity.isResultLayout();
Annie Chin88613232017-01-04 12:21:52 -0800114 final boolean isOneLine = activity.isOneLine();
Annie Chin70ac8ea2016-11-18 14:43:56 -0800115
Annie Chine7c32322017-01-30 18:08:56 -0800116 initializeController(isResultLayout, isOneLine);
Annie Chinbc001882016-11-09 19:41:21 -0800117
Annie Chine7c32322017-01-30 18:08:56 -0800118 final long maxIndex = mEvaluator.getMaxIndex();
Annie Chin06fd3cf2016-11-07 16:04:33 -0800119
Annie Chine7c32322017-01-30 18:08:56 -0800120 final ArrayList<HistoryItem> newDataSet = new ArrayList<>();
Annie Chinbc001882016-11-09 19:41:21 -0800121
Annie Chine7c32322017-01-30 18:08:56 -0800122 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 Chin06fd3cf2016-11-07 16:04:33 -0800131 }
Annie Chine7c32322017-01-30 18:08:56 -0800132 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 Chinbc001882016-11-09 19:41:21 -0800145
146 mAdapter.notifyDataSetChanged();
Annie Chinefa259a2016-11-23 14:15:15 -0800147 }
Annie Chinbc001882016-11-09 19:41:21 -0800148
Annie Chinefa259a2016-11-23 14:15:15 -0800149 @Override
150 public void onStart() {
151 super.onStart();
Justin Klaassen39297782016-12-19 09:11:38 -0800152
Annie Chin88613232017-01-04 12:21:52 -0800153 final Calculator activity = (Calculator) getActivity();
Justin Klaassen39297782016-12-19 09:11:38 -0800154 mDragController.initializeAnimation(activity.isResultLayout(), activity.isOneLine());
Annie Chind0f87d22016-10-24 09:04:12 -0700155 }
156
157 @Override
Justin Klaassen39297782016-12-19 09:11:38 -0800158 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 Chind0f87d22016-10-24 09:04:12 -0700175 }
Annie Chin06fd3cf2016-11-07 16:04:33 -0800176
Annie Chin1838cf72017-01-30 08:46:47 -0800177 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 Chind0f87d22016-10-24 09:04:12 -0700182 }
183
Annie Chin88613232017-01-04 12:21:52 -0800184 private void initializeController(boolean isResult, boolean isOneLine) {
Annie Chind0f87d22016-10-24 09:04:12 -0700185 mDragController.setDisplayFormula(
186 (CalculatorFormula) getActivity().findViewById(R.id.formula));
Annie Chind0f87d22016-10-24 09:04:12 -0700187 mDragController.setDisplayResult(
188 (CalculatorResult) getActivity().findViewById(R.id.result));
Annie Chind0f87d22016-10-24 09:04:12 -0700189 mDragController.setToolbar(getActivity().findViewById(R.id.toolbar));
Annie Chinbc001882016-11-09 19:41:21 -0800190 mDragController.setEvaluator(mEvaluator);
Annie Chin88613232017-01-04 12:21:52 -0800191 mDragController.initializeController(isResult, isOneLine);
Annie Chind0f87d22016-10-24 09:04:12 -0700192 }
193
Christine Franks7485df52016-12-01 13:18:45 -0800194 public boolean stopActionModeOrContextMenu() {
Annie Chin1fcfc312016-12-06 09:36:36 -0800195 if (mRecyclerView == null) {
196 return false;
197 }
Christine Franks7485df52016-12-01 13:18:45 -0800198 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 Chin532b77e2016-12-06 13:30:35 -0800202 if (viewHolder != null && viewHolder.getResult() != null
203 && viewHolder.getResult().stopActionModeOrContextMenu()) {
Christine Franks7485df52016-12-01 13:18:45 -0800204 return true;
205 }
206 }
207 return false;
208 }
Annie Chin52eed7a2017-01-04 15:06:05 -0800209
210 /* Begin override DragCallback methods. */
211
212 @Override
213 public void onStartDraggingOpen() {
214 // no-op
215 }
216
217 @Override
Justin Klaassen39297782016-12-19 09:11:38 -0800218 public void onInstanceStateRestored(boolean isOpen) {
219 if (isOpen) {
220 mRecyclerView.setVisibility(View.VISIBLE);
221 }
222 }
223
224 @Override
Annie Chin52eed7a2017-01-04 15:06:05 -0800225 public void whileDragging(float yFraction) {
Justin Klaassen39297782016-12-19 09:11:38 -0800226 if (isVisible() || isRemoving()) {
227 mDragController.animateViews(yFraction, mRecyclerView);
228 }
Annie Chin52eed7a2017-01-04 15:06:05 -0800229 }
230
231 @Override
232 public boolean shouldCaptureView(View view, int x, int y) {
Justin Klaassen39297782016-12-19 09:11:38 -0800233 return !mRecyclerView.canScrollVertically(1 /* scrolling down */);
Annie Chin52eed7a2017-01-04 15:06:05 -0800234 }
235
236 @Override
237 public int getDisplayHeight() {
238 return 0;
239 }
240
Annie Chin52eed7a2017-01-04 15:06:05 -0800241 /* End override DragCallback methods. */
Annie Chinabd202f2016-10-14 14:23:45 -0700242}