All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.evolver.ai.vaadin.component.ChatAvatarItem Maven / Gradle / Ivy

There is a newer version: 1.5.5
Show newest version
package fi.evolver.ai.vaadin.component;

import java.io.Serial;
import java.time.LocalDateTime;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.HasSize;
import com.vaadin.flow.component.avatar.Avatar;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;

import fi.evolver.utils.DateUtils;

public class ChatAvatarItem extends Composite implements HasSize {
	@Serial
	private static final long serialVersionUID = 1L;

	public ChatAvatarItem(LocalDateTime sendTime, String user, Component content) {
		getContent().setAlignItems(FlexComponent.Alignment.START);

		VerticalLayout column = new VerticalLayout(
				generateHeader(sendTime, user),
				content);

		column.setPadding(false);
		column.setSpacing(false);

		getContent().add(column);
		getContent().getStyle().set("line-height", "var(--lumo-line-height-m)");
		getContent().addComponentAsFirst(new Avatar(user));
	}

	public ChatAvatarItem(String user, Component content) {
		this(LocalDateTime.now(), user, content);
	}

	private static Div generateHeader(LocalDateTime sendTime, String user) {
		Div result = new Div();

		Span userPart = new Span(user);
		userPart.getStyle().set("margin-right", "var(--lumo-space-s)");

		Span dateTimePart = new Span(sendTime.format(DateUtils.FORMAT_DATE_TIME_FI));
		dateTimePart.getStyle()
			.set("color", "var(--lumo-secondary-text-color)")
			.set("font-size", "var(--lumo-font-size-s)");

		result.add(userPart, dateTimePart);

		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy