Merge pull request #105 from informatik-ag-ngl/f/user_status_change_processor

Added the UserStatusChangeProcessor and its implementation
This commit is contained in:
delvh 2020-02-07 16:56:33 +01:00 committed by GitHub
commit 52183b6d3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 4 deletions

View File

@ -14,9 +14,7 @@ import envoy.client.data.Config;
import envoy.client.data.LocalDb; import envoy.client.data.LocalDb;
import envoy.client.util.EnvoyLog; import envoy.client.util.EnvoyLog;
import envoy.data.*; import envoy.data.*;
import envoy.event.Event; import envoy.event.*;
import envoy.event.IdGeneratorRequest;
import envoy.event.MessageStatusChangeEvent;
import envoy.util.SerializationUtils; import envoy.util.SerializationUtils;
/** /**
@ -100,6 +98,8 @@ public class Client implements Closeable {
receiver.removeAllProcessors(); receiver.removeAllProcessors();
// Register processors for message and status handling // Register processors for message and status handling
// Process incoming messages
final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor(); final ReceivedMessageProcessor receivedMessageProcessor = new ReceivedMessageProcessor();
receiver.registerProcessor(Message.class, receivedMessageProcessor); receiver.registerProcessor(Message.class, receivedMessageProcessor);
@ -109,6 +109,9 @@ public class Client implements Closeable {
// Process message status changes // Process message status changes
receiver.registerProcessor(MessageStatusChangeEvent.class, new MessageStatusChangeEventProcessor()); receiver.registerProcessor(MessageStatusChangeEvent.class, new MessageStatusChangeEventProcessor());
// Process user status changes
receiver.registerProcessor(UserStatusChangeEvent.class, new UserStatusChangeProcessor(this));
// Process message ID generation // Process message ID generation
receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator); receiver.registerProcessor(IdGenerator.class, localDb::setIdGenerator);
@ -207,4 +210,16 @@ public class Client implements Closeable {
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public boolean isOnline() { return online; } public boolean isOnline() { return online; }
/**
* @return the contacts of this {@link Client}
* @since Envoy v0.3-alpha
*/
public Contacts getContacts() { return contacts; }
/**
* @param contacts the contacts to set
* @since Envoy v0.3-alpha
*/
public void setContacts(Contacts contacts) { this.contacts = contacts; }
} }

View File

@ -0,0 +1,37 @@
package envoy.client.net;
import java.util.function.Consumer;
import java.util.logging.Logger;
import envoy.client.util.EnvoyLog;
import envoy.event.EventBus;
import envoy.event.UserStatusChangeEvent;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>UserStatusChangeProcessor.java</strong><br>
* Created: <strong>2 Feb 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy v0.3-alpha
*/
public class UserStatusChangeProcessor implements Consumer<UserStatusChangeEvent> {
private Client client;
private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class.getSimpleName());
/**
* @param client the {@link Client} who receives an
* {@link UserStatusChangeEvent}
* @since Envoy v0.3-alpha
*/
public UserStatusChangeProcessor(Client client) { this.client = client; }
@Override
public void accept(UserStatusChangeEvent evt) {
logger.info("Received " + evt);
client.getContacts().getContacts().stream().filter((user) -> user.getId() == evt.getId()).findFirst().get().setStatus(evt.get());
EventBus.getInstance().dispatch(evt);
}
}

View File

@ -26,6 +26,7 @@ import envoy.data.MessageBuilder;
import envoy.data.User; import envoy.data.User;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.event.MessageStatusChangeEvent; import envoy.event.MessageStatusChangeEvent;
import envoy.event.UserStatusChangeEvent;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -211,6 +212,9 @@ public class ChatWindow extends JFrame {
// Listen to theme changes // Listen to theme changes
EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme((Theme) evt.get())); EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme((Theme) evt.get()));
// Listen to user status changes
EventBus.getInstance().register(UserStatusChangeEvent.class, (evt) -> { userList.revalidate(); userList.repaint(); });
// Listen to received messages // Listen to received messages
EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> { EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> {
Message message = ((MessageCreationEvent) evt).get(); Message message = ((MessageCreationEvent) evt).get();