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

@ -8,10 +8,11 @@
<org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android" <org.solovyev.android.view.widgets.DirectionDragButton xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator" xmlns:c="http://schemas.android.com/apk/res/org.solovyev.android.calculator"
a:id="@+id/roundBracketsButton" a:text="()" a:id="@+id/roundBracketsButton"
c:textUp="(" c:textUp="("
a:text="()"
c:textDown=")" c:textDown=")"
c:textLeft="(…)"
c:directionTextScale="0.5" c:directionTextScale="0.5"
style="?digitButtonStyle" style="?digitButtonStyle"
a:onClick="digitButtonClickHandler"/> a:onClick="digitButtonClickHandler"/>

View File

@ -22,22 +22,6 @@
a:textColor="@color/button_operator_text_color" a:textColor="@color/button_operator_text_color"
style="@style/history_item"/> style="@style/history_item"/>
<LinearLayout a:orientation="horizontal"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<TextView a:id="@+id/history_item_comment_label"
a:layout_width="wrap_content"
a:layout_height="fill_parent"
a:text="@string/c_history_item_comment"
style="@style/history_item_label"/>
<TextView a:id="@+id/history_item_comment"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
style="@style/history_item"/>
</LinearLayout>
<LinearLayout a:orientation="horizontal" <LinearLayout a:orientation="horizontal"
a:layout_width="fill_parent" a:layout_width="fill_parent"
a:layout_height="fill_parent"> a:layout_height="fill_parent">

View File

@ -26,6 +26,8 @@
<TextView a:id="@+id/history_edit_expression" <TextView a:id="@+id/history_edit_expression"
a:layout_width="fill_parent" a:layout_width="fill_parent"
a:padding="6dp"
a:textStyle="bold"
a:layout_height="wrap_content" a:layout_height="wrap_content"
style="@style/default_text_size"> style="@style/default_text_size">
</TextView> </TextView>
@ -39,6 +41,9 @@
<EditText a:id="@+id/history_edit_comment" <EditText a:id="@+id/history_edit_comment"
a:layout_width="fill_parent" a:layout_width="fill_parent"
a:layout_height="wrap_content" a:layout_height="wrap_content"
a:minLines="4"
a:maxLines="4"
a:gravity="top|left"
style="@style/default_text_size"> style="@style/default_text_size">
</EditText> </EditText>

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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
-->
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<TextView a:id="@+id/history_time"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
style="@style/history_time"/>
<TextView a:id="@+id/history_item"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:textColor="@color/button_operator_text_color"
style="@style/history_item"/>
<LinearLayout a:orientation="horizontal"
a:layout_width="fill_parent"
a:layout_height="fill_parent">
<TextView a:id="@+id/history_item_comment_label"
a:layout_width="wrap_content"
a:layout_height="fill_parent"
a:text="@string/c_history_item_comment"
style="@style/history_item_label"/>
<TextView a:id="@+id/history_item_comment"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
style="@style/history_item"/>
</LinearLayout>
</LinearLayout>

View File

@ -309,7 +309,7 @@ Check the \'Round result\' preference in application settings - it should be tur
<string name="c_copy_expression">Copy expression</string> <string name="c_copy_expression">Copy expression</string>
<string name="c_copy_result">Copy result</string> <string name="c_copy_result">Copy result</string>
<string name="c_history_expression">Value</string> <string name="c_history_expression">Value</string>
<string name="c_history_item_already_saved">Already saved (see entries above)</string> <string name="c_history_item_already_saved">Saved (see \'Saved history\' tab)</string>
<string name="c_history_comment">Comment</string> <string name="c_history_comment">Comment</string>
<string name="c_save_history">Save history</string> <string name="c_save_history">Save history</string>
<string name="c_edit_history">Modify history</string> <string name="c_edit_history">Modify history</string>

View File

@ -121,28 +121,30 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
final DragButton equalsButton = (DragButton) findViewById(R.id.equalsButton); final DragButton equalsButton = (DragButton) findViewById(R.id.equalsButton);
if (equalsButton != null) { if (equalsButton != null) {
final OnDragListener evalOnDragListener = new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(calculatorModel), dragPreferences), vibrator, preferences); equalsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new EvalDragProcessor(calculatorModel), dragPreferences), vibrator, preferences));
equalsButton.setOnDragListener(evalOnDragListener);
} }
final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) findViewById(R.id.sixDigitButton); final AngleUnitsButton angleUnitsButton = (AngleUnitsButton) findViewById(R.id.sixDigitButton);
if (angleUnitsButton != null) { if (angleUnitsButton != null) {
final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(), dragPreferences), vibrator, preferences); angleUnitsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new AngleUnitsChanger(), dragPreferences), vibrator, preferences));
angleUnitsButton.setOnDragListener(onDragListener);
} }
final NumeralBasesButton numeralBasesButton = (NumeralBasesButton) findViewById(R.id.clearButton); final NumeralBasesButton numeralBasesButton = (NumeralBasesButton) findViewById(R.id.clearButton);
if (numeralBasesButton != null) { if (numeralBasesButton != null) {
final OnDragListener onDragListener = new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(), dragPreferences), vibrator, preferences); numeralBasesButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new NumeralBasesChanger(), dragPreferences), vibrator, preferences));
numeralBasesButton.setOnDragListener(onDragListener);
} }
final DragButton varsButton = (DragButton) findViewById(R.id.varsButton); final DragButton varsButton = (DragButton) findViewById(R.id.varsButton);
if (varsButton != null) { if (varsButton != null) {
final OnDragListener varsOnDragListener = new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(), dragPreferences), vibrator, preferences); varsButton.setOnDragListener(new OnDragListenerVibrator(newOnDragListener(new VarsDragProcessor(), dragPreferences), vibrator, preferences));
varsButton.setOnDragListener(varsOnDragListener);
} }
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); CalculatorEngine.instance.reset(this, preferences);
initMultiplicationButton(); initMultiplicationButton();
@ -589,4 +591,28 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
((Button) multiplicationButton).setText(CalculatorEngine.instance.getMultiplicationSign()); ((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 @NotNull
private HistoryArrayAdapter adapter; private ArrayAdapter<CalculatorHistoryState> adapter;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -63,7 +63,7 @@ public abstract class AbstractHistoryActivity extends ListActivity {
this.finish(); 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); setListAdapter(adapter);
final ListView lv = getListView(); final ListView lv = getListView();
@ -122,6 +122,8 @@ public abstract class AbstractHistoryActivity extends ListActivity {
}); });
} }
protected abstract int getLayoutId();
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
@ -241,18 +243,10 @@ public abstract class AbstractHistoryActivity extends ListActivity {
return result; return result;
} }
private void clearHistory() { protected abstract void clearHistory();
final List<CalculatorHistoryState> historyStates = new ArrayList<CalculatorHistoryState>(CalculatorHistory.instance.getStates());
CalculatorHistory.instance.clear();
for (CalculatorHistoryState historyState : historyStates) {
adapter.remove(historyState);
}
if (adapter.getCount() > 0) { @NotNull
adapter.notifyDataSetChanged(); protected ArrayAdapter<CalculatorHistoryState> getAdapter() {
} else { return adapter;
Toast.makeText(this, R.string.c_history_is_empty, Toast.LENGTH_SHORT).show();
this.finish();
}
} }
} }

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); historyState.setSaved(false);
this.savedHistory.remove(historyState); this.savedHistory.remove(historyState);
save(context); save(context);

