FunctionsActivity

This commit is contained in:
serso 2016-01-24 12:19:49 +01:00
parent 0b5bc2b621
commit b5705702eb
8 changed files with 140 additions and 126 deletions

View File

@ -83,11 +83,11 @@
android:label="@string/c_about" />
<activity
android:name=".math.edit.CalculatorFunctionsActivity"
android:name=".math.edit.FunctionsActivity"
android:label="@string/c_functions" />
<activity
android:name=".math.edit.CalculatorOperatorsActivity"
android:name=".math.edit.OperatorsActivity"
android:label="@string/c_operators" />
<activity

View File

@ -39,8 +39,8 @@ import org.solovyev.android.calculator.about.CalculatorAboutActivity;
import org.solovyev.android.calculator.function.CppFunction;
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.FunctionsActivity;
import org.solovyev.android.calculator.math.edit.OperatorsActivity;
import org.solovyev.android.calculator.math.edit.CalculatorVarsActivity;
import org.solovyev.android.calculator.math.edit.VarEditDialogFragment;
import org.solovyev.android.calculator.math.edit.VarsFragment;
@ -106,7 +106,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener
}
public static void showFunctions(@Nonnull final Context context, boolean detached) {
final Intent intent = new Intent(context, CalculatorFunctionsActivity.class);
final Intent intent = new Intent(context, FunctionsActivity.class);
Activities.addIntentFlags(intent, detached, context);
context.startActivity(intent);
}
@ -116,7 +116,7 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener
}
public static void showOperators(@Nonnull final Context context, boolean detached) {
final Intent intent = new Intent(context, CalculatorOperatorsActivity.class);
final Intent intent = new Intent(context, OperatorsActivity.class);
Activities.addIntentFlags(intent, detached, context);
context.startActivity(intent);
}

View File

@ -27,45 +27,22 @@ import jscl.math.function.ArcTrigonometric;
import jscl.math.function.Comparison;
import jscl.math.function.Function;
import jscl.math.function.Trigonometric;
import org.solovyev.common.collections.Collections;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public enum FunctionCategory implements Category {
trigonometric(100, R.string.c_fun_category_trig) {
@Override
public boolean isInCategory(@Nonnull Function function) {
return (function instanceof Trigonometric || function instanceof ArcTrigonometric) && !hyperbolic_trigonometric.isInCategory(function);
}
},
hyperbolic_trigonometric(300, R.string.c_fun_category_hyper_trig) {
private final Set<String> 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<String> 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<FunctionCategory> getCategoriesByTabOrder() {
final List<FunctionCategory> result = Collections.asList(FunctionCategory.values());
java.util.Collections.sort(result, new Comparator<FunctionCategory>() {
@Override
public int compare(FunctionCategory category, FunctionCategory category1) {
return category.tabOrder - category1.tabOrder;
}
});
return result;
}
@Override
public int title() {
return title;

View File

@ -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());

View File

@ -55,7 +55,7 @@ import java.util.List;
public abstract class BaseEntitiesFragment<E extends MathEntity> extends BaseFragment {
public static final String EXTRA_CATEGORY = "category";
public static final String ARG_CATEGORY = "category";
private static final Comparator<MathEntity> COMPARATOR = new Comparator<MathEntity>() {
@Override
public int compare(MathEntity l, MathEntity r) {
@ -85,7 +85,7 @@ public abstract class BaseEntitiesFragment<E extends MathEntity> 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<E extends MathEntity> 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<E extends MathEntity> 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<E extends MathEntity> extends BaseFra
private EntitiesAdapter(@Nonnull Context context,
@Nonnull List<E> list) {
this.list = list;
Collections.sort(this.list, COMPARATOR);
this.inflater = LayoutInflater.from(context);
}
@ -250,8 +284,8 @@ public abstract class BaseEntitiesFragment<E extends MathEntity> 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<E extends MathEntity> 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;
}
}
}
}
}

View File

@ -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);
}
}
}

View File

@ -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<Function> {
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<Function> {
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<Function> {
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<Function> {
@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

View File

@ -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