history changes

This commit is contained in:
Sergey Solovyev 2011-12-18 17:24:00 +04:00
parent df392185f3
commit 09158df194
6 changed files with 39 additions and 7 deletions

View File

@ -309,6 +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_result">Copy result</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_comment">Comment</string>
<string name="c_save_history">Save history</string>
<string name="c_edit_history">Modify history</string>

View File

@ -14,6 +14,7 @@ import android.os.Bundle;
import android.view.*;
import android.widget.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.history.*;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.android.view.AMenu;
@ -99,6 +100,7 @@ public class CalculatorHistoryActivity extends ListActivity {
if (isAlreadySaved(historyState)) {
menuItems.remove(HistoryItemMenuItem.save);
}
menuItems.remove(HistoryItemMenuItem.remove);
menuItems.remove(HistoryItemMenuItem.edit);
}
@ -128,7 +130,15 @@ public class CalculatorHistoryActivity extends ListActivity {
boolean result = false;
try {
historyState.setSaved(true);
if ( CalculatorHistory.instance.getSavedHistory().contains(historyState) ) {
if ( CollectionsUtils.contains(historyState, CalculatorHistory.instance.getSavedHistory(), new Equalizer<CalculatorHistoryState>() {
@Override
public boolean equals(@Nullable CalculatorHistoryState first, @Nullable CalculatorHistoryState second) {
return first != null && second != null &&
first.getTime().getTime() == second.getTime().getTime() &&
first.getDisplayState().equals(second.getDisplayState()) &&
first.getEditorState().equals(second.getEditorState());
}
}) ) {
result = true;
}
} finally {
@ -199,10 +209,14 @@ public class CalculatorHistoryActivity extends ListActivity {
}
final TextView status = (TextView) result.findViewById(R.id.history_item_status);
if (state.isSaved() || isAlreadySaved(state)) {
if (state.isSaved()) {
status.setText(ResourceCache.instance.getCaption("c_history_item_saved"));
} else {
status.setText(ResourceCache.instance.getCaption("c_history_item_not_saved"));
if ( isAlreadySaved(state) ) {
status.setText(ResourceCache.instance.getCaption("c_history_item_already_saved"));
} else {
status.setText(ResourceCache.instance.getCaption("c_history_item_not_saved"));
}
}
return result;

View File

@ -31,6 +31,17 @@ public class AbstractHistoryState implements Cloneable{
@Transient
private boolean saved;
@Transient
private int id = 0;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@NotNull
public Date getTime() {
return time;

View File

@ -29,6 +29,9 @@ public enum CalculatorHistory implements HistoryHelper<CalculatorHistoryState> {
instance;
// todo serso: not synchronized
private int counter = 0;
@NotNull
private final HistoryHelper<CalculatorHistoryState> history = new SimpleHistoryHelper<CalculatorHistoryState>();
@ -97,6 +100,7 @@ public enum CalculatorHistory implements HistoryHelper<CalculatorHistoryState> {
HistoryUtils.fromXml(value, this.savedHistory);
for (CalculatorHistoryState historyState : savedHistory) {
historyState.setSaved(true);
historyState.setId(counter++);
}
}
}
@ -122,6 +126,7 @@ public enum CalculatorHistory implements HistoryHelper<CalculatorHistoryState> {
} else {
final CalculatorHistoryState savedState = historyState.clone();
savedState.setId(counter++);
savedState.setSaved(true);
savedHistory.add(savedState);
@ -133,8 +138,7 @@ public enum CalculatorHistory implements HistoryHelper<CalculatorHistoryState> {
public void removeSavedHistory(@NotNull CalculatorHistoryState historyState, @NotNull Context context, @NotNull SharedPreferences preferences) {
historyState.setSaved(false);
this.savedHistory.remove(historyState);
save(context);
this.savedHistory.clear();
load(context, preferences);
}
}

View File

@ -77,6 +77,7 @@ public class CalculatorHistoryState extends AbstractHistoryState {
CalculatorHistoryState that = (CalculatorHistoryState) o;
if (this.isSaved() != that.isSaved()) return false;
if (this.getId() != that.getId()) return false;
if (!displayState.equals(that.displayState)) return false;
if (!editorState.equals(that.editorState)) return false;
@ -86,6 +87,7 @@ public class CalculatorHistoryState extends AbstractHistoryState {
@Override
public int hashCode() {
int result = Boolean.valueOf(isSaved()).hashCode();
result = 31 * result + getId();
result = 31 * result + editorState.hashCode();
result = 31 * result + displayState.hashCode();
return result;

View File

@ -91,12 +91,12 @@ public enum HistoryItemMenuItem implements AMenuItem<HistoryItemMenuData> {
@Override
public void doAction(@NotNull HistoryItemMenuData data, @NotNull Context context) {
final CalculatorHistoryState historyState = data.getHistoryState();
data.getAdapter().remove(historyState);
if (historyState.isSaved()) {
data.getAdapter().remove(historyState);
CalculatorHistory.instance.removeSavedHistory(historyState, context, PreferenceManager.getDefaultSharedPreferences(context));
Toast.makeText(context, "History item was removed!", Toast.LENGTH_LONG).show();
data.getAdapter().notifyDataSetChanged();
}
data.getAdapter().notifyDataSetChanged();
}
};