1. useHistoryState event type has been removed
2. ContextMenu is now used for context menu in HistoryFragment
This commit is contained in:
parent
00f75ce825
commit
0badc913c0
@ -330,6 +330,11 @@ public final class App {
|
|||||||
return spannable.toString();
|
return spannable.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static Editor getEditor() {
|
||||||
|
return Locator.getInstance().getEditor();
|
||||||
|
}
|
||||||
|
|
||||||
private static class MyBus extends Bus {
|
private static class MyBus extends Bus {
|
||||||
@Override
|
@Override
|
||||||
public void post(final Object event) {
|
public void post(final Object event) {
|
||||||
|
@ -91,9 +91,6 @@ public enum CalculatorEventType {
|
|||||||
// @Nonnull CalculatorHistoryState
|
// @Nonnull CalculatorHistoryState
|
||||||
history_state_added,
|
history_state_added,
|
||||||
|
|
||||||
// @Nonnull CalculatorHistoryState
|
|
||||||
use_history_state,
|
|
||||||
|
|
||||||
clear_history_requested,
|
clear_history_requested,
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -24,7 +24,6 @@ package org.solovyev.android.calculator;
|
|||||||
|
|
||||||
import org.solovyev.android.Check;
|
import org.solovyev.android.Check;
|
||||||
import org.solovyev.android.calculator.history.EditorHistoryState;
|
import org.solovyev.android.calculator.history.EditorHistoryState;
|
||||||
import org.solovyev.android.calculator.history.HistoryState;
|
|
||||||
import org.solovyev.android.calculator.text.TextProcessor;
|
import org.solovyev.android.calculator.text.TextProcessor;
|
||||||
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
import org.solovyev.android.calculator.text.TextProcessorEditorResult;
|
||||||
import org.solovyev.common.text.Strings;
|
import org.solovyev.common.text.Strings;
|
||||||
@ -34,21 +33,17 @@ import javax.annotation.Nullable;
|
|||||||
|
|
||||||
import static java.lang.Math.min;
|
import static java.lang.Math.min;
|
||||||
|
|
||||||
public class Editor implements CalculatorEventListener {
|
public class Editor {
|
||||||
|
|
||||||
private static final String TAG = App.subTag("Editor");
|
private static final String TAG = App.subTag("Editor");
|
||||||
@Nonnull
|
|
||||||
private final CalculatorEventHolder lastEventHolder;
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private final TextProcessor<TextProcessorEditorResult, String> textProcessor;
|
private final TextProcessor<TextProcessorEditorResult, String> textProcessor;
|
||||||
@Nullable
|
@Nullable
|
||||||
private EditorView view;
|
private EditorView view;
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private EditorState state = EditorState.empty();
|
private EditorState state = EditorState.empty();
|
||||||
public Editor(@Nonnull Calculator calculator, @Nullable TextProcessor<TextProcessorEditorResult, String> textProcessor) {
|
public Editor(@Nullable TextProcessor<TextProcessorEditorResult, String> textProcessor) {
|
||||||
this.textProcessor = textProcessor;
|
this.textProcessor = textProcessor;
|
||||||
calculator.addCalculatorEventListener(this);
|
|
||||||
this.lastEventHolder = new CalculatorEventHolder(CalculatorUtils.createFirstEventDataId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int clamp(int selection, @Nonnull CharSequence text) {
|
public static int clamp(int selection, @Nonnull CharSequence text) {
|
||||||
@ -108,22 +103,10 @@ public class Editor implements CalculatorEventListener {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Nonnull
|
||||||
public void onCalculatorEvent(@Nonnull CalculatorEventData evenData,
|
public EditorState setState(@Nonnull EditorHistoryState state) {
|
||||||
@Nonnull CalculatorEventType eventType,
|
|
||||||
@Nullable Object data) {
|
|
||||||
Check.isMainThread();
|
Check.isMainThread();
|
||||||
final CalculatorEventHolder.Result result = lastEventHolder.apply(evenData);
|
return setText(Strings.getNotEmpty(state.getText(), ""), state.getCursorPosition());
|
||||||
|
|
||||||
if (result.isNewAfter()) {
|
|
||||||
switch (eventType) {
|
|
||||||
case use_history_state:
|
|
||||||
final HistoryState historyState = (HistoryState) data;
|
|
||||||
final EditorHistoryState editorState = historyState.getEditorState();
|
|
||||||
this.setText(Strings.getNotEmpty(editorState.getText(), ""), editorState.getCursorPosition());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
|
@ -92,7 +92,7 @@ public class Locator implements CalculatorLocator {
|
|||||||
this.calculatorPreferenceService = preferenceService;
|
this.calculatorPreferenceService = preferenceService;
|
||||||
this.calculatorPlotter = plotter;
|
this.calculatorPlotter = plotter;
|
||||||
|
|
||||||
editor = new Editor(this.calculator, editorTextProcessor);
|
editor = new Editor(editorTextProcessor);
|
||||||
display = new Display(this.calculator);
|
display = new Display(this.calculator);
|
||||||
calculatorKeyboard = keyboard;
|
calculatorKeyboard = keyboard;
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
package org.solovyev.android.calculator.history;
|
package org.solovyev.android.calculator.history;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
@ -31,16 +32,17 @@ import android.os.Bundle;
|
|||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v4.app.FragmentActivity;
|
import android.support.v4.app.FragmentActivity;
|
||||||
import android.support.v4.app.ListFragment;
|
import android.support.v4.app.ListFragment;
|
||||||
|
import android.text.ClipboardManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.ContextMenu;
|
||||||
import android.view.*;
|
import android.view.*;
|
||||||
import android.widget.AdapterView;
|
import android.widget.*;
|
||||||
import android.widget.ArrayAdapter;
|
|
||||||
import android.widget.ListView;
|
|
||||||
import com.melnykov.fab.FloatingActionButton;
|
import com.melnykov.fab.FloatingActionButton;
|
||||||
import org.solovyev.android.calculator.*;
|
import org.solovyev.android.calculator.*;
|
||||||
import org.solovyev.android.calculator.R;
|
import org.solovyev.android.calculator.R;
|
||||||
import org.solovyev.android.calculator.jscl.JsclOperation;
|
import org.solovyev.android.calculator.jscl.JsclOperation;
|
||||||
import org.solovyev.android.menu.*;
|
import org.solovyev.android.menu.*;
|
||||||
|
import org.solovyev.android.plotter.Check;
|
||||||
import org.solovyev.common.JPredicate;
|
import org.solovyev.common.JPredicate;
|
||||||
import org.solovyev.common.collections.Collections;
|
import org.solovyev.common.collections.Collections;
|
||||||
import org.solovyev.common.equals.Equalizer;
|
import org.solovyev.common.equals.Equalizer;
|
||||||
@ -54,18 +56,11 @@ import java.util.ArrayList;
|
|||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static android.view.Menu.NONE;
|
||||||
import static org.solovyev.android.calculator.CalculatorEventType.clear_history_requested;
|
import static org.solovyev.android.calculator.CalculatorEventType.clear_history_requested;
|
||||||
|
|
||||||
public abstract class BaseHistoryFragment extends ListFragment implements CalculatorEventListener {
|
public abstract class BaseHistoryFragment extends ListFragment implements CalculatorEventListener {
|
||||||
|
|
||||||
/*
|
|
||||||
**********************************************************************
|
|
||||||
*
|
|
||||||
* CONSTANTS
|
|
||||||
*
|
|
||||||
**********************************************************************
|
|
||||||
*/
|
|
||||||
|
|
||||||
public static final Comparator<HistoryState> COMPARATOR = new Comparator<HistoryState>() {
|
public static final Comparator<HistoryState> COMPARATOR = new Comparator<HistoryState>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(HistoryState state1, HistoryState state2) {
|
public int compare(HistoryState state1, HistoryState state2) {
|
||||||
@ -109,7 +104,7 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isAlreadySaved(@Nonnull HistoryState historyState) {
|
public static boolean isAlreadySaved(@Nonnull HistoryState historyState) {
|
||||||
if (historyState.isSaved()) throw new AssertionError();
|
Check.isTrue(!historyState.isSaved());
|
||||||
|
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
try {
|
try {
|
||||||
@ -131,10 +126,6 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void useHistoryItem(@Nonnull final HistoryState state) {
|
|
||||||
Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.use_history_state, state);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static String getHistoryText(@Nonnull HistoryState state) {
|
public static String getHistoryText(@Nonnull HistoryState state) {
|
||||||
final StringBuilder result = new StringBuilder();
|
final StringBuilder result = new StringBuilder();
|
||||||
@ -152,6 +143,14 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
|||||||
return jsclOperation == JsclOperation.simplify ? "≡" : "=";
|
return jsclOperation == JsclOperation.simplify ? "≡" : "=";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void useState(@Nonnull final HistoryState state) {
|
||||||
|
App.getEditor().setState(state.getEditorState());
|
||||||
|
final FragmentActivity activity = getActivity();
|
||||||
|
if (!(activity instanceof CalculatorActivity)) {
|
||||||
|
activity.finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -201,42 +200,11 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
|||||||
final View view,
|
final View view,
|
||||||
final int position,
|
final int position,
|
||||||
final long id) {
|
final long id) {
|
||||||
|
useState((HistoryState) parent.getItemAtPosition(position));
|
||||||
useHistoryItem((HistoryState) parent.getItemAtPosition(position));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
registerForContextMenu(lv);
|
||||||
@Override
|
|
||||||
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
|
|
||||||
final HistoryState historyState = (HistoryState) parent.getItemAtPosition(position);
|
|
||||||
|
|
||||||
final FragmentActivity activity = getActivity();
|
|
||||||
|
|
||||||
final HistoryItemMenuData data = new HistoryItemMenuData(historyState, adapter);
|
|
||||||
|
|
||||||
final List<HistoryItemMenuItem> menuItems = Collections.asList(HistoryItemMenuItem.values());
|
|
||||||
|
|
||||||
if (historyState.isSaved()) {
|
|
||||||
menuItems.remove(HistoryItemMenuItem.save);
|
|
||||||
} else {
|
|
||||||
if (isAlreadySaved(historyState)) {
|
|
||||||
menuItems.remove(HistoryItemMenuItem.save);
|
|
||||||
}
|
|
||||||
menuItems.remove(HistoryItemMenuItem.remove);
|
|
||||||
menuItems.remove(HistoryItemMenuItem.edit);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (historyState.getDisplayState().isValid() && Strings.isEmpty(historyState.getDisplayState().getEditorState().getText())) {
|
|
||||||
menuItems.remove(HistoryItemMenuItem.copy_result);
|
|
||||||
}
|
|
||||||
|
|
||||||
final ContextMenuBuilder<HistoryItemMenuItem, HistoryItemMenuData> menuBuilder = ContextMenuBuilder.newInstance(activity, "history-menu", ListContextMenu.newInstance(menuItems));
|
|
||||||
menuBuilder.build(data).show();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -249,6 +217,124 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
|||||||
PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(preferencesListener);
|
PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(preferencesListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
||||||
|
super.onCreateContextMenu(menu, v, menuInfo);
|
||||||
|
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
|
||||||
|
final HistoryState state = (HistoryState) getListView().getItemAtPosition(info.position);
|
||||||
|
|
||||||
|
if (state.isSaved()) {
|
||||||
|
menu.add(NONE, R.string.c_use, NONE, R.string.c_use);
|
||||||
|
menu.add(NONE, R.string.c_copy_expression, NONE, R.string.c_copy_expression);
|
||||||
|
if (shouldHaveCopyResult(state)) {
|
||||||
|
menu.add(NONE, R.string.c_copy_result, NONE, R.string.c_copy_result);
|
||||||
|
}
|
||||||
|
menu.add(NONE, R.string.c_edit, NONE, R.string.c_edit);
|
||||||
|
menu.add(NONE, R.string.c_remove, NONE, R.string.c_remove);
|
||||||
|
} else {
|
||||||
|
menu.add(NONE, R.string.c_use, NONE, R.string.c_use);
|
||||||
|
menu.add(NONE, R.string.c_copy_expression, NONE, R.string.c_copy_expression);
|
||||||
|
if (shouldHaveCopyResult(state)) {
|
||||||
|
menu.add(NONE, R.string.c_copy_result, NONE, R.string.c_copy_result);
|
||||||
|
}
|
||||||
|
if (!isAlreadySaved(state)) {
|
||||||
|
menu.add(NONE, R.string.c_save, NONE, R.string.c_save);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onContextItemSelected(MenuItem item) {
|
||||||
|
final Context context = getActivity();
|
||||||
|
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
|
||||||
|
final HistoryState state = (HistoryState) getListView().getItemAtPosition(info.position);
|
||||||
|
|
||||||
|
switch (item.getItemId()) {
|
||||||
|
case R.string.c_use:
|
||||||
|
useState(state);
|
||||||
|
return true;
|
||||||
|
case R.string.c_copy_expression:
|
||||||
|
final String editorText = state.getEditorState().getText();
|
||||||
|
if (!Strings.isEmpty(editorText)) {
|
||||||
|
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
|
||||||
|
clipboard.setText(editorText);
|
||||||
|
Toast.makeText(context, context.getText(R.string.c_expression_copied), Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
case R.string.c_copy_result:
|
||||||
|
final String displayText = state.getDisplayState().getEditorState().getText();
|
||||||
|
if (!Strings.isEmpty(displayText)) {
|
||||||
|
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
|
||||||
|
clipboard.setText(displayText);
|
||||||
|
Toast.makeText(context, context.getText(R.string.c_result_copied), Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
case R.string.c_save:
|
||||||
|
if (!state.isSaved()) {
|
||||||
|
createEditHistoryDialog(state, context, true);
|
||||||
|
} else {
|
||||||
|
Toast.makeText(context, context.getText(R.string.c_history_already_saved), Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
case R.string.c_edit:
|
||||||
|
if (state.isSaved()) {
|
||||||
|
createEditHistoryDialog(state, context, false);
|
||||||
|
} else {
|
||||||
|
Toast.makeText(context, context.getText(R.string.c_history_must_be_saved), Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
case R.string.c_remove:
|
||||||
|
if (state.isSaved()) {
|
||||||
|
getAdapter().remove(state);
|
||||||
|
Locator.getInstance().getHistory().removeSavedHistory(state);
|
||||||
|
Toast.makeText(context, context.getText(R.string.c_history_was_removed), Toast.LENGTH_LONG).show();
|
||||||
|
getAdapter().notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
return super.onContextItemSelected(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createEditHistoryDialog(@Nonnull final HistoryState state, @Nonnull final Context context, final boolean save) {
|
||||||
|
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||||
|
final View editView = layoutInflater.inflate(R.layout.history_edit, null);
|
||||||
|
final TextView historyExpression = (TextView) editView.findViewById(R.id.history_edit_expression);
|
||||||
|
historyExpression.setText(BaseHistoryFragment.getHistoryText(state));
|
||||||
|
|
||||||
|
final EditText comment = (EditText) editView.findViewById(R.id.history_edit_comment);
|
||||||
|
comment.setText(state.getComment());
|
||||||
|
|
||||||
|
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
|
||||||
|
.setTitle(save ? R.string.c_save_history : R.string.c_edit_history)
|
||||||
|
.setCancelable(true)
|
||||||
|
.setNegativeButton(R.string.c_cancel, null)
|
||||||
|
.setPositiveButton(R.string.c_save, new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
if (save) {
|
||||||
|
final HistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(state);
|
||||||
|
savedHistoryItem.setComment(comment.getText().toString());
|
||||||
|
Locator.getInstance().getHistory().save();
|
||||||
|
// we don't need to add element to the adapter as adapter of another activity must be updated and not this
|
||||||
|
//data.getAdapter().add(savedHistoryItem);
|
||||||
|
} else {
|
||||||
|
state.setComment(comment.getText().toString());
|
||||||
|
Locator.getInstance().getHistory().save();
|
||||||
|
}
|
||||||
|
getAdapter().notifyDataSetChanged();
|
||||||
|
Toast.makeText(context, context.getText(R.string.c_history_saved), Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.setView(editView);
|
||||||
|
|
||||||
|
builder.create().show();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldHaveCopyResult(@Nonnull HistoryState state) {
|
||||||
|
return !state.getDisplayState().isValid() || !Strings.isEmpty(state.getDisplayState().getEditorState().getText());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
PreferenceManager.getDefaultSharedPreferences(getActivity()).unregisterOnSharedPreferenceChangeListener(preferencesListener);
|
PreferenceManager.getDefaultSharedPreferences(getActivity()).unregisterOnSharedPreferenceChangeListener(preferencesListener);
|
||||||
@ -353,14 +439,6 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
**********************************************************************
|
|
||||||
*
|
|
||||||
* MENU
|
|
||||||
*
|
|
||||||
**********************************************************************
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
this.menu.onCreateOptionsMenu(this.getActivity(), menu);
|
this.menu.onCreateOptionsMenu(this.getActivity(), menu);
|
||||||
@ -425,14 +503,6 @@ public abstract class BaseHistoryFragment extends ListFragment implements Calcul
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
**********************************************************************
|
|
||||||
*
|
|
||||||
* STATIC/INNER
|
|
||||||
*
|
|
||||||
**********************************************************************
|
|
||||||
*/
|
|
||||||
|
|
||||||
private final class HistoryOnPreferenceChangeListener implements SharedPreferences.OnSharedPreferenceChangeListener {
|
private final class HistoryOnPreferenceChangeListener implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -23,20 +23,15 @@
|
|||||||
package org.solovyev.android.calculator.history;
|
package org.solovyev.android.calculator.history;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
import org.solovyev.android.calculator.BaseActivity;
|
import org.solovyev.android.calculator.BaseActivity;
|
||||||
import org.solovyev.android.calculator.CalculatorEventData;
|
|
||||||
import org.solovyev.android.calculator.CalculatorEventListener;
|
|
||||||
import org.solovyev.android.calculator.CalculatorEventType;
|
|
||||||
import org.solovyev.android.calculator.R;
|
import org.solovyev.android.calculator.R;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static org.solovyev.android.calculator.CalculatorFragmentType.history;
|
import static org.solovyev.android.calculator.CalculatorFragmentType.history;
|
||||||
import static org.solovyev.android.calculator.CalculatorFragmentType.saved_history;
|
import static org.solovyev.android.calculator.CalculatorFragmentType.saved_history;
|
||||||
|
|
||||||
public class CalculatorHistoryActivity extends BaseActivity implements CalculatorEventListener {
|
public class CalculatorHistoryActivity extends BaseActivity {
|
||||||
|
|
||||||
public CalculatorHistoryActivity() {
|
public CalculatorHistoryActivity() {
|
||||||
super(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName());
|
super(R.layout.main_empty, CalculatorHistoryActivity.class.getSimpleName());
|
||||||
@ -49,11 +44,4 @@ public class CalculatorHistoryActivity extends BaseActivity implements Calculato
|
|||||||
ui.addTab(this, history, null, R.id.main_layout);
|
ui.addTab(this, history, null, R.id.main_layout);
|
||||||
ui.addTab(this, saved_history, null, R.id.main_layout);
|
ui.addTab(this, saved_history, null, R.id.main_layout);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCalculatorEvent(@Nonnull CalculatorEventData calculatorEventData, @Nonnull CalculatorEventType calculatorEventType, @Nullable Object data) {
|
|
||||||
if (calculatorEventType == CalculatorEventType.use_history_state) {
|
|
||||||
this.finish();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,168 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013 serso aka se.solovyev
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*
|
|
||||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
* Contact details
|
|
||||||
*
|
|
||||||
* Email: se.solovyev@gmail.com
|
|
||||||
* Site: http://se.solovyev.org
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.solovyev.android.calculator.history;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.AlertDialog;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.text.ClipboardManager;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.widget.EditText;
|
|
||||||
import android.widget.TextView;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.solovyev.android.calculator.Locator;
|
|
||||||
import org.solovyev.android.calculator.R;
|
|
||||||
import org.solovyev.android.menu.LabeledMenuItem;
|
|
||||||
import org.solovyev.common.text.Strings;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* User: serso
|
|
||||||
* Date: 12/18/11
|
|
||||||
* Time: 3:09 PM
|
|
||||||
*/
|
|
||||||
public enum HistoryItemMenuItem implements LabeledMenuItem<HistoryItemMenuData> {
|
|
||||||
|
|
||||||
use(R.string.c_use) {
|
|
||||||
@Override
|
|
||||||
public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) {
|
|
||||||
BaseHistoryFragment.useHistoryItem(data.getHistoryState());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
copy_expression(R.string.c_copy_expression) {
|
|
||||||
@Override
|
|
||||||
public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) {
|
|
||||||
final HistoryState historyState = data.getHistoryState();
|
|
||||||
final String text = historyState.getEditorState().getText();
|
|
||||||
if (!Strings.isEmpty(text)) {
|
|
||||||
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
|
|
||||||
clipboard.setText(text);
|
|
||||||
Toast.makeText(context, context.getText(R.string.c_expression_copied), Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
copy_result(R.string.c_copy_result) {
|
|
||||||
@Override
|
|
||||||
public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) {
|
|
||||||
final HistoryState historyState = data.getHistoryState();
|
|
||||||
final String text = historyState.getDisplayState().getEditorState().getText();
|
|
||||||
if (!Strings.isEmpty(text)) {
|
|
||||||
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
|
|
||||||
clipboard.setText(text);
|
|
||||||
Toast.makeText(context, context.getText(R.string.c_result_copied), Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
save(R.string.c_save) {
|
|
||||||
@Override
|
|
||||||
public void onClick(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context) {
|
|
||||||
final HistoryState historyState = data.getHistoryState();
|
|
||||||
if (!historyState.isSaved()) {
|
|
||||||
createEditHistoryDialog(data, context, true);
|
|
||||||
} else {
|
|
||||||
Toast.makeText(context, context.getText(R.string.c_history_already_saved), Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
edit(R.string.c_edit) {
|
|
||||||
@Override
|
|
||||||
public void onClick(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context) {
|
|
||||||
final HistoryState historyState = data.getHistoryState();
|
|
||||||
if (historyState.isSaved()) {
|
|
||||||
createEditHistoryDialog(data, context, false);
|
|
||||||
} else {
|
|
||||||
Toast.makeText(context, context.getText(R.string.c_history_must_be_saved), Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
remove(R.string.c_remove) {
|
|
||||||
@Override
|
|
||||||
public void onClick(@Nonnull HistoryItemMenuData data, @Nonnull Context context) {
|
|
||||||
final HistoryState historyState = data.getHistoryState();
|
|
||||||
if (historyState.isSaved()) {
|
|
||||||
data.getAdapter().remove(historyState);
|
|
||||||
Locator.getInstance().getHistory().removeSavedHistory(historyState);
|
|
||||||
Toast.makeText(context, context.getText(R.string.c_history_was_removed), Toast.LENGTH_LONG).show();
|
|
||||||
data.getAdapter().notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final int captionId;
|
|
||||||
|
|
||||||
private HistoryItemMenuItem(int captionId) {
|
|
||||||
this.captionId = captionId;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void createEditHistoryDialog(@Nonnull final HistoryItemMenuData data, @Nonnull final Context context, final boolean save) {
|
|
||||||
final HistoryState historyState = data.getHistoryState();
|
|
||||||
|
|
||||||
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
||||||
final View editView = layoutInflater.inflate(R.layout.history_edit, null);
|
|
||||||
final TextView historyExpression = (TextView) editView.findViewById(R.id.history_edit_expression);
|
|
||||||
historyExpression.setText(BaseHistoryFragment.getHistoryText(historyState));
|
|
||||||
|
|
||||||
final EditText comment = (EditText) editView.findViewById(R.id.history_edit_comment);
|
|
||||||
comment.setText(historyState.getComment());
|
|
||||||
|
|
||||||
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
|
|
||||||
.setTitle(save ? R.string.c_save_history : R.string.c_edit_history)
|
|
||||||
.setCancelable(true)
|
|
||||||
.setNegativeButton(R.string.c_cancel, null)
|
|
||||||
.setPositiveButton(R.string.c_save, new DialogInterface.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
if (save) {
|
|
||||||
final HistoryState savedHistoryItem = Locator.getInstance().getHistory().addSavedState(historyState);
|
|
||||||
savedHistoryItem.setComment(comment.getText().toString());
|
|
||||||
Locator.getInstance().getHistory().save();
|
|
||||||
// we don't need to add element to the adapter as adapter of another activity must be updated and not this
|
|
||||||
//data.getAdapter().add(savedHistoryItem);
|
|
||||||
} else {
|
|
||||||
historyState.setComment(comment.getText().toString());
|
|
||||||
Locator.getInstance().getHistory().save();
|
|
||||||
}
|
|
||||||
data.getAdapter().notifyDataSetChanged();
|
|
||||||
Toast.makeText(context, context.getText(R.string.c_history_saved), Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.setView(editView);
|
|
||||||
|
|
||||||
builder.create().show();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
|
||||||
@Override
|
|
||||||
public String getCaption(@Nonnull Context context) {
|
|
||||||
return context.getString(captionId);
|
|
||||||
}
|
|
||||||
}
|
|
@ -41,7 +41,7 @@ public class EditorTest extends AbstractCalculatorTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
this.editor = new Editor(Locator.getInstance().getCalculator(), null);
|
this.editor = new Editor(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user