Fragments

This commit is contained in:
Sergey Solovyev 2012-09-27 00:10:33 +04:00
parent fe4ba72c7a
commit 9987670225
23 changed files with 635 additions and 480 deletions

View File

@ -39,15 +39,15 @@
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_help" android:name=".help.HelpScreensActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_functions" android:name=".math.edit.CalculatorFunctionsTabActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_functions" android:name=".math.edit.CalculatorFunctionsFragmentActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_functions" android:name=".math.edit.CalculatorFunctionsActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_operators" android:name=".math.edit.CalculatorOperatorsActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_vars_and_constants" android:name=".math.edit.CalculatorVarsTabActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_vars_and_constants" android:name=".math.edit.CalculatorVarsFragmentActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_vars_and_constants" android:name=".math.edit.CalculatorVarsActivity"/>
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_vars_and_constants" android:name=".math.edit.CalculatorVarsFragment"/>
<activity android:label="@string/c_plot_graph" android:name=".plot.CalculatorPlotActivity"/>

View File

@ -1,24 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent"
a:layout_height="match_parent"
a:orientation="vertical"
a:id="@+id/ad_parent_view"
style="?fragmentLayoutStyle"
a:orientation="vertical">
a:layout_width="match_parent"
a:layout_height="match_parent">
<TextView a:id="@+id/fragmentTitle"
a:layout_height="wrap_content"
a:layout_width="match_parent"
style="?fragmentTitleStyle"/>
<include layout="@layout/ad"/>
<ListView
a:layout_width="match_parent"
a:layout_height="match_parent"
a:id="@android:id/list"
a:divider="@null"
a:dividerHeight="0dp"
a:cacheColorHint="@android:color/transparent"/>
<ListView style="?fragmentListViewStyle"/>
</LinearLayout>

View File

@ -9,21 +9,24 @@
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:id="@+id/ad_parent_view"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
style="?fragmentLayoutStyle"
a:layout_width="match_parent"
a:layout_height="match_parent">
<ListView
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:layout_weight="1"
a:id="@android:id/list"/>
<TextView a:id="@+id/fragmentTitle"
a:layout_height="wrap_content"
a:layout_width="match_parent"
style="?fragmentTitleStyle"/>
<Button
<ListView style="?fragmentListViewStyle"/>
<!-- todo serso: uncomment button-->
<!-- <Button
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_gravity="center_horizontal"
a:text="@string/c_add"
style="@style/add_var_button"
a:onClick="addVarButtonClickHandler"/>
style="?buttonStyle"
a:onClick="addVarButtonClickHandler"/>-->
</LinearLayout>

View File

@ -8,5 +8,7 @@
<attr name="fragmentLayoutStyle" format="reference" />
<attr name="secondPaneStyle" format="reference" />
<attr name="fragmentTitleStyle" format="reference" />
<attr name="fragmentListViewStyle" format="reference" />
<attr name="buttonStyle" format="reference" />
</resources>

View File

@ -25,9 +25,24 @@
<item name="android:layout_margin">5dp</item>
</style>
<style name="default_fragment_list_view_style">
<item name="android:id">@android:id/list</item>
<item name="android:divider">@null</item>
<item name="android:dividerHeight">0dp</item>
<item name="android:cacheColorHint">@android:color/transparent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_weight">1</item>
</style>
<style name="default_fragment_title_style">
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">25sp</item>
<item name="android:layout_marginLeft">6dp</item>
</style>
<style name="default_button_style" parent="button_style">
<item name="android:background">@drawable/metro_button_blue</item>
</style>
<style name="default_fragment_layout_style">
@ -45,5 +60,7 @@
<item name="fragmentLayoutStyle">@style/default_fragment_layout_style</item>
<item name="secondPaneStyle">@style/default_second_pane_style</item>
<item name="fragmentTitleStyle">@style/default_fragment_title_style</item>
<item name="fragmentListViewStyle">@style/default_fragment_list_view_style</item>
<item name="buttonStyle">@style/default_button_style</item>
</style>
</resources>

View File

@ -15,8 +15,9 @@ import android.preference.PreferenceManager;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.util.TypedValue;
import android.view.*;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import net.robotmedia.billing.BillingController;
@ -28,6 +29,8 @@ import org.solovyev.android.FontSizeAdjuster;
import org.solovyev.android.calculator.about.CalculatorReleaseNotesActivity;
import org.solovyev.android.calculator.history.CalculatorHistoryFragment;
import org.solovyev.android.calculator.history.CalculatorSavedHistoryFragment;
import org.solovyev.android.calculator.math.edit.CalculatorVarsFragment;
import org.solovyev.android.calculator.model.VarCategory;
import org.solovyev.android.fragments.FragmentUtils;
import org.solovyev.android.prefs.Preference;
import org.solovyev.android.view.ColorButton;
@ -74,6 +77,10 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Font
if (findViewById(R.id.main_second_pane) != null) {
activityHelper.addTab(this, "history", CalculatorHistoryFragment.class, null, R.string.c_history, R.id.main_second_pane);
activityHelper.addTab(this, "saved_history", CalculatorSavedHistoryFragment.class, null, R.string.c_saved_history, R.id.main_second_pane);
for (VarCategory category : VarCategory.getCategoriesByTabOrder()) {
activityHelper.addTab(this, "vars_" + category.name(), CalculatorVarsFragment.class, CalculatorVarsFragment.createBundleFor(category.name()), category.getCaptionId(), R.id.main_second_pane);
}
activityHelper.restoreSavedTab(this);
}
@ -211,11 +218,11 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Font
*/
@Override
public void adjustFontSize(@NotNull TextView view) {
float fontPixelSize = view.getTextSize();
/*float fontPixelSize = view.getTextSize();
Display display = getWindowManager().getDefaultDisplay();
int h = Math.min(display.getWidth(), display.getHeight());
float ratio = (float) h / HVGA_WIDTH_PIXELS;
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio);
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontPixelSize * ratio);*/
}
@Override

