history activity

This commit is contained in:
serso
2011-10-15 14:52:05 +04:00
parent 59ddbb7ee0
commit 40d37934c8
16 changed files with 280 additions and 26 deletions

View File

@@ -0,0 +1,27 @@
/*
* 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;
import org.jetbrains.annotations.NotNull;
import java.util.Date;
/**
* User: serso
* Date: 10/15/11
* Time: 1:45 PM
*/
public class AbstractHistoryState {
@NotNull
private final Date time = new Date();
@NotNull
public Date getTime() {
return time;
}
}

View File

@@ -37,6 +37,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
public static final String INSERT_TEXT_INTENT = "org.solovyev.android.calculator.CalculatorActivity.insertText";
public static final String INSERT_TEXT_INTENT_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorActivity.insertText.extraString";
public static final String SET_TEXT_INTENT = "org.solovyev.android.calculator.CalculatorActivity.setText";
public static final String SET_TEXT_INTENT_EXTRA_STRING = "org.solovyev.android.calculator.CalculatorActivity.settText.extraString";
private static final int HVGA_WIDTH_PIXELS = 320;
@NotNull
@@ -46,7 +50,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private CalculatorView calculatorView;
@NotNull
private BroadcastReceiver insertTextReceiver;
private BroadcastReceiver textReceiver;
private volatile boolean initialized;
@@ -106,7 +110,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
calculatorView = new CalculatorView(this, CalculatorModel.instance);
insertTextReceiver = new BroadcastReceiver() {
textReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (INSERT_TEXT_INTENT.equals(intent.getAction())) {
@@ -119,11 +123,22 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
}
});
}
} else if (SET_TEXT_INTENT.equals(intent.getAction())) {
final String s = intent.getStringExtra(SET_TEXT_INTENT_EXTRA_STRING);
if (!StringUtils.isEmpty(s)) {
calculatorView.doTextOperation(new CalculatorView.TextOperation() {
@Override
public void doOperation(@NotNull EditText editor) {
editor.setText(s);
}
});
}
}
}
};
registerReceiver(insertTextReceiver, new IntentFilter(INSERT_TEXT_INTENT));
registerReceiver(textReceiver, new IntentFilter(INSERT_TEXT_INTENT));
registerReceiver(textReceiver, new IntentFilter(SET_TEXT_INTENT));
}
private synchronized void firstTimeInit() {
@@ -140,7 +155,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@Override
protected void onDestroy() {
unregisterReceiver(insertTextReceiver);
unregisterReceiver(textReceiver);
super.onDestroy();
}
@@ -154,6 +169,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
this.calculatorView.evaluate();
}
@SuppressWarnings({"UnusedDeclaration"})
public void historyButtonClickHandler(@NotNull View v) {
this.showHistory();
}
@SuppressWarnings({"UnusedDeclaration"})
public void eraseButtonClickHandler(@NotNull View v) {
calculatorView.doTextOperation(new CalculatorView.TextOperation() {
@@ -267,6 +287,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
showSettings();
result = true;
break;
case R.id.main_menu_item_history:
showHistory();
result = true;
break;
case R.id.main_menu_item_about:
showAbout();
result = true;
@@ -282,6 +306,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
return result;
}
private void showHistory() {
startActivity(new Intent(this, CalculatorHistoryActivity.class));
}
private void showSettings() {
startActivity(new Intent(this, CalculatorPreferencesActivity.class));
}

View File

@@ -42,6 +42,7 @@ public class CalculatorDisplayHistoryState {
this.valid = valid;
}
@NotNull
public EditorHistoryState getEditorHistoryState() {
return editorHistoryState;
}

View File

@@ -12,6 +12,8 @@ import org.solovyev.common.utils.history.HistoryAction;
import org.solovyev.common.utils.history.HistoryHelper;
import org.solovyev.common.utils.history.SimpleHistoryHelper;
import java.util.List;
/**
* User: serso
* Date: 10/9/11
@@ -68,5 +70,9 @@ public enum CalculatorHistory implements HistoryHelper<CalculatorHistoryState> {
historyHelper.addState(currentState);
}
@NotNull
@Override
public List<CalculatorHistoryState> getStates() {
return historyHelper.getStates();
}
}

View File

@@ -0,0 +1,99 @@
/*
* 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;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.jetbrains.annotations.NotNull;
import org.solovyev.common.utils.Filter;
import org.solovyev.common.utils.FilterRule;
import org.solovyev.common.utils.FilterRulesChain;
import org.solovyev.common.utils.StringUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
* User: serso
* Date: 10/15/11
* Time: 1:13 PM
*/
public class CalculatorHistoryActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history_activity);
final List<CalculatorHistoryState> historyList = getHistoryList();
if ( historyList.isEmpty() ) {
Toast.makeText(this, R.string.c_history_is_empty, Toast.LENGTH_SHORT).show();
this.finish();
}
setListAdapter(new HistoryArrayAdapter(this, R.layout.history, R.id.history_item, historyList));
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final Intent intent = new Intent(CalculatorActivity.SET_TEXT_INTENT);
intent.putExtra(CalculatorActivity.SET_TEXT_INTENT_EXTRA_STRING, ((CalculatorHistoryState) parent.getItemAtPosition(position)).getEditorState().getText());
sendOrderedBroadcast(intent, null);
CalculatorHistoryActivity.this.finish();
}
});
}
private static List<CalculatorHistoryState> getHistoryList() {
final List<CalculatorHistoryState> calculatorHistoryStates = new ArrayList<CalculatorHistoryState>(CalculatorHistory.instance.getStates());
final FilterRulesChain<CalculatorHistoryState> filterRulesChain = new FilterRulesChain<CalculatorHistoryState>();
filterRulesChain.addFilterRule(new FilterRule<CalculatorHistoryState>() {
@Override
public boolean isFiltered(CalculatorHistoryState object) {
return object == null || StringUtils.isEmpty(object.getEditorState().getText());
}
});
new Filter<CalculatorHistoryState>(filterRulesChain).filter(calculatorHistoryStates.iterator());
return calculatorHistoryStates;
}
private static class HistoryArrayAdapter extends ArrayAdapter<CalculatorHistoryState> {
private HistoryArrayAdapter(Context context, int resource, int textViewResourceId, @NotNull List<CalculatorHistoryState> historyList) {
super(context, resource, textViewResourceId, historyList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewGroup result = (ViewGroup) super.getView(position, convertView, parent);
final CalculatorHistoryState state = getItem(position);
final TextView time = (TextView) result.findViewById(R.id.history_time);
time.setText(new SimpleDateFormat().format(state.getTime()));
final TextView editor = (TextView) result.findViewById(R.id.history_item);
editor.setText(state.getEditorState().getText() + "=" + state.getDisplayState().getEditorHistoryState().getText());
return result;
}
}
}

View File

@@ -12,7 +12,7 @@ import org.jetbrains.annotations.NotNull;
* Date: 9/11/11
* Time: 12:16 AM
*/
public class CalculatorHistoryState {
public class CalculatorHistoryState extends AbstractHistoryState{
@NotNull
private EditorHistoryState editorState;
@@ -20,7 +20,8 @@ public class CalculatorHistoryState {
@NotNull
private CalculatorDisplayHistoryState displayState;
public CalculatorHistoryState(@NotNull EditorHistoryState editorState, @NotNull CalculatorDisplayHistoryState displayState) {
public CalculatorHistoryState(@NotNull EditorHistoryState editorState,
@NotNull CalculatorDisplayHistoryState displayState) {
this.editorState = editorState;
this.displayState = displayState;
}

View File

@@ -167,7 +167,7 @@ public class ColorButton extends Button {
if (topDr != null) {
canvas.save();
canvas.translate(scrollX + compoundPaddingLeft + (hspace - topDr.getBounds().width()) / 2,
scrollY + getPaddingTop() + vspace / 2);
scrollY + getPaddingTop() + vspace / 2);
topDr.draw(canvas);
canvas.restore();
}