feat: improve current user init process

This commit is contained in:
Arnaud Vergnet 2020-12-07 15:02:31 +01:00
parent fde0d4344b
commit d351f25e3a
7 changed files with 131 additions and 45 deletions

View file

@ -27,9 +27,6 @@ public class MainApp extends Application {
@Override
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 MainController main = mainLoader.getController();
final Parent content = mainLoader.load();

View file

@ -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();
}
}

View file

@ -7,25 +7,23 @@ import fr.insa.clavardator.ui.dialogs.EditUsernameDialogController;
import fr.insa.clavardator.ui.dialogs.SnackbarController;
import fr.insa.clavardator.ui.users.UserListController;
import fr.insa.clavardator.users.CurrentUser;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.StackPane;
import java.io.IOException;
import java.net.SocketException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
public class MainController implements Initializable {
private final CurrentUser currentUser;
@FXML
private StackPane root;
@FXML
private StackPane mainContainer;
@FXML
private ToolbarController toolbarController;
@FXML
@ -38,9 +36,23 @@ public class MainController implements Initializable {
private ChatController chatController;
@FXML
private LoadingScreenController loadingController;
@FXML
private ErrorScreenController errorController;
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) {
editUserDialogController.setOnDismissListener(() -> {
if (mode == EditUsernameDialogController.Mode.INITIAL) {
@ -65,6 +77,10 @@ public class MainController implements Initializable {
editUserDialogController.show(root, mode);
}
private void openAboutDialog() {
aboutDialogController.show(root);
}
private void showChat() {
loadingController.hide();
mainContainer.setVisible(true);
@ -76,35 +92,29 @@ public class MainController implements Initializable {
openEditUsernameDialog(isError ? EditUsernameDialogController.Mode.ERROR : EditUsernameDialogController.Mode.INITIAL);
}
private void showError() {
mainContainer.setVisible(false);
loadingController.hide();
errorController.show();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
loadingController.show("Initialisation de Clavardator...");
snackbar = new JFXSnackbar(root);
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
Platform.runLater(() -> CurrentUser.getInstance().setState(CurrentUser.State.VALID));
t.cancel();
try {
currentUser.init();
System.out.println(currentUser.getId());
} catch (SocketException e) {
showError();
}
}, 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.addUserSelectedListener((user) -> chatController.setRemoteUser(user));
chatController.addAttachmentListener(() -> System.out.println("attach event"));
chatController.addSendListener((text) -> System.out.println("sent : " + text));
toolbarController.addEditListener(() -> openEditUsernameDialog(EditUsernameDialogController.Mode.EDIT));
toolbarController.addAboutListener(() -> aboutDialogController.show(root));
toolbarController.addAboutListener(this::openAboutDialog);
}
}

View file

@ -1,36 +1,54 @@
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 {
private static CurrentUser instance = null;
private State state;
private CurrentUser(int uniqueId, String username) {
super(uniqueId, username);
private CurrentUser() {
super();
}
private CurrentUser(int uniqueId) {
super(uniqueId);
state = State.UNINITIALIZED;
}
/**
* Initializes and returns the current user.
*
* @param uniqueId the unique id of the current user
* @param username The username of the current user
* Returns the current.
*/
public static CurrentUser initCurrentUser(int uniqueId, String username) {
assert (instance == null);
instance = new CurrentUser(uniqueId, username);
public static CurrentUser getInstance() {
if (instance == null)
instance = new CurrentUser();
return instance;
}
/**
* Returns the current user or null if it has not been initialized yet.
*
* @see CurrentUser#initCurrentUser(int uniqueId, String username)
*/
public static CurrentUser getInstance() {
return instance;
public void init() throws SocketException {
final List<InetAddress> addresses = NetUtil.listAllLocalAddresses();
if (addresses.size() > 0) {
id = addresses.get(0).hashCode();
} else {
throw new SocketException();
}
// 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() {

View file

@ -8,7 +8,7 @@ import java.io.Serializable;
public class User implements Serializable, Comparable<User> {
private String username;
public final int id;
public int id;
// Make this class observable
protected final transient PropertyChangeSupport pcs = new PropertyChangeSupport(this);
@ -19,6 +19,13 @@ public class User implements Serializable, Comparable<User> {
pcs.removePropertyChangeListener(listener);
}
public User() {
}
public User(int id) {
this.id = id;
}
public User(int id, String username) {
this.id = id;
this.username = username;

View 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>

View file

@ -20,5 +20,6 @@
</HBox>
</VBox>
</StackPane>
<fx:include source="errorScreen.fxml" fx:id="error"/>
<fx:include source="loadingScreen.fxml" fx:id="loading"/>
</StackPane>