Ask user before adding a contact
This commit is contained in:
parent
d03bbb6f5e
commit
bc17203367
@ -49,7 +49,7 @@ public class PersistentLocalDB extends LocalDB {
|
|||||||
|
|
||||||
// Initialize local database directory
|
// Initialize local database directory
|
||||||
if (localDBDir.exists() && !localDBDir.isDirectory())
|
if (localDBDir.exists() && !localDBDir.isDirectory())
|
||||||
throw new IOException(String.format("LocalDbDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
|
throw new IOException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
|
||||||
usersFile = new File(localDBDir, "users.db");
|
usersFile = new File(localDBDir, "users.db");
|
||||||
idGeneratorFile = new File(localDBDir, "id_generator.db");
|
idGeneratorFile = new File(localDBDir, "id_generator.db");
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ public class Client implements Closeable {
|
|||||||
socket = new Socket(config.getServer(), config.getPort());
|
socket = new Socket(config.getServer(), config.getPort());
|
||||||
logger.fine("Successfully established TCP connection to server");
|
logger.fine("Successfully established TCP connection to server");
|
||||||
|
|
||||||
// Create message receiver
|
// Create object receiver
|
||||||
receiver = new Receiver(socket.getInputStream());
|
receiver = new Receiver(socket.getInputStream());
|
||||||
|
|
||||||
// Register user creation processor, contact list processor and message cache
|
// Register user creation processor, contact list processor and message cache
|
||||||
@ -108,7 +108,7 @@ public class Client implements Closeable {
|
|||||||
|
|
||||||
online = true;
|
online = true;
|
||||||
|
|
||||||
// Remove user creation processor
|
// Remove all processors as they are only used during the handshake
|
||||||
receiver.removeAllProcessors();
|
receiver.removeAllProcessors();
|
||||||
|
|
||||||
logger.info("Handshake completed.");
|
logger.info("Handshake completed.");
|
||||||
|
@ -14,6 +14,9 @@ import envoy.util.EnvoyLog;
|
|||||||
import envoy.util.SerializationUtils;
|
import envoy.util.SerializationUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Receives objects from the server and passes them to processor objects based
|
||||||
|
* on their class.
|
||||||
|
* <p>
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
* File: <strong>Receiver.java</strong><br>
|
* File: <strong>Receiver.java</strong><br>
|
||||||
* Created: <strong>30.12.2019</strong><br>
|
* Created: <strong>30.12.2019</strong><br>
|
||||||
@ -32,12 +35,19 @@ public class Receiver extends Thread {
|
|||||||
* Creates an instance of {@link Receiver}.
|
* Creates an instance of {@link Receiver}.
|
||||||
*
|
*
|
||||||
* @param in the {@link InputStream} to parse objects from
|
* @param in the {@link InputStream} to parse objects from
|
||||||
|
* @since Envoy Client v0.3-alpha
|
||||||
*/
|
*/
|
||||||
public Receiver(InputStream in) {
|
public Receiver(InputStream in) {
|
||||||
super("Receiver");
|
super("Receiver");
|
||||||
this.in = in;
|
this.in = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the receiver loop. When an object is read, it is passed to the
|
||||||
|
* appropriate processor.
|
||||||
|
*
|
||||||
|
* @since Envoy Client v0.3-alpha
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
@ -65,7 +75,7 @@ public class Receiver extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
logger.info("Connection probably closed by client. Exiting receiver thread...");
|
// Connection probably closed by client.
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.SEVERE, "Error on receiver thread", e);
|
logger.log(Level.SEVERE, "Error on receiver thread", e);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -78,11 +88,14 @@ public class Receiver extends Thread {
|
|||||||
*
|
*
|
||||||
* @param processorClass the object class accepted by the processor
|
* @param processorClass the object class accepted by the processor
|
||||||
* @param processor the object processor
|
* @param processor the object processor
|
||||||
|
* @since Envoy Client v0.3-alpha
|
||||||
*/
|
*/
|
||||||
public <T> void registerProcessor(Class<T> processorClass, Consumer<T> processor) { processors.put(processorClass, processor); }
|
public <T> void registerProcessor(Class<T> processorClass, Consumer<T> processor) { processors.put(processorClass, processor); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes all object processors registered at this {@link Receiver}.
|
* Removes all object processors registered at this {@link Receiver}.
|
||||||
|
*
|
||||||
|
* @since Envoy Client v0.3-alpha
|
||||||
*/
|
*/
|
||||||
public void removeAllProcessors() { processors.clear(); }
|
public void removeAllProcessors() { processors.clear(); }
|
||||||
}
|
}
|
||||||
|
@ -96,29 +96,28 @@ public final class ChatScene {
|
|||||||
message.setStatus(e.get());
|
message.setStatus(e.get());
|
||||||
|
|
||||||
// Update UI if in current chat
|
// Update UI if in current chat
|
||||||
if (currentChat != null && message.getSenderID() == currentChat.getRecipient().getID()) Platform.runLater(() -> messageList.refresh());
|
if (currentChat != null && message.getSenderID() == currentChat.getRecipient().getID()) Platform.runLater(messageList::refresh);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Listen to user status changes
|
// Listen to user status changes
|
||||||
eventBus.register(UserStatusChangeEvent.class, e -> Platform.runLater(userList::refresh));
|
eventBus.register(UserStatusChangeEvent.class, e -> Platform.runLater(userList::refresh));
|
||||||
|
|
||||||
// Listen to contacts changes
|
// Listen to contacts changes
|
||||||
eventBus.register(ContactOperationEvent.class, e -> {
|
eventBus.register(ContactOperationEvent.class, e -> {
|
||||||
final var contact = e.get();
|
final var contact = e.get();
|
||||||
Platform.runLater(() -> {
|
|
||||||
switch (e.getOperationType()) {
|
switch (e.getOperationType()) {
|
||||||
case ADD:
|
case ADD:
|
||||||
localDB.getUsers().put(contact.getName(), contact);
|
localDB.getUsers().put(contact.getName(), contact);
|
||||||
localDB.getChats().add(new Chat(contact));
|
localDB.getChats().add(new Chat(contact));
|
||||||
userList.getItems().add(contact);
|
Platform.runLater(() -> userList.getItems().add(contact));
|
||||||
break;
|
break;
|
||||||
case REMOVE:
|
case REMOVE:
|
||||||
localDB.getUsers().remove(contact.getName());
|
localDB.getUsers().remove(contact.getName());
|
||||||
localDB.getChats().removeIf(c -> c.getRecipient().getID() == contact.getID());
|
localDB.getChats().removeIf(c -> c.getRecipient().getID() == contact.getID());
|
||||||
userList.getItems().removeIf(c -> c.getID() == contact.getID());
|
Platform.runLater(() -> userList.getItems().removeIf(c -> c.getID() == contact.getID()));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
package envoy.client.ui.controller;
|
package envoy.client.ui.controller;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.*;
|
||||||
import javafx.scene.control.ListView;
|
import javafx.scene.control.Alert.AlertType;
|
||||||
import javafx.scene.control.TextField;
|
|
||||||
|
|
||||||
import envoy.client.event.SendEvent;
|
import envoy.client.event.SendEvent;
|
||||||
import envoy.client.ui.SceneContext;
|
import envoy.client.ui.SceneContext;
|
||||||
@ -59,7 +58,10 @@ public class ContactSearchScene {
|
|||||||
@FXML
|
@FXML
|
||||||
private void initialize() {
|
private void initialize() {
|
||||||
contactList.setCellFactory(e -> new UserListCell());
|
contactList.setCellFactory(e -> new UserListCell());
|
||||||
eventBus.register(ContactSearchResult.class, response -> Optional.of(response.get()).ifPresent(list -> contactList.getItems().addAll(list)));
|
eventBus.register(ContactSearchResult.class, response -> Platform.runLater(() -> {
|
||||||
|
contactList.getItems().clear();
|
||||||
|
contactList.getItems().addAll(response.get());
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -104,14 +106,20 @@ public class ContactSearchScene {
|
|||||||
*/
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
private void contactListClicked() {
|
private void contactListClicked() {
|
||||||
Optional.ofNullable(contactList.getSelectionModel().getSelectedItems()).ifPresent(contacts -> contacts.forEach(contact -> {
|
final var contact = contactList.getSelectionModel().getSelectedItem();
|
||||||
|
if (contact != null) {
|
||||||
|
final var alert = new Alert(AlertType.CONFIRMATION);
|
||||||
|
alert.setTitle("Add Contact to Contact List");
|
||||||
|
alert.setHeaderText("Add the user " + contact.getName() + " to your contact list?");
|
||||||
|
alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> {
|
||||||
final var event = new ContactOperationEvent(contact, ElementOperation.ADD);
|
final var event = new ContactOperationEvent(contact, ElementOperation.ADD);
|
||||||
// Sends the event to the server
|
// Sends the event to the server
|
||||||
eventBus.dispatch(new SendEvent(event));
|
eventBus.dispatch(new SendEvent(event));
|
||||||
// Updates the UI
|
// Updates the UI
|
||||||
eventBus.dispatch(event);
|
eventBus.dispatch(event);
|
||||||
logger.log(Level.INFO, "Added contact " + contact);
|
logger.log(Level.INFO, "Added contact " + contact);
|
||||||
}));
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
|
Reference in New Issue
Block a user