DbTradeAlert for Android: Add Security and Watchlist Management – Part 4

First post in this series: Introduction to DbTradeAlert

Previous post: DbTradeAlert for Android: Add Security and Watchlist Management – Part 3


2.4 Add Save Functionality for New Securities

Saving new securities is implemented like its watchlist counterpart with two additions:

  • Make shure date and float values were entered correctly
  • Stop the user from entering a duplicate symbol
public class SecurityEditActivity extends AppCompatActivity {
    // ...

    private Date getDateFromEditText(Integer editTextId) {
        Date result = null;
        EditText editText = (EditText) findViewById(editTextId);
        if (editText.length() > 0) {
            DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
            String text = editText.getText().toString();
            try {
                result = dateFormat.parse(text);
            } catch (ParseException e) {
                Log.e(CLASS_NAME, Utils.EXCEPTION_CAUGHT, e);
                Toast.makeText(
                        this, "Error: '" + text + "' is not a valid date", Toast.LENGTH_SHORT)
                        .show();
            }
        }
        return result;
    } // getDateFromEditText()

    // ...

    public void onOkButtonClick(View view) {
        Float basePrice = getFloatFromEditText(R.id.basePriceEditText);
        Date basePriceDate = getDateFromEditText(R.id.basePriceDateEditText);
        Float lowerTarget = getFloatFromEditText(R.id.lowerTargetEditText);
        Float maxPrice = getFloatFromEditText(R.id.maxPriceEditText);
        Date maxPriceDate = getDateFromEditText(R.id.maxPriceDateEditText);
        String notes = Utils.getStringFromEditText(this, R.id.notesEditText);
        String symbol = Utils.getStringFromEditText(this, R.id.symbolEditText);
        Float upperTarget = getFloatFromEditText(R.id.upperTargetEditText);
        Float trailingTarget = getFloatFromEditText(R.id.trailingTargetEditText);
        long[] watchlistIds = Utils.getSelectedListViewItemIds(this, R.id.watchlistsListView);
        String errorMessage = this.dbHelper.updateOrCreateSecurity(basePrice, basePriceDate,
                lowerTarget, maxPrice, maxPriceDate, notes, this.securityId,
                symbol, trailingTarget, upperTarget, watchlistIds);
        if (TextUtils.isEmpty(errorMessage)) {
            setResult(RESULT_OK, getIntent());
            finish();
        } else {
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
    } // onOkButtonClick()

    // ...
}

The SQL is similar to that saving watchlists with the only twist of avoiding duplicate symbols. DbHelper.updateOrCreateSecurity() first checks whether securityId equals NEW_ITEM_ID. If that’s the case it looks for an existing security with the same symbol. If it finds one it aborts the transaction and returns an error message.

SecurityEditActivity.onOkButtonClick() checks whether DbHelper.updateOrCreateSecurity() returned an non-empty string. If that’s the case it shows a Toast with that string and stays on screen.

Finally SecurityManagementActivity needs to be extended:

  • onActivityResult() calls refreshSecuritiesListView() if resultCode was RESULT_OK
  • refreshSecuritiesListView() does exactly what its name says

Try the new functionality:

  1. Open the Manage Securities screen
  2. Tap New to show the Edit Security screen – it says “Add Security”
  3. Enter a symbol that’s already in use and tap OK – you’ll get a message that you cannot add another security with that symbol and the Edit Security screen stays
  4. Enter a new symbol and select one or more watchlists
  5. Tap OK – the Edit Security screen closes and the Manage Securities screen shows the new security – without its name because no quotes were downloaded yet
  6. Also tap OK in the Manage Securities screen to close it
  7. Navigate to one of the watchlists that include the new security – there is no report for it because no quotes were downloaded yet
  8. Tap refresh – DbTradeAlert shows a complete report for the new security
  9. Optional: commit changes

2.5 Add Edit Functionality for Existing Securities

Editing a security - symbol read-only

Editing a security – symbol read-only

Editing securities is implemented like editing watchlists and I’ll skip repeating the explanation. One difference is that SecurityEditActivity.onCreate() calls symbolEditText.setEnabled(false) for existing securities. And DbHelper.readSecurity() of course has to join the Quote table for its Name field.

When the user taps OK the security and its connection to watchlists are saved like for a new security. If the user entered a duplicate symbol the error message from DbHelper is shown and the screen stays open.

And finally WatchlistsManagementActivity.onActivityResult() receives a resultCode of RESULT_OK and refreshes is list of securities if this was an addition – as it shows only the symbols and these are immutable there is no need for a refresh after updating a security.

Give it a try:

  1. Open the Manage Securities screen
  2. Tap Edit on one of the securities to show the Edit Security screen – it says “Edit Security”
  3. Change one of the fields or the watchlists that will include the security
  4. Tap OK
  5. Also tap OK in the Manage Securities screen
  6. Check if the changes were applied correctly
  7. Optional: commit changes

2.6 Add Delete Functionality for Securities

The functionality to delete securities uses the same pattern as for editing them – for explanations see deleting watchlists.

Deleting the data is straightforward – DbHelper.deleteSecurity() first deletes the security’s quote, then the records connecting the security to any watchlists and after that deletes the security itself. Of course it wraps everything in a transaction. The log entries look like this if the security was in a single watchlist:
… V/DbHelper: deleteSecurity(): securityId = 6
… V/DbHelper: deleteSecurity(): result of db.delete() from quote = 1
… V/DbHelper: deleteSecurity(): result of db.delete() from securities_in_watchlists = 1
… V/DbHelper: deleteSecurity(): result of db.delete() from security = 1
… D/DbHelper: deleteSecurity(): success!

Deleting a security

Deleting a security

To try it:

  1. Open the Manage Securities screen
  2. Tap Delete on one of the securities
  3. Tap Ok in the confirmation dialog – note that it displays the security’s name to avoid any mishaps
  4. Also tap OK in the Manage Securities screen
  5. Check if the security is gone
  6. Optional: commit changes

Next post: Add Reminders

Advertisements
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s