From 498b2f5429444388816b451cabc22683eac0d037 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 17 Jan 2016 22:09:40 +0100 Subject: [PATCH] EditFunctionDialog --- .../calculator/BaseDialogFragment.java | 11 + .../CalculatorActivityLauncher.java | 17 +- ...ragment.java => EditFunctionFragment.java} | 189 ++++++++++-------- .../function/FunctionEditorSaver.java | 29 ++- .../function/FunctionParamsView.java | 13 +- .../history/EditHistoryFragment.java | 4 - .../math/edit/FunctionsFragment.java | 10 +- .../res/layout/fragment_function_edit.xml | 80 ++++++++ ...m.xml => fragment_function_edit_param.xml} | 0 .../main/res/layout/fragment_history_edit.xml | 1 - app/src/main/res/layout/function_edit.xml | 119 ----------- 11 files changed, 222 insertions(+), 251 deletions(-) rename app/src/main/java/org/solovyev/android/calculator/function/{FunctionEditDialogFragment.java => EditFunctionFragment.java} (66%) create mode 100644 app/src/main/res/layout/fragment_function_edit.xml rename app/src/main/res/layout/{function_edit_param.xml => fragment_function_edit_param.xml} (100%) delete mode 100644 app/src/main/res/layout/function_edit.xml diff --git a/app/src/main/java/org/solovyev/android/calculator/BaseDialogFragment.java b/app/src/main/java/org/solovyev/android/calculator/BaseDialogFragment.java index 8c47fd32..8489bb9d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/BaseDialogFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/BaseDialogFragment.java @@ -5,6 +5,7 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.design.widget.TextInputLayout; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; @@ -45,4 +46,14 @@ public abstract class BaseDialogFragment extends DialogFragment { @NonNull protected abstract View onCreateDialogView(@NonNull Context context, @NonNull LayoutInflater inflater, @Nullable Bundle savedInstanceState); + + protected static void setError(@NonNull TextInputLayout textInput, @NonNull String error) { + textInput.setError(error); + textInput.setErrorEnabled(true); + } + + protected static void clearError(@NonNull TextInputLayout textInput) { + textInput.setError(null); + textInput.setErrorEnabled(false); + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java index 6d125725..ce669c0a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java @@ -33,16 +33,12 @@ import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; - +import jscl.math.Generic; import org.solovyev.android.Activities; import org.solovyev.android.calculator.about.CalculatorAboutActivity; -import org.solovyev.android.calculator.function.FunctionEditDialogFragment; +import org.solovyev.android.calculator.function.EditFunctionFragment; import org.solovyev.android.calculator.history.CalculatorHistoryActivity; -import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; -import org.solovyev.android.calculator.math.edit.CalculatorOperatorsActivity; -import org.solovyev.android.calculator.math.edit.CalculatorVarsActivity; -import org.solovyev.android.calculator.math.edit.VarsFragment; -import org.solovyev.android.calculator.math.edit.VarEditDialogFragment; +import org.solovyev.android.calculator.math.edit.*; import org.solovyev.android.calculator.matrix.CalculatorMatrixActivity; import org.solovyev.android.calculator.plot.CalculatorPlotActivity; import org.solovyev.android.calculator.plot.CalculatorPlotter; @@ -51,12 +47,9 @@ import org.solovyev.common.msg.Message; import org.solovyev.common.msg.MessageType; import org.solovyev.common.text.Strings; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import jscl.math.Generic; +import java.util.List; /** * User: serso @@ -168,7 +161,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener if (viewState.valid) { final String functionValue = viewState.text; if (!Strings.isEmpty(functionValue)) { - FunctionEditDialogFragment.showDialog(FunctionEditDialogFragment.Input.newFromDisplay(viewState), context); + EditFunctionFragment.showDialog(EditFunctionFragment.Input.newFromDisplay(viewState), context); } else { getNotifier().showMessage(R.string.empty_function_error, MessageType.error); } diff --git a/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java similarity index 66% rename from app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java rename to app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java index 9bfb091d..e43248b4 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditDialogFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java @@ -23,17 +23,22 @@ package org.solovyev.android.calculator.function; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; -import android.support.v4.app.DialogFragment; +import android.support.annotation.NonNull; +import android.support.design.widget.TextInputLayout; import android.support.v4.app.FragmentManager; -import android.support.v7.app.ActionBarActivity; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; +import android.widget.Button; import android.widget.EditText; +import butterknife.Bind; +import butterknife.ButterKnife; import jscl.math.Generic; import jscl.math.function.Constant; import jscl.math.function.CustomFunction; @@ -43,6 +48,7 @@ import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.math.edit.CalculatorFunctionsActivity; import org.solovyev.android.calculator.math.edit.FunctionsFragment; import org.solovyev.android.calculator.math.edit.MathEntityRemover; +import org.solovyev.android.calculator.math.edit.VarEditorSaver; import org.solovyev.android.calculator.model.AFunction; import javax.annotation.Nonnull; @@ -51,23 +57,24 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; -/** - * User: serso - * Date: 11/13/12 - * Time: 11:34 PM - */ -public class FunctionEditDialogFragment extends DialogFragment implements CalculatorEventListener { - - private static final String INPUT = "input"; +public class EditFunctionFragment extends BaseDialogFragment implements CalculatorEventListener { + private static final String ARG_INPUT = "input"; + @Bind(R.id.function_params) + FunctionParamsView paramsView; + @Bind(R.id.function_name_label) + TextInputLayout nameLabel; + @Bind(R.id.function_name) + EditText nameView; + @Bind(R.id.function_body) + EditText bodyView; + @Bind(R.id.function_description) + EditText descriptionView; private Input input; - public FunctionEditDialogFragment() { - } - @Nonnull - public static FunctionEditDialogFragment create(@Nonnull Input input) { - final FunctionEditDialogFragment fragment = new FunctionEditDialogFragment(); + public static EditFunctionFragment create(@Nonnull Input input) { + final EditFunctionFragment fragment = new EditFunctionFragment(); fragment.input = input; final Bundle args = new Bundle(); args.putParcelable("input", input); @@ -76,8 +83,8 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul } public static void showDialog(@Nonnull Input input, @Nonnull Context context) { - if (context instanceof ActionBarActivity) { - FunctionEditDialogFragment.showDialog(input, ((ActionBarActivity) context).getSupportFragmentManager()); + if (context instanceof AppCompatActivity) { + EditFunctionFragment.showDialog(input, ((AppCompatActivity) context).getSupportFragmentManager()); } else { final Intent intent = new Intent(context, CalculatorFunctionsActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); @@ -93,87 +100,97 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - - if (input == null) { - input = getArguments().getParcelable("input"); - if (input == null) throw new AssertionError(); - } + input = getArguments().getParcelable(ARG_INPUT); } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View result = inflater.inflate(R.layout.function_edit, container, false); - - if (savedInstanceState != null) { - final Parcelable input = savedInstanceState.getParcelable(INPUT); - if (input instanceof Input) { - this.input = (Input) input; - } - } - - return result; - } - - @Override - public void onViewCreated(@Nonnull View root, Bundle savedInstanceState) { - super.onViewCreated(root, savedInstanceState); - - final FunctionParamsView paramsView = (FunctionParamsView) root.findViewById(R.id.function_params_layout); - - final AFunction.Builder builder; + protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) { + builder.setNegativeButton(R.string.c_cancel, null); + builder.setPositiveButton(R.string.ok, null); final AFunction function = input.getFunction(); - if (function != null) { - builder = new AFunction.Builder(function); - } else { - builder = new AFunction.Builder(); + builder.setTitle(function == null ? R.string.function_create_function : R.string.function_edit_function); + if(function != null) { + builder.setNeutralButton(R.string.c_remove, null); } + } - final List parameterNames = input.getParameterNames(); - if (parameterNames != null) { - paramsView.init(parameterNames); - } else { - paramsView.init(); - } - - final EditText editName = (EditText) root.findViewById(R.id.function_edit_name); - // show soft keyboard automatically - editName.requestFocus(); - editName.setText(input.getName()); - - final EditText editDescription = (EditText) root.findViewById(R.id.function_edit_description); - editDescription.setText(input.getDescription()); - - final EditText editContent = (EditText) root.findViewById(R.id.function_edit_value); - editContent.setText(input.getContent()); - - root.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() { + @NonNull + @Override + public AlertDialog onCreateDialog(Bundle savedInstanceState) { + final AlertDialog dialog = super.onCreateDialog(savedInstanceState); + dialog.setCanceledOnTouchOutside(false); + dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override - public void onClick(View v) { - dismiss(); + public void onShow(DialogInterface d) { + nameView.requestFocus(); + nameView.selectAll(); + + final Button ok = dialog.getButton(AlertDialog.BUTTON_POSITIVE); + ok.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + tryClose(); + } + }); + final AFunction function = input.getFunction(); + if (function != null) { + final Function customFunction = new CustomFunction.Builder(function).create(); + final Button neutral = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); + neutral.setOnClickListener(MathEntityRemover.newFunctionRemover(customFunction, null, getActivity(), EditFunctionFragment.this)); + } } }); + return dialog; + } - root.findViewById(R.id.save_button).setOnClickListener(new FunctionEditorSaver(builder, function, root, Locator.getInstance().getEngine().getFunctionsRegistry(), this)); - - if (function == null) { - // CREATE MODE - getDialog().setTitle(R.string.function_create_function); - - root.findViewById(R.id.remove_button).setVisibility(View.GONE); - } else { - // EDIT MODE - getDialog().setTitle(R.string.function_edit_function); - - final Function customFunction = new CustomFunction.Builder(function).create(); - root.findViewById(R.id.remove_button).setOnClickListener(MathEntityRemover.newFunctionRemover(customFunction, null, this.getActivity(), FunctionEditDialogFragment.this)); + private void tryClose() { + if (validate()) { + applyData(); + dismiss(); } } - @Override - public void onSaveInstanceState(@Nonnull Bundle out) { - super.onSaveInstanceState(out); + private void applyData() { - out.putParcelable(INPUT, FunctionEditorSaver.readInput(input.getFunction(), getView())); + } + + private boolean validate() { + if (!validateName()) { + return false; + } + return true; + } + + private boolean validateName() { + final String name = nameView.getText().toString(); + if (!VarEditorSaver.isValidName(name)) { + setError(nameLabel, getString(R.string.function_name_is_not_valid)); + return false; + } + clearError(nameLabel); + return true; + } + + @NonNull + @Override + protected View onCreateDialogView(@NonNull Context context, @NonNull LayoutInflater inflater, @Nullable Bundle savedInstanceState) { + final View view = inflater.inflate(R.layout.fragment_function_edit, null); + ButterKnife.bind(this, view); + + if (savedInstanceState == null) { + final List parameterNames = input.getParameterNames(); + if (parameterNames != null) { + paramsView.init(parameterNames); + } else { + paramsView.init(); + } + + nameView.setText(input.getName()); + descriptionView.setText(input.getDescription()); + bodyView.setText(input.getContent()); + } + + return view; } @Override @@ -196,7 +213,7 @@ public class FunctionEditDialogFragment extends DialogFragment implements Calcul case function_removed: case function_added: case function_changed: - if (calculatorEventData.getSource() == FunctionEditDialogFragment.this) { + if (calculatorEventData.getSource() == EditFunctionFragment.this) { dismiss(); } break; diff --git a/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java b/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java index 8f5729c9..18292027 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/FunctionEditorSaver.java @@ -24,9 +24,11 @@ package org.solovyev.android.calculator.function; import android.view.View; import android.widget.EditText; - -import org.solovyev.android.calculator.FunctionsRegistry; +import jscl.CustomFunctionCalculationException; +import jscl.math.function.Function; +import jscl.math.function.IFunction; import org.solovyev.android.calculator.EntitiesRegistry; +import org.solovyev.android.calculator.FunctionsRegistry; import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.math.edit.VarEditorSaver; @@ -34,15 +36,10 @@ import org.solovyev.android.calculator.model.AFunction; import org.solovyev.common.msg.MessageType; import org.solovyev.common.text.Strings; -import java.util.Collections; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import jscl.CustomFunctionCalculationException; -import jscl.math.function.Function; -import jscl.math.function.IFunction; +import java.util.Collections; +import java.util.List; public class FunctionEditorSaver implements View.OnClickListener { @@ -76,27 +73,27 @@ public class FunctionEditorSaver implements View.OnClickListener { } @Nonnull - public static FunctionEditDialogFragment.Input readInput(@Nullable IFunction function, @Nonnull View root) { - final EditText editName = (EditText) root.findViewById(R.id.function_edit_name); + public static EditFunctionFragment.Input readInput(@Nullable IFunction function, @Nonnull View root) { + final EditText editName = (EditText) root.findViewById(R.id.function_name); String name = editName.getText().toString(); - final EditText editValue = (EditText) root.findViewById(R.id.function_edit_value); + final EditText editValue = (EditText) root.findViewById(R.id.function_body); String content = editValue.getText().toString(); - final EditText editDescription = (EditText) root.findViewById(R.id.function_edit_description); + final EditText editDescription = (EditText) root.findViewById(R.id.function_description); String description = editDescription.getText().toString(); - final FunctionParamsView editParams = (FunctionParamsView) root.findViewById(R.id.function_params_layout); + final FunctionParamsView editParams = (FunctionParamsView) root.findViewById(R.id.function_params); List parameterNames = editParams.getParameterNames(); - return FunctionEditDialogFragment.Input.newInstance(function, name, content, description, parameterNames); + return EditFunctionFragment.Input.newInstance(function, name, content, description, parameterNames); } @Override public void onClick(View v) { final Integer error; - final FunctionEditDialogFragment.Input input = readInput(null, view); + final EditFunctionFragment.Input input = readInput(null, view); final String name = input.getName(); final String content = input.getContent(); diff --git a/app/src/main/java/org/solovyev/android/calculator/function/FunctionParamsView.java b/app/src/main/java/org/solovyev/android/calculator/function/FunctionParamsView.java index 177411c4..604b391a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/FunctionParamsView.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/FunctionParamsView.java @@ -32,17 +32,14 @@ import android.view.ViewGroup; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; - +import jscl.text.MutableInt; import org.solovyev.android.calculator.R; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import jscl.text.MutableInt; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; public class FunctionParamsView extends LinearLayout { @@ -98,7 +95,7 @@ public class FunctionParamsView extends LinearLayout { final Integer id = paramsCount.intValue(); - final View editParamView = inflater.inflate(R.layout.function_edit_param, null); + final View editParamView = inflater.inflate(R.layout.fragment_function_edit_param, null); editParamView.setTag(getParamTag(id)); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java index b07f4f75..49bc7050 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java @@ -5,7 +5,6 @@ import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.design.widget.TextInputLayout; import android.support.v4.app.FragmentManager; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; @@ -38,9 +37,6 @@ public class EditHistoryFragment extends BaseDialogFragment { @Bind(R.id.history_comment) EditText commentView; - @Bind(R.id.history_comment_label) - TextInputLayout commentLabel; - public static void show(@NonNull HistoryState state, boolean newState, @NonNull FragmentManager fm) { final EditHistoryFragment fragment = new EditHistoryFragment(); final Bundle args = new Bundle(); diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java index f6d8b147..0b57b72a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsFragment.java @@ -31,7 +31,7 @@ import android.view.View; import jscl.math.function.Function; import jscl.math.function.IFunction; import org.solovyev.android.calculator.*; -import org.solovyev.android.calculator.function.FunctionEditDialogFragment; +import org.solovyev.android.calculator.function.EditFunctionFragment; import org.solovyev.common.text.Strings; import javax.annotation.Nonnull; @@ -55,9 +55,9 @@ public class FunctionsFragment extends BaseEntitiesFragment { final Bundle bundle = getArguments(); if (bundle != null) { - final FunctionEditDialogFragment.Input input = bundle.getParcelable(EXTRA_FUNCTION); + final EditFunctionFragment.Input input = bundle.getParcelable(EXTRA_FUNCTION); if (input != null) { - FunctionEditDialogFragment.showDialog(input, getFragmentManager()); + EditFunctionFragment.showDialog(input, getFragmentManager()); // in order to stop intent for other tabs bundle.remove(EXTRA_FUNCTION); } @@ -73,7 +73,7 @@ public class FunctionsFragment extends BaseEntitiesFragment { fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - FunctionEditDialogFragment.showDialog(FunctionEditDialogFragment.Input.newInstance(), getFragmentManager()); + EditFunctionFragment.showDialog(EditFunctionFragment.Input.newInstance(), getFragmentManager()); } }); } @@ -106,7 +106,7 @@ public class FunctionsFragment extends BaseEntitiesFragment { return true; case R.string.c_edit: if (function instanceof IFunction) { - FunctionEditDialogFragment.showDialog(FunctionEditDialogFragment.Input.newFromFunction((IFunction) function), activity.getSupportFragmentManager()); + EditFunctionFragment.showDialog(EditFunctionFragment.Input.newFromFunction((IFunction) function), activity.getSupportFragmentManager()); } return true; case R.string.c_remove: diff --git a/app/src/main/res/layout/fragment_function_edit.xml b/app/src/main/res/layout/fragment_function_edit.xml new file mode 100644 index 00000000..ec6db2ea --- /dev/null +++ b/app/src/main/res/layout/fragment_function_edit.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/function_edit_param.xml b/app/src/main/res/layout/fragment_function_edit_param.xml similarity index 100% rename from app/src/main/res/layout/function_edit_param.xml rename to app/src/main/res/layout/fragment_function_edit_param.xml diff --git a/app/src/main/res/layout/fragment_history_edit.xml b/app/src/main/res/layout/fragment_history_edit.xml index 3fd5e26e..676afd34 100644 --- a/app/src/main/res/layout/fragment_history_edit.xml +++ b/app/src/main/res/layout/fragment_history_edit.xml @@ -43,7 +43,6 @@ diff --git a/app/src/main/res/layout/function_edit.xml b/app/src/main/res/layout/function_edit.xml deleted file mode 100644 index 367812bc..00000000 --- a/app/src/main/res/layout/function_edit.xml +++ /dev/null @@ -1,119 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - -