Пример #1
0
  @Override
  public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    // Handle select all/none first, and if it isn't those then don't do anything if we haven't
    // selected any items.
    if (item.getItemId() == R.id.action_select_all) {
      adapter.selectAll();
      return true;
    } else if (item.getItemId() == R.id.action_select_none) {
      adapter.clearSelections();
      return true;
    } else if (adapter.getSelectedItemCount() == 0) return true;

    // Handle actions.
    switch (item.getItemId()) {
      case R.id.action_tag:
        //noinspection unchecked
        TaggingActivity.start(this, adapter.getSelectedRealmObjects());
        return true;
      case R.id.action_rate:
        int initialRating =
            adapter.getSelectedItemCount() == 1
                ? ((RBook) adapter.getSelectedRealmObjects().get(0)).rating
                : 0;
        Dialogs.ratingDialog(getContext(), initialRating);
        return true;
      case R.id.action_mark_as:
        Dialogs.markAsDialog(getActivity());
        return true;
      case R.id.action_add_to_list:
        Dialogs.addToListDialogOrSnack(getActivity(), realm);
        return true;
      case R.id.action_re_import:
        Dialogs.simpleConfirmDialog(
            getContext(),
            R.string.title_re_import_books,
            R.string.prompt_re_import_books,
            R.string.action_re_import,
            R.id.action_re_import);
        return true;
      case R.id.action_remove:
        Dialogs.simpleConfirmDialog(
            getContext(),
            R.string.title_remove_books,
            R.string.prompt_remove_from_recents,
            R.string.action_remove,
            R.id.action_remove);
        return true;
      case R.id.action_delete:
        Dialogs.confirmCheckBoxDialog(
            getContext(),
            R.string.title_delete_books,
            R.string.prompt_delete_books,
            R.string.prompt_delete_from_device_too,
            R.string.action_delete,
            R.id.action_delete);
        return true;
      default:
        return false;
    }
  }
Пример #2
0
  /**
   * Called when we wish to take some action.
   *
   * @param event {@link ActionEvent}.
   */
  @Subscribe
  public void onActionEvent(ActionEvent event) {
    //noinspection unchecked
    List<RBook> selectedItems = adapter.getSelectedRealmObjects();

    switch (event.getActionId()) {
      case R.id.action_clear:
        realm.executeTransaction(
            tRealm -> {
              // Set isInRecents to false for all RBooks which currently have it set to true.
              RealmResults<RBook> recentBooks =
                  tRealm.where(RBook.class).equalTo("isInRecents", true).findAll();
              for (int i = recentBooks.size() - 1; i >= 0; i--)
                recentBooks.get(i).isInRecents = false;
            });
        break;
      case R.id.action_rate:
        ActionHelper.rateBooks(realm, selectedItems, (Integer) event.getData());
        break;
      case R.id.action_read:
        // Save the position to update so we can refresh the list correctly when resuming.
        this.moveToTop = event.getPosToUpdate();
        break;
      case R.id.action_mark_as:
        int whichMark = (int) event.getData();
        ActionHelper.markBooks(
            realm,
            selectedItems,
            whichMark < 2 ? MarkType.NEW : MarkType.UPDATED,
            whichMark % 2 == 0);
        break;
      case R.id.action_add_to_list:
        ActionHelper.addBooksToList(realm, selectedItems, (String) event.getData());
        break;
      case R.id.action_re_import:
        ActionHelper.reImportBooks(selectedItems);
        break;
      case R.id.action_remove:
        realm.executeTransaction(
            tRealm -> {
              // Set isInRecents to false for all selected RBooks.
              for (RBook book : selectedItems) book.isInRecents = false;
            });
        break;
      case R.id.action_delete:
        ActionHelper.deleteBooks(realm, selectedItems, (boolean) event.getData());
        break;
    }
    if (actionMode != null) actionMode.finish();
  }