From b5705702eb1a5f8049c4a3e6652142ccd478d010 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 24 Jan 2016 12:19:49 +0100 Subject: [PATCH] FunctionsActivity --- app/src/main/AndroidManifest.xml | 4 +- .../CalculatorActivityLauncher.java | 8 +- .../android/calculator/FunctionCategory.java | 75 +++++++----------- .../function/EditFunctionFragment.java | 9 +-- .../math/edit/BaseEntitiesFragment.java | 78 +++++++++++++++++-- ...nsActivity.java => FunctionsActivity.java} | 28 +++---- .../math/edit/FunctionsFragment.java | 58 ++++---------- ...rsActivity.java => OperatorsActivity.java} | 6 +- 8 files changed, 140 insertions(+), 126 deletions(-) rename app/src/main/java/org/solovyev/android/calculator/math/edit/{CalculatorFunctionsActivity.java => FunctionsActivity.java} (62%) rename app/src/main/java/org/solovyev/android/calculator/math/edit/{CalculatorOperatorsActivity.java => OperatorsActivity.java} (90%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4ea3a118..ebcdd071 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -83,11 +83,11 @@ android:label="@string/c_about" /> names = new HashSet<>(Arrays.asList("sinh", "cosh", "tanh", "coth", "asinh", "acosh", "atanh", "acoth")); - - @Override - public boolean isInCategory(@Nonnull Function function) { - return names.contains(function.getName()); - } - }, - - comparison(200, R.string.c_fun_category_comparison) { - @Override - public boolean isInCategory(@Nonnull Function function) { - return function instanceof Comparison; - } - }, - - my(0, R.string.c_fun_category_my) { + my(R.string.c_fun_category_my) { @Override public boolean isInCategory(@Nonnull Function function) { return !function.isSystem(); } }, - common(50, R.string.c_fun_category_common) { + common(R.string.c_fun_category_common) { @Override public boolean isInCategory(@Nonnull Function function) { for (FunctionCategory category : values()) { @@ -78,30 +55,38 @@ public enum FunctionCategory implements Category { return true; } + }, + + trigonometric(R.string.c_fun_category_trig) { + @Override + public boolean isInCategory(@Nonnull Function function) { + return (function instanceof Trigonometric || function instanceof ArcTrigonometric) && !hyperbolic_trigonometric.isInCategory(function); + } + }, + + comparison(R.string.c_fun_category_comparison) { + @Override + public boolean isInCategory(@Nonnull Function function) { + return function instanceof Comparison; + } + }, + + hyperbolic_trigonometric(R.string.c_fun_category_hyper_trig) { + + private final Set names = new HashSet<>(Arrays.asList("sinh", "cosh", "tanh", "coth", "asinh", "acosh", "atanh", "acoth")); + + @Override + public boolean isInCategory(@Nonnull Function function) { + return names.contains(function.getName()); + } }; - public final int tabOrder; public final int title; - FunctionCategory(int tabOrder, @StringRes int title) { - this.tabOrder = tabOrder; + FunctionCategory(@StringRes int title) { this.title = title; } - @Nonnull - public static List getCategoriesByTabOrder() { - final List result = Collections.asList(FunctionCategory.values()); - - java.util.Collections.sort(result, new Comparator() { - @Override - public int compare(FunctionCategory category, FunctionCategory category1) { - return category.tabOrder - category1.tabOrder; - } - }); - - return result; - } - @Override public int title() { return title; 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 63770664..c514568a 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 @@ -46,8 +46,7 @@ import jscl.math.function.CustomFunction; import jscl.math.function.Function; import org.solovyev.android.Check; 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.FunctionsActivity; import org.solovyev.android.calculator.math.edit.VarEditorSaver; import org.solovyev.android.calculator.view.EditTextCompat; import org.solovyev.common.math.MathRegistry; @@ -107,10 +106,10 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC } public static void showDialog(@Nonnull CppFunction function, @Nonnull Context context) { - if (!(context instanceof CalculatorFunctionsActivity)) { - final Intent intent = new Intent(context, CalculatorFunctionsActivity.class); + if (!(context instanceof FunctionsActivity)) { + final Intent intent = new Intent(context, FunctionsActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra(FunctionsFragment.EXTRA_FUNCTION, function); + intent.putExtra(FunctionsActivity.EXTRA_FUNCTION, function); context.startActivity(intent); } else { EditFunctionFragment.showDialog(function, ((AppCompatActivity) context).getSupportFragmentManager()); diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java index 279d89b2..96d45b14 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/BaseEntitiesFragment.java @@ -55,7 +55,7 @@ import java.util.List; public abstract class BaseEntitiesFragment extends BaseFragment { - public static final String EXTRA_CATEGORY = "category"; + public static final String ARG_CATEGORY = "category"; private static final Comparator COMPARATOR = new Comparator() { @Override public int compare(MathEntity l, MathEntity r) { @@ -85,7 +85,7 @@ public abstract class BaseEntitiesFragment extends BaseFra } static void putCategory(@Nonnull Bundle bundle, @Nonnull String categoryId) { - bundle.putString(EXTRA_CATEGORY, categoryId); + bundle.putString(ARG_CATEGORY, categoryId); } @Override @@ -94,7 +94,7 @@ public abstract class BaseEntitiesFragment extends BaseFra final Bundle bundle = getArguments(); if (bundle != null) { - category = bundle.getString(EXTRA_CATEGORY); + category = bundle.getString(ARG_CATEGORY); } } @@ -164,6 +164,39 @@ public abstract class BaseEntitiesFragment extends BaseFra } } + protected void onEntityAdded(@NonNull E entity) { + final EntitiesAdapter adapter = getAdapter(); + if (adapter == null) { + return; + } + if (!isInCategory(entity)) { + return; + } + adapter.add(entity); + } + + protected void onEntityChanged(@NonNull E entity) { + final EntitiesAdapter adapter = getAdapter(); + if (adapter == null) { + return; + } + if (!isInCategory(entity)) { + return; + } + adapter.update(entity); + } + + protected void onEntityRemoved(@NonNull E entity) { + final EntitiesAdapter adapter = getAdapter(); + if (adapter == null) { + return; + } + if (!isInCategory(entity)) { + return; + } + adapter.remove(entity); + } + @Nullable protected abstract String getDescription(@NonNull E entity); @@ -227,6 +260,7 @@ public abstract class BaseEntitiesFragment extends BaseFra private EntitiesAdapter(@Nonnull Context context, @Nonnull List list) { this.list = list; + Collections.sort(this.list, COMPARATOR); this.inflater = LayoutInflater.from(context); } @@ -250,8 +284,8 @@ public abstract class BaseEntitiesFragment extends BaseFra return list.get(position); } - public void set(int position, @Nonnull E function) { - list.set(position, function); + public void set(int position, @Nonnull E entity) { + list.set(position, entity); } public void sort() { @@ -260,11 +294,39 @@ public abstract class BaseEntitiesFragment extends BaseFra } public void add(@Nonnull E entity) { - list.add(entity); + final int itemCount = getItemCount(); + for (int i = 0; i < itemCount; i++) { + final E adapterEntity = getItem(i); + if (COMPARATOR.compare(adapterEntity, entity) > 0) { + list.add(i, entity); + notifyItemInserted(i); + return; + } + } + list.add(itemCount, entity); + notifyItemInserted(itemCount); } - public void remove(@Nonnull E function) { - list.remove(function); + public void remove(@Nonnull E entity) { + final int i = list.indexOf(entity); + if (i >= 0) { + list.remove(i); + notifyItemRemoved(i); + } + } + + public void update(@NonNull E entity) { + if (!entity.isIdDefined()) { + return; + } + for (int i = 0; i < adapter.getItemCount(); i++) { + final E adapterEntity = adapter.getItem(i); + if (adapterEntity.isIdDefined() && entity.getId().equals(adapterEntity.getId())) { + adapter.set(i, entity); + notifyItemChanged(i); + break; + } + } } } } diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsActivity.java similarity index 62% rename from app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java rename to app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsActivity.java index d4d6b99e..b9d256dc 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorFunctionsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/FunctionsActivity.java @@ -23,6 +23,7 @@ package org.solovyev.android.calculator.math.edit; import android.os.Bundle; +import android.os.Parcelable; import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.FunctionCategory; @@ -30,31 +31,30 @@ import org.solovyev.android.calculator.R; import javax.annotation.Nullable; -public class CalculatorFunctionsActivity extends BaseActivity { +public class FunctionsActivity extends BaseActivity { - public CalculatorFunctionsActivity() { - super(R.layout.main_empty, CalculatorFunctionsActivity.class.getSimpleName()); + public static final String EXTRA_FUNCTION = "function"; + + public FunctionsActivity() { + super(R.layout.main_empty, FunctionsActivity.class.getSimpleName()); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - final Bundle bundle = getIntent().getExtras(); + final Bundle extras = getIntent().getExtras(); + final Parcelable function = extras != null ? extras.getParcelable(EXTRA_FUNCTION) : null; final CalculatorFragmentType fragmentType = CalculatorFragmentType.functions; - for (FunctionCategory category : FunctionCategory.getCategoriesByTabOrder()) { - final Bundle fragmentParameters; - - if (category == FunctionCategory.my && bundle != null) { - BaseEntitiesFragment.putCategory(bundle, category.name()); - fragmentParameters = bundle; - } else { - fragmentParameters = BaseEntitiesFragment.createBundleFor(category.name()); + for (FunctionCategory category : FunctionCategory.values()) { + final Bundle arguments = new Bundle(2); + if (category == FunctionCategory.my && function != null) { + arguments.putParcelable(FunctionsFragment.ARG_FUNCTION, function); } - - ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), fragmentParameters, category.title, R.id.main_layout); + arguments.putString(FunctionsFragment.ARG_CATEGORY, category.name()); + ui.addTab(this, fragmentType.createSubFragmentTag(category.name()), fragmentType.getFragmentClass(), arguments, category.title, R.id.main_layout); } } } 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 a2988a23..47a8309f 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 @@ -25,33 +25,25 @@ package org.solovyev.android.calculator.math.edit; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.FragmentActivity; -import android.view.ContextMenu; -import android.view.LayoutInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; - +import android.view.*; import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; - +import jscl.math.function.Function; +import jscl.math.function.IFunction; import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.function.CppFunction; import org.solovyev.android.calculator.function.EditFunctionFragment; import org.solovyev.common.text.Strings; -import java.util.ArrayList; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; - -import jscl.math.function.Function; -import jscl.math.function.IFunction; +import java.util.ArrayList; +import java.util.List; public class FunctionsFragment extends BaseEntitiesFragment { - public static final String EXTRA_FUNCTION = "function"; + public static final String ARG_FUNCTION = "function"; @Inject FunctionsRegistry registry; @Inject @@ -71,11 +63,11 @@ public class FunctionsFragment extends BaseEntitiesFragment { final Bundle bundle = getArguments(); if (bundle != null) { - final CppFunction function = bundle.getParcelable(EXTRA_FUNCTION); + final CppFunction function = bundle.getParcelable(ARG_FUNCTION); if (function != null) { EditFunctionFragment.showDialog(function, getFragmentManager()); - // in order to stop intent for other tabs - bundle.remove(EXTRA_FUNCTION); + // don't want to show it again + bundle.remove(ARG_FUNCTION); } } } @@ -111,7 +103,7 @@ public class FunctionsFragment extends BaseEntitiesFragment { protected void onClick(@Nonnull Function function) { keyboard.buttonPressed(function.getName()); final FragmentActivity activity = getActivity(); - if (activity instanceof CalculatorFunctionsActivity) { + if (activity instanceof FunctionsActivity) { activity.finish(); } } @@ -171,41 +163,17 @@ public class FunctionsFragment extends BaseEntitiesFragment { @Subscribe public void onFunctionAdded(@NonNull final FunctionsRegistry.AddedEvent event) { - if (!isInCategory(event.function)) { - return; - } - final EntitiesAdapter adapter = getAdapter(); - adapter.add(event.function); - adapter.sort(); + onEntityAdded(event.function); } @Subscribe public void onFunctionChanged(@NonNull final FunctionsRegistry.ChangedEvent event) { - if (!isInCategory(event.newFunction)) { - return; - } - if (!event.oldFunction.isIdDefined()) { - return; - } - final EntitiesAdapter adapter = getAdapter(); - if (adapter == null) { - return; - } - for (int i = 0; i < adapter.getItemCount(); i++) { - final Function adapterFunction = adapter.getItem(i); - if (adapterFunction.isIdDefined() && event.oldFunction.getId().equals(adapterFunction.getId())) { - adapter.set(i, adapterFunction); - break; - } - } - adapter.sort(); + onEntityChanged(event.newFunction); } @Subscribe public void onFunctionRemoved(@NonNull final FunctionsRegistry.RemovedEvent event) { - final EntitiesAdapter adapter = getAdapter(); - adapter.remove(event.function); - adapter.notifyDataSetChanged(); + onEntityRemoved(event.function); } @Nullable diff --git a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java similarity index 90% rename from app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java rename to app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java index f1401b53..655a467a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/edit/CalculatorOperatorsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/edit/OperatorsActivity.java @@ -35,10 +35,10 @@ import org.solovyev.android.calculator.R; import javax.annotation.Nonnull; import javax.annotation.Nullable; -public class CalculatorOperatorsActivity extends BaseActivity implements CalculatorEventListener { +public class OperatorsActivity extends BaseActivity implements CalculatorEventListener { - public CalculatorOperatorsActivity() { - super(R.layout.main_empty, CalculatorOperatorsActivity.class.getSimpleName()); + public OperatorsActivity() { + super(R.layout.main_empty, OperatorsActivity.class.getSimpleName()); } @Override