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

Initial post step version #2000

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,248 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 TweetWallFX
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.tweetwallfx.conference.stepengine.steps;

import java.util.Arrays;
import java.util.Collection;

import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tweetwallfx.controls.WordleSkin;
import org.tweetwallfx.emoji.control.EmojiFlow;
import org.tweetwallfx.stepengine.api.DataProvider;
import org.tweetwallfx.stepengine.api.Step;
import org.tweetwallfx.stepengine.api.StepEngine.MachineContext;
import org.tweetwallfx.stepengine.api.config.AbstractConfig;
import org.tweetwallfx.stepengine.api.config.StepEngineSettings;
import org.tweetwallfx.stepengine.dataproviders.TweetStreamDataProvider;
import org.tweetwallfx.stepengine.dataproviders.TweetUserProfileImageDataProvider;
import org.tweetwallfx.tweet.api.Tweet;

import javafx.animation.FadeTransition;
import javafx.geometry.Insets;
import javafx.scene.CacheHint;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.Duration;

public class ShowPosts implements Step {
private static final Logger LOGGER = LoggerFactory.getLogger(ShowPosts.class);
private final Config config;

private ShowPosts(Config config) {
this.config = config;
}

@Override
public void doStep(MachineContext context) {
final var wordleSkin = (WordleSkin) context.get("WordleSkin");
final var dataProvider = context.getDataProvider(TweetStreamDataProvider.class);

var existingPostsNode = (Pane)wordleSkin.getNode().lookup("#postsNode");
if (null == existingPostsNode) {
LOGGER.debug("Initializing posts node");

var postsNode = new Pane();
postsNode.getStyleClass().add("posts");
postsNode.setId("postsNode");
postsNode.setOpacity(0);

var title = new Label("Latest Posts");

title.setPrefWidth(config.width);
title.getStyleClass().add("title");
title.setPrefHeight(config.titleHeight);
title.setAlignment(Pos.CENTER);

postsNode.getChildren().add(title);

final var fadeIn = new FadeTransition(Duration.millis(500), postsNode);
fadeIn.setFromValue(0);
fadeIn.setToValue(1);
fadeIn.setOnFinished(e -> {
LOGGER.info("Calling proceed from ShowPosts");
context.proceed();
});
postsNode.setLayoutX(config.layoutX);
postsNode.setLayoutY(config.layoutY);
postsNode.setMinWidth(config.width);
postsNode.setMaxWidth(config.width);
postsNode.setPrefWidth(config.width);
postsNode.setCacheHint(CacheHint.SPEED);
postsNode.setCache(true);
int col = 0;
int row = 0;

for (Tweet post : dataProvider.getTweets()) {
Pane postPane = createPostNode(context, post);
double postWidth = (config.width - (config.columns - 1) * config.postHGap) / config.columns;
postPane.setMinWidth(postWidth);
postPane.setMaxWidth(postWidth);
postPane.setPrefWidth(postWidth);
postPane.setMinHeight(config.postHeight);
postPane.setMaxHeight(config.postHeight);
postPane.setPrefHeight(config.postHeight);
postPane.setLayoutX(col * (postWidth + config.postHGap));
postPane.setLayoutY(config.titleHeight + config.postVGap + (config.postHeight + config.postVGap) * row);
postsNode.getChildren().add(postPane);
col++;
if (col == config.columns) {
row++;
col = 0;
}
if (row >= config.rows) {
break;
}
}

Platform.runLater(() -> {
wordleSkin.getPane().getChildren().add(postsNode);
fadeIn.play();
});
}
}

private Pane createPostNode(MachineContext context, Tweet post) {
final var userImageProvider = context.getDataProvider(TweetUserProfileImageDataProvider.class);
final var bpPostPane = new BorderPane();
bpPostPane.getStyleClass().add("postDisplay");
bpPostPane.setCenter(createSinglePostDisplay(post, userImageProvider));
return bpPostPane;
}

private HBox createSinglePostDisplay(final Tweet post,
final TweetUserProfileImageDataProvider userImageProvider) {
var postFlow = new EmojiFlow();
postFlow.setText(post.getDisplayEnhancedText());
postFlow.setEmojiFitWidth(config.postFontSize);
postFlow.setEmojiFitHeight(config.postFontSize);
postFlow.getStyleClass().add("postFlow");
postFlow.setMaxHeight(Double.MAX_VALUE);

var postUser = new EmojiFlow();
postUser.setText(post.getUser().getName());
postUser.setEmojiFitWidth(config.postUserFontSize);
postUser.setEmojiFitHeight(config.postFontSize);
postUser.getStyleClass().add("postUserName");
postUser.setMaxHeight(Double.MAX_VALUE);

VBox imageBox = new VBox(createPostUserImage(userImageProvider, post));
imageBox.setPadding(new Insets(10, 0, 10, 10));

VBox postContentBox = new VBox(postUser, postFlow);
postContentBox.setSpacing(10);
postContentBox.setPadding(new Insets(10, 10, 10, 0));

HBox postBox = new HBox(imageBox, postContentBox);
postBox.setCacheHint(CacheHint.QUALITY);
postBox.setSpacing(10);

return postBox;
}

private Node createPostUserImage(TweetUserProfileImageDataProvider postImageProvider, Tweet post) {
var image = postImageProvider.getImageBig(post.getUser());
var postUserImage = new ImageView(image);
postUserImage.getStyleClass().add("postUserImage");
postUserImage.setPreserveRatio(true);
if (image.getWidth() > image.getHeight()) {
postUserImage.setFitHeight(config.avatarSize);
} else {
postUserImage.setFitWidth(config.avatarSize);
}

// avatar image clipping
if (config.circularAvatar) {
Circle circle = new Circle(config.avatarSize / 2f, config.avatarSize / 2f, config.avatarSize / 2f);
postUserImage.setClip(circle);
} else {
Rectangle clip = new Rectangle(config.avatarSize, config.avatarSize);
clip.setArcWidth(config.avatarArcSize);
clip.setArcHeight(config.avatarArcSize);
postUserImage.setClip(clip);
}
return postUserImage;
}

static String fixup(String source) {
return source.replaceAll("[\n\r]+", "|").replaceAll("[\u200D]","");
}

@Override
public boolean requiresPlatformThread() {
return false;
}

@Override
public java.time.Duration preferredStepDuration(final MachineContext context) {
return java.time.Duration.ofMillis(config.getStepDuration());
}

/**
* Implementation of {@link Step.Factory} as Service implementation creating
* {@link ShowPosts}.
*/
public static final class FactoryImpl implements Step.Factory {
@Override
public Step create(final StepEngineSettings.StepDefinition stepDefinition) {
return new ShowPosts(stepDefinition.getConfig(ShowPosts.Config.class));
}

@Override
public Class<ShowPosts> getStepClass() {
return ShowPosts.class;
}

@Override
public Collection<Class<? extends DataProvider>> getRequiredDataProviders(final StepEngineSettings.StepDefinition stepSettings) {
return Arrays.asList(TweetStreamDataProvider.class, TweetUserProfileImageDataProvider.class);
}
}

public static class Config extends AbstractConfig {
public double layoutX = 0;
public double layoutY = 0;
public double width = 800;
public double titleHeight = 60;
public double postVGap = 10;
public double postHGap = 10;
public double postHeight = 150;
public int postFontSize = 13;
public int postUserFontSize = 13;
public int columns = 1;
public int rows = 1;
public int avatarSize = 64;
public int avatarArcSize = 20;
public boolean circularAvatar = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,8 @@ public void doStep(final MachineContext context) {
int col = 0;
int row = 0;

Iterator<SessionData> iterator = dataProvider.getFilteredSessionData().iterator();
String oldRoom = null;
while (iterator.hasNext()) {
var sessionData = iterator.next();
for (SessionData sessionData : dataProvider.getFilteredSessionData()) {
if (null == oldRoom) {
oldRoom = sessionData.room.getName();
}
Expand Down Expand Up @@ -266,7 +264,7 @@ private Pane createSessionNode(final MachineContext context, final SessionData s
if (config.showTags) {
var tags = new FlowPane();
tags.getStyleClass().add("tags");
sessionData.tags.stream().forEach(tag -> {
sessionData.tags.forEach(tag -> {
var tagLabel = new Label(tag);
tagLabel.getStyleClass().add("tagLabel");
tags.getChildren().add(tagLabel);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.tweetwallfx.conference.stepengine.steps.ShowPosts$FactoryImpl
org.tweetwallfx.conference.stepengine.steps.ShowSchedule$FactoryImpl
org.tweetwallfx.conference.stepengine.steps.ShowTopRated$FactoryImpl
org.tweetwallfx.conference.stepengine.steps.SpeakerImageMosaicStep$FactoryImpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018-2023 TweetWallFX
* Copyright (c) 2018-2024 TweetWallFX
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -53,10 +53,11 @@ public class ImageContentFilterStep implements FilterStep<Tweet> {

private static final Logger LOG = LoggerFactory.getLogger(ImageContentFilterStep.class);
private static final Map<Integer, Function<MediaTweetEntry, String>> MTE_SIZE_TO_URL_FUNCTIONS = Map.of(
0, mte -> mte.getMediaUrl() + ":thumb",
1, mte -> mte.getMediaUrl() + ":small",
2, mte -> mte.getMediaUrl() + ":medium",
3, mte -> mte.getMediaUrl() + ":large");
0, mte -> mte.getMediaUrl(MediaTweetEntry.SizeHint.THUMB),
1, mte -> mte.getMediaUrl(MediaTweetEntry.SizeHint.SMALL),
2, mte -> mte.getMediaUrl(MediaTweetEntry.SizeHint.MEDIUM),
3, mte -> mte.getMediaUrl(MediaTweetEntry.SizeHint.LARGE)
);

private final Config config;
private final ImageContentAnalysis.SafeSearch requiredSafeSearch;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018-2022 TweetWallFX
* Copyright (c) 2018-2024 TweetWallFX
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -48,10 +48,10 @@ public final class PhotoImageCache extends URLContentCacheBase {
static {
final Map<Integer, Function<MediaTweetEntry, String>> tmp = new HashMap<>();

tmp.put(0, mte -> mte.getMediaUrl() + ":thumb");
tmp.put(1, mte -> mte.getMediaUrl() + ":small");
tmp.put(2, mte -> mte.getMediaUrl() + ":medium");
tmp.put(3, mte -> mte.getMediaUrl() + ":large");
tmp.put(0, mte -> mte.getMediaUrl(MediaTweetEntry.SizeHint.THUMB));
tmp.put(1, mte -> mte.getMediaUrl(MediaTweetEntry.SizeHint.SMALL));
tmp.put(2, mte -> mte.getMediaUrl(MediaTweetEntry.SizeHint.MEDIUM));
tmp.put(3, mte -> mte.getMediaUrl(MediaTweetEntry.SizeHint.LARGE));

MTE_SIZE_TO_URL_FUNCTIONS = Collections.unmodifiableMap(tmp);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2019 TweetWallFX
* Copyright (c) 2015-2024 TweetWallFX
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -37,9 +37,10 @@ public interface MediaTweetEntry extends TweetEntry {
/**
* Returns the media URL.
*
* @param sizeHint optional size hint
* @return the media URL
*/
String getMediaUrl();
String getMediaUrl(SizeHint sizeHint);

/**
* Returns size variations of the media.
Expand All @@ -48,6 +49,10 @@ public interface MediaTweetEntry extends TweetEntry {
*/
Map<Integer, Size> getSizes();

enum SizeHint {
THUMB, SMALL, MEDIUM, LARGE
}

interface Size extends java.io.Serializable {

Integer THUMB = 0;
Expand Down
Loading
Loading