/**
   * Called when one of the cards is clicked.
   *
   * @param event {@link BookCardClickEvent}.
   */
  @Subscribe
  public void onCardClicked(BookCardClickEvent event) {
    // Get the associated RBook.
    RBook book = books.where().equalTo("relPath", event.getRelPath()).findFirst();

    if (actionMode != null) {
      if (event.getType() == BookCardClickEvent.Type.LONG)
        adapter.extendSelectionTo(event.getAdapterPosition());
      else adapter.toggleSelected(event.getAdapterPosition());
      return;
    }
    // Do something based on the click type.
    switch (event.getType()) {
      case NORMAL:
        // Open the book file.
        ActionHelper.openBookUsingIntent(realm, book, event.getLayoutPosition());
        break;
      case LONG:
        // Start multi-select.
        adapter.toggleSelected(event.getAdapterPosition());
        startActionMode();
        break;
      case QUICK_TAG:
        TaggingActivity.start(this, book);
        break;
    }
  }
 /**
  * When called, update the item at the position carried in the event.
  *
  * @param event {@link UpdatePosEvent}.
  */
 @Subscribe(sticky = true)
 public void onUpdatePosEvent(UpdatePosEvent event) {
   // Remove the sticky event.
   EventBus.getDefault().removeStickyEvent(event);
   // If the event's position is ALL_POSITIONS, indicate the whole dataset changed. Otherwise,
   // update the item
   // at the position in the event.
   if (event.getPosition() == UpdatePosEvent.ALL_POSITIONS) adapter.notifyDataSetChanged();
   else adapter.notifyItemChanged(event.getPosition());
 }
 @Override
 public void onDestroyActionMode(ActionMode mode) {
   adapter.setSelectionMode(false);
   // Set status bar color back to normal.
   getActivity()
       .getWindow()
       .setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark));
   adapter.clearSelections();
   actionMode = null;
   recyclerView.setSwipe(true);
 }
 @Override
 public void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   // Save adapter state if we're in action mode.
   if (actionMode != null) {
     adapter.saveInstanceState(outState);
     outState.putBoolean(C.IS_IN_ACTION_MODE, true);
   }
 }
 @Override
 public void onResume() {
   super.onResume();
   SnackKiosk.startSnacking(this);
   if (moveToTop != -1) {
     adapter.notifyItemMoved(moveToTop, 0);
     moveToTop = -1;
   }
 }
 @Override
 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
   mode.getMenuInflater().inflate(R.menu.recent_action_mode, menu);
   recyclerView.setSwipe(false);
   adapter.setSelectionMode(true);
   // Change status bar color to be dark to correspond to dark toolbar color.
   getActivity()
       .getWindow()
       .setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.grey900));
   return true;
 }
  /**
   * Create a {@link RealmRecyclerViewAdapter} based on the current view options and return it.
   *
   * @return New {@link RealmRecyclerViewAdapter}. Will return null if we cannot get the activity
   *     context, if {@link #books} is null or invalid, or if the current value of {@link #cardType}
   *     is not valid.
   */
  private BaseBookCardAdapter makeAdapter() {
    if (books == null || !books.isValid()) return null;

    BaseBookCardAdapter adapter = null;

    // Create a new adapter based on the card type.
    switch (cardType) {
      case NORMAL:
        adapter = new BookCardAdapter(getActivity(), books);
        break;
      case NO_COVER:
        adapter = new BookCardNoCoverAdapter(getActivity(), books);
        break;
      case COMPACT:
        adapter = new BookCardCompactAdapter(getActivity(), books);
        break;
    }

    adapter.setSwipeHandler(this);
    return adapter;
  }
 @Override
 public void onDestroy() {
   super.onDestroy();
   // Close adapter.
   if (adapter != null) adapter.close();
   // Close Realm.
   if (realm != null) {
     realm.close();
     realm = null;
   }
   if (actionMode != null) actionMode.finish();
 }
  @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;
    }
  }
Exemple #10
0
  /**
   * Uses the current view options to change the card layout currently in use. Preserves the
   * position currently scrolled to in the list before switching adapters.
   */
  private void changeCardType() {
    // Store the current last visible item position so that we can scroll back to it after switching
    // adapters.
    int currLastVisPos = recyclerView.getLayoutManager().findLastCompletelyVisibleItemPosition();

    // Swap the adapter
    if (adapter != null) adapter.close();
    adapter = makeAdapter();
    recyclerView.setAdapter(adapter);

    // Scroll back to the same position.
    if (currLastVisPos != RecyclerView.NO_POSITION)
      recyclerView.getRecyclerView().scrollToPosition(currLastVisPos);
  }
Exemple #11
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();
  }
Exemple #12
0
  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // Read prefs to fill in vars.
    readPrefs();

    // Get Realm.
    realm = Realm.getDefaultInstance();

    initUi();

    // If we have a saved instance state, check to see if we were in action mode.
    if (savedInstanceState != null && savedInstanceState.getBoolean(C.IS_IN_ACTION_MODE)) {
      // If we were in action mode, restore the adapter's state and start action mode.
      adapter.restoreInstanceState(savedInstanceState);
      startActionMode();
    }
  }