feat: improve current user init process
This commit is contained in:
parent
fde0d4344b
commit
d351f25e3a
7 changed files with 131 additions and 45 deletions
|
@ -27,9 +27,6 @@ public class MainApp extends Application {
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage stage) throws Exception {
|
public void start(Stage stage) throws Exception {
|
||||||
|
|
||||||
// Find a place to call this init function
|
|
||||||
CurrentUser.initCurrentUser(1, "Moi");
|
|
||||||
|
|
||||||
final FXMLLoader mainLoader = new FXMLLoader(getClass().getResource("ui/scene.fxml"));
|
final FXMLLoader mainLoader = new FXMLLoader(getClass().getResource("ui/scene.fxml"));
|
||||||
final MainController main = mainLoader.getController();
|
final MainController main = mainLoader.getController();
|
||||||
final Parent content = mainLoader.load();
|
final Parent content = mainLoader.load();
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package fr.insa.clavardator.ui;
|
||||||
|
|
||||||
|
import javafx.fxml.FXML;
|
||||||
|
import javafx.fxml.Initializable;
|
||||||
|
import javafx.scene.layout.VBox;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
public class ErrorScreenController implements Initializable {
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private VBox container;
|
||||||
|
|
||||||
|
public void show() {
|
||||||
|
container.setVisible(true);
|
||||||
|
}
|
||||||
|
public void hide() {
|
||||||
|
container.setVisible(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private void onPress() {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(URL location, ResourceBundle resources) {
|
||||||
|
hide();
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,25 +7,23 @@ import fr.insa.clavardator.ui.dialogs.EditUsernameDialogController;
|
||||||
import fr.insa.clavardator.ui.dialogs.SnackbarController;
|
import fr.insa.clavardator.ui.dialogs.SnackbarController;
|
||||||
import fr.insa.clavardator.ui.users.UserListController;
|
import fr.insa.clavardator.ui.users.UserListController;
|
||||||
import fr.insa.clavardator.users.CurrentUser;
|
import fr.insa.clavardator.users.CurrentUser;
|
||||||
import javafx.application.Platform;
|
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.fxml.Initializable;
|
import javafx.fxml.Initializable;
|
||||||
import javafx.scene.layout.StackPane;
|
import javafx.scene.layout.StackPane;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.SocketException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
|
|
||||||
public class MainController implements Initializable {
|
public class MainController implements Initializable {
|
||||||
|
|
||||||
|
private final CurrentUser currentUser;
|
||||||
@FXML
|
@FXML
|
||||||
private StackPane root;
|
private StackPane root;
|
||||||
@FXML
|
@FXML
|
||||||
private StackPane mainContainer;
|
private StackPane mainContainer;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private ToolbarController toolbarController;
|
private ToolbarController toolbarController;
|
||||||
@FXML
|
@FXML
|
||||||
|
@ -38,9 +36,23 @@ public class MainController implements Initializable {
|
||||||
private ChatController chatController;
|
private ChatController chatController;
|
||||||
@FXML
|
@FXML
|
||||||
private LoadingScreenController loadingController;
|
private LoadingScreenController loadingController;
|
||||||
|
@FXML
|
||||||
|
private ErrorScreenController errorController;
|
||||||
private JFXSnackbar snackbar;
|
private JFXSnackbar snackbar;
|
||||||
|
|
||||||
|
public MainController() {
|
||||||
|
currentUser = CurrentUser.getInstance();
|
||||||
|
currentUser.addObserver(propertyChangeEvent -> {
|
||||||
|
if (propertyChangeEvent.getPropertyName().equals("state")) {
|
||||||
|
final CurrentUser.State state = (CurrentUser.State) propertyChangeEvent.getNewValue();
|
||||||
|
if (state == CurrentUser.State.VALID)
|
||||||
|
showChat();
|
||||||
|
else
|
||||||
|
showLogin(state == CurrentUser.State.INVALID);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void openEditUsernameDialog(EditUsernameDialogController.Mode mode) {
|
private void openEditUsernameDialog(EditUsernameDialogController.Mode mode) {
|
||||||
editUserDialogController.setOnDismissListener(() -> {
|
editUserDialogController.setOnDismissListener(() -> {
|
||||||
if (mode == EditUsernameDialogController.Mode.INITIAL) {
|
if (mode == EditUsernameDialogController.Mode.INITIAL) {
|
||||||
|
@ -65,6 +77,10 @@ public class MainController implements Initializable {
|
||||||
editUserDialogController.show(root, mode);
|
editUserDialogController.show(root, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void openAboutDialog() {
|
||||||
|
aboutDialogController.show(root);
|
||||||
|
}
|
||||||
|
|
||||||
private void showChat() {
|
private void showChat() {
|
||||||
loadingController.hide();
|
loadingController.hide();
|
||||||
mainContainer.setVisible(true);
|
mainContainer.setVisible(true);
|
||||||
|
@ -76,35 +92,29 @@ public class MainController implements Initializable {
|
||||||
openEditUsernameDialog(isError ? EditUsernameDialogController.Mode.ERROR : EditUsernameDialogController.Mode.INITIAL);
|
openEditUsernameDialog(isError ? EditUsernameDialogController.Mode.ERROR : EditUsernameDialogController.Mode.INITIAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void showError() {
|
||||||
|
mainContainer.setVisible(false);
|
||||||
|
loadingController.hide();
|
||||||
|
errorController.show();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(URL url, ResourceBundle rb) {
|
public void initialize(URL url, ResourceBundle rb) {
|
||||||
loadingController.show("Initialisation de Clavardator...");
|
loadingController.show("Initialisation de Clavardator...");
|
||||||
snackbar = new JFXSnackbar(root);
|
snackbar = new JFXSnackbar(root);
|
||||||
|
|
||||||
Timer t = new Timer();
|
try {
|
||||||
t.schedule(new TimerTask() {
|
currentUser.init();
|
||||||
@Override
|
System.out.println(currentUser.getId());
|
||||||
public void run() {
|
} catch (SocketException e) {
|
||||||
Platform.runLater(() -> CurrentUser.getInstance().setState(CurrentUser.State.VALID));
|
showError();
|
||||||
t.cancel();
|
}
|
||||||
}
|
|
||||||
}, 3000);
|
|
||||||
|
|
||||||
CurrentUser.getInstance().addObserver(propertyChangeEvent -> {
|
|
||||||
if (propertyChangeEvent.getPropertyName().equals("state")) {
|
|
||||||
final CurrentUser.State state = (CurrentUser.State) propertyChangeEvent.getNewValue();
|
|
||||||
if (state == CurrentUser.State.VALID)
|
|
||||||
showChat();
|
|
||||||
else
|
|
||||||
showLogin(state == CurrentUser.State.INVALID);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
userListController.addRefreshUserListener(() -> System.out.println("refresh event"));
|
userListController.addRefreshUserListener(() -> System.out.println("refresh event"));
|
||||||
userListController.addUserSelectedListener((user) -> chatController.setRemoteUser(user));
|
userListController.addUserSelectedListener((user) -> chatController.setRemoteUser(user));
|
||||||
chatController.addAttachmentListener(() -> System.out.println("attach event"));
|
chatController.addAttachmentListener(() -> System.out.println("attach event"));
|
||||||
chatController.addSendListener((text) -> System.out.println("sent : " + text));
|
chatController.addSendListener((text) -> System.out.println("sent : " + text));
|
||||||
toolbarController.addEditListener(() -> openEditUsernameDialog(EditUsernameDialogController.Mode.EDIT));
|
toolbarController.addEditListener(() -> openEditUsernameDialog(EditUsernameDialogController.Mode.EDIT));
|
||||||
toolbarController.addAboutListener(() -> aboutDialogController.show(root));
|
toolbarController.addAboutListener(this::openAboutDialog);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,54 @@
|
||||||
package fr.insa.clavardator.users;
|
package fr.insa.clavardator.users;
|
||||||
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import fr.insa.clavardator.network.NetUtil;
|
||||||
|
import javafx.application.Platform;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
public class CurrentUser extends User {
|
public class CurrentUser extends User {
|
||||||
private static CurrentUser instance = null;
|
private static CurrentUser instance = null;
|
||||||
|
|
||||||
private State state;
|
private State state;
|
||||||
|
|
||||||
private CurrentUser(int uniqueId, String username) {
|
private CurrentUser() {
|
||||||
super(uniqueId, username);
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
private CurrentUser(int uniqueId) {
|
||||||
|
super(uniqueId);
|
||||||
state = State.UNINITIALIZED;
|
state = State.UNINITIALIZED;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes and returns the current user.
|
* Returns the current.
|
||||||
*
|
|
||||||
* @param uniqueId the unique id of the current user
|
|
||||||
* @param username The username of the current user
|
|
||||||
*/
|
*/
|
||||||
public static CurrentUser initCurrentUser(int uniqueId, String username) {
|
public static CurrentUser getInstance() {
|
||||||
assert (instance == null);
|
if (instance == null)
|
||||||
instance = new CurrentUser(uniqueId, username);
|
instance = new CurrentUser();
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public void init() throws SocketException {
|
||||||
* Returns the current user or null if it has not been initialized yet.
|
final List<InetAddress> addresses = NetUtil.listAllLocalAddresses();
|
||||||
*
|
if (addresses.size() > 0) {
|
||||||
* @see CurrentUser#initCurrentUser(int uniqueId, String username)
|
id = addresses.get(0).hashCode();
|
||||||
*/
|
} else {
|
||||||
public static CurrentUser getInstance() {
|
throw new SocketException();
|
||||||
return instance;
|
}
|
||||||
|
// TODO replace by db and network calls
|
||||||
|
Timer t = new Timer();
|
||||||
|
t.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Platform.runLater(() -> setState(CurrentUser.State.VALID));
|
||||||
|
t.cancel();
|
||||||
|
}
|
||||||
|
}, 3000);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public State getState() {
|
public State getState() {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.io.Serializable;
|
||||||
|
|
||||||
public class User implements Serializable, Comparable<User> {
|
public class User implements Serializable, Comparable<User> {
|
||||||
private String username;
|
private String username;
|
||||||
public final int id;
|
public int id;
|
||||||
|
|
||||||
// Make this class observable
|
// Make this class observable
|
||||||
protected final transient PropertyChangeSupport pcs = new PropertyChangeSupport(this);
|
protected final transient PropertyChangeSupport pcs = new PropertyChangeSupport(this);
|
||||||
|
@ -19,6 +19,13 @@ public class User implements Serializable, Comparable<User> {
|
||||||
pcs.removePropertyChangeListener(listener);
|
pcs.removePropertyChangeListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public User(int id, String username) {
|
public User(int id, String username) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
|
|
22
src/main/resources/fr/insa/clavardator/ui/errorScreen.fxml
Normal file
22
src/main/resources/fr/insa/clavardator/ui/errorScreen.fxml
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import javafx.scene.control.Label?>
|
||||||
|
<?import javafx.scene.layout.VBox?>
|
||||||
|
<?import com.jfoenix.controls.JFXButton?>
|
||||||
|
<?import org.kordamp.ikonli.javafx.FontIcon?>
|
||||||
|
<VBox xmlns:fx="http://javafx.com/fxml"
|
||||||
|
xmlns="http://javafx.com/javafx"
|
||||||
|
fx:controller="fr.insa.clavardator.ui.ErrorScreenController"
|
||||||
|
stylesheets="@styles.css"
|
||||||
|
styleClass="container"
|
||||||
|
alignment="CENTER"
|
||||||
|
spacing="30"
|
||||||
|
fx:id="container">
|
||||||
|
<Label>Une erreur est survenue et Clavardator ne peut pas démarrer</Label>
|
||||||
|
<JFXButton styleClass="background-danger" onAction="#onPress">
|
||||||
|
<graphic>
|
||||||
|
<FontIcon iconLiteral="fas-times"/>
|
||||||
|
</graphic>
|
||||||
|
Quitter
|
||||||
|
</JFXButton>
|
||||||
|
</VBox>
|
|
@ -20,5 +20,6 @@
|
||||||
</HBox>
|
</HBox>
|
||||||
</VBox>
|
</VBox>
|
||||||
</StackPane>
|
</StackPane>
|
||||||
|
<fx:include source="errorScreen.fxml" fx:id="error"/>
|
||||||
<fx:include source="loadingScreen.fxml" fx:id="loading"/>
|
<fx:include source="loadingScreen.fxml" fx:id="loading"/>
|
||||||
</StackPane>
|
</StackPane>
|
||||||
|
|
Loading…
Reference in a new issue