From 01cd44a43209be6dc14dec0a8c125be466b3cea7 Mon Sep 17 00:00:00 2001 From: Anthony Ingle Date: Sun, 1 May 2022 02:45:30 -0400 Subject: [PATCH] typing infix operators inserts previous solution before it --- Calculator/CalculatorView.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Calculator/CalculatorView.swift b/Calculator/CalculatorView.swift index 6e2eef5..dacde7e 100644 --- a/Calculator/CalculatorView.swift +++ b/Calculator/CalculatorView.swift @@ -82,6 +82,15 @@ struct CalculatorView: View { }, // On every update of the text by keyboard onTextChange: { newExpression in + if !history.isEmpty && + (newExpression == "+" || + newExpression == "*" || + newExpression == "/" || + newExpression == "^" || + newExpression == "%") + { + expression.insert(contentsOf: history[0].solution, at: expression.startIndex) + } do { historyIndex = -1 // reset the history index counter for arrow keys solution = try evaluateExpression(newExpression)