Variables refactor
This commit is contained in:
@@ -37,10 +37,11 @@ public class CppVariable implements Jsonable, Parcelable {
|
||||
return new CppVariable[size];
|
||||
}
|
||||
};
|
||||
public static final int NO_ID = CppFunction.NO_ID;
|
||||
private static final String JSON_NAME = "n";
|
||||
private static final String JSON_VALUE = "v";
|
||||
private static final String JSON_DESCRIPTION = "d";
|
||||
protected int id = CppFunction.NO_ID;
|
||||
protected int id = NO_ID;
|
||||
@Nonnull
|
||||
protected String name;
|
||||
@Nonnull
|
||||
@@ -62,7 +63,7 @@ public class CppVariable implements Jsonable, Parcelable {
|
||||
}
|
||||
|
||||
protected CppVariable(@NonNull IConstant that) {
|
||||
id = that.isIdDefined() ? that.getId() : CppFunction.NO_ID;
|
||||
id = that.isIdDefined() ? that.getId() : NO_ID;
|
||||
name = that.getName();
|
||||
value = nullToEmpty(that.getValue());
|
||||
description = nullToEmpty(that.getDescription());
|
||||
@@ -71,7 +72,7 @@ public class CppVariable implements Jsonable, Parcelable {
|
||||
|
||||
private CppVariable(@NonNull JSONObject json) throws JSONException {
|
||||
this.name = json.getString(JSON_NAME);
|
||||
this.value = json.optString(JSON_DESCRIPTION);
|
||||
this.value = json.optString(JSON_VALUE);
|
||||
this.description = json.optString(JSON_DESCRIPTION);
|
||||
}
|
||||
|
||||
@@ -88,6 +89,11 @@ public class CppVariable implements Jsonable, Parcelable {
|
||||
return new Builder(name);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static CppVariable.Builder builder(@NonNull String name, double value) {
|
||||
return new Builder(name).withValue(value);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Builder builder(@NonNull IConstant constant) {
|
||||
return new Builder(constant);
|
||||
@@ -152,6 +158,15 @@ public class CppVariable implements Jsonable, Parcelable {
|
||||
dest.writeByte((byte) (system ? 1 : 0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (id == NO_ID) {
|
||||
return name + "=" + value;
|
||||
} else {
|
||||
return name + "[#" + id + "]=" + value;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
@NonNull
|
||||
private final CppVariable variable;
|
||||
@@ -179,6 +194,12 @@ public class CppVariable implements Jsonable, Parcelable {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder withValue(double value) {
|
||||
Check.isTrue(!built);
|
||||
return withValue(Double.toString(value));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder withSystem(boolean system) {
|
||||
Check.isTrue(!built);
|
||||
|
@@ -22,7 +22,9 @@
|
||||
|
||||
package org.solovyev.android.calculator.variables;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
@@ -45,11 +47,11 @@ import jscl.math.function.IConstant;
|
||||
import org.solovyev.android.Activities;
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.entities.EntityRemovalDialog;
|
||||
import org.solovyev.android.calculator.functions.FunctionsRegistry;
|
||||
import org.solovyev.android.calculator.keyboard.FloatingKeyboard;
|
||||
import org.solovyev.android.calculator.keyboard.FloatingKeyboardWindow;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
import org.solovyev.android.calculator.math.edit.VarEditorSaver;
|
||||
import org.solovyev.android.calculator.view.EditTextCompat;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
@@ -59,25 +61,25 @@ import javax.inject.Inject;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.solovyev.android.calculator.functions.CppFunction.NO_ID;
|
||||
import static org.solovyev.android.calculator.variables.CppVariable.NO_ID;
|
||||
|
||||
public class EditVariableFragment extends BaseDialogFragment implements CalculatorEventListener, View.OnFocusChangeListener, View.OnKeyListener, View.OnClickListener {
|
||||
public class EditVariableFragment extends BaseDialogFragment implements View.OnFocusChangeListener, View.OnKeyListener, View.OnClickListener {
|
||||
|
||||
private static final String ARG_VARIABLE = "variable";
|
||||
private final static List<Character> ACCEPTABLE_CHARACTERS = Arrays.asList(Strings.toObjects(("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_" + GreekFloatingKeyboard.ALPHABET).toCharArray()));
|
||||
@NonNull
|
||||
private final KeyboardUser keyboardUser = new KeyboardUser();
|
||||
@Bind(R.id.variable_name_label)
|
||||
TextInputLayout nameLabel;
|
||||
@Bind(R.id.variable_name)
|
||||
EditTextCompat nameView;
|
||||
@NonNull
|
||||
private final FloatingKeyboardWindow keyboardWindow = new FloatingKeyboardWindow(new PopupWindow.OnDismissListener() {
|
||||
@Override
|
||||
public void onDismiss() {
|
||||
nameView.setShowSoftInputOnFocusCompat(true);
|
||||
}
|
||||
});
|
||||
@NonNull
|
||||
private final KeyboardUser keyboardUser = new KeyboardUser();
|
||||
@Bind(R.id.variable_name_label)
|
||||
TextInputLayout nameLabel;
|
||||
@Bind(R.id.variable_name)
|
||||
EditTextCompat nameView;
|
||||
@Bind(R.id.variable_keyboard_button)
|
||||
Button keyboardButton;
|
||||
@Bind(R.id.variable_value_label)
|
||||
@@ -188,13 +190,23 @@ public class EditVariableFragment extends BaseDialogFragment implements Calculat
|
||||
neutral.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// FIXME: 2016-01-30 removal dialog
|
||||
// showRemovalDialog(function);
|
||||
showRemovalDialog(variable);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void showRemovalDialog(@NonNull final CppVariable variable) {
|
||||
EntityRemovalDialog.showForVariable(getActivity(), variable.name, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
|
||||
variablesRegistry.remove(variable.toJsclBuilder().create());
|
||||
dismiss();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void tryClose() {
|
||||
if (validate() && applyData()) {
|
||||
dismiss();
|
||||
@@ -202,6 +214,17 @@ public class EditVariableFragment extends BaseDialogFragment implements Calculat
|
||||
}
|
||||
|
||||
private boolean applyData() {
|
||||
try {
|
||||
final CppVariable newVariable = CppVariable.builder(nameView.getText().toString())
|
||||
.withId(isNewVariable() ? NO_ID : variable.id)
|
||||
.withValue(valueView.getText().toString())
|
||||
.withDescription(descriptionView.getText().toString()).build();
|
||||
final IConstant oldVariable = isNewVariable() ? null : variablesRegistry.getById(variable.id);
|
||||
variablesRegistry.add(newVariable.toJsclBuilder(), oldVariable);
|
||||
return true;
|
||||
} catch (RuntimeException e) {
|
||||
setError(valueLabel, e.getLocalizedMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -225,7 +248,7 @@ public class EditVariableFragment extends BaseDialogFragment implements Calculat
|
||||
|
||||
private boolean validateName() {
|
||||
final String name = nameView.getText().toString();
|
||||
if (!VarEditorSaver.isValidName(name)) {
|
||||
if (!Engine.isValidName(name)) {
|
||||
setError(nameLabel, getString(R.string.c_name_is_not_valid));
|
||||
return false;
|
||||
}
|
||||
@@ -266,34 +289,7 @@ public class EditVariableFragment extends BaseDialogFragment implements Calculat
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
Locator.getInstance().getCalculator().addCalculatorEventListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
Locator.getInstance().getCalculator().removeCalculatorEventListener(this);
|
||||
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
switch (calculatorEventType) {
|
||||
case constant_removed:
|
||||
case constant_added:
|
||||
case constant_changed:
|
||||
if (calculatorEventData.getSource() == this) {
|
||||
dismiss();
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("InflateParams")
|
||||
@NonNull
|
||||
@Override
|
||||
protected View onCreateDialogView(@NonNull Context context, @NonNull LayoutInflater inflater, @android.support.annotation.Nullable Bundle savedInstanceState) {
|
||||
|
@@ -9,20 +9,22 @@ import org.solovyev.common.math.MathEntity;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
class JsclConstant extends CppVariable implements IConstant {
|
||||
class JsclConstant implements IConstant {
|
||||
|
||||
@Nonnull
|
||||
private final CppVariable variable;
|
||||
private Double doubleValue;
|
||||
private Constant constant;
|
||||
|
||||
JsclConstant(@Nonnull CppVariable variable) {
|
||||
super(variable);
|
||||
this.variable = variable;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Constant getConstant() {
|
||||
if (constant == null) {
|
||||
constant = new Constant(name);
|
||||
constant = new Constant(variable.name);
|
||||
}
|
||||
return constant;
|
||||
}
|
||||
@@ -30,18 +32,18 @@ class JsclConstant extends CppVariable implements IConstant {
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return description;
|
||||
return variable.description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefined() {
|
||||
return !Strings.isNullOrEmpty(value);
|
||||
return !Strings.isNullOrEmpty(variable.value);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getValue() {
|
||||
return value;
|
||||
return variable.value;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -50,9 +52,9 @@ class JsclConstant extends CppVariable implements IConstant {
|
||||
if (doubleValue != null) {
|
||||
return doubleValue;
|
||||
}
|
||||
if (!Strings.isNullOrEmpty(value)) {
|
||||
if (!Strings.isNullOrEmpty(variable.value)) {
|
||||
try {
|
||||
doubleValue = Double.valueOf(value);
|
||||
doubleValue = Double.valueOf(variable.value);
|
||||
} catch (NumberFormatException e) {
|
||||
// do nothing - string is not a double
|
||||
}
|
||||
@@ -63,34 +65,34 @@ class JsclConstant extends CppVariable implements IConstant {
|
||||
@Nonnull
|
||||
@Override
|
||||
public String toJava() {
|
||||
return Strings.nullToEmpty(value);
|
||||
return Strings.nullToEmpty(variable.value);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
return variable.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSystem() {
|
||||
return system;
|
||||
return variable.system;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Integer getId() {
|
||||
return id == CppFunction.NO_ID ? null : id;
|
||||
return variable.id == CppVariable.NO_ID ? null : variable.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(@Nonnull Integer id) {
|
||||
this.id = id;
|
||||
variable.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIdDefined() {
|
||||
return id != CppFunction.NO_ID;
|
||||
return variable.id != CppVariable.NO_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -99,14 +101,14 @@ class JsclConstant extends CppVariable implements IConstant {
|
||||
throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + o.getClass());
|
||||
}
|
||||
final IConstant that = ((IConstant) o);
|
||||
this.name = that.getName();
|
||||
this.value = that.getValue();
|
||||
this.description = that.getDescription();
|
||||
this.system = that.isSystem();
|
||||
variable.name = that.getName();
|
||||
variable.value = Strings.nullToEmpty(that.getValue());
|
||||
variable.description = Strings.nullToEmpty(that.getDescription());
|
||||
variable.system = that.isSystem();
|
||||
if (that.isIdDefined()) {
|
||||
this.id = that.getId();
|
||||
variable.id = that.getId();
|
||||
} else {
|
||||
this.id = CppFunction.NO_ID;
|
||||
variable.id = CppVariable.NO_ID;
|
||||
}
|
||||
this.doubleValue = null;
|
||||
this.constant = null;
|
||||
|
@@ -24,243 +24,25 @@ package org.solovyev.android.calculator.variables;
|
||||
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.simpleframework.xml.Transient;
|
||||
import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.text.Strings;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import jscl.math.function.Constant;
|
||||
import jscl.math.function.ExtendedConstant;
|
||||
import jscl.math.function.IConstant;
|
||||
|
||||
@Root(name = "var")
|
||||
public class OldVar implements IConstant {
|
||||
|
||||
@Transient
|
||||
private Integer id;
|
||||
public class OldVar {
|
||||
|
||||
@Element
|
||||
@Nonnull
|
||||
private String name;
|
||||
String name;
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String value;
|
||||
String value;
|
||||
|
||||
@Element
|
||||
private boolean system;
|
||||
boolean system;
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String description;
|
||||
|
||||
@Transient
|
||||
private Constant constant;
|
||||
|
||||
private OldVar() {
|
||||
}
|
||||
|
||||
private OldVar(@Nonnull Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void copy(@Nonnull MathEntity o) {
|
||||
if (o instanceof IConstant) {
|
||||
final IConstant that = ((IConstant) o);
|
||||
this.name = that.getName();
|
||||
this.value = that.getValue();
|
||||
this.description = that.getDescription();
|
||||
this.system = that.isSystem();
|
||||
if (that.isIdDefined()) {
|
||||
this.id = that.getId();
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Trying to make a copy of unsupported type: " + o.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Double getDoubleValue() {
|
||||
Double result = null;
|
||||
if (value != null) {
|
||||
try {
|
||||
result = Double.valueOf(value);
|
||||
} catch (NumberFormatException e) {
|
||||
// do nothing - string is not a double
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String toJava() {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public boolean isSystem() {
|
||||
return system;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Integer getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(@Nonnull Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIdDefined() {
|
||||
return this.id != null;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public Constant getConstant() {
|
||||
if (constant == null) {
|
||||
constant = new Constant(this.name);
|
||||
}
|
||||
return constant;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefined() {
|
||||
return !Strings.isEmpty(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ExtendedConstant.toString(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
OldVar var = (OldVar) o;
|
||||
|
||||
if (!name.equals(var.name)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
|
||||
public static class Builder implements JBuilder<OldVar>, MathEntityBuilder<OldVar> {
|
||||
|
||||
@Nonnull
|
||||
private String name;
|
||||
|
||||
@Nullable
|
||||
private String value;
|
||||
|
||||
private boolean system = false;
|
||||
|
||||
@Nullable
|
||||
private String description;
|
||||
|
||||
@Nullable
|
||||
private Integer id;
|
||||
|
||||
public Builder() {
|
||||
}
|
||||
|
||||
public Builder(@Nonnull OldVar var) {
|
||||
this.name = var.name;
|
||||
this.value = var.value;
|
||||
this.system = var.system;
|
||||
this.description = var.description;
|
||||
this.id = var.id;
|
||||
}
|
||||
|
||||
public Builder(@Nonnull IConstant iConstant) {
|
||||
this.name = iConstant.getName();
|
||||
|
||||
this.value = iConstant.getValue();
|
||||
|
||||
this.system = iConstant.isSystem();
|
||||
this.description = iConstant.getDescription();
|
||||
if (iConstant.isIdDefined()) {
|
||||
this.id = iConstant.getId();
|
||||
}
|
||||
}
|
||||
|
||||
public Builder(@Nonnull String name, @Nonnull Double value) {
|
||||
this(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public Builder(@Nonnull String name, @Nullable String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@Nonnull
|
||||
public Builder setName(@Nonnull String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder setValue(@Nullable String value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setSystem(boolean system) {
|
||||
this.system = system;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public Builder setDescription(@Nullable String description) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public OldVar create() {
|
||||
final OldVar result;
|
||||
if (id != null) {
|
||||
result = new OldVar(id);
|
||||
} else {
|
||||
result = new OldVar();
|
||||
}
|
||||
|
||||
result.name = name;
|
||||
result.value = value;
|
||||
result.system = system;
|
||||
result.description = description;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
String description;
|
||||
}
|
||||
|
@@ -23,15 +23,13 @@
|
||||
package org.solovyev.android.calculator.variables;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static com.google.common.base.Strings.nullToEmpty;
|
||||
|
||||
@Root(name = "vars")
|
||||
@@ -49,13 +47,13 @@ public class OldVars {
|
||||
public static List<CppVariable> toCppVariables(@Nonnull OldVars oldVariables) {
|
||||
final List<CppVariable> variables = new ArrayList<>();
|
||||
for (OldVar oldVar : oldVariables.list) {
|
||||
final String name = oldVar.getName();
|
||||
final String name = oldVar.name;
|
||||
if (TextUtils.isEmpty(name)) {
|
||||
continue;
|
||||
}
|
||||
variables.add(CppVariable.builder(name)
|
||||
.withValue(nullToEmpty(oldVar.getValue()))
|
||||
.withDescription(nullToEmpty(oldVar.getDescription())).build());
|
||||
.withValue(nullToEmpty(oldVar.value))
|
||||
.withDescription(nullToEmpty(oldVar.description)).build());
|
||||
}
|
||||
return variables;
|
||||
}
|
||||
|
@@ -24,12 +24,14 @@ package org.solovyev.android.calculator.variables;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.BaseActivity;
|
||||
import org.solovyev.android.calculator.CalculatorFragmentType;
|
||||
import org.solovyev.android.calculator.R;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class VariablesActivity extends BaseActivity implements CalculatorEventListener {
|
||||
public class VariablesActivity extends BaseActivity {
|
||||
|
||||
public static final String EXTRA_VARIABLE = "variable";
|
||||
private static final CalculatorFragmentType FRAGMENT_TYPE = CalculatorFragmentType.variables;
|
||||
@@ -62,13 +64,4 @@ public class VariablesActivity extends BaseActivity implements CalculatorEventLi
|
||||
final Class<? extends Fragment> fragmentClass = FRAGMENT_TYPE.getFragmentClass();
|
||||
ui.addTab(this, fragmentTag, fragmentClass, arguments, category.title(), R.id.main_layout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
switch (calculatorEventType) {
|
||||
case use_constant:
|
||||
this.finish();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -22,19 +22,20 @@
|
||||
|
||||
package org.solovyev.android.calculator.variables;
|
||||
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.*;
|
||||
import com.squareup.otto.Bus;
|
||||
import com.squareup.otto.Subscribe;
|
||||
import jscl.math.function.IConstant;
|
||||
import org.solovyev.android.Check;
|
||||
import org.solovyev.android.calculator.*;
|
||||
import org.solovyev.android.calculator.entities.Category;
|
||||
import org.solovyev.android.calculator.entities.EntityRemovalDialog;
|
||||
import org.solovyev.android.calculator.math.MathType;
|
||||
import org.solovyev.android.calculator.math.edit.BaseEntitiesFragment;
|
||||
import org.solovyev.android.calculator.math.edit.MathEntityRemover;
|
||||
import org.solovyev.common.JPredicate;
|
||||
import org.solovyev.common.collections.Collections;
|
||||
import org.solovyev.common.text.Strings;
|
||||
@@ -45,7 +46,7 @@ import javax.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class VariablesFragment extends BaseEntitiesFragment<IConstant> implements CalculatorEventListener {
|
||||
public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
|
||||
|
||||
@Inject
|
||||
VariablesRegistry registry;
|
||||
@@ -77,9 +78,9 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> implement
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View root, Bundle savedInstanceState) {
|
||||
super.onViewCreated(root, savedInstanceState);
|
||||
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
final View view = super.onCreateView(inflater, container, savedInstanceState);
|
||||
bus.register(this);
|
||||
fab.setVisibility(View.VISIBLE);
|
||||
fab.attachToRecyclerView(recyclerView);
|
||||
fab.setOnClickListener(new View.OnClickListener() {
|
||||
@@ -88,11 +89,22 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> implement
|
||||
EditVariableFragment.showDialog(null, getFragmentManager());
|
||||
}
|
||||
});
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
bus.unregister(this);
|
||||
super.onDestroyView();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onClick(@NonNull IConstant constant) {
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_constant, constant);
|
||||
keyboard.buttonPressed(constant.getName());
|
||||
final FragmentActivity activity = getActivity();
|
||||
if (activity instanceof VariablesActivity) {
|
||||
activity.finish();
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@@ -115,23 +127,6 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> implement
|
||||
return registry.getCategory(var);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
||||
switch (calculatorEventType) {
|
||||
case constant_added:
|
||||
processConstantAdded((IConstant) data);
|
||||
break;
|
||||
|
||||
case constant_changed:
|
||||
processConstantChanged((Change<IConstant>) data);
|
||||
break;
|
||||
|
||||
case constant_removed:
|
||||
processConstantRemoved((IConstant) data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull IConstant constant, @Nonnull MenuItem.OnMenuItemClickListener listener) {
|
||||
addMenu(menu, R.string.c_use, listener);
|
||||
@@ -146,17 +141,23 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> implement
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull IConstant constant) {
|
||||
protected boolean onMenuItemClicked(@Nonnull MenuItem item, @Nonnull final IConstant constant) {
|
||||
FragmentActivity activity = getActivity();
|
||||
switch (item.getItemId()) {
|
||||
case R.string.c_use:
|
||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_constant, constant);
|
||||
onClick(constant);
|
||||
return true;
|
||||
case R.string.c_edit:
|
||||
EditVariableFragment.showDialog(CppVariable.builder(constant).build(), activity);
|
||||
return true;
|
||||
case R.string.c_remove:
|
||||
MathEntityRemover.newConstantRemover(constant, null, activity, activity).showConfirmationDialog();
|
||||
EntityRemovalDialog.showForVariable(getActivity(), constant.getName(), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Check.isTrue(which == DialogInterface.BUTTON_POSITIVE);
|
||||
registry.remove(constant);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
case R.string.c_copy_value:
|
||||
final String value = constant.getValue();
|
||||
@@ -168,45 +169,19 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> implement
|
||||
return false;
|
||||
}
|
||||
|
||||
private void processConstantRemoved(@Nonnull final IConstant constant) {
|
||||
if (this.isInCategory(constant)) {
|
||||
getUiHandler().post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final EntitiesAdapter adapter = getAdapter();
|
||||
adapter.remove(constant);
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
@Subscribe
|
||||
public void onVariableRemoved(@NonNull VariablesRegistry.RemovedEvent e) {
|
||||
onEntityRemoved(e.variable);
|
||||
}
|
||||
|
||||
private void processConstantChanged(@Nonnull final Change<IConstant> change) {
|
||||
final IConstant newConstant = change.getNewValue();
|
||||
if (this.isInCategory(newConstant)) {
|
||||
getUiHandler().post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final EntitiesAdapter adapter = getAdapter();
|
||||
adapter.remove(change.getOldValue());
|
||||
adapter.add(newConstant);
|
||||
adapter.sort();
|
||||
}
|
||||
});
|
||||
}
|
||||
@Subscribe
|
||||
public void onVariableAdded(@NonNull VariablesRegistry.AddedEvent e) {
|
||||
onEntityAdded(e.variable);
|
||||
}
|
||||
|
||||
private void processConstantAdded(@Nonnull final IConstant constant) {
|
||||
if (this.isInCategory(constant)) {
|
||||
getUiHandler().post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final EntitiesAdapter adapter = getAdapter();
|
||||
adapter.add(constant);
|
||||
adapter.sort();
|
||||
}
|
||||
});
|
||||
}
|
||||
@Subscribe
|
||||
public void onVariableChanged(@NonNull VariablesRegistry.ChangedEvent e) {
|
||||
onEntityChanged(e.newVariable);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
Reference in New Issue
Block a user