View File

@ -10,10 +10,10 @@ import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.about.CalculatorAboutTabActivity;
import org.solovyev.android.calculator.help.CalculatorHelpTabActivity;
import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity;
import org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity;
import org.solovyev.android.calculator.math.edit.CalculatorFunctionsFragmentActivity;
import org.solovyev.android.calculator.math.edit.CalculatorOperatorsActivity;
import org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity;
import org.solovyev.android.calculator.math.edit.CalculatorVarsActivity;
import org.solovyev.android.calculator.math.edit.CalculatorVarsFragment;
import org.solovyev.android.calculator.math.edit.CalculatorVarsFragmentActivity;
import org.solovyev.android.calculator.plot.CalculatorPlotActivity;
import org.solovyev.common.text.StringUtils;
@ -41,7 +41,7 @@ public class CalculatorActivityLauncher {
}
public static void showFunctions(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorFunctionsTabActivity.class));
context.startActivity(new Intent(context, CalculatorFunctionsFragmentActivity.class));
}
public static void showOperators(@NotNull final Context context) {
@ -49,7 +49,7 @@ public class CalculatorActivityLauncher {
}
public static void showVars(@NotNull final Context context) {
context.startActivity(new Intent(context, CalculatorVarsTabActivity.class));
context.startActivity(new Intent(context, CalculatorVarsFragmentActivity.class));
}
public static void plotGraph(@NotNull final Context context, @NotNull Generic generic, @NotNull Constant constant){
@ -65,9 +65,9 @@ public class CalculatorActivityLauncher {
if (viewState.isValid() ) {
final String varValue = viewState.getText();
if (!StringUtils.isEmpty(varValue)) {
if (CalculatorVarsActivity.isValidValue(varValue)) {
final Intent intent = new Intent(context, CalculatorVarsTabActivity.class);
intent.putExtra(CalculatorVarsActivity.CREATE_VAR_EXTRA_STRING, varValue);
if (CalculatorVarsFragment.isValidValue(varValue)) {
final Intent intent = new Intent(context, CalculatorVarsFragmentActivity.class);
intent.putExtra(CalculatorVarsFragment.CREATE_VAR_EXTRA_STRING, varValue);
context.startActivity(intent);
} else {
Toast.makeText(context, R.string.c_not_valid_result, Toast.LENGTH_SHORT).show();

View File

@ -11,4 +11,6 @@ import org.jetbrains.annotations.NotNull;
public interface CalculatorFragmentHelper {
boolean isPane(@NotNull Fragment fragment);
void setPaneTitle(@NotNull Fragment fragment, int titleResId);
}

View File

@ -1,6 +1,8 @@
package org.solovyev.android.calculator;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;
import org.jetbrains.annotations.NotNull;
/**
@ -14,4 +16,13 @@ public class CalculatorFragmentHelperImpl implements CalculatorFragmentHelper {
public boolean isPane(@NotNull Fragment fragment) {
return fragment.getActivity() instanceof CalculatorActivity;
}
public void setPaneTitle(@NotNull Fragment fragment, int titleResId) {
final TextView fragmentTitle = (TextView) fragment.getView().findViewById(R.id.fragmentTitle);
if (!isPane(fragment)) {
fragmentTitle.setVisibility(View.GONE);
} else {
fragmentTitle.setText(fragment.getString(titleResId).toUpperCase());
}
}
}

View File

@ -15,7 +15,6 @@ import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockListFragment;
import com.google.ads.AdView;
import org.jetbrains.annotations.NotNull;
@ -114,12 +113,7 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
logDebug("onViewCreated");
final TextView fragmentTitle = (TextView) view.findViewById(R.id.fragmentTitle);
if (!fragmentHelper.isPane(this)) {
fragmentTitle.setVisibility(View.GONE);
} else {
fragmentTitle.setText(this.getString(getFragmentTitleResId()).toUpperCase());
}
fragmentHelper.setPaneTitle(this, getTitleResId());
adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList<CalculatorHistoryState>());
setListAdapter(adapter);
@ -172,7 +166,7 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
adView = AdsController.getInstance().inflateAd(this.getActivity(), (ViewGroup)view.findViewById(R.id.ad_parent_view), R.id.ad_parent_view);
}
protected abstract int getFragmentTitleResId();
protected abstract int getTitleResId();
@Override
public void onDestroy() {

View File

@ -21,7 +21,7 @@ import java.util.List;
public class CalculatorHistoryFragment extends AbstractCalculatorHistoryFragment {
@Override
protected int getFragmentTitleResId() {
protected int getTitleResId() {
return R.string.c_history;
}

View File

@ -21,7 +21,7 @@ import java.util.List;
public class CalculatorSavedHistoryFragment extends AbstractCalculatorHistoryFragment {
@Override
protected int getFragmentTitleResId() {
protected int getTitleResId() {
return R.string.c_saved_history;
}

View File

@ -7,7 +7,6 @@
package org.solovyev.android.calculator.math.edit;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
@ -15,19 +14,18 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.actionbarsherlock.app.SherlockListFragment;
import com.google.ads.AdView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.ads.AdsController;
import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.CalculatorMathRegistry;
import org.solovyev.android.calculator.*;
import org.solovyev.android.menu.AMenuBuilder;
import org.solovyev.android.menu.LabeledMenuItem;
import org.solovyev.android.menu.MenuImpl;
import org.solovyev.common.equals.EqualsTool;
import org.solovyev.common.filter.FilterRule;
import org.solovyev.common.filter.Filter;
import org.solovyev.common.filter.FilterRule;
import org.solovyev.common.math.MathEntity;
import org.solovyev.common.text.StringUtils;
@ -40,12 +38,29 @@ import java.util.List;
* Date: 12/21/11
* Time: 9:24 PM
*/
public abstract class AbstractMathEntityListActivity<T extends MathEntity> extends ListActivity {
public abstract class AbstractMathEntityListFragment<T extends MathEntity> extends SherlockListFragment {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
public static final String MATH_ENTITY_CATEGORY_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorVarsActivity_math_entity_category";
protected final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray()));
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@Nullable
private MathEntityArrayAdapter<T> adapter;
@ -55,47 +70,35 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
@Nullable
private AdView adView;
static void createTab(@NotNull Context context,
@NotNull TabHost tabHost,
@NotNull String tabId,
@NotNull String categoryId,
int tabCaptionId,
@NotNull Class<? extends Activity> activityClass,
@Nullable Intent parentIntent) {
@NotNull
private CalculatorFragmentHelper fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper();
TabHost.TabSpec spec;
final Intent intent;
if (parentIntent != null) {
intent = new Intent(parentIntent);
} else {
intent = new Intent();
}
intent.setClass(context, activityClass);
intent.putExtra(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec(tabId).setIndicator(context.getString(tabCaptionId)).setContent(intent);
tabHost.addTab(spec);
}
protected int getLayoutId() {
protected int getLayoutResId() {
return R.layout.math_entities;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
adView = AdsController.getInstance().inflateAd(this);
final Intent intent = getIntent();
if ( intent != null ) {
category = intent.getStringExtra(MATH_ENTITY_CATEGORY_EXTRA_STRING);
final Bundle bundle = getArguments();
if ( bundle != null ) {
category = bundle.getString(MATH_ENTITY_CATEGORY_EXTRA_STRING);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(getLayoutResId(), container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.fragmentHelper.setPaneTitle(this, getTitleResId());
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
@ -107,8 +110,6 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
final long id) {
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(((MathEntity) parent.getItemAtPosition(position)).getName());
AbstractMathEntityListActivity.this.finish();
}
});
@ -120,17 +121,21 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
final List<LabeledMenuItem<T>> menuItems = getMenuItemsOnLongClick(item);
if (!menuItems.isEmpty()) {
final AMenuBuilder<LabeledMenuItem<T>, T> menuBuilder = AMenuBuilder.newInstance(AbstractMathEntityListActivity.this, MenuImpl.newInstance(menuItems));
final AMenuBuilder<LabeledMenuItem<T>, T> menuBuilder = AMenuBuilder.newInstance(AbstractMathEntityListFragment.this.getActivity(), MenuImpl.newInstance(menuItems));
menuBuilder.create(item).show();
}
return true;
}
});
adView = AdsController.getInstance().inflateAd(this.getActivity(), (ViewGroup)view.findViewById(R.id.ad_parent_view), R.id.ad_parent_view);
}
protected abstract int getTitleResId();
@Override
protected void onDestroy() {
public void onDestroy() {
if (this.adView != null) {
this.adView.destroy();
}
@ -141,10 +146,10 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
protected abstract List<LabeledMenuItem<T>> getMenuItemsOnLongClick(@NotNull T item);
@Override
protected void onResume() {
public void onResume() {
super.onResume();
adapter = new MathEntityArrayAdapter<T>(getDescriptionGetter(), this, R.layout.math_entity, R.id.math_entity_text, getMathEntitiesByCategory());
adapter = new MathEntityArrayAdapter<T>(getDescriptionGetter(), this.getActivity(), R.layout.math_entity, R.id.math_entity_text, getMathEntitiesByCategory());
setListAdapter(adapter);
sort();
@ -216,7 +221,7 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
if (!StringUtils.isEmpty(mathEntityDescription)) {
TextView description = (TextView) result.findViewById(R.id.math_entity_description);
if (description == null) {
final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final ViewGroup itemView = (ViewGroup) layoutInflater.inflate(R.layout.math_entity, null);
description = (TextView) itemView.findViewById(R.id.math_entity_description);
itemView.removeView(description);
@ -273,4 +278,44 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
this.adapter.notifyDataSetChanged();
}
}
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
static void createTab(@NotNull Context context,
@NotNull TabHost tabHost,
@NotNull String tabId,
@NotNull String categoryId,
int tabCaptionId,
@NotNull Class<? extends Activity> activityClass,
@Nullable Intent parentIntent) {
TabHost.TabSpec spec;
final Intent intent;
if (parentIntent != null) {
intent = new Intent(parentIntent);
} else {
intent = new Intent();
}
intent.setClass(context, activityClass);
intent.putExtra(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec(tabId).setIndicator(context.getString(tabCaptionId)).setContent(intent);
tabHost.addTab(spec);
}
@NotNull
public static Bundle createBundleFor(@NotNull String categoryId) {
final Bundle result = new Bundle(1);
result.putString(MATH_ENTITY_CATEGORY_EXTRA_STRING, categoryId);
return result;
}
}

View File

@ -26,54 +26,12 @@ import java.util.List;
* Date: 10/29/11
* Time: 4:55 PM
*/
public class CalculatorFunctionsActivity extends AbstractMathEntityListActivity<Function> {
private static enum LongClickMenuItem implements LabeledMenuItem<Function> {
use(R.string.c_use) {
@Override
public void onClick(@NotNull Function data, @NotNull Context context) {
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName());
if (context instanceof Activity) {
((Activity) context).finish();
}
}
},
/*edit(R.string.c_edit) {
@Override
public void doAction(@NotNull Function data, @NotNull Context context) {
if (context instanceof AbstractMathEntityListActivity) {
}
}
},*/
copy_description(R.string.c_copy_description) {
@Override
public void onClick(@NotNull Function data, @NotNull Context context) {
final String text = CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(data.getName());
if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
}
}
};
private final int captionId;
LongClickMenuItem(int captionId) {
this.captionId = captionId;
}
@NotNull
@Override
public String getCaption(@NotNull Context context) {
return context.getString(captionId);
}
}
public class CalculatorFunctionsActivity extends AbstractMathEntityListFragment<Function> {
public static final String CREATE_FUN_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorFunctionsTabActivity_create_fun";
@Override
protected void onCreate(Bundle savedInstanceState) {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@ -104,6 +62,11 @@ public class CalculatorFunctionsActivity extends AbstractMathEntityListActivity<
}*/
}
@Override
protected int getTitleResId() {
return R.string.c_functions;
}
@NotNull
@Override
protected List<LabeledMenuItem<Function>> getMenuItemsOnLongClick(@NotNull Function item) {
@ -218,4 +181,54 @@ public class CalculatorFunctionsActivity extends AbstractMathEntityListActivity<
protected String getMathEntityCategory(@NotNull Function function) {
return CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getCategory(function);
}
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
private static enum LongClickMenuItem implements LabeledMenuItem<Function> {
use(R.string.c_use) {
@Override
public void onClick(@NotNull Function data, @NotNull Context context) {
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName());
if (context instanceof Activity) {
((Activity) context).finish();
}
}
},
/*edit(R.string.c_edit) {
@Override
public void doAction(@NotNull Function data, @NotNull Context context) {
if (context instanceof AbstractMathEntityListActivity) {
}
}
},*/
copy_description(R.string.c_copy_description) {
@Override
public void onClick(@NotNull Function data, @NotNull Context context) {
final String text = CalculatorLocatorImpl.getInstance().getEngine().getFunctionsRegistry().getDescription(data.getName());
if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
}
}
};
private final int captionId;
LongClickMenuItem(int captionId) {
this.captionId = captionId;
}
@NotNull
@Override
public String getCaption(@NotNull Context context) {
return context.getString(captionId);
}
}
}

View File

@ -0,0 +1,67 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.math.edit;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.history.CalculatorHistoryFragment;
import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity;
import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry;
/**
* User: serso
* Date: 12/21/11
* Time: 10:33 PM
*/
public class CalculatorFunctionsFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@NotNull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName());
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState);
for (AndroidFunctionsMathRegistry.Category category : AndroidFunctionsMathRegistry.Category.getCategoriesByTabOrder()) {
activityHelper.addTab(this, category.name(), CalculatorHistoryFragment.class, null, category.getCaptionId(), R.id.main_layout);
}
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState);
}
@Override
protected void onResume() {
super.onResume();
activityHelper.onResume(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
//To change body of implemented methods use File | Settings | File Templates.
}
}

View File

@ -1,52 +0,0 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.math.edit;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.LastTabSaver;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.AndroidFunctionsMathRegistry;
/**
* User: serso
* Date: 12/21/11
* Time: 10:33 PM
*/
public class CalculatorFunctionsTabActivity extends TabActivity {
@Nullable
private LastTabSaver lastTabSaver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
final TabHost tabHost = getTabHost();
for (AndroidFunctionsMathRegistry.Category category : AndroidFunctionsMathRegistry.Category.getCategoriesByTabOrder()) {
AbstractMathEntityListActivity.createTab(this, tabHost, category.name(), category.name(), category.getCaptionId(), CalculatorFunctionsActivity.class, null);
}
this.lastTabSaver = new LastTabSaver(this, AndroidFunctionsMathRegistry.Category.common.name());
AndroidUtils.centerAndWrapTabsFor(tabHost);
}
@Override
protected void onDestroy() {
if (lastTabSaver != null) {
this.lastTabSaver.destroy();
}
super.onDestroy();
}
}

View File

@ -20,41 +20,11 @@ import java.util.List;
* Time: 1:53 PM
*/
public class CalculatorOperatorsActivity extends AbstractMathEntityListActivity<Operator> {
public class CalculatorOperatorsActivity extends AbstractMathEntityListFragment<Operator> {
private static enum LongClickMenuItem implements LabeledMenuItem<Operator> {
use(R.string.c_use) {
@Override
public void onClick(@NotNull Operator data, @NotNull Context context) {
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName());
if (context instanceof Activity) {
((Activity) context).finish();
}
}
},
copy_description(R.string.c_copy_description) {
@Override
public void onClick(@NotNull Operator data, @NotNull Context context) {
final String text = OperatorDescriptionGetter.instance.getDescription(context, data.getName());
if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
}
}
};
private final int captionId;
LongClickMenuItem(int captionId) {
this.captionId = captionId;
}
@NotNull
@Override
public String getCaption(@NotNull Context context) {
return context.getString(captionId);
}
protected int getTitleResId() {
return R.string.c_operators;
}
@NotNull
@ -62,7 +32,7 @@ public class CalculatorOperatorsActivity extends AbstractMathEntityListActivity<
protected List<LabeledMenuItem<Operator>> getMenuItemsOnLongClick(@NotNull Operator item) {
final List<LabeledMenuItem<Operator>> result = new ArrayList<LabeledMenuItem<Operator>>(Arrays.asList(LongClickMenuItem.values()));
if ( StringUtils.isEmpty(OperatorDescriptionGetter.instance.getDescription(this, item.getName())) ) {
if ( StringUtils.isEmpty(OperatorDescriptionGetter.instance.getDescription(this.getActivity(), item.getName())) ) {
result.remove(LongClickMenuItem.copy_description);
}
@ -112,5 +82,48 @@ public class CalculatorOperatorsActivity extends AbstractMathEntityListActivity<
}
}
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
private static enum LongClickMenuItem implements LabeledMenuItem<Operator> {
use(R.string.c_use) {
@Override
public void onClick(@NotNull Operator data, @NotNull Context context) {
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName());
if (context instanceof Activity) {
((Activity) context).finish();
}
}
},
copy_description(R.string.c_copy_description) {
@Override
public void onClick(@NotNull Operator data, @NotNull Context context) {
final String text = OperatorDescriptionGetter.instance.getDescription(context, data.getName());
if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
}
}
};
private final int captionId;
LongClickMenuItem(int captionId) {
this.captionId = captionId;
}
@NotNull
@Override
public String getCaption(@NotNull Context context) {
return context.getString(captionId);
}
}
}

