Merge branch 'develop' into f/new_ui
This commit is contained in:
@ -8,6 +8,8 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Random;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@ -33,7 +35,10 @@ import javafx.util.Duration;
|
||||
|
||||
import envoy.client.data.*;
|
||||
import envoy.client.data.audio.AudioRecorder;
|
||||
import envoy.client.data.commands.SystemCommandBuilder;
|
||||
import envoy.client.data.commands.SystemCommandsMap;
|
||||
import envoy.client.event.MessageCreationEvent;
|
||||
import envoy.client.event.SendEvent;
|
||||
import envoy.client.net.Client;
|
||||
import envoy.client.net.WriteProxy;
|
||||
import envoy.client.ui.*;
|
||||
@ -126,6 +131,8 @@ public final class ChatScene implements Restorable {
|
||||
private Attachment pendingAttachment;
|
||||
private boolean postingPermanentlyDisabled;
|
||||
|
||||
private final SystemCommandsMap messageTextAreaCommands = new SystemCommandsMap();
|
||||
|
||||
private static final Settings settings = Settings.getInstance();
|
||||
private static final EventBus eventBus = EventBus.getInstance();
|
||||
private static final Logger logger = EnvoyLog.getLogger(ChatScene.class);
|
||||
@ -221,7 +228,7 @@ public final class ChatScene implements Restorable {
|
||||
switch (e.getOperationType()) {
|
||||
case ADD:
|
||||
if (contact instanceof User) localDB.getUsers().put(contact.getName(), (User) contact);
|
||||
Chat chat = contact instanceof User ? new Chat(contact) : new GroupChat(client.getSender(), contact);
|
||||
final Chat chat = contact instanceof User ? new Chat(contact) : new GroupChat(client.getSender(), contact);
|
||||
Platform.runLater(() -> chatList.getItems().add(chat));
|
||||
break;
|
||||
case REMOVE:
|
||||
@ -231,6 +238,22 @@ public final class ChatScene implements Restorable {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes all {@code SystemCommands} used in {@code ChatScene}.
|
||||
*
|
||||
* @since Envoy Client v0.2-beta
|
||||
*/
|
||||
private void initializeSystemCommandsMap() {
|
||||
final var builder = new SystemCommandBuilder();
|
||||
// Do A Barrel roll initialization
|
||||
final var random = new Random();
|
||||
builder.setAction(text -> doABarrelRoll(Integer.parseInt(text.get(0)), Double.parseDouble(text.get(1))))
|
||||
.setDefaults(Integer.toString(random.nextInt(3) + 1), Double.toString(random.nextDouble() * 3 + 1))
|
||||
.setDescription("See for yourself :)")
|
||||
.setNumberOfArguments(2);
|
||||
messageTextAreaCommands.add("DABR", builder.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes all necessary data via dependency injection-
|
||||
*
|
||||
@ -251,9 +274,12 @@ public final class ChatScene implements Restorable {
|
||||
chatList.setItems(chats);
|
||||
contactLabel.setText(localDB.getUser().getName());
|
||||
MessageControl.setLocalDB(localDB);
|
||||
MessageControl.setSceneContext(sceneContext);
|
||||
|
||||
if (!client.isOnline()) updateInfoLabel("You are offline", "infoLabel-info");
|
||||
|
||||
recorder = new AudioRecorder();
|
||||
initializeSystemCommandsMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -362,7 +388,9 @@ public final class ChatScene implements Restorable {
|
||||
});
|
||||
recorder.start();
|
||||
} else {
|
||||
pendingAttachment = new Attachment(recorder.finish(), AttachmentType.VOICE);
|
||||
pendingAttachment = new Attachment(recorder.finish(), "Voice_recording_"
|
||||
+ DateTimeFormatter.ofPattern("yyyy_MM_dd-HH_mm_ss").format(LocalDateTime.now()) + "." + AudioRecorder.FILE_FORMAT,
|
||||
AttachmentType.VOICE);
|
||||
recording = false;
|
||||
Platform.runLater(() -> {
|
||||
voiceButton.setGraphic(new ImageView(IconUtil.loadIconThemeSensitive("microphone", DEFAULT_ICON_SIZE)));
|
||||
@ -413,7 +441,7 @@ public final class ChatScene implements Restorable {
|
||||
// Create the pending attachment
|
||||
try {
|
||||
final var fileBytes = Files.readAllBytes(file.toPath());
|
||||
pendingAttachment = new Attachment(fileBytes, type);
|
||||
pendingAttachment = new Attachment(fileBytes, file.getName(), type);
|
||||
checkPostConditions(false);
|
||||
// Setting the preview image as image of the attachmentView
|
||||
if (type == AttachmentType.PICTURE)
|
||||
@ -426,20 +454,34 @@ public final class ChatScene implements Restorable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates every element in our application by 360° in at most 2.75s.
|
||||
* Rotates every element in our application by (at most 4 *) 360° in at most
|
||||
* 2.75s.
|
||||
*
|
||||
* @since Envoy Client v0.1-beta
|
||||
*/
|
||||
@FXML
|
||||
private void doABarrelRoll() {
|
||||
final var random = new Random();
|
||||
doABarrelRoll(random.nextInt(3) + 1, random.nextDouble() * 3 + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates every element in our application by {@code rotations}*360° in
|
||||
* {@code an}.
|
||||
*
|
||||
* @param rotations the amount of times the scene is rotated by 360°
|
||||
* @param animationTime the time in seconds that this animation lasts
|
||||
* @since Envoy Client v0.1-beta
|
||||
*/
|
||||
private void doABarrelRoll(int rotations, double animationTime) {
|
||||
// contains all Node objects in ChatScene in alphabetical order
|
||||
final var rotatableNodes = new Node[] { attachmentButton, attachmentView, contactLabel, infoLabel, messageList, messageTextArea,
|
||||
postButton, remainingChars, rotateButton, scene, settingsButton, chatList, voiceButton };
|
||||
final var random = new Random();
|
||||
final var rotatableNodes = new Node[] { attachmentButton, attachmentView, contactLabel, infoLabel, messageList, messageTextArea, postButton,
|
||||
remainingChars, rotateButton, scene, settingsButton, chatList, voiceButton };
|
||||
for (final var node : rotatableNodes) {
|
||||
// Defines at most four whole rotation in at most 4s
|
||||
final var rotateTransition = new RotateTransition(Duration.seconds(random.nextDouble() * 3 + 1), node);
|
||||
rotateTransition.setByAngle((random.nextInt(3) + 1) * 360);
|
||||
// Sets the animation duration to {animationTime}
|
||||
final var rotateTransition = new RotateTransition(Duration.seconds(animationTime), node);
|
||||
// rotates every element {rotations} times
|
||||
rotateTransition.setByAngle(rotations * 360);
|
||||
rotateTransition.play();
|
||||
// This is needed as for some strange reason objects could stop before being
|
||||
// rotated back to 0°
|
||||
@ -459,10 +501,28 @@ public final class ChatScene implements Restorable {
|
||||
private void checkKeyCombination(KeyEvent e) {
|
||||
// Checks whether the text is too long
|
||||
messageTextUpdated();
|
||||
// Sending an IsTyping event if none has been sent for
|
||||
// IsTyping#millisecondsActive
|
||||
if (client.isOnline() && currentChat.getLastWritingEvent() + IsTyping.millisecondsActive <= System.currentTimeMillis()) {
|
||||
eventBus.dispatch(new SendEvent(new IsTyping(getChatID(), currentChat.getRecipient().getID())));
|
||||
currentChat.lastWritingEventWasNow();
|
||||
}
|
||||
// Automatic sending of messages via (ctrl +) enter
|
||||
checkPostConditions(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the id that should be used to send things to the server:
|
||||
* the id of 'our' {@link User} if the recipient of that object is another User,
|
||||
* else the id of the {@link Group} 'our' user is sending to.
|
||||
*
|
||||
* @return an id that can be sent to the server
|
||||
* @since Envoy Client v0.2-beta
|
||||
*/
|
||||
private long getChatID() {
|
||||
return currentChat.getRecipient() instanceof User ? client.getSender().getID() : currentChat.getRecipient().getID();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param e the keys that have been pressed
|
||||
* @since Envoy Client v0.1-beta
|
||||
@ -531,7 +591,7 @@ public final class ChatScene implements Restorable {
|
||||
return;
|
||||
}
|
||||
final var text = messageTextArea.getText().strip();
|
||||
try {
|
||||
if (!messageTextAreaCommands.executeIfAnyPresent(text)) try {
|
||||
// Creating the message and its metadata
|
||||
final var builder = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator())
|
||||
.setText(text);
|
||||
@ -619,10 +679,15 @@ public final class ChatScene implements Restorable {
|
||||
private void copyAndPostMessage() {
|
||||
final var messageText = messageTextArea.getText();
|
||||
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(messageText), null);
|
||||
final var image = attachmentView.getImage();
|
||||
final var messageAttachment = pendingAttachment;
|
||||
postMessage();
|
||||
messageTextArea.setText(messageText);
|
||||
updateRemainingCharsLabel();
|
||||
postButton.setDisable(messageText.isBlank());
|
||||
attachmentView.setImage(image);
|
||||
if (attachmentView.getImage() != null) attachmentView.setVisible(true);
|
||||
pendingAttachment = messageAttachment;
|
||||
}
|
||||
|
||||
@FXML
|
||||
|
Reference in New Issue
Block a user