Back to project page DisposableIncome-OldJava.
The source code is released under:
MIT License
If you think the Android project DisposableIncome-OldJava listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package uk.co.wilka.disposableincome; /* w w w . j a va 2s.c o m*/ import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.text.Editable; import android.text.InputType; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.*; import java.text.SimpleDateFormat; import java.util.*; public class MainActivity extends ListActivity implements AdapterView.OnItemSelectedListener { public static final int PredictedWithdrawalColor = Color.rgb(86, 156, 214); public static final int WithdrawalColor = Color.WHITE; public static final int PredictedWithdrawalInThePastColor = Color.RED; public static final int PredictedWithdrawalAfterThisMonth = Color.GRAY; public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM d"); private static final int NEW_CASH_WITHDRAWAL = 1600; private static final int EDIT_CASH_WITHDRAWAL = 1601; private DatabaseHelper database; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); updateDisplay(); } private void updateDisplay() { IAccessData db = getDatabase(); Cash monthlyBudget = db.getMonthlyBudget(); ArrayList<CashWithdraw> withdraws = db.getWithdrawsStartingThisMonth(); setListAdapter(MakeListAdapter(withdraws)); ((Button)findViewById(R.id.monthlyBudgetButton)).setText(String.format("%d", monthlyBudget.getPounds())); WithdrawalForMonthCalculator withdrawalCalculator = getWithdrawalForThisMonthCalculator(withdraws); WithdrawalForMonthCalculator nextMonthWithdrawalCalculator = getWithdrawalForNextMonthCalculator(withdraws); Cash actualRemaining = monthlyBudget.minus(withdrawalCalculator.getTotalActual()); Cash predicted = monthlyBudget.minus(withdrawalCalculator.getTotalActual().plus(withdrawalCalculator.getTotalPredicted())); Cash endOfNextMonth = monthlyBudget.minus(nextMonthWithdrawalCalculator.getTotalPredicted()); ((TextView)findViewById(R.id.actualRemainingTextView)).setText(String.format("%d", actualRemaining.getPounds())); ((TextView)findViewById(R.id.predictedAtMonthEndTextView)).setText(String.format("%d (%d n/m)", predicted.getPounds(), endOfNextMonth.getPounds())); ((TextView)findViewById(R.id.actualUntilEndOfMonthCountDown)).setText(getEndOfMonthText(actualRemaining)); } private WithdrawalForMonthCalculator getWithdrawalForThisMonthCalculator(ArrayList<CashWithdraw> withdraws) { WithdrawDateComparer dateComparer = createDataComparerForThisMonth(); return new WithdrawalForMonthCalculator(dateComparer, withdraws); } private WithdrawalForMonthCalculator getWithdrawalForNextMonthCalculator(ArrayList<CashWithdraw> withdraws) { WithdrawDateComparer dateComparer = createDataComparerForNextMonth(); return new WithdrawalForMonthCalculator(dateComparer, withdraws); } private WithdrawDateComparer createDataComparerForThisMonth() { return new WithdrawDateComparer(new CalendarProvider()); } private WithdrawDateComparer createDataComparerForNextMonth() { return new WithdrawDateComparer(new PlusOneMonthCalendarProvider(new CalendarProvider())); } private ListAdapter MakeListAdapter(ArrayList<CashWithdraw> withdraws) { ArrayList<CashWithdraw> withdrawsToDisplay = new ArrayList<CashWithdraw>(); WithdrawDateComparer dateComparer = createDataComparerForThisMonth(); for (CashWithdraw withdraw : withdraws) { if(!dateComparer.isBeforeThisMonth(withdraw)) { withdrawsToDisplay.add(withdraw); } } Collections.sort(withdrawsToDisplay, new Comparator<CashWithdraw>() { public int compare(CashWithdraw a, CashWithdraw b) { return a.getDate().compareTo(b.getDate()); } }); return new CashWithdrawArrayAdapter(withdrawsToDisplay); } public String getEndOfMonthText(Cash actualRemaining) { int daysUntilMonthEnd = createDataComparerForThisMonth().getDaysUntilMonthEnd(); int poundsPerDayRemaining = actualRemaining.getPounds() / daysUntilMonthEnd; return String.format("(%d per day)", poundsPerDayRemaining); } class CashWithdrawArrayAdapter extends ArrayAdapter<CashWithdraw> { private ArrayList<CashWithdraw> cashWithdraws; public CashWithdrawArrayAdapter(ArrayList<CashWithdraw> cashWithdraws) { super(MainActivity.this, R.layout.withdraw_row, R.id.date, cashWithdraws); this.cashWithdraws = cashWithdraws; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = super.getView(position, convertView, parent); CashWithdraw withdraw = cashWithdraws.get(position); TextView dateTextView = (TextView)row.findViewById(R.id.date); TextView amountTextView = (TextView) row.findViewById(R.id.amount); TextView notesTextView = (TextView) row.findViewById(R.id.notes); dateTextView.setText(dateTextFor(withdraw)); String predicatedIndicator = withdraw.getWithdrawType() == WithdrawType.Predicted ? "*" : ""; amountTextView.setText(String.format("%s%s", withdraw.getAmount().getPounds(), predicatedIndicator)); notesTextView.setText(String.format("%s", withdraw.getNotes())); int color = getColorForWithdrawal(withdraw); dateTextView.setTextColor(color); amountTextView.setTextColor(color); notesTextView.setTextColor(color); return row; } } private int getColorForWithdrawal(CashWithdraw withdraw) { int color = withdraw.getWithdrawType() == WithdrawType.Actual ? WithdrawalColor : PredictedWithdrawalColor; WithdrawDateComparer dateComparer = createDataComparerForThisMonth(); if(withdraw.getWithdrawType() == WithdrawType.Predicted) { if(dateComparer.isInThePast(withdraw)) { color = PredictedWithdrawalInThePastColor; } else if(dateComparer.isAfterThisMonth(withdraw)) { color = PredictedWithdrawalAfterThisMonth; } } return color; } private IAccessData getDatabase() { if(database == null) { database = new DatabaseHelper(this); } return database; } @Override protected void onPause() { super.onPause(); if(database != null) { database.close(); } database = null; } public void monthlyBudgetButtonClicked(View v) { final IAccessData db = getDatabase(); final EditText input = new EditText(this); input.setInputType(InputType.TYPE_CLASS_NUMBER); input.setText(String.format("%d", db.getMonthlyBudget().getPounds())); new AlertDialog.Builder(this) .setMessage("Monthly budget") .setView(input) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Editable value = input.getText(); try { int newMonthlyBudget = Integer.parseInt(value.toString()); if (newMonthlyBudget > 0) { db.setMonthlyBudget(Cash.fromPounds(newMonthlyBudget)); updateDisplay(); } else { Toast.makeText(MainActivity.this, "Budget must be above zero", Toast.LENGTH_LONG).show(); } } catch (NumberFormatException e) { Toast.makeText(MainActivity.this, "Not a valid number", Toast.LENGTH_LONG).show(); } } }) .setNegativeButton("Cancel", null) .show(); } public void clearClicked(View v) { AlertDialog.Builder ab = new AlertDialog.Builder(this); ab.setMessage("Really clear all items?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { getDatabase().clearAllWithdrawals(); updateDisplay(); } }) .setNegativeButton("No", null) .show(); } public void newWithdrawClicked(View v) { Intent i = new Intent(this, CashWithdrawalDetailsActivity.class); startActivityForResult(i, NEW_CASH_WITHDRAWAL); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == NEW_CASH_WITHDRAWAL) { if (resultCode == RESULT_OK) { CashWithdraw withdraw = (CashWithdraw)data.getParcelableExtra(CashWithdrawalDetailsActivity.CashWithdrawId); getDatabase().addNewWithdrawal(withdraw); } }else if(requestCode == EDIT_CASH_WITHDRAWAL) { if (resultCode == RESULT_OK) { CashWithdraw newWithdraw = (CashWithdraw)data.getParcelableExtra(CashWithdrawalDetailsActivity.CashWithdrawId); CashWithdraw oldWithdraw = (CashWithdraw)data.getParcelableExtra(CashWithdrawalDetailsActivity.CashWithdrawToEditId); getDatabase().updateWithdrawal(oldWithdraw, newWithdraw); } else if(resultCode == CashWithdrawalDetailsActivity.RESULT_DELETE_WITHDRAWAL){ CashWithdraw withdraw = (CashWithdraw)data.getParcelableExtra(CashWithdrawalDetailsActivity.CashWithdrawToEditId); getDatabase().removeWithdrawal(withdraw); } } updateDisplay(); } @Override protected void onListItemClick(ListView listView, View view, int position, long id) { super.onListItemClick(listView, view, position, id); final CashWithdraw withdraw = (CashWithdraw) listView.getItemAtPosition(position); Intent intent = new Intent(MainActivity.this, CashWithdrawalDetailsActivity.class); intent.putExtra(CashWithdrawalDetailsActivity.CashWithdrawToEditId, withdraw); startActivityForResult(intent, EDIT_CASH_WITHDRAWAL); updateDisplay(); } private String dateTextFor(CashWithdraw withdrawal) { return DATE_FORMAT.format(withdrawal.getDate()); } public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { } public void onNothingSelected(AdapterView<?> adapterView) { } }