Skip to content

Commit

Permalink
#126 Allow insert an operator before selected term: implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
mkulesh committed Oct 5, 2022
1 parent 9fc77aa commit 605efb9
Show file tree
Hide file tree
Showing 42 changed files with 151 additions and 201 deletions.
20 changes: 12 additions & 8 deletions app/src/main/java/com/mkulesh/micromath/formula/FormulaBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,6 @@ public FormulaBase(FormulaList formulaList, LinearLayout layout, int termDepth)
* Primitives
*--------------------------------------------------------*/

/**
* Procedure checks whether this formula is a root formula
*/
public boolean isRootFormula()
{
return getBaseType() != BaseType.TERM;
}

/**
* Getter for parent list of formulas
*/
Expand Down Expand Up @@ -521,6 +513,18 @@ public boolean enableDetails()
return false;
}

@Override
public boolean isRootFormula()
{
return getBaseType() != BaseType.TERM;
}

@Override
public void setInsertBefore(boolean insertBefore)
{
formulaList.getDocumentSettings().insertBefore = insertBefore;
}

/*--------------------------------------------------------*
* Read/write interface
*--------------------------------------------------------*/
Expand Down
19 changes: 17 additions & 2 deletions app/src/main/java/com/mkulesh/micromath/formula/FormulaTerm.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,21 @@ protected void initializeMainLayout()
}
}

public TermField getInsertionRefTerm()
{
if (getTerms().size() == 2)
{
// put whole text into the term that depends on "insertBefore" flag
return formulaRoot.getFormulaList().getDocumentSettings().insertBefore ?
getTerms().get(1) : getTerms().get(0);
}
else
{
// put whole text into the first term
return getArgumentTerm();
}
}

