Add callback when reply is updated.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 16 Oct 2013 19:37:47 +0000 (21:37 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:29 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Reply.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultPostReply.java

index 03091e3..af81054 100644 (file)
@@ -63,6 +63,7 @@ import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.Reply;
+import net.pterodactylus.sone.data.Reply.Modifier.ReplyUpdated;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
@@ -839,7 +840,7 @@ public class Core extends AbstractService implements SoneProvider {
                                        continue;
                                }
                                if (reply.getTime() < getSoneFollowingTime(sone)) {
-                                       reply.modify().setKnown().update();
+                                       reply.modify().setKnown().update(Optional.<ReplyUpdated<PostReply>>absent());
                                } else if (!reply.isKnown()) {
                                        eventBus.post(new NewPostReplyFoundEvent(reply));
                                }
@@ -1134,7 +1135,7 @@ public class Core extends AbstractService implements SoneProvider {
                }
                database.storePostReplies(sone, replies);
                for (PostReply reply : replies) {
-                       reply.modify().setKnown().update();
+                       reply.modify().setKnown().update(Optional.<ReplyUpdated<PostReply>>absent());
                }
 
                logger.info(String.format("Sone loaded successfully: %s", sone));
@@ -1234,8 +1235,7 @@ public class Core extends AbstractService implements SoneProvider {
         */
        public void markReplyKnown(PostReply reply) {
                boolean previouslyKnown = reply.isKnown();
-               reply.modify().setKnown().update();
-               eventBus.post(new MarkPostReplyKnownEvent(reply));
+               reply.modify().setKnown().update(postReplyUpdated());
                if (!previouslyKnown) {
                        touchConfiguration();
                }
@@ -1852,6 +1852,15 @@ public class Core extends AbstractService implements SoneProvider {
                });
        }
 
+       public Optional<ReplyUpdated<PostReply>> postReplyUpdated() {
+               return Optional.<ReplyUpdated<PostReply>>of(new ReplyUpdated<PostReply>() {
+                       @Override
+                       public void replyUpdated(PostReply reply) {
+                               eventBus.post(new MarkPostReplyKnownEvent(reply));
+                       }
+               });
+       }
+
        public Optional<ImageCreated> imageCreated() {
                return Optional.<ImageCreated>of(new ImageCreated() {
                        @Override
index 2c2a7bd..c660f27 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.data;
 
 import java.util.Comparator;
 
+import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
 
 /**
@@ -96,7 +97,13 @@ public interface Reply<T extends Reply<T>> extends Identified {
        interface Modifier<T> {
 
                Modifier<T> setKnown();
-               T update();
+               T update(Optional<ReplyUpdated<T>> replyUpdated);
+
+               interface ReplyUpdated<T> {
+
+                       void replyUpdated(T reply);
+
+               }
 
        }
 
index 705586b..41e64b0 100644 (file)
@@ -84,10 +84,13 @@ public class DefaultPostReply extends DefaultReply<PostReply> implements PostRep
                        }
 
                        @Override
-                       public PostReply update() {
+                       public PostReply update(Optional<ReplyUpdated<PostReply>> replyUpdated) {
                                if (known) {
                                        database.setPostReplyKnown(DefaultPostReply.this);
                                }
+                               if (replyUpdated.isPresent()) {
+                                       replyUpdated.get().replyUpdated(DefaultPostReply.this);
+                               }
                                return DefaultPostReply.this;
                        }
                };