View File

@ -10,12 +10,13 @@ import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.ClipboardManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.*;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import jscl.math.function.IConstant;
@ -39,94 +40,36 @@ import java.util.List;
* Date: 9/28/11
* Time: 10:55 PM
*/
public class CalculatorVarsActivity extends AbstractMathEntityListActivity<IConstant> {
private static enum LongClickMenuItem implements LabeledMenuItem<IConstant>{
use(R.string.c_use) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName());
if (context instanceof Activity) {
((Activity) context).finish();
}
}
},
edit(R.string.c_edit) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
if (context instanceof AbstractMathEntityListActivity) {
createEditVariableDialog((AbstractMathEntityListActivity<IConstant>)context, data, data.getName(), StringUtils.getNotEmpty(data.getValue(), ""), data.getDescription());
}
}
},
remove(R.string.c_remove) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
if (context instanceof AbstractMathEntityListActivity) {
new MathEntityRemover<IConstant>(data, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), ((AbstractMathEntityListActivity<IConstant>) context)).showConfirmationDialog();
}
}
},
copy_value(R.string.c_copy_value) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
final String text = data.getValue();
if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
}
}
},
copy_description(R.string.c_copy_description) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName());
if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
}
}
};
private final int captionId;
LongClickMenuItem(int captionId) {
this.captionId = captionId;
}
@NotNull
@Override
public String getCaption(@NotNull Context context) {
return context.getString(captionId);
}
}
public class CalculatorVarsFragment extends AbstractMathEntityListFragment<IConstant> {
public static final String CREATE_VAR_EXTRA_STRING = "org.solovyev.android.calculator.math.edit.CalculatorVarsTabActivity_create_var";
@Override
protected int getLayoutId() {
return R.layout.vars;
protected int getLayoutResId() {
return R.layout.vars_fragment;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
if (intent != null) {
final String varValue = intent.getStringExtra(CREATE_VAR_EXTRA_STRING);
final Bundle bundle = getArguments();
if (bundle != null) {
final String varValue = bundle.getString(CREATE_VAR_EXTRA_STRING);
if (!StringUtils.isEmpty(varValue)) {
createEditVariableDialog(this, null, null, varValue, null);
// in order to stop intent for other tabs
intent.removeExtra(CREATE_VAR_EXTRA_STRING);
bundle.remove(CREATE_VAR_EXTRA_STRING);
}
}
}
@Override
protected int getTitleResId() {
return R.string.c_vars;
}
@NotNull
@Override
protected List<LabeledMenuItem<IConstant>> getMenuItemsOnLongClick(@NotNull IConstant item) {
@ -179,17 +122,19 @@ public class CalculatorVarsActivity extends AbstractMathEntityListActivity<ICons
return CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getCategory(var);
}
private static void createEditVariableDialog(@NotNull final AbstractMathEntityListActivity<IConstant> activity,
private static void createEditVariableDialog(@NotNull final AbstractMathEntityListFragment<IConstant> fragment,
@Nullable final IConstant var,
@Nullable final String name,
@Nullable final String value,
@Nullable final String description) {
final FragmentActivity activity = fragment.getActivity();
if (var == null || !var.isSystem()) {
final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE);
final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View editView = layoutInflater.inflate(R.layout.var_edit, null);
final String errorMsg = activity.getString(R.string.c_char_is_not_accepted);
final String errorMsg = fragment.getString(R.string.c_char_is_not_accepted);
final EditText editName = (EditText) editView.findViewById(R.id.var_edit_name);
editName.setText(name);
@ -233,9 +178,9 @@ public class CalculatorVarsActivity extends AbstractMathEntityListActivity<ICons
final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setCancelable(true)
.setNegativeButton(R.string.c_cancel, null)
.setPositiveButton(R.string.c_save, new VarEditorSaver<IConstant>(varBuilder, var, editView, activity, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), new VarEditorSaver.EditorCreator<IConstant>() {
.setPositiveButton(R.string.c_save, new VarEditorSaver<IConstant>(varBuilder, var, editView, fragment, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), new VarEditorSaver.EditorCreator<IConstant>() {
@Override
public void showEditor(@NotNull AbstractMathEntityListActivity<IConstant> activity, @Nullable IConstant editedInstance, @Nullable String name, @Nullable String value, @Nullable String description) {
public void showEditor(@NotNull AbstractMathEntityListFragment<IConstant> activity, @Nullable IConstant editedInstance, @Nullable String name, @Nullable String value, @Nullable String description) {
createEditVariableDialog(activity, editedInstance, name, value, description);
}
}))
@ -248,9 +193,9 @@ public class CalculatorVarsActivity extends AbstractMathEntityListActivity<ICons
builder.setNeutralButton(R.string.c_remove, new MathEntityRemover<IConstant>(var, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createEditVariableDialog(activity, var, name, value, description);
createEditVariableDialog(fragment, var, name, value, description);
}
}, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), activity));
}, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), fragment));
} else {
// CREATE mode
@ -259,7 +204,7 @@ public class CalculatorVarsActivity extends AbstractMathEntityListActivity<ICons
builder.create().show();
} else {
Toast.makeText(activity, activity.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show();
Toast.makeText(activity, fragment.getString(R.string.c_sys_var_cannot_be_changed), Toast.LENGTH_LONG).show();
}
}
@ -268,7 +213,8 @@ public class CalculatorVarsActivity extends AbstractMathEntityListActivity<ICons
return true;
}
@Override
// todo serso: menu
/* @Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.var_menu, menu);
@ -289,6 +235,76 @@ public class CalculatorVarsActivity extends AbstractMathEntityListActivity<ICons
}
return result;
}*/
/*
**********************************************************************
*
* STATIC
*
**********************************************************************
*/
private static enum LongClickMenuItem implements LabeledMenuItem<IConstant>{
use(R.string.c_use) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
CalculatorLocatorImpl.getInstance().getKeyboard().digitButtonPressed(data.getName());
if (context instanceof Activity) {
((Activity) context).finish();
}
}
},
edit(R.string.c_edit) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
/*if (context instanceof AbstractMathEntityListFragment) {
createEditVariableDialog((AbstractMathEntityListFragment<IConstant>)context, data, data.getName(), StringUtils.getNotEmpty(data.getValue(), ""), data.getDescription());
}*/
}
},
remove(R.string.c_remove) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
/*if (context instanceof AbstractMathEntityListFragment) {
new MathEntityRemover<IConstant>(data, null, CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry(), ((AbstractMathEntityListFragment<IConstant>) context)).showConfirmationDialog();
}*/
}
},
copy_value(R.string.c_copy_value) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
final String text = data.getValue();
if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
}
}
},
copy_description(R.string.c_copy_description) {
@Override
public void onClick(@NotNull IConstant data, @NotNull Context context) {
final String text = CalculatorLocatorImpl.getInstance().getEngine().getVarsRegistry().getDescription(data.getName());
if (!StringUtils.isEmpty(text)) {
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(text);
}
}
};
private final int captionId;
LongClickMenuItem(int captionId) {
this.captionId = captionId;
}
@NotNull
@Override
public String getCaption(@NotNull Context context) {
return context.getString(captionId);
}
}
}

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.math.edit;
import android.content.Intent;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.history.CalculatorHistoryFragment;
import org.solovyev.android.calculator.history.CalculatorHistoryFragmentActivity;
import org.solovyev.android.calculator.model.VarCategory;
/**
* User: serso
* Date: 12/21/11
* Time: 11:05 PM
*/
public class CalculatorVarsFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@NotNull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName());
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityHelper.onCreate(this, savedInstanceState);
final Bundle bundle;
final Intent intent = getIntent();
if (intent != null) {
bundle = intent.getExtras();
} else {
bundle = null;
}
for (VarCategory category : VarCategory.getCategoriesByTabOrder()) {
if (category == VarCategory.my) {
activityHelper.addTab(this, category.name(), CalculatorHistoryFragment.class, bundle, category.getCaptionId(), R.id.main_layout);
} else {
activityHelper.addTab(this, category.name(), CalculatorHistoryFragment.class, null, category.getCaptionId(), R.id.main_layout);
}
}
CalculatorLocatorImpl.getInstance().getCalculator().addCalculatorEventListener(this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
activityHelper.onSaveInstanceState(this, outState);
}
@Override
protected void onResume() {
super.onResume();
activityHelper.onResume(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
CalculatorLocatorImpl.getInstance().getCalculator().removeCalculatorEventListener(this);
}
@Override
public void onCalculatorEvent(@NotNull CalculatorEventData calculatorEventData, @NotNull CalculatorEventType calculatorEventType, @Nullable Object data) {
//To change body of implemented methods use File | Settings | File Templates.
}
}

