diff --git a/res/layout/expense_editor.xml b/res/layout/expense_editor.xml index 25ce9b0..e3c7b9d 100644 --- a/res/layout/expense_editor.xml +++ b/res/layout/expense_editor.xml @@ -41,10 +41,13 @@ android:layout_height="wrap_content" android:text="@string/expense_payer" /> - + android:layout_height="wrap_content" + android:paddingLeft="12dp" + android:text="@string/expense_payer_prompt" /> = 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(); @@ -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); @@ -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 payerAdapter = new ArrayAdapter( - 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() { @@ -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) { @@ -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); @@ -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(); } @@ -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); @@ -378,6 +407,11 @@ private boolean validate() { valid = false; } + if (expense.getPersonId() < 0) { + payerView.setError(errRequired); + valid = false; + } + if (customSplitCheckBox.isChecked()) { int numEnabled = 0; @@ -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 weights = null; if (customSplitCheckBox.isChecked()) {