protected boolean splitIntoTerms(final String src, final TermTypeIf t, boolean pasteFromClipboard)
{
if (src == null || src.isEmpty() || (t != null && t.getLowerCaseName().equals(src.toLowerCase(Locale.ENGLISH))))
Expand All @@ -401,8 +416,8 @@ protected boolean splitIntoTerms(final String src, final TermTypeIf t, boolean p
}
if (sepPosition < 0)
{
// no separator found: put whole text into the first term
final TermField term = getArgumentTerm();
// no separator found:
final TermField term = getInsertionRefTerm();
if (term != null)
{
term.setText(src);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ public void addOperatorCode(String code, final Object extPar)

if (isTerm() && !term.getTerms().isEmpty() && savedState != null)
{
final TermField tf = term.getArgumentTerm();
final TermField tf = term.getInsertionRefTerm();
if (tf != null && tf.getEditText() != null && tf.getEditText().isConversionEnabled())
{
tf.readFromBundle(savedState, "savedState");
Expand Down
51 changes: 36 additions & 15 deletions app/src/main/java/com/mkulesh/micromath/plots/PlotFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public boolean onNewTerm(TermField owner, String s, boolean requestFocus)
}

// create and prepare new function
Function2D f = ownerFunc.addNewAfter();
Function2D f = ownerFunc.addNewFunction(getFormulaList().getDocumentSettings().insertBefore);
if (f == null)
{
return true;
Expand Down Expand Up @@ -708,7 +708,7 @@ private void ensureFunctionsNumber(int number)
}
while (functions.size() < number)
{
functions.add(ownerFunc.addNewAfter());
functions.add(ownerFunc.addNewFunction(false));
}
reIndexTerms();
updateTextSize();
Expand Down Expand Up @@ -803,7 +803,6 @@ private class Function2D implements FunctionIf

private LinearLayout xLayout = null;
private TermField x = null;
private CustomTextView xSeparator = null;
private LinearLayout yLayout = null;
private TermField y = null;
private CustomTextView settingsView = null;
Expand Down Expand Up @@ -902,7 +901,7 @@ void initializePrimaryY()
settingsView.prepare(CustomTextView.SymbolType.HOR_LINE, getFormulaList().getActivity(), PlotFunction.this);
}

Function2D addNewAfter()
Function2D addNewFunction(final boolean insertBefore)
{
int xViewIndex = ViewUtils.getViewIndex(xDataLayout, xLayout);
int yViewIndex = ViewUtils.getViewIndex(yDataLayout, settingsView);
Expand All @@ -920,8 +919,17 @@ Function2D addNewAfter()
final String tag = (String) t.getTag();
if (Y_FUNCTION_TAG.equals(tag) && t instanceof LinearLayout)
{
f.initializeY((LinearLayout) t, terms.indexOf(y) + 1);
yDataLayout.addView(t, ++yViewIndex);
final int idx = insertBefore ? terms.indexOf(y) : terms.indexOf(y) + 1;
f.initializeY((LinearLayout) t, idx);
if (insertBefore)
{
yViewIndex--;
}
else
{
yViewIndex++;
}
yDataLayout.addView(t, yViewIndex);
}
else if (Y_SETTINGS_TAG.equals(tag) && t instanceof CustomTextView)
{
Expand All @@ -932,14 +940,23 @@ else if (Y_SETTINGS_TAG.equals(tag) && t instanceof CustomTextView)
}
else if (X_SEPARATOR_TAG.equals(tag) && t instanceof CustomTextView)
{
f.xSeparator = (CustomTextView) t;
f.xSeparator.setText(getContext().getResources().getString(R.string.formula_term_separator));
xDataLayout.addView(t, ++xViewIndex);
final CustomTextView xSeparator = (CustomTextView) t;
xSeparator.setText(getContext().getResources().getString(R.string.formula_term_separator));
if (!insertBefore)
{
xViewIndex++;
}
xDataLayout.addView(t, xViewIndex);
}
else if (X_FUNCTION_TAG.equals(tag) && t instanceof LinearLayout)
{
f.initializeX((LinearLayout) t, terms.indexOf(x) + 1);
xDataLayout.addView(t, ++xViewIndex);
final int idx = insertBefore ? terms.indexOf(x) : terms.indexOf(x) + 1;
f.initializeX((LinearLayout) t, idx);
if (!insertBefore)
{
xViewIndex++;
}
xDataLayout.addView(t, xViewIndex);
}
}
return f;
Expand All @@ -949,18 +966,22 @@ void erase()
{
terms.remove(x);
terms.remove(y);
xDataLayout.removeView(xLayout);
removeSeparator();
xDataLayout.removeView(xLayout);
yDataLayout.removeView(settingsView);
yDataLayout.removeView(yLayout);
}

void removeSeparator()
{
if (xSeparator != null)
for (int i = 1; i < xDataLayout.getChildCount(); i++)
{
xDataLayout.removeView(xSeparator);
xSeparator = null;
if (xDataLayout.getChildAt(i) == xLayout &&
X_SEPARATOR_TAG.equals(xDataLayout.getChildAt(i - 1).getTag()))
{
xDataLayout.removeViewAt(i - 1);
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public class DocumentProperties
private ScaledDimensions scaledDimensions = null;
public boolean redefineAllowed = true;

// flag that controls term insertion mode: not saved in state, set from the context menu
public boolean insertBefore = false;

/**
* Default constructor
*/
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/mkulesh/micromath/utils/ViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ public static void invalidateLayout(View v, final LinearLayout l)
public static void updateMenuIconColor(Context context, MenuItem m)
{
CompatUtils.setDrawableColorAttr(context, m.getIcon(),
m.isEnabled() ? R.attr.colorMicroMathIcon : R.attr.colorPrimaryDark);
m.isEnabled() ?
(m.isChecked() ? R.attr.colorAccent : R.attr.colorMicroMathIcon)
: R.attr.colorPrimaryDark);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ActionMode;

Expand All @@ -33,6 +35,7 @@ public class ContextMenuHandler
{
enum Type
{
INSERT_BEFORE(R.id.context_menu_insert_before),
EXPAND(R.id.context_menu_expand),
CUT(R.id.context_menu_cut),
COPY(R.id.context_menu_copy),
Expand All @@ -55,6 +58,7 @@ int getResId()
private final Context context;
private FormulaChangeIf formulaChangeIf = null;
private androidx.appcompat.view.ActionMode actionMode = null;
private Menu contextMenu = null;
private View actionModeOwner = null;

public ContextMenuHandler(Context context)
Expand All @@ -68,6 +72,7 @@ public ContextMenuHandler(Context context)

public void initialize(TypedArray a)
{
enabled[Type.INSERT_BEFORE.ordinal()] = a.getBoolean(R.styleable.CustomViewExtension_contextMenuInsertBefore, true);
enabled[Type.EXPAND.ordinal()] = a.getBoolean(R.styleable.CustomViewExtension_contextMenuExpand, true);
enabled[Type.CUT.ordinal()] = a.getBoolean(R.styleable.CustomViewExtension_contextMenuCut, true);
enabled[Type.COPY.ordinal()] = a.getBoolean(R.styleable.CustomViewExtension_contextMenuCopy, true);
Expand Down Expand Up @@ -115,6 +120,7 @@ public boolean onCreateActionMode(ActionMode mode, Menu menu)
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
contextMenu = menu;
return false; // Return false if nothing is done
}

Expand All @@ -124,7 +130,7 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item)
{
if (formulaChangeIf != null)
{
if (processMenu(item.getItemId()))
if (processMenu(item))
{
mode.finish();
}
Expand All @@ -137,9 +143,11 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item)
public void onDestroyActionMode(ActionMode mode)
{
actionMode = null;
contextMenu = null;
if (formulaChangeIf != null)
{
formulaChangeIf.finishActionMode(actionModeOwner);
formulaChangeIf.setInsertBefore(false);
}
}
};
Expand All @@ -148,6 +156,7 @@ public void startActionMode(AppCompatActivity activity, View actionModeOwner, Fo
{
this.actionModeOwner = actionModeOwner;
this.formulaChangeIf = formulaChangeIf;
formulaChangeIf.setInsertBefore(false);

ArrayList<View> list = null;
if (this.actionModeOwner != null && this.actionModeOwner instanceof CustomEditText)
Expand All @@ -166,10 +175,26 @@ public void startActionMode(AppCompatActivity activity, View actionModeOwner, Fo
actionMode = activity.startSupportActionMode(actionModeCallback);
}

private boolean processMenu(int itemId)
@Nullable MenuItem getMenuItem(Type t)
{
switch (itemId)
return contextMenu != null ? contextMenu.findItem(t.getResId()) : null;
}

private boolean processMenu(final @NonNull MenuItem item)
{
switch (item.getItemId())
{
case R.id.context_menu_insert_before:
{
final MenuItem m = getMenuItem(Type.INSERT_BEFORE);
if (m != null)
{
m.setChecked(!m.isChecked());
formulaChangeIf.setInsertBefore(m.isChecked());
ViewUtils.updateMenuIconColor(context, m);
}
return false;
}
case R.id.context_menu_expand:
FormulaChangeIf newIf = formulaChangeIf.onExpandSelection(actionModeOwner);
if (newIf != null)
Expand All @@ -178,6 +203,18 @@ private boolean processMenu(int itemId)
formulaChangeIf.onTermSelection(null, true, null);
actionModeOwner = null;
}
else
{
item.setVisible(false);
}
if (newIf == null || newIf.isRootFormula())
{
final MenuItem m = getMenuItem(Type.INSERT_BEFORE);
if (m != null)
{
m.setVisible(false);
}
}
return false;
case R.id.context_menu_cut:
if (actionModeOwner != null && actionModeOwner instanceof CustomEditText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,14 @@ public interface FormulaChangeIf
* Procedure returns whether the object details view is enabled for this formula
*/
boolean enableDetails();

/**
* Procedure checks whether this formula is a root formula
*/
boolean isRootFormula();

/**
* Procedure sets the flag that a new term shall be added before existing term
*/
void setInsertBefore(boolean insertBefore);
}
Binary file modified app/src/main/res/drawable-hdpi/ic_add_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-hdpi/ic_add_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-hdpi/ic_add_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-hdpi/ic_add_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-mdpi/ic_add_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-mdpi/ic_add_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-mdpi/ic_add_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-mdpi/ic_add_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xhdpi/ic_add_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xhdpi/ic_add_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xhdpi/ic_add_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xhdpi/ic_add_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xxhdpi/ic_add_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xxhdpi/ic_add_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xxhdpi/ic_add_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xxhdpi/ic_add_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xxxhdpi/ic_add_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xxxhdpi/ic_add_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xxxhdpi/ic_add_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable-xxxhdpi/ic_add_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions app/src/main/res/layout/formula_equation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/formula_left_term_key"
micromath:contextMenuInsertBefore="false"
micromath:equationName="true" />

<com.mkulesh.micromath.widgets.CustomTextView
android:id="@+id/formula_equation_assign"
style="@style/FormulaTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
micromath:contextMenuInsertBefore="false"
android:text="@string/formula_assign_definition" />

<com.mkulesh.micromath.widgets.CustomEditText
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/formula_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
style="@style/FormulaTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
micromath:contextMenuInsertBefore="false"
android:text="@string/formula_result_definition" />

<com.mkulesh.micromath.widgets.CustomTextView
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/image_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
android:layout_height="100dp"
android:maxLines="9999"
android:text="@string/image_fragment_text"
micromath:contextMenuInsertBefore="false"
micromath:contextMenuCopy="true"
micromath:contextMenuCut="true"
micromath:contextMenuExpand="true"
Expand Down
Loading

0 comments on commit 605efb9

Please sign in to comment.