View File

@ -1,56 +0,0 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.calculator.math.edit;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.AndroidUtils;
import org.solovyev.android.LastTabSaver;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.model.VarCategory;
/**
* User: serso
* Date: 12/21/11
* Time: 11:05 PM
*/
public class CalculatorVarsTabActivity extends TabActivity {
@Nullable
private LastTabSaver lastTabSaver;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
final TabHost tabHost = getTabHost();
for (VarCategory category : VarCategory.getCategoriesByTabOrder()) {
if (category == VarCategory.my) {
AbstractMathEntityListActivity.createTab(this, tabHost, category.name(), category.name(), category.getCaptionId(), CalculatorVarsActivity.class, getIntent());
} else {
AbstractMathEntityListActivity.createTab(this, tabHost, category.name(), category.name(), category.getCaptionId(), CalculatorVarsActivity.class, null);
}
}
this.lastTabSaver = new LastTabSaver(this, VarCategory.my.name());
AndroidUtils.centerAndWrapTabsFor(tabHost);
}
@Override
protected void onDestroy() {
if (lastTabSaver != null) {
lastTabSaver.destroy();
}
super.onDestroy();
}
}

View File

@ -34,7 +34,7 @@ import org.solovyev.common.text.StringUtils;
public class FunctionEditorSaver implements DialogInterface.OnClickListener{
public static interface EditorCreator<T extends MathEntity> {
void showEditor(@NotNull AbstractMathEntityListActivity<T> activity,
void showEditor(@NotNull AbstractMathEntityListFragment<T> activity,
@Nullable CustomFunction editedInstance,
@Nullable String name,
@Nullable String value,
@ -55,7 +55,7 @@ public class FunctionEditorSaver implements DialogInterface.OnClickListener{
private final CalculatorMathRegistry<Function> mathRegistry;
@NotNull
private final AbstractMathEntityListActivity<Function> activity;
private final AbstractMathEntityListFragment<Function> fragment;
@NotNull
private View editView;
@ -63,13 +63,13 @@ public class FunctionEditorSaver implements DialogInterface.OnClickListener{
public FunctionEditorSaver(@NotNull MathEntityBuilder<CustomFunction> varBuilder,
@Nullable CustomFunction editedInstance,
@NotNull View editView,
@NotNull AbstractMathEntityListActivity<Function> activity,
@NotNull AbstractMathEntityListFragment<Function> fragment,
@NotNull CalculatorMathRegistry<Function> mathRegistry,
@NotNull EditorCreator<Function> editorCreator) {
this.varBuilder = varBuilder;
this.editedInstance = editedInstance;
this.editView = editView;
this.activity = activity;
this.fragment = fragment;
this.mathRegistry = mathRegistry;
this.editorCreator = editorCreator;
}
@ -112,7 +112,7 @@ public class FunctionEditorSaver implements DialogInterface.OnClickListener{
error = null;
} else {
// value is not empty => must be a number
boolean valid = CalculatorVarsActivity.isValidValue(value);
boolean valid = CalculatorVarsFragment.isValidValue(value);
if (valid) {
varBuilder.setName(name);
@ -134,21 +134,21 @@ public class FunctionEditorSaver implements DialogInterface.OnClickListener{
}
if (error != null) {
Toast.makeText(activity, activity.getString(error), Toast.LENGTH_LONG).show();
editorCreator.showEditor(activity, editedInstance, name, value, null, description);
Toast.makeText(fragment.getActivity(), fragment.getString(error), Toast.LENGTH_LONG).show();
editorCreator.showEditor(fragment, editedInstance, name, value, null, description);
} else {
final Function addedVar = mathRegistry.add(varBuilder);
if (activity.isInCategory(addedVar)) {
if (fragment.isInCategory(addedVar)) {
if (editedInstance != null) {
activity.removeFromAdapter(editedInstance);
fragment.removeFromAdapter(editedInstance);
}
activity.addToAdapter(addedVar);
fragment.addToAdapter(addedVar);
}
mathRegistry.save();
if (activity.isInCategory(addedVar)) {
activity.sort();
if (fragment.isInCategory(addedVar)) {
fragment.sort();
}
}
}

View File

@ -34,25 +34,25 @@ class MathEntityRemover<T extends MathEntity> implements DialogInterface.OnClick
private final CalculatorMathRegistry<? super T> varsRegistry;
@NotNull
private final AbstractMathEntityListActivity<T> activity;
private final AbstractMathEntityListFragment<T> fragment;
public MathEntityRemover(@NotNull T mathEntity,
@Nullable DialogInterface.OnClickListener callbackOnCancel,
@NotNull CalculatorMathRegistry<? super T> varsRegistry,
@NotNull AbstractMathEntityListActivity<T> activity) {
this(mathEntity, callbackOnCancel, false, varsRegistry, activity);
@NotNull AbstractMathEntityListFragment<T> fragment) {
this(mathEntity, callbackOnCancel, false, varsRegistry, fragment);
}
public MathEntityRemover(@NotNull T mathEntity,
@Nullable DialogInterface.OnClickListener callbackOnCancel,
boolean confirmed,
@NotNull CalculatorMathRegistry<? super T> varsRegistry,
@NotNull AbstractMathEntityListActivity<T> activity) {
@NotNull AbstractMathEntityListFragment<T> fragment) {
this.mathEntity = mathEntity;
this.callbackOnCancel = callbackOnCancel;
this.confirmed = confirmed;
this.varsRegistry = varsRegistry;
this.activity = activity;
this.fragment = fragment;
}
@Override
@ -60,28 +60,28 @@ class MathEntityRemover<T extends MathEntity> implements DialogInterface.OnClick
if (!confirmed) {
showConfirmationDialog();
} else {
if (activity.isInCategory(mathEntity)) {
activity.removeFromAdapter(mathEntity);
if (fragment.isInCategory(mathEntity)) {
fragment.removeFromAdapter(mathEntity);
}
varsRegistry.remove(mathEntity);
varsRegistry.save();
if (activity.isInCategory(mathEntity)) {
activity.notifyAdapter();
if (fragment.isInCategory(mathEntity)) {
fragment.notifyAdapter();
}
}
}
public void showConfirmationDialog() {
final TextView question = new TextView(activity);
question.setText(String.format(activity.getString(R.string.c_var_removal_confirmation_question), mathEntity.getName()));
final TextView question = new TextView(fragment.getActivity());
question.setText(String.format(fragment.getString(R.string.c_var_removal_confirmation_question), mathEntity.getName()));
question.setPadding(6, 6, 6, 6);
final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
final AlertDialog.Builder builder = new AlertDialog.Builder(fragment.getActivity())
.setCancelable(true)
.setView(question)
.setTitle(R.string.c_var_removal_confirmation)
.setNegativeButton(R.string.c_no, callbackOnCancel)
.setPositiveButton(R.string.c_yes, new MathEntityRemover<T>(mathEntity, callbackOnCancel, true, varsRegistry, activity));
.setPositiveButton(R.string.c_yes, new MathEntityRemover<T>(mathEntity, callbackOnCancel, true, varsRegistry, fragment));
builder.create().show();
}

View File

@ -32,7 +32,7 @@ import org.solovyev.common.text.StringUtils;
class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickListener {
public static interface EditorCreator<T extends MathEntity> {
void showEditor(@NotNull AbstractMathEntityListActivity<T> activity,
void showEditor(@NotNull AbstractMathEntityListFragment<T> activity,
@Nullable T editedInstance,
@Nullable String name,
@Nullable String value,
@ -52,7 +52,7 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
private final CalculatorMathRegistry<T> mathRegistry;
@NotNull
private final AbstractMathEntityListActivity<T> activity;
private final AbstractMathEntityListFragment<T> fragment;
@NotNull
private View editView;
@ -60,13 +60,13 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
public VarEditorSaver(@NotNull MathEntityBuilder<? extends T> varBuilder,
@Nullable T editedInstance,
@NotNull View editView,
@NotNull AbstractMathEntityListActivity<T> activity,
@NotNull AbstractMathEntityListFragment<T> fragment,
@NotNull CalculatorMathRegistry<T> mathRegistry,
@NotNull EditorCreator<T> editorCreator) {
this.varBuilder = varBuilder;
this.editedInstance = editedInstance;
this.editView = editView;
this.activity = activity;
this.fragment = fragment;
this.mathRegistry = mathRegistry;
this.editorCreator = editorCreator;
}
@ -109,7 +109,7 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
error = null;
} else {
// value is not empty => must be a number
boolean valid = CalculatorVarsActivity.isValidValue(value);
boolean valid = CalculatorVarsFragment.isValidValue(value);
if (valid) {
varBuilder.setName(name);
@ -131,21 +131,21 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
}
if (error != null) {
Toast.makeText(activity, activity.getString(error), Toast.LENGTH_LONG).show();
editorCreator.showEditor(activity, editedInstance, name, value, description);
Toast.makeText(fragment.getActivity(), fragment.getString(error), Toast.LENGTH_LONG).show();
editorCreator.showEditor(fragment, editedInstance, name, value, description);
} else {
final T addedVar = mathRegistry.add(varBuilder);
if (activity.isInCategory(addedVar)) {
if (fragment.isInCategory(addedVar)) {
if (editedInstance != null) {
activity.removeFromAdapter(editedInstance);
fragment.removeFromAdapter(editedInstance);
}
activity.addToAdapter(addedVar);
fragment.addToAdapter(addedVar);
}
mathRegistry.save();
if (activity.isInCategory(addedVar)) {
activity.sort();
if (fragment.isInCategory(addedVar)) {
fragment.sort();
}
}
}