improve username setting flow
This commit is contained in:
parent
5cf0279279
commit
c13b589054
5 changed files with 46 additions and 20 deletions
|
@ -63,15 +63,18 @@ public class MainController implements Initializable {
|
|||
}
|
||||
|
||||
private void openEditUsernameDialog(EditUsernameDialogController.Mode mode) {
|
||||
editUserDialogController.setOnDismissListener(() -> {
|
||||
if (mode == EditUsernameDialogController.Mode.INITIAL) {
|
||||
final boolean initialMode = mode == EditUsernameDialogController.Mode.INITIAL;
|
||||
editUserDialogController.setOnCancelListener(() -> {
|
||||
if (initialMode) {
|
||||
System.exit(0);
|
||||
} else {
|
||||
showChat();
|
||||
}
|
||||
});
|
||||
editUserDialogController.setOnSuccessListener(() -> {
|
||||
showSnackbarEvent("Nom d'utilisateur changé !", SnackbarController.Mode.SUCCESS);
|
||||
if (!initialMode) {
|
||||
showSnackbarEvent("Nom d'utilisateur changé !", SnackbarController.Mode.SUCCESS);
|
||||
}
|
||||
});
|
||||
editUserDialogController.show(root, mode);
|
||||
}
|
||||
|
@ -109,23 +112,27 @@ public class MainController implements Initializable {
|
|||
}
|
||||
|
||||
private void startChat() {
|
||||
Log.v(this.getClass().getSimpleName(), "Chat started");
|
||||
discoverActiveUsers();
|
||||
startListening();
|
||||
Platform.runLater(this::showChat);
|
||||
}
|
||||
|
||||
private void showChat() {
|
||||
Log.v(this.getClass().getSimpleName(), "Chat shown");
|
||||
loadingController.hide();
|
||||
mainContainer.setVisible(true);
|
||||
}
|
||||
|
||||
private void showLogin(boolean isError) {
|
||||
Log.v(this.getClass().getSimpleName(), "Login shown");
|
||||
mainContainer.setVisible(false);
|
||||
loadingController.hide();
|
||||
openEditUsernameDialog(isError ? EditUsernameDialogController.Mode.ERROR : EditUsernameDialogController.Mode.INITIAL);
|
||||
}
|
||||
|
||||
private void showError() {
|
||||
Log.v(this.getClass().getSimpleName(), "Error shown");
|
||||
mainContainer.setVisible(false);
|
||||
loadingController.hide();
|
||||
errorController.show();
|
||||
|
@ -138,8 +145,9 @@ public class MainController implements Initializable {
|
|||
|
||||
try {
|
||||
currentUser.init();
|
||||
Log.v("INIT", "Current user: " + currentUser.getUsername() + " / " + currentUser.getId());
|
||||
Log.v("INIT", "Current user: " + currentUser.getId());
|
||||
} catch (SocketException e) {
|
||||
Log.e("INIT", "Could not init user", e);
|
||||
showError();
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import javafx.fxml.FXML;
|
|||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.control.Label;
|
||||
|
||||
import java.beans.PropertyChangeEvent;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -33,9 +34,16 @@ public class ToolbarController implements Initializable {
|
|||
aboutListeners.forEach(ButtonPressEvent::onPress);
|
||||
}
|
||||
|
||||
private void onUsernameChange(PropertyChangeEvent propertyChangeEvent) {
|
||||
if (propertyChangeEvent.getPropertyName().equals("username")) {
|
||||
final String newUsername = (String) propertyChangeEvent.getNewValue();
|
||||
currentUsernameLabel.setText(newUsername);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
currentUsernameLabel.setText(CurrentUser.getInstance().getUsername());
|
||||
CurrentUser.getInstance().addObserver(this::onUsernameChange);
|
||||
editListeners = new ArrayList<>();
|
||||
aboutListeners = new ArrayList<>();
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.jfoenix.controls.JFXDialog;
|
|||
import com.jfoenix.controls.JFXTextField;
|
||||
import com.jfoenix.validation.base.ValidatorBase;
|
||||
import fr.insa.clavardator.ui.ButtonPressEvent;
|
||||
import fr.insa.clavardator.users.CurrentUser;
|
||||
import javafx.beans.property.ReadOnlyBooleanWrapper;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.fxml.FXML;
|
||||
|
@ -42,11 +43,12 @@ public class EditUsernameDialogController implements Initializable {
|
|||
private State currentState;
|
||||
private Validator validator;
|
||||
private ButtonPressEvent successListener;
|
||||
private ButtonPressEvent dismissListener;
|
||||
private ButtonPressEvent cancelListener;
|
||||
|
||||
@FXML
|
||||
private void onConfirm() {
|
||||
setLocked(true);
|
||||
CurrentUser.getInstance().setUsername(textField.getText());
|
||||
// TODO replace by network call
|
||||
Timer t = new Timer();
|
||||
t.schedule(new TimerTask() {
|
||||
|
@ -61,15 +63,17 @@ public class EditUsernameDialogController implements Initializable {
|
|||
|
||||
@FXML
|
||||
private void onCancel() {
|
||||
if (cancelListener != null) {
|
||||
cancelListener.onPress();
|
||||
}
|
||||
hide();
|
||||
}
|
||||
|
||||
public void setOnSuccessListener(ButtonPressEvent listener) {
|
||||
this.successListener = listener;
|
||||
}
|
||||
|
||||
public void setOnDismissListener(ButtonPressEvent listener) {
|
||||
this.dismissListener = listener;
|
||||
public void setOnCancelListener(ButtonPressEvent listener) {
|
||||
this.cancelListener = listener;
|
||||
}
|
||||
|
||||
public void show(StackPane root, Mode mode) {
|
||||
|
@ -82,9 +86,6 @@ public class EditUsernameDialogController implements Initializable {
|
|||
|
||||
public void hide() {
|
||||
dialog.close();
|
||||
if (dismissListener != null) {
|
||||
dismissListener.onPress();
|
||||
}
|
||||
}
|
||||
|
||||
private void setLocked(boolean state) {
|
||||
|
@ -148,7 +149,9 @@ public class EditUsernameDialogController implements Initializable {
|
|||
setFieldError(State.VALID);
|
||||
setLocked(false);
|
||||
hide();
|
||||
successListener.onPress();
|
||||
if (successListener != null) {
|
||||
successListener.onPress();
|
||||
}
|
||||
}
|
||||
|
||||
private void onError() {
|
||||
|
|
|
@ -5,6 +5,7 @@ import fr.insa.clavardator.ui.UserSelectedEvent;
|
|||
import fr.insa.clavardator.users.PeerUser;
|
||||
import fr.insa.clavardator.users.User;
|
||||
import fr.insa.clavardator.users.UserList;
|
||||
import fr.insa.clavardator.util.Log;
|
||||
import javafx.application.Platform;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
|
@ -54,10 +55,12 @@ public class UserListController implements Initializable {
|
|||
}
|
||||
|
||||
private void onUserConnected(PeerUser user) {
|
||||
Log.v(this.getClass().getSimpleName(), "Add user to UI");
|
||||
Platform.runLater(() -> peerUserListView.getItems().add(user));
|
||||
}
|
||||
|
||||
private void onUserDisconnected(PeerUser user) {
|
||||
Log.v(this.getClass().getSimpleName(), "Remove user from UI");
|
||||
Platform.runLater(() -> peerUserListView.getItems().remove(user));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package fr.insa.clavardator.users;
|
||||
|
||||
import fr.insa.clavardator.network.NetUtil;
|
||||
import fr.insa.clavardator.util.Log;
|
||||
import javafx.application.Platform;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
@ -37,29 +38,32 @@ public class CurrentUser extends User {
|
|||
} else {
|
||||
throw new SocketException();
|
||||
}
|
||||
setUsername("Moi2");
|
||||
// TODO place by db username fetching
|
||||
// TODO replace by db username fetching
|
||||
Timer t = new Timer();
|
||||
t.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
Platform.runLater(() -> setState(CurrentUser.State.VALID));
|
||||
setState(State.NONE);
|
||||
t.cancel();
|
||||
}
|
||||
}, 500);
|
||||
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
return state;
|
||||
@Override
|
||||
public void setUsername(String newUsername) {
|
||||
Log.v(this.getClass().getSimpleName(),
|
||||
"Username changed from " + getUsername() + " to " + newUsername);
|
||||
super.setUsername(newUsername);
|
||||
setState(State.VALID);
|
||||
}
|
||||
|
||||
public void setState(State state) {
|
||||
Log.v(this.getClass().getSimpleName(),
|
||||
"State changed from " + this.state.toString() + " to " + state.toString());
|
||||
instance.pcs.firePropertyChange("state", this.state, state);
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
|
||||
public enum State {
|
||||
UNINITIALIZED,
|
||||
VALID,
|
||||
|
|
Loading…
Reference in a new issue