diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 9ef36537..8710ac44 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -3,8 +3,8 @@ + a:versionCode="18" + a:versionName="1.2.18"> diff --git a/res/values-ru/strings.xml b/res/values-ru/strings.xml index 1d826ad3..6a09fef4 100644 --- a/res/values-ru/strings.xml +++ b/res/values-ru/strings.xml @@ -308,4 +308,9 @@ e ^ i = 0.5403 + 0.84147i\n Изменить Сохранённая история + История уже была сохранена! + История должна быть сохранена перед редактированием! + История успешно удалена! + История успешно сохранена! + diff --git a/res/values/strings.xml b/res/values/strings.xml index c41564d6..94952df7 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -315,5 +315,9 @@ Check the \'Round result\' preference in application settings - it should be tur Modify history Modify Saved history + History was already saved! + History must be saved before editing! + History was successfully removed! + History was successfully saved! diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 59970085..9ec2ed5a 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -556,7 +556,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh } calculatorModel = CalculatorModel.instance.init(this, preferences, CalculatorEngine.instance); - calculatorModel.evaluate(); + calculatorModel.evaluate(calculatorModel.getDisplay().getJsclOperation()); } @Override diff --git a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java index 70b9c8b3..69663c3e 100644 --- a/src/main/java/org/solovyev/android/calculator/CalculatorModel.java +++ b/src/main/java/org/solovyev/android/calculator/CalculatorModel.java @@ -32,7 +32,6 @@ import org.solovyev.android.calculator.model.CalculatorEvalException; import org.solovyev.android.calculator.model.CalculatorParseException; import org.solovyev.android.calculator.model.Var; import org.solovyev.android.view.*; -import org.solovyev.android.view.prefs.ResourceCache; import org.solovyev.common.msg.Message; import org.solovyev.common.utils.CollectionsUtils; import org.solovyev.common.utils.MutableObject; @@ -215,6 +214,10 @@ public enum CalculatorModel implements CursorControl, HistoryControl 1) { copyResult(activity, cd); } else { - - final AMenu menu = new EnumMenu(CalculatorDisplayMenuItem.class); - - final AlertDialog.Builder menuBuilder = new AlertDialog.Builder(activity); - menuBuilder.setItems(menu.getMenuCaptions(), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int item) { - final AMenuItem menuItem = menu.itemAt(item); - if ( menuItem != null ){ - menuItem.doAction(new CalculatorDisplayMenuData(cd, genericResult, notSystemConstants), activity); - } - } - }); - menuBuilder.create().show(); - + final AMenuBuilder menuBuilder = AMenuBuilder.newInstance(activity, CalculatorDisplayMenuItem.class); + menuBuilder.create(new CalculatorDisplayMenuData(cd, genericResult, notSystemConstants)).show(); } } else { copyResult(activity, cd); @@ -467,7 +458,7 @@ public enum CalculatorModel implements CursorControl, HistoryControl { - plot("c_plot"){ + plot(R.string.c_plot){ @Override public void doAction(@NotNull CalculatorDisplayMenuData data, @NotNull Context context) { final Constant constant = CollectionsUtils.getFirstCollectionElement(data.getNotSystemConstants()); @@ -475,25 +466,23 @@ public enum CalculatorModel implements CursorControl, HistoryControl()); setListAdapter(adapter); @@ -104,18 +100,8 @@ public abstract class AbstractHistoryActivity extends ListActivity { menuItems.remove(HistoryItemMenuItem.copy_result); } - final AMenu historyItemMenu = new MenuImpl(menuItems); - - final AlertDialog.Builder menuDialogBuilder = new AlertDialog.Builder(context); - menuDialogBuilder.setItems(historyItemMenu.getMenuCaptions(), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int item) { - final AMenuItem historyItemMenuItem = historyItemMenu.itemAt(item); - if (historyItemMenuItem != null) { - historyItemMenuItem.doAction(data, context); - } - } - }); - menuDialogBuilder.create().show(); + final AMenuBuilder menuBuilder = AMenuBuilder.newInstance(context, MenuImpl.newInstance(menuItems)); + menuBuilder.create(data).show(); return true; } diff --git a/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java b/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java index 6ee9d671..4a0ac87c 100644 --- a/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java +++ b/src/main/java/org/solovyev/android/calculator/history/HistoryArrayAdapter.java @@ -7,6 +7,7 @@ package org.solovyev.android.calculator.history; import android.content.Context; +import android.content.res.Resources; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; @@ -56,12 +57,12 @@ public class HistoryArrayAdapter extends ArrayAdapter { final TextView status = (TextView) result.findViewById(R.id.history_item_status); if (status != null) { if (state.isSaved()) { - status.setText(ResourceCache.instance.getCaption("c_history_item_saved")); + status.setText(getContext().getString(R.string.c_history_item_saved)); } else { if ( AbstractHistoryActivity.isAlreadySaved(state) ) { - status.setText(ResourceCache.instance.getCaption("c_history_item_already_saved")); + status.setText(getContext().getString(R.string.c_history_item_already_saved)); } else { - status.setText(ResourceCache.instance.getCaption("c_history_item_not_saved")); + status.setText(getContext().getString(R.string.c_history_item_not_saved)); } } } diff --git a/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java b/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java index fa0fde4a..57ea2ec8 100644 --- a/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java +++ b/src/main/java/org/solovyev/android/calculator/history/HistoryItemMenuItem.java @@ -30,18 +30,18 @@ import org.solovyev.common.utils.StringUtils; */ public enum HistoryItemMenuItem implements AMenuItem { - use("c_use_expression") { + use(R.string.c_use_expression) { @Override public void doAction(@NotNull HistoryItemMenuData data, @NotNull Context context) { if (context instanceof AbstractHistoryActivity) { AbstractHistoryActivity.useHistoryItem(data.getHistoryState(), (AbstractHistoryActivity) context); } else { - Log.e(HistoryItemMenuItem.class.getName(), CalculatorHistoryActivity.class + " must be passed as context!"); + Log.e(HistoryItemMenuItem.class.getName(), AbstractHistoryActivity.class + " must be passed as context!"); } } }, - copy_expression("c_copy_expression") { + copy_expression(R.string.c_copy_expression) { @Override public void doAction(@NotNull HistoryItemMenuData data, @NotNull Context context) { final CalculatorHistoryState calculatorHistoryState = data.getHistoryState(); @@ -54,7 +54,7 @@ public enum HistoryItemMenuItem implements AMenuItem { } }, - copy_result("c_copy_result") { + copy_result(R.string.c_copy_result) { @Override public void doAction(@NotNull HistoryItemMenuData data, @NotNull Context context) { final CalculatorHistoryState calculatorHistoryState = data.getHistoryState(); @@ -67,38 +67,38 @@ public enum HistoryItemMenuItem implements AMenuItem { } }, - save("c_save") { + save(R.string.c_save) { @Override public void doAction(@NotNull final HistoryItemMenuData data, @NotNull final Context context) { final CalculatorHistoryState historyState = data.getHistoryState(); if (!historyState.isSaved()) { createEditHistoryDialog(data, context, true); } else { - Toast.makeText(context, "History item was already saved!", Toast.LENGTH_LONG).show(); + Toast.makeText(context, context.getText(R.string.c_history_already_saved), Toast.LENGTH_LONG).show(); } } }, - edit("c_edit") { + edit(R.string.c_edit) { @Override public void doAction(@NotNull final HistoryItemMenuData data, @NotNull final Context context) { final CalculatorHistoryState historyState = data.getHistoryState(); if (historyState.isSaved()) { createEditHistoryDialog(data, context, false); } else { - Toast.makeText(context, "History item must be saved before editing!", Toast.LENGTH_LONG).show(); + Toast.makeText(context, context.getText(R.string.c_history_must_be_saved), Toast.LENGTH_LONG).show(); } } }, - remove("c_remove") { + remove(R.string.c_remove) { @Override public void doAction(@NotNull HistoryItemMenuData data, @NotNull Context context) { final CalculatorHistoryState historyState = data.getHistoryState(); if (historyState.isSaved()) { data.getAdapter().remove(historyState); CalculatorHistory.instance.removeSavedHistory(historyState, context); - Toast.makeText(context, "History item was removed!", Toast.LENGTH_LONG).show(); + Toast.makeText(context, context.getText(R.string.c_history_was_removed), Toast.LENGTH_LONG).show(); data.getAdapter().notifyDataSetChanged(); } } @@ -133,7 +133,7 @@ public enum HistoryItemMenuItem implements AMenuItem { CalculatorHistory.instance.save(context); } data.getAdapter().notifyDataSetChanged(); - Toast.makeText(context, "History item was successfully saved!", Toast.LENGTH_LONG).show(); + Toast.makeText(context, context.getText(R.string.c_history_saved), Toast.LENGTH_LONG).show(); } }) .setView(editView); @@ -141,22 +141,15 @@ public enum HistoryItemMenuItem implements AMenuItem { builder.create().show(); } - @NotNull - private final String captionId; + private final int captionId; - private HistoryItemMenuItem(@NotNull String captionId) { + private HistoryItemMenuItem(int captionId) { this.captionId = captionId; } @NotNull @Override - public String getCaption() { - final String caption = ResourceCache.instance.getCaption(getCaptionId()); - return caption == null ? this.name() : caption; - } - - @NotNull - public String getCaptionId() { - return captionId; + public String getCaption(@NotNull Context context) { + return context.getString(captionId); } } diff --git a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java index 3254c764..9a5afa82 100644 --- a/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java +++ b/src/main/java/org/solovyev/android/calculator/model/CalculatorEngine.java @@ -135,7 +135,7 @@ public enum CalculatorEngine { return evaluate(operation, expression, null); } - public Result evaluate(@NotNull JsclOperation operation, + public Result evaluate(@NotNull final JsclOperation operation, @NotNull String expression, @Nullable MessageRegistry mr) throws CalculatorParseException, CalculatorEvalException { synchronized (lock) { @@ -161,7 +161,6 @@ public enum CalculatorEngine { }*/ final String jsclExpression = sb.toString(); - final JsclOperation finalOperation = operation; final MutableObject calculationResult = new MutableObject(null); final MutableObject parseException = new MutableObject(null); @@ -178,7 +177,7 @@ public enum CalculatorEngine { //Log.d(CalculatorEngine.class.getName(), "Calculation thread started work: " + thread.getName()); //System.out.println(jsclExpression); calculationThread.setObject(thread); - final Generic genericResult = finalOperation.evaluateGeneric(jsclExpression); + final Generic genericResult = operation.evaluateGeneric(jsclExpression); // NOTE: toString() method must be called here as ArithmeticOperationException may occur in it (just to avoid later check!) genericResult.toString(); @@ -223,7 +222,7 @@ public enum CalculatorEngine { } if (parseExceptionObject != null || evalExceptionObject != null) { - if (finalOperation == JsclOperation.numeric && + if (operation == JsclOperation.numeric && ( preparedExpression.isExistsUndefinedVar() || ( evalExceptionObject != null && evalExceptionObject.getCause() instanceof NumeralBaseException)) ) { return evaluate(JsclOperation.simplify, expression, mr); } diff --git a/src/main/java/org/solovyev/android/view/AMenu.java b/src/main/java/org/solovyev/android/view/AMenu.java index 2b495afb..9175cdcb 100644 --- a/src/main/java/org/solovyev/android/view/AMenu.java +++ b/src/main/java/org/solovyev/android/view/AMenu.java @@ -6,6 +6,7 @@ package org.solovyev.android.view; +import android.content.Context; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -14,10 +15,10 @@ import org.jetbrains.annotations.Nullable; * Date: 12/18/11 * Time: 1:30 PM */ -public interface AMenu { +public interface AMenu, D> { @Nullable T itemAt(int i); @NotNull - CharSequence[] getMenuCaptions(); + CharSequence[] getMenuCaptions(@NotNull final Context context); } diff --git a/src/main/java/org/solovyev/android/view/AMenuBuilder.java b/src/main/java/org/solovyev/android/view/AMenuBuilder.java new file mode 100644 index 00000000..3a63cb81 --- /dev/null +++ b/src/main/java/org/solovyev/android/view/AMenuBuilder.java @@ -0,0 +1,60 @@ +package org.solovyev.android.view; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import org.jetbrains.annotations.NotNull; + +/** + * User: serso + * Date: 12/19/11 + * Time: 10:54 AM + */ +public class AMenuBuilder, D>{ + + @NotNull + private final Context context; + + @NotNull + private final AlertDialog.Builder menuBuilder; + + @NotNull + private final AMenu menu; + + @NotNull + public static , D> AMenuBuilder newInstance(@NotNull Context context, @NotNull Class enumClass) { + return new AMenuBuilder(context, EnumMenu.newInstance(enumClass)); + } + + @NotNull + public static , D> AMenuBuilder newInstance(@NotNull Context context, @NotNull AMenu menu) { + return new AMenuBuilder(context, menu); + } + + private AMenuBuilder(@NotNull Context context, @NotNull AMenu menu) { + this.context = context; + this.menuBuilder = new AlertDialog.Builder(context); + this.menu = menu; + } + + @NotNull + public AlertDialog.Builder getMenuBuilder() { + return menuBuilder; + } + + @NotNull + public AlertDialog create(@NotNull final D data) { + menuBuilder.setItems(menu.getMenuCaptions(context), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int item) { + final AMenuItem menuItem = menu.itemAt(item); + if (menuItem != null) { + menuItem.doAction(data, context); + } + } + }); + + return menuBuilder.create(); + } + + +} diff --git a/src/main/java/org/solovyev/android/view/AMenuItem.java b/src/main/java/org/solovyev/android/view/AMenuItem.java index 2d3d04ef..125e9f9b 100644 --- a/src/main/java/org/solovyev/android/view/AMenuItem.java +++ b/src/main/java/org/solovyev/android/view/AMenuItem.java @@ -17,7 +17,7 @@ import org.jetbrains.annotations.NotNull; public interface AMenuItem { @NotNull - String getCaption(); + String getCaption(@NotNull Context context); void doAction(@NotNull T data, @NotNull Context context); } diff --git a/src/main/java/org/solovyev/android/view/EnumMenu.java b/src/main/java/org/solovyev/android/view/EnumMenu.java index 620c1a0e..e71da538 100644 --- a/src/main/java/org/solovyev/android/view/EnumMenu.java +++ b/src/main/java/org/solovyev/android/view/EnumMenu.java @@ -6,6 +6,7 @@ package org.solovyev.android.view; +import android.content.Context; import org.jetbrains.annotations.NotNull; /** @@ -13,13 +14,18 @@ import org.jetbrains.annotations.NotNull; * Date: 12/18/11 * Time: 1:34 PM */ -public class EnumMenu implements AMenu { +public class EnumMenu, D> implements AMenu { @NotNull - private final AMenu menu; + private final AMenu menu; - public EnumMenu(Class enumClass) { - this.menu = new MenuImpl(enumClass.getEnumConstants()); + @NotNull + public static , D> AMenu newInstance(@NotNull Class enumClass) { + return new EnumMenu(enumClass); + } + + private EnumMenu(Class enumClass) { + this.menu = MenuImpl.newInstance(enumClass.getEnumConstants()); } @Override @@ -29,7 +35,7 @@ public class EnumMenu implements AMenu { @NotNull @Override - public CharSequence[] getMenuCaptions() { - return this.menu.getMenuCaptions(); + public CharSequence[] getMenuCaptions(@NotNull final Context context) { + return this.menu.getMenuCaptions(context); } } diff --git a/src/main/java/org/solovyev/android/view/MenuImpl.java b/src/main/java/org/solovyev/android/view/MenuImpl.java index b2d1a764..da7b7440 100644 --- a/src/main/java/org/solovyev/android/view/MenuImpl.java +++ b/src/main/java/org/solovyev/android/view/MenuImpl.java @@ -6,6 +6,7 @@ package org.solovyev.android.view; +import android.content.Context; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.solovyev.common.utils.CollectionsUtils; @@ -18,15 +19,25 @@ import java.util.List; * Date: 12/18/11 * Time: 1:31 PM */ -public class MenuImpl implements AMenu { +public class MenuImpl, D> implements AMenu { private final List menuItems = new ArrayList(); - public MenuImpl(T... menuItems) { + @NotNull + public static , D> AMenu newInstance(T... menuItems) { + return new MenuImpl(menuItems); + } + + @NotNull + public static , D> AMenu newInstance(@NotNull List menuItems) { + return new MenuImpl(menuItems); + } + + private MenuImpl(T... menuItems) { this(CollectionsUtils.asList(menuItems)); } - public MenuImpl(@NotNull List menuItems) { + private MenuImpl(@NotNull List menuItems) { this.menuItems.addAll(menuItems); } @@ -42,10 +53,10 @@ public class MenuImpl implements AMenu { @Override @NotNull - public CharSequence[] getMenuCaptions() { + public CharSequence[] getMenuCaptions(@NotNull final Context context) { final CharSequence[] result = new CharSequence[this.menuItems.size()]; for (int i = 0; i < this.menuItems.size(); i++) { - result[i] = this.menuItems.get(i).getCaption(); + result[i] = this.menuItems.get(i).getCaption(context); } return result; } diff --git a/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java b/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java index 7f0b0311..adda64b9 100644 --- a/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java +++ b/src/test/java/org/solovyev/android/calculator/history/HistoryUtilsTest.java @@ -204,9 +204,11 @@ public class HistoryUtilsTest { for (CalculatorHistoryState historyState : history.getStates()) { historyState.setId(0); + historyState.setSaved(true); } for (CalculatorHistoryState historyState : historyFromXml.getStates()) { historyState.setId(0); + historyState.setSaved(true); } Assert.assertTrue(EqualsTool.areEqual(history.getStates(), historyFromXml.getStates(), new CollectionEqualizer(null))); }