コード例 #1
0
  private void callUpdate(MessageEntry messageEntry) throws StorageException {
    DatabaseHelper instance = DatabaseHelper.getInstance(this);
    final SQLiteDatabase db = instance.getWritableDatabase();
    db.beginTransaction();

    try {
      CommandBusHelper.sendMessage(this, GetMessageCommand.CANCEL_UPDATE);

      // Update the database
      instance.insertOrUpdateMessage(db, messageEntry);

      Long serverId = messageEntry.getServerId();
      Long posted = messageEntry.getPosted();
      String messString = messageEntry.getMessage();

      Command command;
      if (serverId != null && posted != -1l) command = new EditMessageCommand(messString, serverId);
      else if (posted == -1l) command = new DeleteMessageCommand(serverId);
      else command = new PostMessageCommand(messString);

      CommandBusHelper.submitCommandSync(this, command);
      db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
    }
  }
コード例 #2
0
  private void promptForAddEdit(final MessageEntry messageEntry) {
    final boolean isEdit = messageEntry != null;
    String message = messageEntry == null ? "" : messageEntry.getMessage();

    final EditText text = new EditText(ExampleActivity.this);
    text.setText(message);
    String promptText = isEdit ? "Edit" : "Add";
    new AlertDialog.Builder(ExampleActivity.this)
        .setTitle(promptText + " a message")
        .setMessage(promptText + " text:")
        .setView(text)
        .setNeutralButton(
            "Cancel",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
              }
            })
        .setPositiveButton(
            "OK",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(final DialogInterface dialogInterface, int i) {
                String value = text.getText().toString().trim();
                if (StringUtils.isNotEmpty(value)) {
                  new AsyncTask() {
                    @Override
                    protected Object doInBackground(Object... objects) {
                      try {
                        String messString = text.getText().toString();

                        MessageEntry localEntry = messageEntry;
                        if (localEntry == null) {
                          localEntry =
                              new MessageEntry(null, null, System.currentTimeMillis(), messString);
                        }
                        localEntry.setMessage(messString);
                        callUpdate(localEntry);
                      } catch (StorageException e) {
                        throw new RuntimeException(e);
                      }
                      return null;
                    }

                    @Override
                    protected void onPostExecute(Object o) {
                      dialogInterface.dismiss();
                    }
                  }.execute();
                }
              }
            })
        .setNegativeButton(
            "Delete",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialogInterface, int i) {
                new AsyncTask() {
                  @Override
                  protected Object doInBackground(Object... objects) {

                    try {

                      MessageEntry localEntry = messageEntry;

                      localEntry.setPosted(-1l);

                      callUpdate(localEntry);

                    } catch (StorageException e) {
                      e.printStackTrace(); // To change body of catch statement use File |
                      // Settings | File Templates.
                    }
                    return null;
                  }
                }.execute();
              }
            })
        .show();
  }