history changes

This commit is contained in:
Sergey Solovyev
2011-12-19 00:58:42 +04:00
parent dc40632cc0
commit 452f5ebde7
13 changed files with 141 additions and 58 deletions

View File

@@ -121,28 +121,30 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
final DragButton equalsButton = (DragButton) findViewById(R.id.equalsButton);
if (equalsButton != null) {
final OnDragListener evalOnDragListener = new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(calculatorModel), dragPreferences), vibrator, preferences);
equalsButton.setOnDragListener(evalOnDragListener);
equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(calculatorModel), dragPreferences), vibrator, preferences));
}
final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) findViewById(R.id.sixDigitButton);
if (angleUnitsButton != null) {
final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(), dragPreferences), vibrator, preferences);
angleUnitsButton.setOnDragListener(onDragListener);
angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(), dragPreferences), vibrator, preferences));
}
final NumeralBasesButton numeralBasesButton = (NumeralBasesButton) findViewById(R.id.clearButton);
if (numeralBasesButton != null) {
final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(), dragPreferences), vibrator, preferences);
numeralBasesButton.setOnDragListener(onDragListener);
numeralBasesButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(), dragPreferences), vibrator, preferences));
}
final DragButton varsButton = (DragButton) findViewById(R.id.varsButton);
if (varsButton != null) {
final OnDragListener varsOnDragListener = new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(), dragPreferences), vibrator, preferences);
varsButton.setOnDragListener(varsOnDragListener);
varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(), dragPreferences), vibrator, preferences));
}
final DragButton roundBracketsButton = (DragButton) findViewById(R.id.roundBracketsButton);
if ( roundBracketsButton != null ) {
roundBracketsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new RoundBracketsDrgProcessor(), dragPreferences), vibrator, preferences));
}
CalculatorEngine.instance.reset(this, preferences);
initMultiplicationButton();
@@ -589,4 +591,28 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
((Button) multiplicationButton).setText(CalculatorEngine.instance.getMultiplicationSign());
}
}
private static class RoundBracketsDrgProcessor implements SimpleOnDragListener.DragProcessor {
@Override
public boolean processDragEvent(@NotNull DragDirection dragDirection, @NotNull DragButton dragButton, @NotNull Point2d startPoint2d, @NotNull MotionEvent motionEvent) {
boolean result = false;
if ( dragDirection == DragDirection.left ) {
CalculatorModel.instance.doTextOperation(new CalculatorModel.TextOperation() {
@Override
public void doOperation(@NotNull EditText editor) {
final int cursorPosition = editor.getSelectionStart();
final StringBuilder text = new StringBuilder("(");
final String oldText = editor.getText().toString();
text.append(oldText.substring(0, cursorPosition));
text.append(")");
text.append(oldText.substring(cursorPosition));
editor.setText(text);
editor.setSelection(cursorPosition + 2);
}
});
result = true;
}
return result;
}
}
}

View File

