Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add theme API to MessageListItem #979

Merged
merged 6 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public MessageListPage() {
addButton("setUserImage", () -> foo.setUserImage("/test2.jpg"));
addButton("setAbbreviation", () -> foo.setUserAbbreviation("CD"));
addButton("setUserColorIndex", () -> foo.setUserColorIndex(2));
addButton("addThemeNames", () -> foo.addThemeNames("foo", "bar"));
addButton("removeThemeNames", () -> foo.removeThemeNames("foo", "bar"));

addButton("setItems", () -> messageList
.setItems(new MessageListItem(null, null, "sender3")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ public void updateItemPropertiesAfterRendering_messagesUpdated() {
clickElementWithJs("setUserColorIndex");
Assert.assertEquals("Unexpected userColorIndex prop", 2,
msg.getUserColorIndex());

clickElementWithJs("addThemeNames");
Assert.assertEquals("Unexpected theme prop after adding theme names",
"foo bar", msg.getTheme());

clickElementWithJs("removeThemeNames");
Assert.assertEquals("Unexpected theme prop after removing theme names",
null, msg.getTheme());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
import java.io.Serializable;
import java.net.URI;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand Down Expand Up @@ -58,6 +63,8 @@ public class MessageListItem implements Serializable {
private Command pendingHandle;
private AbstractStreamResource imageResource;

private Set<String> themeNames = new LinkedHashSet<>();

/**
* Creates an empty message list item. Use the setter methods to configure
* what will be displayed in the message.
Expand Down Expand Up @@ -282,6 +289,52 @@ public void setUserColorIndex(Integer userColorIndex) {
propsChanged();
}

/**
* Adds one or more theme names to this message. Multiple theme names can be
* specified by using multiple parameters.
*
* @param themeNames
* the theme name or theme names to be added to the message
*/
public void addThemeNames(String... themeNames) {
sissbruecker marked this conversation as resolved.
Show resolved Hide resolved
this.themeNames.addAll(Arrays.asList(themeNames));
propsChanged();
}

/**
* Removes one or more theme names from this message. Multiple theme names
* can be specified by using multiple parameters.
*
* @param themeNames
* the theme name or theme names to be removed from the message
*/
public void removeThemeNames(String... themeNames) {
this.themeNames.removeAll(Arrays.asList(themeNames));
propsChanged();
}

/**
* Checks if the message has the given theme name.
*
* @param themeName
* the theme name to check for
* @return <code>true</code> if the message has the given theme name,
* <code>false</code> otherwise
*/
public boolean hasThemeName(String themeName) {
return themeNames.contains(themeName);
}

// Used only for Jackson serialization
@JsonGetter
private String getTheme() {
if (themeNames.isEmpty()) {
return null;
} else {
return themeNames.stream().collect(Collectors.joining(" "));
}
}

/**
* Gets the image resource of the message sender's avatar.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.messages.MessageList;
import com.vaadin.flow.component.messages.MessageListItem;
import com.vaadin.flow.internal.JsonUtils;
import com.vaadin.flow.server.StreamResource;

import elemental.json.JsonType;
import elemental.json.JsonValue;

public class MessageListTest {

private MessageList messageList;
Expand Down Expand Up @@ -91,4 +95,51 @@ public void setImageAsUrl_streamResourceBecomesNull() {
item1.setUserImage("foo/bar");
Assert.assertNull(item1.getUserImageResource());
}

@Test
public void addThemeNames_serialize_separatedBySpaces() {
item1.addThemeNames("foo", "bar");
item1.addThemeNames("baz");
Assert.assertEquals("foo bar baz", getSerializedThemeProperty(item1));
}

@Test
public void addThemeNames_serialize_noDuplicates() {
item1.addThemeNames("foo", "foo");
Assert.assertEquals("foo", getSerializedThemeProperty(item1));
}

@Test
public void removeThemeNames_serialize_themeNamesRemoved() {
item1.addThemeNames("foo", "bar", "baz");
item1.removeThemeNames("foo", "bar", "qux");
Assert.assertEquals("baz", getSerializedThemeProperty(item1));
}

@Test
public void clearThemeNames_serialize_nullProperty() {
item1.addThemeNames("foo");
item1.removeThemeNames("foo");
Assert.assertNull(getSerializedThemeProperty(item1));
}

@Test
public void hasThemeName_falseForNonExistingThemeName() {
Assert.assertFalse(item1.hasThemeName("foo"));
}

@Test
public void hasThemeName_trueForExistingThemeName() {
item1.addThemeNames("foo");
Assert.assertTrue(item1.hasThemeName("foo"));
}

private String getSerializedThemeProperty(MessageListItem item) {
JsonValue theme = JsonUtils.beanToJson(item).get("theme");
if (theme.getType() == JsonType.NULL) {
return null;
} else {
return theme.asString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,13 @@ public int getUserColorIndex() {
return getPropertyInteger("userColorIndex");
}

/**
* Gets the {@code theme} attribute of this element.
*
* @return the {@code theme} attribute
*/
public String getTheme() {
return getAttribute("theme");
}

}