/** * Moves the given {@link RBookListItem}s to the start of {@code bookList}. * * @param realm Instance of Realm to use. * @param bookList Book list which owns {@code itemsToMove}. * @param itemsToMove Items to move. Items must already exist in Realm and be owned by {@code * bookList}. */ public static void moveItemsToStart( Realm realm, RBookList bookList, List<RBookListItem> itemsToMove) { bookList.throwIfSmartList(); if (itemsToMove == null || itemsToMove.isEmpty()) return; realm.executeTransaction( tRealm -> { // Get the next first open position. Long nextFirstPos = bookList.listItems.where().min("pos").longValue() - C.LIST_ITEM_GAP; // Loop through itemsToMove backwards and move those items to the start of this list. for (int i = itemsToMove.size() - 1; i >= 0; i--) { itemsToMove.get(i).pos = nextFirstPos; nextFirstPos -= C.LIST_ITEM_GAP; } }); }
/** * Moves the given {@link RBookListItem}s to the end of {@code bookList}. * * @param realm Instance of Realm to use. * @param bookList Book list which owns {@code itemsToMove}. * @param itemsToMove Items to move. Items must already exist in Realm and be owned by {@code * bookList}. */ public static void moveItemsToEnd( Realm realm, RBookList bookList, List<RBookListItem> itemsToMove) { bookList.throwIfSmartList(); if (itemsToMove == null || itemsToMove.isEmpty()) return; realm.executeTransaction( tRealm -> { // Get the next last open position. Long nextLastPos = bookList.nextPos; // Loop through itemsToMove and move those items to the end of this list. for (RBookListItem item : itemsToMove) { item.pos = nextLastPos; nextLastPos += C.LIST_ITEM_GAP; } // Set bookList's nextPos. bookList.nextPos = nextLastPos; }); }