@@ -50,7 +50,7 @@ public abstract class AbstractHistoryActivity extends ListActivity {
@NotNull
private HistoryArrayAdapter adapter;
private ArrayAdapter<CalculatorHistoryState> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -63,7 +63,7 @@ public abstract class AbstractHistoryActivity extends ListActivity {
this.finish();
}*/
adapter = new HistoryArrayAdapter(this, R.layout.history, R.id.history_item, new ArrayList<CalculatorHistoryState>());
adapter = new HistoryArrayAdapter(this, getLayoutId(), R.id.history_item, new ArrayList<CalculatorHistoryState>());
setListAdapter(adapter);
final ListView lv = getListView();
@@ -122,6 +122,8 @@ public abstract class AbstractHistoryActivity extends ListActivity {
});
}
protected abstract int getLayoutId();
@Override
protected void onResume() {
super.onResume();
@@ -241,18 +243,10 @@ public abstract class AbstractHistoryActivity extends ListActivity {
return result;
}
private void clearHistory() {
final List<CalculatorHistoryState> historyStates = new ArrayList<CalculatorHistoryState>(CalculatorHistory.instance.getStates());
CalculatorHistory.instance.clear();
for (CalculatorHistoryState historyState : historyStates) {
adapter.remove(historyState);
}
protected abstract void clearHistory();
if (adapter.getCount() > 0) {
adapter.notifyDataSetChanged();
} else {
Toast.makeText(this, R.string.c_history_is_empty, Toast.LENGTH_SHORT).show();
this.finish();
}
@NotNull
protected ArrayAdapter<CalculatorHistoryState> getAdapter() {
return adapter;
}
}

View File

@@ -136,8 +136,12 @@ public enum CalculatorHistory implements HistoryHelper<CalculatorHistoryState> {
}
}
public void clearSavedHistory(@NotNull Context context) {
this.savedHistory.clear();
save(context);
}
public void removeSavedHistory(@NotNull CalculatorHistoryState historyState, @NotNull Context context, @NotNull SharedPreferences preferences) {
public void removeSavedHistory(@NotNull CalculatorHistoryState historyState, @NotNull Context context) {
historyState.setSaved(false);
this.savedHistory.remove(historyState);
save(context);

View File

@@ -7,6 +7,7 @@
package org.solovyev.android.calculator.history;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.R;
import java.util.ArrayList;
import java.util.List;
@@ -17,9 +18,20 @@ import java.util.List;
* Time: 7:39 PM
*/
public class HistoryActivityTab extends AbstractHistoryActivity {
@Override
protected int getLayoutId() {
return R.layout.history;
}
@NotNull
@Override
protected List<CalculatorHistoryState> getHistoryItems() {
return new ArrayList<CalculatorHistoryState>(CalculatorHistory.instance.getStates());
}
@Override
protected void clearHistory() {
CalculatorHistory.instance.clear();
getAdapter().clear();
}
}

View File

@@ -44,21 +44,25 @@ public class HistoryArrayAdapter extends ArrayAdapter<CalculatorHistoryState> {
editor.setText(AbstractHistoryActivity.getHistoryText(state));
final TextView commentView = (TextView) result.findViewById(R.id.history_item_comment);
final String comment = state.getComment();
if (!StringUtils.isEmpty(comment)) {
commentView.setText(comment);
} else {
commentView.setText("");
if (commentView != null) {
final String comment = state.getComment();
if (!StringUtils.isEmpty(comment)) {
commentView.setText(comment);
} else {
commentView.setText("");
}
}
final TextView status = (TextView) result.findViewById(R.id.history_item_status);
if (state.isSaved()) {
status.setText(ResourceCache.instance.getCaption("c_history_item_saved"));
} else {
if ( AbstractHistoryActivity.isAlreadySaved(state) ) {
status.setText(ResourceCache.instance.getCaption("c_history_item_already_saved"));
if (status != null) {
if (state.isSaved()) {
status.setText(ResourceCache.instance.getCaption("c_history_item_saved"));
} else {
status.setText(ResourceCache.instance.getCaption("c_history_item_not_saved"));
if ( AbstractHistoryActivity.isAlreadySaved(state) ) {
status.setText(ResourceCache.instance.getCaption("c_history_item_already_saved"));
} else {
status.setText(ResourceCache.instance.getCaption("c_history_item_not_saved"));
}
}
}

View File

@@ -6,6 +6,7 @@
package org.solovyev.android.calculator.history;
import android.widget.ArrayAdapter;
import org.jetbrains.annotations.NotNull;
/**
@@ -16,12 +17,12 @@ import org.jetbrains.annotations.NotNull;
public class HistoryItemMenuData {
@NotNull
private final HistoryArrayAdapter adapter;
private final ArrayAdapter<CalculatorHistoryState> adapter;
@NotNull
private final CalculatorHistoryState historyState;
public HistoryItemMenuData(@NotNull CalculatorHistoryState historyState, HistoryArrayAdapter adapter) {
public HistoryItemMenuData(@NotNull CalculatorHistoryState historyState, ArrayAdapter<CalculatorHistoryState> adapter) {
this.historyState = historyState;
this.adapter = adapter;
}
@@ -32,7 +33,7 @@ public class HistoryItemMenuData {
}
@NotNull
public HistoryArrayAdapter getAdapter() {
public ArrayAdapter<CalculatorHistoryState> getAdapter() {
return adapter;
}
}

View File

@@ -10,7 +10,6 @@ import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.PreferenceManager;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.LayoutInflater;
@@ -98,7 +97,7 @@ public enum HistoryItemMenuItem implements AMenuItem<HistoryItemMenuData> {
final CalculatorHistoryState historyState = data.getHistoryState();
if (historyState.isSaved()) {
data.getAdapter().remove(historyState);
CalculatorHistory.instance.removeSavedHistory(historyState, context, PreferenceManager.getDefaultSharedPreferences(context));
CalculatorHistory.instance.removeSavedHistory(historyState, context);
Toast.makeText(context, "History item was removed!", Toast.LENGTH_LONG).show();
data.getAdapter().notifyDataSetChanged();
}

View File

@@ -7,6 +7,7 @@
package org.solovyev.android.calculator.history;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.R;
import java.util.ArrayList;
import java.util.List;
@@ -17,9 +18,20 @@ import java.util.List;
* Time: 7:40 PM
*/
public class SavedHistoryActivityTab extends AbstractHistoryActivity {
@Override
protected int getLayoutId() {
return R.layout.saved_history;
}
@NotNull
@Override
protected List<CalculatorHistoryState> getHistoryItems() {
return new ArrayList<CalculatorHistoryState>(CalculatorHistory.instance.getSavedHistory());
}
@Override
protected void clearHistory() {
CalculatorHistory.instance.clearSavedHistory(this);
getAdapter().clear();
}
}