🎨 Replace QC filter with Kotlin version
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / triggers / NewComicTrigger.java
index 45c290f..d2e0688 100644 (file)
@@ -19,6 +19,8 @@ package net.pterodactylus.rhynodge.triggers;
 
 import static com.google.common.base.Preconditions.*;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import net.pterodactylus.rhynodge.Reaction;
@@ -45,6 +47,9 @@ public class NewComicTrigger implements Trigger {
        /** The new comics. */
        private final List<Comic> newComics = Lists.newArrayList();
 
+       /** The latest comic state. */
+       private ComicState mergedComicState;
+
        @Override
        public State mergeStates(State previousState, State currentState) {
                checkArgument(previousState instanceof ComicState, "previous state must be a comic state");
@@ -54,7 +59,7 @@ public class NewComicTrigger implements Trigger {
                ComicState currentComicState = (ComicState) currentState;
 
                /* copy old state into new state. */
-               ComicState mergedComicState = new ComicState();
+               mergedComicState = new ComicState();
                for (Comic comic : previousComicState) {
                        mergedComicState.add(comic);
                }
@@ -121,16 +126,42 @@ public class NewComicTrigger implements Trigger {
                html.append("<body>");
 
                for (Comic newComic : newComics) {
-                       html.append("<h1>").append(StringEscapeUtils.escapeHtml4(newComic.title())).append("</h1>\n");
-                       for (Strip strip : newComic) {
-                               html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(strip.imageUrl()));
-                               html.append("\" alt=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
-                               html.append("\" title=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
-                               html.append("\"></div>\n");
+                       generateComicHtml(html, newComic);
+               }
+
+               List<Comic> latestComics = new ArrayList<Comic>(mergedComicState.comics());
+               Collections.reverse(latestComics);
+               int comicCount = 0;
+               for (Comic comic : latestComics) {
+                       if (newComics.contains(comic)) {
+                               continue;
+                       }
+                       generateComicHtml(html, comic);
+                       if (++comicCount == 7) {
+                               break;
                        }
                }
 
                return html.append("</body>").toString();
        }
 
+       /**
+        * Generates the HTML for a single comic.
+        *
+        * @param html
+        *              The string builder to append the HTML to
+        * @param comic
+        *              The comic to render
+        */
+       private void generateComicHtml(StringBuilder html, Comic comic) {
+               html.append("<h1>").append(StringEscapeUtils.escapeHtml4(comic.title())).append("</h1>\n");
+               for (Strip strip : comic) {
+                       html.append("<div><img src=\"").append(StringEscapeUtils.escapeHtml4(strip.imageUrl()));
+                       html.append("\" alt=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
+                       html.append("\" title=\"").append(StringEscapeUtils.escapeHtml4(strip.comment()));
+                       html.append("\"></div>\n");
+                       html.append("<div>").append(StringEscapeUtils.escapeHtml4(strip.comment())).append("</div>\n");
+               }
+       }
+
 }