View File

@ -7,6 +7,7 @@
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -17,9 +18,20 @@ import java.util.List;
* Time: 7:39 PM * Time: 7:39 PM
*/ */
public class HistoryActivityTab extends AbstractHistoryActivity { public class HistoryActivityTab extends AbstractHistoryActivity {
@Override
protected int getLayoutId() {
return R.layout.history;
}
@NotNull @NotNull
@Override @Override
protected List<CalculatorHistoryState> getHistoryItems() { protected List<CalculatorHistoryState> getHistoryItems() {
return new ArrayList<CalculatorHistoryState>(CalculatorHistory.instance.getStates()); 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)); editor.setText(AbstractHistoryActivity.getHistoryText(state));
final TextView commentView = (TextView) result.findViewById(R.id.history_item_comment); final TextView commentView = (TextView) result.findViewById(R.id.history_item_comment);
final String comment = state.getComment(); if (commentView != null) {
if (!StringUtils.isEmpty(comment)) { final String comment = state.getComment();
commentView.setText(comment); if (!StringUtils.isEmpty(comment)) {
} else { commentView.setText(comment);
commentView.setText(""); } else {
commentView.setText("");
}
} }
final TextView status = (TextView) result.findViewById(R.id.history_item_status); final TextView status = (TextView) result.findViewById(R.id.history_item_status);
if (state.isSaved()) { if (status != null) {
status.setText(ResourceCache.instance.getCaption("c_history_item_saved")); if (state.isSaved()) {
} else { status.setText(ResourceCache.instance.getCaption("c_history_item_saved"));
if ( AbstractHistoryActivity.isAlreadySaved(state) ) {
status.setText(ResourceCache.instance.getCaption("c_history_item_already_saved"));
} else { } 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; package org.solovyev.android.calculator.history;
import android.widget.ArrayAdapter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -16,12 +17,12 @@ import org.jetbrains.annotations.NotNull;
public class HistoryItemMenuData { public class HistoryItemMenuData {
@NotNull @NotNull
private final HistoryArrayAdapter adapter; private final ArrayAdapter<CalculatorHistoryState> adapter;
@NotNull @NotNull
private final CalculatorHistoryState historyState; private final CalculatorHistoryState historyState;
public HistoryItemMenuData(@NotNull CalculatorHistoryState historyState, HistoryArrayAdapter adapter) { public HistoryItemMenuData(@NotNull CalculatorHistoryState historyState, ArrayAdapter<CalculatorHistoryState> adapter) {
this.historyState = historyState; this.historyState = historyState;
this.adapter = adapter; this.adapter = adapter;
} }
@ -32,7 +33,7 @@ public class HistoryItemMenuData {
} }
@NotNull @NotNull
public HistoryArrayAdapter getAdapter() { public ArrayAdapter<CalculatorHistoryState> getAdapter() {
return adapter; return adapter;
} }
} }

View File

@ -10,7 +10,6 @@ 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;
import android.preference.PreferenceManager;
import android.text.ClipboardManager; import android.text.ClipboardManager;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -98,7 +97,7 @@ public enum HistoryItemMenuItem implements AMenuItem<HistoryItemMenuData> {
final CalculatorHistoryState historyState = data.getHistoryState(); final CalculatorHistoryState historyState = data.getHistoryState();
if (historyState.isSaved()) { if (historyState.isSaved()) {
data.getAdapter().remove(historyState); 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(); Toast.makeText(context, "History item was removed!", Toast.LENGTH_LONG).show();
data.getAdapter().notifyDataSetChanged(); data.getAdapter().notifyDataSetChanged();
} }

View File

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