From 2e1925694108434cbb542918c03c3972a3f95903 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 23 Jan 2016 14:03:46 +0100 Subject: [PATCH] isNewFunction + fix for RuntimeExceptions from registry --- .../function/EditFunctionFragment.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java index 513ff0aa..fb7cbc4c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java @@ -142,12 +142,16 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) { builder.setNegativeButton(R.string.c_cancel, null); builder.setPositiveButton(R.string.ok, null); - builder.setTitle(function == null ? R.string.function_create_function : R.string.function_edit_function); - if (function != null) { + builder.setTitle(isNewFunction() ? R.string.function_create_function : R.string.function_edit_function); + if (!isNewFunction()) { builder.setNeutralButton(R.string.c_remove, null); } } + private boolean isNewFunction() { + return function == null || function.id == NO_ID; + } + @NonNull @Override public AlertDialog onCreateDialog(Bundle savedInstanceState) { @@ -170,7 +174,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC tryClose(); } }); - if (function != null) { + if (!isNewFunction()) { final Button neutral = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); neutral.setOnClickListener(new View.OnClickListener() { @Override @@ -276,25 +280,26 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC } private void tryClose() { - if (validate()) { - applyData(); + if (validate() && applyData()) { dismiss(); } } - private void applyData() { + private boolean applyData() { try { final String body = calculator.prepareExpression(bodyView.getText().toString()).getExpression(); final CppFunction newFunction = CppFunction.builder(nameView.getText().toString(), body) - .withId(function == null ? NO_ID : function.id) + .withId(isNewFunction() ? NO_ID : function.id) .withParameters(collectParameters()) .withDescription(descriptionView.getText().toString()).build(); - final Function oldFunction = (function == null || function.id == NO_ID) ? null : registry.getById(function.id); + final Function oldFunction = isNewFunction() ? null : registry.getById(function.id); registry.add(newFunction.toCustomFunctionBuilder(), oldFunction); - } catch (ParseException e) { + return true; + } catch (RuntimeException e) { setError(bodyLabel, e.getLocalizedMessage()); } + return false; } private boolean validate() { @@ -314,7 +319,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC setError(nameLabel, getString(R.string.function_already_exists)); return false; } - if (function != null && !existingFunction.getId().equals(function.getId())) { + if (!isNewFunction() && !existingFunction.getId().equals(function.getId())) { setError(nameLabel, getString(R.string.function_already_exists)); return false; }