Skip to content

Commit

Permalink
Replace limited spinner widget for selecting the payer.
Browse files Browse the repository at this point in the history
The spinner widget is rather limited because it does not allow to have
no item selected by default (in order to force the user to make an
explicit choice), and it cannot show an error indicator. I'm replacing
it with a textview (using the spinner's style) and an alert dialog.
  • Loading branch information
inguin committed Sep 11, 2012
1 parent 37edac6 commit 15aad28
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 30 deletions.
7 changes: 5 additions & 2 deletions res/layout/expense_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@
android:layout_height="wrap_content"
android:text="@string/expense_payer" />

<Spinner
<TextView
android:id="@+id/expense_payer"
style="@android:style/Widget.DeviceDefault.Light.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:text="@string/expense_payer_prompt" />

<TextView
android:layout_width="wrap_content"
Expand Down
87 changes: 59 additions & 28 deletions src/ivl/android/moneybalance/ExpenseEditorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
Expand All @@ -53,7 +55,6 @@
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
Expand All @@ -79,7 +80,7 @@ private enum Mode { NEW_EXPENSE, EDIT_EXPENSE };

private AutoCompleteTextView titleView;
private EditText amountView;
private Spinner payerView;
private TextView payerView;
private TextView dateView;

private static class CustomSplitEntry {
Expand All @@ -93,8 +94,6 @@ private static class CustomSplitEntry {
private TableLayout customSplitTable;

private CurrencyHelper currencyHelper;
// XXX: private long currencyDivider;
// private final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -103,7 +102,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.expense_editor);
titleView = (AutoCompleteTextView) findViewById(R.id.expense_title);
amountView = (EditText) findViewById(R.id.expense_amount);
payerView = (Spinner) findViewById(R.id.expense_payer);
payerView = (TextView) findViewById(R.id.expense_payer);
dateView = (TextView) findViewById(R.id.expense_date);

customSplitCheckBox = (CheckBox) findViewById(R.id.custom_split);
Expand All @@ -113,18 +112,17 @@ protected void onCreate(Bundle savedInstanceState) {

long expenseId = intent.getLongExtra(PARAM_EXPENSE_ID, -1);
long calculationId = -1;
long personId = -1;

mode = (expenseId >= 0 ? Mode.EDIT_EXPENSE : Mode.NEW_EXPENSE);
if (mode == Mode.EDIT_EXPENSE) {
setTitle(R.string.edit_expense);
expense = expenseDataSource.get(expenseId);
titleView.setText(expense.getTitle());
personId = expense.getPersonId();
} else {
setTitle(R.string.new_expense);
expense = new Expense();
personId = intent.getLongExtra(PARAM_PERSON_ID, -1);
long personId = intent.getLongExtra(PARAM_PERSON_ID, -1);
expense.setPersonId(personId);
long millis = intent.getLongExtra(PARAM_DATE, -1);
if (millis > 0) {
Calendar cal = Calendar.getInstance();
Expand All @@ -133,9 +131,8 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

Person person = null;
if (personId >= 0) {
person = personDataSource.get(personId);
if (expense.getPersonId() >= 0) {
Person person = personDataSource.get(expense.getPersonId());
calculationId = person.getCalculationId();
} else {
calculationId = intent.getLongExtra(PARAM_CALCULATION_ID, -1);
Expand All @@ -160,22 +157,9 @@ protected void onCreate(Bundle savedInstanceState) {

persons = calculation.getPersons();

int selectedPayer = 0;
if (person != null) {
for (Person p : persons) {
if (person.getId() == p.getId()) break;
selectedPayer++;
}
}

ArrayAdapter<Person> payerAdapter = new ArrayAdapter<Person>(
this, android.R.layout.simple_spinner_item, persons);
payerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
payerView.setAdapter(payerAdapter);
payerView.setSelection(selectedPayer);

createCustomSplitRows();
updateCustomSplit();
updatePayer();
updateDate();

customSplitCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
Expand All @@ -187,6 +171,13 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

amountView.addTextChangedListener(updateCustomSplitTextWatcher);

payerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickPayer();
}
});

dateView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -285,6 +276,13 @@ private void updateDate() {
dateView.setText(format.format(expense.getDate().getTime()));
}

private void updatePayer() {
payerView.setText(R.string.expense_payer_prompt);
for (Person p : persons)
if (p.getId() == expense.getPersonId())
payerView.setText(p.getName());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.expense_editor_options, menu);
Expand Down Expand Up @@ -337,6 +335,36 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
fragment.show(getFragmentManager(), "datePicker");
}

private void pickPayer() {
DialogFragment fragment = new DialogFragment() {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
CharSequence[] personsArray = new CharSequence[persons.size()];
int selected = -1;
for (int i = 0; i < persons.size(); i++) {
Person person = persons.get(i);
personsArray[i] = person.getName();
if (person.getId() == expense.getPersonId())
selected = i;
}

AlertDialog.Builder builder = new AlertDialog.Builder(ExpenseEditorActivity.this);
builder.setTitle(R.string.expense_payer_prompt);
builder.setSingleChoiceItems(personsArray, selected, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
Person payer = persons.get(i);
expense.setPersonId(payer.getId());
updatePayer();
dismiss();
}
});
return builder.create();
}
};
fragment.show(getFragmentManager(), "personSelector");
}

private String getExpenseTitle() {
return titleView.getText().toString().trim();
}
Expand All @@ -362,6 +390,7 @@ private boolean validate() {

titleView.setError(null);
amountView.setError(null);
payerView.setError(null);
customSplitCheckBox.setError(null);
for (int i = 0; i < customSplitEntries.length; i++)
customSplitEntries[i].weight.setError(null);
Expand All @@ -378,6 +407,11 @@ private boolean validate() {
valid = false;
}

if (expense.getPersonId() < 0) {
payerView.setError(errRequired);
valid = false;
}

if (customSplitCheckBox.isChecked()) {
int numEnabled = 0;

Expand Down Expand Up @@ -405,11 +439,8 @@ private boolean validate() {
}

private void save() throws ParseException {
Person payer = (Person) payerView.getSelectedItem();

expense.setTitle(getExpenseTitle());
expense.setAmount(getAmount());
expense.setPersonId(payer.getId());

Map<Long, Double> weights = null;
if (customSplitCheckBox.isChecked()) {
Expand Down

0 comments on commit 15aad28

Please sign in to comment.