Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bfffafde authored by Annie Chin's avatar Annie Chin
Browse files

Snapshot display empty state once on creation of HistoryFragment.

Bug: 34698125
Test: Pasting while dragging history down no longer causes crashes.

Remove EvaluatorStateUtils since we now only check the display state once.

Change-Id: I22d0f3f0c967abcd8a8f70dd4cc157247bea8bed
parent fe9f1b08
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ public final class DragController {
    private boolean mAnimationInitialized;

    private boolean mOneLine;
    private boolean mIsDisplayEmpty;

    private AnimationController mAnimationController;

@@ -66,9 +67,10 @@ public final class DragController {
        mEvaluator = evaluator;
    }

    public void initializeController(boolean isResult, boolean oneLine) {
    public void initializeController(boolean isResult, boolean oneLine, boolean isDisplayEmpty) {
        mOneLine = oneLine;
        if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) {
        mIsDisplayEmpty = isDisplayEmpty;
        if (mIsDisplayEmpty) {
            // Empty display
            mAnimationController = new EmptyAnimationController();
        } else if (isResult) {
@@ -107,7 +109,8 @@ public final class DragController {
        if (yFraction > 0 && vh != null) {
            recyclerView.setVisibility(View.VISIBLE);
        }
        if (vh != null && !EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) {
        if (vh != null && !mIsDisplayEmpty
                && vh.getItemViewType() == HistoryAdapter.HISTORY_VIEW_TYPE) {
            final AlignedTextView formula = vh.getFormula();
            final CalculatorResult result = vh.getResult();
            final TextView date = vh.getDate();
@@ -157,7 +160,7 @@ public final class DragController {

            date.setTranslationY(mAnimationController.getDateTranslationY(yFraction));
            divider.setTranslationY(mAnimationController.getDateTranslationY(yFraction));
        } else if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) {
        } else if (mIsDisplayEmpty) {
            // There is no current expression but we still need to collect information
            // to translate the other viewholders.
            if (!mAnimationInitialized) {
@@ -186,9 +189,9 @@ public final class DragController {
    /**
     * Reset all initialized values.
     */
    public void initializeAnimation(boolean isResult, boolean oneLine) {
    public void initializeAnimation(boolean isResult, boolean oneLine, boolean isDisplayEmpty) {
        mAnimationInitialized = false;
        initializeController(isResult, oneLine);
        initializeController(isResult, oneLine, isDisplayEmpty);
    }

    public interface AnimateTextInterface {
+0 −32
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.calculator2;

/**
 * Utils class to get the state of the passed-in Evaluator.
 */

public class EvaluatorStateUtils {
    public static boolean isDisplayEmpty(Evaluator evaluator) {
        if (evaluator != null) {
            final CalculatorExpr mainExpr = evaluator.getExpr(Evaluator.MAIN_INDEX);
            return mainExpr == null || mainExpr.isEmpty();
        } else {
            return true;
        }
    }
}
+7 −2
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHold
    private static final String TAG = "HistoryAdapter";

    private static final int EMPTY_VIEW_TYPE = 0;
    private static final int HISTORY_VIEW_TYPE = 1;
    public static final int HISTORY_VIEW_TYPE = 1;

    private Evaluator mEvaluator;

@@ -44,6 +44,7 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHold

    private boolean mIsResultLayout;
    private boolean mIsOneLine;
    private boolean mIsDisplayEmpty;

    public HistoryAdapter(ArrayList<HistoryItem> dataSet) {
        mDataSet = dataSet;
@@ -135,12 +136,16 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHold
        mIsOneLine = isOneLine;
    }

    public void setIsDisplayEmpty(boolean isDisplayEmpty) {
        mIsDisplayEmpty = isDisplayEmpty;
    }

    public void setEvaluator(Evaluator evaluator) {
        mEvaluator = evaluator;
    }

    private int getEvaluatorIndex(int position) {
        if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator) || mIsResultLayout) {
        if (mIsDisplayEmpty || mIsResultLayout) {
            return (int) (mEvaluator.getMaxIndex() - position);
        } else {
            // Account for the additional "Current Expression" with the +1.
+15 −6
Original line number Diff line number Diff line
@@ -47,6 +47,8 @@ public class HistoryFragment extends Fragment implements DragLayout.DragCallback

    private ArrayList<HistoryItem> mDataSet = new ArrayList<>();

    private boolean mIsDisplayEmpty;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -113,13 +115,19 @@ public class HistoryFragment extends Fragment implements DragLayout.DragCallback
        final boolean isResultLayout = activity.isResultLayout();
        final boolean isOneLine = activity.isOneLine();

        initializeController(isResultLayout, isOneLine);
        // Snapshot display state here. For the rest of the lifecycle of this current
        // HistoryFragment, this is what we will consider the display state.
        // In rare cases, the display state can change after our adapter is initialized.
        final CalculatorExpr mainExpr = mEvaluator.getExpr(Evaluator.MAIN_INDEX);
        mIsDisplayEmpty = mainExpr == null || mainExpr.isEmpty();

        initializeController(isResultLayout, isOneLine, mIsDisplayEmpty);

        final long maxIndex = mEvaluator.getMaxIndex();

        final ArrayList<HistoryItem> newDataSet = new ArrayList<>();

        if (!EvaluatorStateUtils.isDisplayEmpty(mEvaluator) && !isResultLayout) {
        if (!mIsDisplayEmpty && !isResultLayout) {
            // Add the current expression as the first element in the list (the layout is
            // reversed and we want the current expression to be the last one in the
            // RecyclerView).
@@ -142,7 +150,7 @@ public class HistoryFragment extends Fragment implements DragLayout.DragCallback
        mAdapter.setDataSet(mDataSet);
        mAdapter.setIsResultLayout(isResultLayout);
        mAdapter.setIsOneLine(activity.isOneLine());

        mAdapter.setIsDisplayEmpty(mIsDisplayEmpty);
        mAdapter.notifyDataSetChanged();
    }

@@ -151,7 +159,8 @@ public class HistoryFragment extends Fragment implements DragLayout.DragCallback
        super.onStart();

        final Calculator activity = (Calculator) getActivity();
        mDragController.initializeAnimation(activity.isResultLayout(), activity.isOneLine());
        mDragController.initializeAnimation(activity.isResultLayout(), activity.isOneLine(), 
                mIsDisplayEmpty);
    }

    @Override
@@ -181,14 +190,14 @@ public class HistoryFragment extends Fragment implements DragLayout.DragCallback
        }
    }

    private void initializeController(boolean isResult, boolean isOneLine) {
    private void initializeController(boolean isResult, boolean isOneLine, boolean isDisplayEmpty) {
        mDragController.setDisplayFormula(
                (CalculatorFormula) getActivity().findViewById(R.id.formula));
        mDragController.setDisplayResult(
                (CalculatorResult) getActivity().findViewById(R.id.result));
        mDragController.setToolbar(getActivity().findViewById(R.id.toolbar));
        mDragController.setEvaluator(mEvaluator);
        mDragController.initializeController(isResult, isOneLine);
        mDragController.initializeController(isResult, isOneLine, isDisplayEmpty);
    }

    public boolean stopActionModeOrContextMenu() {