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

Commit 9179623c authored by Annie Chin's avatar Annie Chin
Browse files

Miscellaneous fixes

-Fix fragment animation on exit
-Use DateUtils.RelativeTimeSpanString instead of SimpleDateFormat
-Fix RecyclerView ordering and remove arbitrary "25" pre-seeding
-Cancel evaluation for only the id corresponding to the recycled
ViewHolder

Fixes: 32918645
Fixes: 32945018
Fixes: 33000429

Change-Id: I55e3a101a02aec8fe03d43ad0b60343d1fa36940
parent 9db3ee2c
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -505,7 +505,6 @@ public class Calculator extends Activity
    public void onBackPressed() {
        if (!stopActionModeOrContextMenu()) {
            if (mDragLayout.isOpen()) {
                // Close the layout and remove the fragment.
                mDragLayout.setClosed();
                popFragmentBackstack();
                return;
+6 −12
Original line number Diff line number Diff line
@@ -53,10 +53,10 @@ public final class DragController {

        if (evaluator != null) {
            // Initialize controller
            if (isDisplayEmpty()) {
            if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) {
                // Empty display
                mAnimationController = new EmptyAnimationController();
            } else if (mEvaluator.hasResult(Evaluator.MAIN_INDEX)) {
            } else if (isResultState()) {
                // Result
                mAnimationController = new ResultAnimationController();
            } else {
@@ -67,14 +67,8 @@ public final class DragController {
        }
    }

    // There is no formula or result in the CalculatorDisplay.
    private boolean isDisplayEmpty() {
        if (mEvaluator != null) {
            final CalculatorExpr mainExpr = mEvaluator.getExpr(Evaluator.MAIN_INDEX);
            return mainExpr == null || mainExpr.isEmpty();
        } else {
            return false;
        }
    private boolean isResultState() {
        return mDisplayResult.getTranslationY() != 0;
    }

    public void setDisplayFormula(CalculatorFormula formula) {
@@ -92,7 +86,7 @@ public final class DragController {
    public void animateViews(float yFraction, RecyclerView recyclerView, int itemCount) {
        final HistoryAdapter.ViewHolder vh = (HistoryAdapter.ViewHolder)
                recyclerView.findViewHolderForAdapterPosition(0);
        if (vh != null && !isDisplayEmpty()) {
        if (vh != null && !EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) {
            final CalculatorFormula formula = vh.getFormula();
            final CalculatorResult result = vh.getResult();
            final TextView date = vh.getDate();
@@ -132,7 +126,7 @@ public final class DragController {

                date.setTranslationY(mAnimationController.getDateTranslationY(yFraction));
            }
        } else if (isDisplayEmpty()) {
        } else if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) {
            // There is no current expression but we still need to collect information
            // to translate the other viewholders.
            if (!mAnimationInitialized) {
+1 −1
Original line number Diff line number Diff line
@@ -184,7 +184,6 @@ public class DragLayout extends RelativeLayout {
            c.onClosed();
        }
        mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, 0);
        mHistoryFrame.setVisibility(GONE);
        mIsOpen = false;
    }

@@ -233,6 +232,7 @@ public class DragLayout extends RelativeLayout {
                // The view stopped moving.
                if (mDraggingBorder == 0) {
                    setClosed();
                    mHistoryFrame.setVisibility(GONE);
                } else if (mDraggingBorder == mVerticalRange) {
                    setOpen();
                }
+32 −0
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;
        }
    }
}
+25 −14
Original line number Diff line number Diff line
@@ -39,13 +39,12 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHold

    private List<HistoryItem> mDataSet;

    private boolean mHasCurrentExpression = true;

    public HistoryAdapter(Calculator calculator, ArrayList<HistoryItem> dataSet,
            String currentExpressionDescription) {
        mEvaluator = Evaluator.getInstance(calculator);
        mDataSet = dataSet;
        mCurrentExpressionDescription = currentExpressionDescription;
        setHasStableIds(true);
    }

    @Override
@@ -71,41 +70,44 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHold

        holder.mFormula.setText(item.getFormula());
        // Note: HistoryItems that are not the current expression will always have interesting ops.
        holder.mResult.setEvaluator(mEvaluator, item.getId());
        if (mHasCurrentExpression && position == 0) {
        holder.mResult.setEvaluator(mEvaluator, item.getEvaluatorIndex());
        if (item.getEvaluatorIndex() == Evaluator.MAIN_INDEX) {
            holder.mDate.setText(mCurrentExpressionDescription);
            holder.mDate.setContentDescription(mCurrentExpressionDescription);
        } else {
            holder.mDate.setText(item.getDateString());
            holder.mDate.setContentDescription(item.getDateDescription());
        }
        }

    public void setHasCurrentExpression(boolean has) {
        mHasCurrentExpression = has;
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        if (holder.getItemViewType() == EMPTY_VIEW_TYPE) {
            return;
        }
        mEvaluator.cancel(holder.getItemId(), true);

        holder.mDate.setContentDescription(null);
        holder.mDate.setText(null);
        holder.mFormula.setText(null);
        holder.mResult.setText(null);

        // TODO: Only cancel the calculation for the recycled view
        mEvaluator.cancelAll(true);

        super.onViewRecycled(holder);
    }

    @Override
    public long getItemId(int position) {
        return mDataSet.get(position).getEvaluatorIndex();
    }

    @Override
    public int getItemViewType(int position) {
        HistoryItem item = mDataSet.get(position);

        // Continue to lazy-fill the data set
        if (item == null) {
            item = new HistoryItem(position, mEvaluator.getTimeStamp(position),
                    mEvaluator.getExprAsSpannable(position));
            final int evaluatorIndex = getEvaluatorIndex(position);
            item = new HistoryItem(evaluatorIndex, mEvaluator.getTimeStamp(evaluatorIndex),
                    mEvaluator.getExprAsSpannable(evaluatorIndex));
            mDataSet.set(position, item);
        }
        return item.isEmptyView() ? EMPTY_VIEW_TYPE : HISTORY_VIEW_TYPE;
@@ -120,6 +122,15 @@ public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHold
        mDataSet = dataSet;
    }

    private int getEvaluatorIndex(int position) {
        if (EvaluatorStateUtils.isDisplayEmpty(mEvaluator)) {
            return (int) mEvaluator.getMaxIndex() - position;
        } else {
            // Account for the additional "Current Expression" with the +1.
            return (int) mEvaluator.getMaxIndex() - position + 1;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mDate;
Loading