Skip to content

Commit

Permalink
refactor(core): change book navigation on #getPage
Browse files Browse the repository at this point in the history
BREAKING CHANGE: #build now is used to fill sendables with their actionable rows and #setActionableRows is now #addActionableRows

Signed-off-by: Jasper Lutz Severino <jasperlutzseverino@gmail.com>
  • Loading branch information
lutzseverino committed Aug 8, 2022
1 parent cb645e0 commit b694ca3
Showing 1 changed file with 113 additions and 80 deletions.
193 changes: 113 additions & 80 deletions core/src/main/java/com/lutzseverino/discordbooks/book/Book.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
package com.lutzseverino.discordbooks.book;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.lutzseverino.discordbooks.discord.component.ActionableRow;
import com.lutzseverino.discordbooks.discord.component.Clickable;
import com.lutzseverino.discordbooks.discord.component.impl.ClickableImpl;
import com.lutzseverino.discordbooks.discord.message.Sendable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.UnaryOperator;

@SuppressWarnings("UnusedReturnValue")
public class Book {
private final List<Sendable> pages = new ArrayList<>();
private final Clickable previousClickable = new ClickableImpl(Clickable.Style.PRIMARY).setDisplay("Previous");
private final Clickable nextClickable = new ClickableImpl(Clickable.Style.PRIMARY).setDisplay("Next");
private final List<Clickable> clickables = new ArrayList<>(List.of(this.previousClickable, this.nextClickable));
private final List<ActionableRow> actionableRows = new ArrayList<>(List.of(new ActionableRow(this.clickables)));
private final List<String> owners = new ArrayList<>();
private String name;

public Book(String name) {
this.name = name;
private Clickable previousClickable = new ClickableImpl(Clickable.Style.PRIMARY).setDisplay("Previous");
private Clickable nextClickable = new ClickableImpl(Clickable.Style.PRIMARY).setDisplay("Next");
private final List<Clickable> clickables = new ArrayList<>(List.of(previousClickable, nextClickable));
private final List<ActionableRow> actionableRows = new ArrayList<>(List.of(ActionableRow.of(clickables)));
private String id;

public Book(String id) {
this.id = id;
}

public Book() {
this.id = "";
}

/**
* @return the name of the book
* @return the ID of the book
*/
public String getName() {
return name;
public String getId() {
return id;
}


/**
* @param name the name of the book
* @param id the ID of the book
* @return this book, for chained calls
*/
public void setName(String name) {
this.name = name;
public Book setId(String id) {
this.id = id;
return this;
}

/**
* Calls to set up navigation and returns
* a given page.
*
* @param index the index
* @return the page at the index
* @throws IndexOutOfBoundsException if the index is out of bounds
*/
@NotNull public Sendable getPage(int index) {
setNavigation(index);
return pages.get(index);
}

Expand All @@ -58,37 +67,13 @@ public List<Sendable> getPages() {
return pages;
}

/**
* Uses the provided page as the index, then adds
* one and returns the resulting page.
*
* @param page the index
* @return the next page, null if out of bounds
*/
@Nullable public Sendable getNextPage(Sendable page) {
int index = pages.indexOf(page);

return index == -1 ? null : pages.get(index + 1);
}

/**
* Uses the provided page as the index, then subtracts
* one and returns the resulting page.
*
* @param page the index
* @return the next page, null if out of bounds
*/
@Nullable public Sendable getPreviousPage(Sendable page) {
int index = pages.indexOf(page);

return index == -1 ? null : pages.get(index - 1);
}

/**
* @param pages the pages to add
* @return this book, for chained calls
*/
public void addPages(Sendable @NotNull ... pages) {
public Book addPages(Sendable @NotNull ... pages) {
this.pages.addAll(List.of(pages));
return this;
}

/**
Expand All @@ -98,35 +83,38 @@ public void addPages(Sendable @NotNull ... pages) {
* @see #getClickables()
* @see #getClickableRow()
*/
private List<Clickable> getClickables() {
return this.clickables;
public List<Clickable> getClickables() {
return clickables;
}

/**
* Clears the default list and sets the items of
* the {@link ActionableRow} of {@link Clickable}s.
*
* @param clickables the {@link Clickable}s to set
* @see #setClickables(Clickable...)
* @return this book, for chained calls
* @see #addClickables(Clickable...)
* @see #setNextClickable(UnaryOperator)
* @see #setPreviousClickable(UnaryOperator)
*/
public void setClickables(List<Clickable> clickables) {
public Book addClickables(List<Clickable> clickables) {
this.clickables.clear();
this.clickables.addAll(clickables);
return this;
}

/**
* Sets the items of the {@link ActionableRow} of {@link Clickable}s.
* Clears the default list and sets the items of
* the {@link ActionableRow} of {@link Clickable}s.
*
* @param clickables the {@link Clickable}s to set
* @see #setClickables(Clickable...)
* @return this book, for chained calls
* @see #addClickables(Clickable...)
* @see #setNextClickable(UnaryOperator)
* @see #setPreviousClickable(UnaryOperator)
*/
public void setClickables(Clickable... clickables) {
this.clickables.clear();
this.clickables.addAll(List.of(clickables));
public Book addClickables(Clickable... clickables) {
return addClickables(List.of(clickables));
}

/**
Expand All @@ -144,9 +132,26 @@ public Clickable getNextClickable() {
* at send time.
*
* @param nextClickable the operator to modify the {@link Clickable}
* @return this book, for chained calls
*/
public void setNextClickable(@NotNull UnaryOperator<Clickable> nextClickable) {
public Book setNextClickable(@NotNull UnaryOperator<Clickable> nextClickable) {
nextClickable.apply(this.nextClickable);
return this;
}

/**
* Allows you to set the base {@link Clickable} used to navigate to
* the next page.
* <p>
* Some settings, such as the disabled state or ID will be overridden
* at send time.
*
* @param nextClickable the {@link Clickable}
* @return this book, for chained calls
*/
@JsonSetter private Book setNextClickable(Clickable nextClickable) {
this.nextClickable = nextClickable;
return this;
}

/**
Expand All @@ -164,9 +169,26 @@ public Clickable getPreviousClickable() {
* at send time.
*
* @param previousClickable the operator to modify the {@link Clickable}
* @return this book, for chained calls
*/
public void setPreviousClickable(@NotNull UnaryOperator<Clickable> previousClickable) {
public Book setPreviousClickable(@NotNull UnaryOperator<Clickable> previousClickable) {
previousClickable.apply(this.previousClickable);
return this;
}

/**
* Allows you to set the base {@link Clickable} used to navigate to
* the previous page.
* <p>
* Some settings, such as the disabled state or ID will be overridden
* at send time.
*
* @param previousClickable the {@link Clickable}
* @return this book, for chained calls
*/
@JsonSetter private Book setPreviousClickable(Clickable previousClickable) {
this.previousClickable = previousClickable;
return this;
}

/**
Expand All @@ -175,10 +197,13 @@ public void setPreviousClickable(@NotNull UnaryOperator<Clickable> previousClick
*
* @return the {@link ActionableRow} of {@link Clickable}s
*/
public ActionableRow getClickableRow() {
return new ActionableRow(clickables);
@JsonIgnore public ActionableRow getClickableRow() {
return ActionableRow.of(clickables);
}

/**
* @return the list of {@link ActionableRow}s
*/
public List<ActionableRow> getActionableRows() {
return actionableRows;
}
Expand All @@ -194,10 +219,12 @@ public List<ActionableRow> getActionableRows() {
* library doesn't limit the number of rows by itself.
*
* @param actionRows the {@link ActionableRow}s to set
* @return this book, for chained calls
*/
public void setActionableRows(List<ActionableRow> actionRows) {
this.actionableRows.clear();
this.actionableRows.addAll(actionRows);
public Book addActionableRows(List<ActionableRow> actionRows) {
actionableRows.clear();
actionableRows.addAll(actionRows);
return this;
}


Expand All @@ -212,10 +239,11 @@ public void setActionableRows(List<ActionableRow> actionRows) {
* library doesn't limit the number of rows by itself.
*
* @param actionRows the {@link ActionableRow}s to set
* @return this book, for chained calls
*/
public void setActionableRows(ActionableRow... actionRows) {
this.actionableRows.clear();
this.actionableRows.addAll(List.of(actionRows));

public Book addActionableRows(ActionableRow... actionRows) {
return addActionableRows(List.of(actionRows));
}

/**
Expand All @@ -228,40 +256,45 @@ public List<String> getOwners() {
/**
* @param ids the IDs of the owners of the book
*/
public void setOwners(String... ids) {
this.owners.addAll(List.of(ids));
public Book setOwners(List<String> ids) {
owners.clear();
owners.addAll(ids);
return this;
}

/**
* @param ids the IDs of the owners of the book
*/
public void setOwners(List<String> ids) {
this.owners.addAll(ids);
@JsonSetter public Book setOwners(String... ids) {
return setOwners(List.of(ids));
}

/**
* Builds the first page of the book, modifying
* the stored {@link Clickable}s
* Adds the {@link ActionableRow}s to all
* the pages of the book.
* <p>
* This method should be called after setting
* the book up.
*
* @return the built {@link Sendable}
* @return this book, for chained calls
*/
public Sendable build() {
return build(0);
public Book build() {
for (Sendable page : pages) {
page.addActionableRows(actionableRows);
}
return this;
}

/**
* Builds a page of the book, modifying
* the stored {@link Clickable}s
* Applies the navigation of the book
* for a given index.
*
* @param index the page number to build
* @return the built {@link Sendable}
* @param index the index
*/
public Sendable build(int index) {
Sendable sendable = pages.get(index);

previousClickable.setDisabled(index == 0).setId("book:" + getName() + "@" + (index - 1));
nextClickable.setDisabled(index == pages.size() - 1).setId("book:" + getName() + "@" + (index + 1));
private Book setNavigation(int index) {
previousClickable.setDisabled(index == 0).setId("book:" + id + "@" + (index - 1));
nextClickable.setDisabled(index == pages.size() - 1).setId("book:" + id + "@" + (index + 1));

return sendable.setActionableRows(actionableRows);
return this;
}
}

0 comments on commit b694ca3

Please sign in to comment.