+var added

This commit is contained in:
serso 2011-11-02 15:19:01 +04:00
parent 0fa1b6a16a
commit 656c19998d
7 changed files with 123 additions and 26 deletions

View File

@ -7,7 +7,10 @@
--> -->
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:calc="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/varsButton" a:id="@+id/varsButton"
calc:directionTextScale="0.5"
calc:textUp="+π"
a:text="π,…" a:text="π,…"
a:textStyle="italic" a:textStyle="italic"
a:onClick="varsButtonClickHandler" a:onClick="varsButtonClickHandler"

View File

@ -140,4 +140,7 @@
<string name="c_fun_description_lt">Меньше - возвращает 1, если первый аргумент меньше второго, иначе 0.</string> <string name="c_fun_description_lt">Меньше - возвращает 1, если первый аргумент меньше второго, иначе 0.</string>
<string name="c_fun_description_gt">Больше - возвращает 1, если первый аргумент больше второго, иначе 0.</string> <string name="c_fun_description_gt">Больше - возвращает 1, если первый аргумент больше второго, иначе 0.</string>
<string name="c_empty_var_error">Невозможно создать пустую константу!</string>
<string name="c_not_valid_result">Результат не валиден!</string>
</resources> </resources>

View File

@ -141,4 +141,7 @@
<string name="c_fun_description_lt">Lesser function - gives 1 if first argument is less than second, 0 otherwise.</string> <string name="c_fun_description_lt">Lesser function - gives 1 if first argument is less than second, 0 otherwise.</string>
<string name="c_fun_description_gt">Greater function - gives 1 if first argument is greater than second, 0 otherwise.</string> <string name="c_fun_description_gt">Greater function - gives 1 if first argument is greater than second, 0 otherwise.</string>
<string name="c_empty_var_error">Unable to create empty constant!</string>
<string name="c_not_valid_result">Current result is not valid!</string>
</resources> </resources>

View File

@ -31,6 +31,7 @@ import org.solovyev.android.view.FontSizeAdjuster;
import org.solovyev.android.view.widgets.*; import org.solovyev.android.view.widgets.*;
import org.solovyev.common.BooleanMapper; import org.solovyev.common.BooleanMapper;
import org.solovyev.common.utils.Announcer; import org.solovyev.common.utils.Announcer;
import org.solovyev.common.utils.Point2d;
import org.solovyev.common.utils.history.HistoryAction; import org.solovyev.common.utils.history.HistoryAction;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -42,8 +43,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private static final int HVGA_WIDTH_PIXELS = 320; private static final int HVGA_WIDTH_PIXELS = 320;
private static final long ON_CLICK_VIBRATION_TIME = 100;
@NotNull @NotNull
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class); private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
@ -107,11 +106,35 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
equalsButton.setOnDragListener(evalOnDragListener); equalsButton.setOnDragListener(evalOnDragListener);
} }
final DragButton varsButton = (DragButton) findViewById(R.id.varsButton);
if (varsButton != null) {
final OnDragListener varsOnDragListener = new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(), dragPreferences), vibrator, preferences);
varsButton.setOnDragListener(varsOnDragListener);
}
CalculatorEngine.instance.reset(this, preferences); CalculatorEngine.instance.reset(this, preferences);
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
} }
private class VarsDragProcessor implements SimpleOnDragListener.DragProcessor {
@Override
public boolean processDragEvent(@NotNull DragDirection dragDirection,
@NotNull DragButton dragButton,
@NotNull Point2d startPoint2d,
@NotNull MotionEvent motionEvent) {
boolean result = false;
if (dragDirection == DragDirection.up) {
CalculatorActivityLauncher.createVar(CalculatorActivity.this, CalculatorActivity.this.calculatorModel);
result = true;
}
return result;
}
}
private void setDefaultValues(@NotNull SharedPreferences preferences) { private void setDefaultValues(@NotNull SharedPreferences preferences) {
if (!preferences.contains(CalculatorEngine.GROUPING_SEPARATOR_P_KEY)) { if (!preferences.contains(CalculatorEngine.GROUPING_SEPARATOR_P_KEY)) {
final Locale locale = Locale.getDefault(); final Locale locale = Locale.getDefault();
@ -246,7 +269,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@SuppressWarnings({"UnusedDeclaration"}) @SuppressWarnings({"UnusedDeclaration"})
public void historyButtonClickHandler(@NotNull View v) { public void historyButtonClickHandler(@NotNull View v) {
this.showHistory(); CalculatorActivityLauncher.showHistory(this);
} }
@SuppressWarnings({"UnusedDeclaration"}) @SuppressWarnings({"UnusedDeclaration"})
@ -307,12 +330,12 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@SuppressWarnings({"UnusedDeclaration"}) @SuppressWarnings({"UnusedDeclaration"})
public void functionsButtonClickHandler(@NotNull View v) { public void functionsButtonClickHandler(@NotNull View v) {
startActivity(new Intent(this, CalculatorFunctionsActivity.class)); CalculatorActivityLauncher.showFunctions(this);
} }
@SuppressWarnings({"UnusedDeclaration"}) @SuppressWarnings({"UnusedDeclaration"})
public void varsButtonClickHandler(@NotNull View v) { public void varsButtonClickHandler(@NotNull View v) {
startActivity(new Intent(this, CalculatorVarsActivity.class)); CalculatorActivityLauncher.showVars(this);
} }
private final static String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"; private final static String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted";
@ -364,15 +387,15 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.main_menu_item_settings: case R.id.main_menu_item_settings:
showSettings(); CalculatorActivityLauncher.showSettings(this);
result = true; result = true;
break; break;
case R.id.main_menu_item_history: case R.id.main_menu_item_history:
showHistory(); CalculatorActivityLauncher.showHistory(this);
result = true; result = true;
break; break;
case R.id.main_menu_item_about: case R.id.main_menu_item_about:
showAbout(); CalculatorActivityLauncher.showAbout(this);
result = true; result = true;
break; break;
case R.id.main_menu_item_exit: case R.id.main_menu_item_exit:
@ -386,18 +409,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
return result; return result;
} }
private void showHistory() {
startActivity(new Intent(this, CalculatorHistoryActivity.class));
}
private void showSettings() {
startActivity(new Intent(this, CalculatorPreferencesActivity.class));
}
private void showAbout() {
startActivity(new Intent(this, AboutActivity.class));
}
/** /**
* The font sizes in the layout files are specified for a HVGA display. * The font sizes in the layout files are specified for a HVGA display.
* Adjust the font sizes accordingly if we are running on a different * Adjust the font sizes accordingly if we are running on a different

View File

@ -0,0 +1,56 @@
package org.solovyev.android.calculator;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.common.utils.StringUtils;
/**
* User: serso
* Date: 11/2/11
* Time: 2:18 PM
*/
public class CalculatorActivityLauncher {
public static void showHistory(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorHistoryActivity.class));
}
public static void showSettings(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorPreferencesActivity.class));
}
public static void showAbout(@NotNull final Context context) {
context.startActivity(new Intent(context, AboutActivity.class));
}
public static void showFunctions(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorFunctionsActivity.class));
}
public static void showVars(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorVarsActivity.class));
}
public static void createVar(@NotNull final Context context, @NotNull CalculatorModel calculatorModel) {
if (calculatorModel.getDisplay().isValid() ) {
final String varValue = calculatorModel.getDisplay().getText().toString();
if (!StringUtils.isEmpty(varValue)) {
if (CalculatorVarsActivity.isValid(varValue)) {
final Intent intent = new Intent(context, CalculatorVarsActivity.class);
intent.putExtra(CalculatorVarsActivity.CREATE_VAR_EXTRA_STRING, varValue);
context.startActivity(intent);
} else {
Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, R.string.c_empty_var_error, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show();
}
}
}

View File

@ -352,4 +352,9 @@ public enum CalculatorModel implements CursorControl, HistoryControl<CalculatorH
public CalculatorEditor getEditor() { public CalculatorEditor getEditor() {
return editor; return editor;
} }
@NotNull
public CalculatorDisplay getDisplay() {
return display;
}
} }

