/**
  * Inserts text into the formula EditText. If an equation was recently solved, it will replace the
  * formula's text instead of appending.
  */
 protected void insert(String text) {
   // Add left parenthesis after functions.
   if (mCurrentState.equals(CalculatorState.INPUT)
       || mCurrentState.equals(CalculatorState.GRAPHING)
       || mFormulaEditText.isCursorModified()) {
     mFormulaEditText.insert(text);
   } else {
     mFormulaEditText.setText(text);
     incrementGroupId();
   }
 }
  @Override
  protected void onResume() {
    super.onResume();

    // Load up to date history
    mPersist = new Persist(this);
    mPersist.load();
    mHistory = mPersist.getHistory();
    incrementGroupId();

    // When history is open, the display is saved as a Display Entry. Cache it if it exists.
    HistoryEntry displayEntry = null;
    if (mHistoryAdapter != null) {
      displayEntry = mHistoryAdapter.getDisplayEntry();
    }

    // Create a new History Adapter (with the up-to-date history)
    mHistoryAdapter = new HistoryAdapter(this, mEvaluator.getSolver(), mHistory);
    mHistoryAdapter.setOnItemClickListener(
        new HistoryAdapter.OnItemClickListener() {
          @Override
          public void onItemClick(final HistoryEntry entry) {
            mDisplayView.collapse(
                new AnimationFinishedListener() {
                  @Override
                  public void onAnimationFinished() {
                    mFormulaEditText.setText(entry.getFormula());
                  }
                });
          }
        });
    mHistoryAdapter.setOnItemLongclickListener(
        new HistoryAdapter.OnItemLongClickListener() {
          @Override
          public void onItemLongClick(HistoryEntry entry) {
            Clipboard.copy(getBaseContext(), entry.getResult());
          }
        });

    // Restore the Display Entry (if it existed)
    if (displayEntry != null) {
      mHistoryAdapter.setDisplayEntry(displayEntry.getFormula(), displayEntry.getResult());
    }

    // Observe! Set! Typical adapter stuff.
    mHistory.setObserver(
        new History.Observer() {
          @Override
          public void notifyDataSetChanged() {
            mHistoryAdapter.notifyDataSetChanged();
          }
        });
    mDisplayView.setAdapter(mHistoryAdapter);
    mDisplayView.attachToRecyclerView(
        new ItemTouchHelper(
            new ItemTouchHelper.SimpleCallback(
                ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT,
                ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
              @Override
              public boolean onMove(
                  RecyclerView recyclerView,
                  RecyclerView.ViewHolder viewHolder,
                  RecyclerView.ViewHolder target) {
                return false;
              }

              @Override
              public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                if (viewHolder.getAdapterPosition() < mHistory.getEntries().size()) {
                  HistoryEntry item = mHistory.getEntries().get(viewHolder.getAdapterPosition());
                  mHistory.remove(item);
                  mHistoryAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
                }
              }
            }));
    mDisplayView.scrollToMostRecent();
  }