@Override
  public void onRuleRemoved(final AutoPlaylist.Rule rule, final int index) {
    editedRules.remove(index - 1);
    adapter.notifyItemRemoved(index);

    Snackbar.make(findViewById(R.id.list), "Removed rule", Snackbar.LENGTH_LONG)
        .setAction(
            R.string.action_undo,
            new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                editedRules.add(index - 1, rule);
                adapter.notifyItemInserted(index);
              }
            })
        .show();
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.add:
        editedRules.add(new AutoPlaylist.Rule(AutoPlaylist.Rule.EMPTY));
        adapter.notifyItemInserted(editedRules.size());
        return true;
      case R.id.discard:
        if (rulesChanged()) {
          AlertDialog dialog =
              new AlertDialog.Builder(this)
                  .setMessage("Discard changes?")
                  .setPositiveButton(
                      "Discard",
                      new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          finish();
                        }
                      })
                  .setNegativeButton(R.string.action_cancel, null)
                  .show();

          Themes.themeAlertDialog(dialog);
        } else {
          finish();
        }
        return true;
      case android.R.id.home:
        if (validateName()) {
          if (!editedReference.isEqual(reference) || rulesChanged()) {
            saveChanges();
          }
        } else {
          return true;
        }
        break;
    }
    return super.onOptionsItemSelected(item);
  }