View File

@ -10,6 +10,7 @@ import android.app.AlertDialog;
import android.app.ListActivity; import android.app.ListActivity;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -34,6 +35,8 @@ import java.util.List;
*/ */
public class CalculatorVarsActivity extends ListActivity { public class CalculatorVarsActivity extends ListActivity {
public static final String CREATE_VAR_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_create_var";
private final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё".toCharArray())); private final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё".toCharArray()));
@NotNull @NotNull
@ -78,6 +81,14 @@ public class CalculatorVarsActivity extends ListActivity {
} }
}); });
final Intent intent = getIntent();
if ( intent != null ) {
final String varValue = intent.getStringExtra(CREATE_VAR_EXTRA_STRING);
if (!StringUtils.isEmpty(varValue)) {
createEditVariableDialog(null, null, varValue, null);
}
}
} }
@SuppressWarnings({"UnusedDeclaration"}) @SuppressWarnings({"UnusedDeclaration"})
@ -209,12 +220,7 @@ public class CalculatorVarsActivity extends ListActivity {
error = null; error = null;
} else { } else {
// value is not empty => must be a number // value is not empty => must be a number
boolean correctDouble = true; boolean correctDouble = isValid(value);
try {
Double.valueOf(value);
} catch (NumberFormatException e) {
correctDouble = false;
}
if (correctDouble) { if (correctDouble) {
varBuilder.setName(name); varBuilder.setName(name);
@ -253,6 +259,16 @@ public class CalculatorVarsActivity extends ListActivity {
} }
} }
public static boolean isValid(@NotNull String value) {
boolean valid = true;
try {
Double.valueOf(value);
} catch (NumberFormatException e) {
valid = false;
}
return valid;
}
private class VarsArrayAdapter extends ArrayAdapter<Var> { private class VarsArrayAdapter extends ArrayAdapter<Var> {
private VarsArrayAdapter(Context context, int resource, int textViewResourceId, List<Var> objects) { private VarsArrayAdapter(Context context, int resource, int textViewResourceId, List<Var> objects) {