write the formula above the result

This commit is contained in:
tibbi
2016-01-03 21:30:49 +01:00
parent 1a7cd32464
commit fe5efdd3c3
6 changed files with 77 additions and 1 deletions

View File

@ -4,4 +4,6 @@ public interface Calculator {
void setValue(String value); void setValue(String value);
void setValueDouble(double d); void setValueDouble(double d);
void setFormula(String value);
} }

View File

@ -8,17 +8,20 @@ public class CalculatorImpl {
private String lastKey; private String lastKey;
private String lastOperation; private String lastOperation;
private Calculator callback; private Calculator callback;
private boolean isFirstOperation;
public CalculatorImpl(Calculator calculatorInterface) { public CalculatorImpl(Calculator calculatorInterface) {
callback = calculatorInterface; callback = calculatorInterface;
resetValues(); resetValues();
setValue("0"); setValue("0");
setFormula("");
} }
public CalculatorImpl(Calculator calculatorInterface, String value) { public CalculatorImpl(Calculator calculatorInterface, String value) {
callback = calculatorInterface; callback = calculatorInterface;
resetValues(); resetValues();
displayedValue = value; displayedValue = value;
setFormula("");
} }
private void resetValueIfNeeded() { private void resetValueIfNeeded() {
@ -35,6 +38,7 @@ public class CalculatorImpl {
lastKey = ""; lastKey = "";
lastOperation = ""; lastOperation = "";
displayedValue = ""; displayedValue = "";
isFirstOperation = true;
} }
public void setValue(String value) { public void setValue(String value) {
@ -42,6 +46,22 @@ public class CalculatorImpl {
displayedValue = value; displayedValue = value;
} }
private void setFormula(String value) {
callback.setFormula(value);
}
private void updateFormula() {
final String first = Formatter.doubleToString(baseValue);
final String second = Formatter.doubleToString(secondValue);
final String sign = getSign(lastOperation);
if (sign.equals("")) {
setFormula(sign + first);
} else if (!sign.isEmpty()) {
setFormula(first + sign + second);
}
}
public void setLastKey(String lastKey) { public void setLastKey(String lastKey) {
this.lastKey = lastKey; this.lastKey = lastKey;
} }
@ -77,6 +97,10 @@ public class CalculatorImpl {
} }
public void calculateResult() { public void calculateResult() {
if (!isFirstOperation) {
updateFormula();
}
switch (lastOperation) { switch (lastOperation) {
case Constants.PLUS: case Constants.PLUS:
updateResult(baseValue + secondValue); updateResult(baseValue + secondValue);
@ -102,6 +126,7 @@ public class CalculatorImpl {
default: default:
break; break;
} }
isFirstOperation = false;
} }
private void divideNumbers() { private void divideNumbers() {
@ -162,6 +187,7 @@ public class CalculatorImpl {
public void handleReset() { public void handleReset() {
resetValues(); resetValues();
setValue("0"); setValue("0");
setFormula("");
} }
public void handleEquals() { public void handleEquals() {
@ -190,6 +216,26 @@ public class CalculatorImpl {
setValue(value); setValue(value);
} }
private String getSign(String lastOperation) {
switch (lastOperation) {
case Constants.PLUS:
return "+";
case Constants.MINUS:
return "-";
case Constants.MULTIPLY:
return "*";
case Constants.DIVIDE:
return "/";
case Constants.MODULO:
return "%";
case Constants.POWER:
return "^";
case Constants.ROOT:
return "";
}
return "";
}
public void numpadClicked(int id) { public void numpadClicked(int id) {
if (lastKey.equals(Constants.EQUALS)) if (lastKey.equals(Constants.EQUALS))
lastOperation = Constants.EQUALS; lastOperation = Constants.EQUALS;

View File

@ -13,6 +13,7 @@ import butterknife.OnLongClick;
public class MainActivity extends AppCompatActivity implements Calculator { public class MainActivity extends AppCompatActivity implements Calculator {
@Bind(R.id.result) TextView result; @Bind(R.id.result) TextView result;
@Bind(R.id.formula) TextView formula;
private CalculatorImpl calc; private CalculatorImpl calc;
@ -29,6 +30,9 @@ public class MainActivity extends AppCompatActivity implements Calculator {
final Resources res = getResources(); final Resources res = getResources();
result.setBackgroundColor(res.getColor(android.R.color.white)); result.setBackgroundColor(res.getColor(android.R.color.white));
result.setTextColor(res.getColor(R.color.dark_grey)); result.setTextColor(res.getColor(R.color.dark_grey));
formula.setBackgroundColor(res.getColor(android.R.color.white));
formula.setTextColor(res.getColor(R.color.dark_grey));
} }
@OnClick(R.id.btn_plus) @OnClick(R.id.btn_plus)
@ -104,6 +108,11 @@ public class MainActivity extends AppCompatActivity implements Calculator {
calc.setLastKey(Constants.DIGIT); calc.setLastKey(Constants.DIGIT);
} }
@Override
public void setFormula(String value) {
formula.setText(value);
}
public CalculatorImpl getCalc() { public CalculatorImpl getCalc() {
return calc; return calc;
} }

View File

@ -184,6 +184,12 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
} }
@Override
public void setFormula(String value) {
remoteViews.setTextViewText(R.id.formula, value);
widgetManager.updateAppWidget(widgetIds, remoteViews);
}
@Override @Override
public void onDeleted(Context context, int[] appWidgetIds) { public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds); super.onDeleted(context, appWidgetIds);

View File

@ -9,6 +9,18 @@
android:orientation="vertical" android:orientation="vertical"
tools:context="calculator.simplemobiletools.com.simple_calculator.MainActivity"> tools:context="calculator.simplemobiletools.com.simple_calculator.MainActivity">
<TextView
android:id="@+id/formula"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.1"
android:gravity="right|bottom"
android:lines="1"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:textColor="@android:color/white"
android:textSize="@dimen/formula_text_size"/>
<TextView <TextView
android:id="@+id/result" android:id="@+id/result"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -3,5 +3,6 @@
<dimen name="result_padding">64dp</dimen> <dimen name="result_padding">64dp</dimen>
<dimen name="config_text_size">18sp</dimen> <dimen name="config_text_size">18sp</dimen>
<dimen name="button_text_size">28sp</dimen> <dimen name="button_text_size">28sp</dimen>
<dimen name="display_text_size">36sp</dimen> <dimen name="formula_text_size">20sp</dimen>
<dimen name="display_text_size">40sp</dimen>
</resources> </resources>