move user state outside of peer user

This commit is contained in:
Arnaud Vergnet 2021-01-25 14:51:09 +01:00
parent 32364461be
commit 33f355b90b
6 changed files with 41 additions and 24 deletions

View file

@ -3,6 +3,7 @@ package fr.insa.clavardator.client.ui.chat;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import fr.insa.clavardator.client.users.PeerUser;
import fr.insa.clavardator.lib.users.UserState;
import fr.insa.clavardator.lib.util.ErrorCallback;
import fr.insa.clavardator.lib.util.Log;
import javafx.application.Platform;
@ -157,8 +158,8 @@ public class ChatFooterController implements Initializable {
private void onUserStateChange(PropertyChangeEvent propertyChangeEvent) {
final String propName = propertyChangeEvent.getPropertyName();
if (propName.equals("state")) {
final PeerUser.State newState = (PeerUser.State) propertyChangeEvent.getNewValue();
Platform.runLater(() -> setEnabled(newState == PeerUser.State.CONNECTED));
final UserState newState = (UserState) propertyChangeEvent.getNewValue();
Platform.runLater(() -> setEnabled(newState == UserState.CONNECTED));
}
}

View file

@ -1,6 +1,7 @@
package fr.insa.clavardator.client.ui.users;
import fr.insa.clavardator.client.users.PeerUser;
import fr.insa.clavardator.lib.users.UserState;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@ -41,8 +42,8 @@ public class UserActiveIndicatorController implements Initializable {
*/
private void onStateChange(PropertyChangeEvent propertyChangeEvent) {
if (propertyChangeEvent.getPropertyName().equals("state")) {
final PeerUser.State newState = (PeerUser.State) propertyChangeEvent.getNewValue();
Platform.runLater(() -> updateState(newState == PeerUser.State.CONNECTED));
final UserState newState = (UserState) propertyChangeEvent.getNewValue();
Platform.runLater(() -> updateState(newState == UserState.CONNECTED));
}
}

View file

@ -3,6 +3,7 @@ package fr.insa.clavardator.client.ui.users;
import com.jfoenix.controls.JFXButton;
import fr.insa.clavardator.client.ui.ButtonPressEvent;
import fr.insa.clavardator.client.users.PeerUser;
import fr.insa.clavardator.lib.users.UserState;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@ -56,8 +57,8 @@ public class UserListItemController implements Initializable {
final String newUsername = (String) propertyChangeEvent.getNewValue();
Platform.runLater(() -> button.setText(newUsername));
} else if (propName.equals("state") && !selected) {
final PeerUser.State newState = (PeerUser.State) propertyChangeEvent.getNewValue();
Platform.runLater(() -> resetBackground(newState == PeerUser.State.CONNECTED));
final UserState newState = (UserState) propertyChangeEvent.getNewValue();
Platform.runLater(() -> resetBackground(newState == UserState.CONNECTED));
}
}

View file

@ -8,6 +8,7 @@ import fr.insa.clavardator.lib.message.Message;
import fr.insa.clavardator.lib.network.TcpConnection;
import fr.insa.clavardator.lib.users.User;
import fr.insa.clavardator.lib.users.UserInformation;
import fr.insa.clavardator.lib.users.UserState;
import fr.insa.clavardator.lib.util.ErrorCallback;
import fr.insa.clavardator.lib.util.Log;
import org.jetbrains.annotations.NotNull;
@ -20,7 +21,7 @@ import java.util.Date;
public class PeerUser extends User implements Comparable<PeerUser> {
protected transient ChatHistory history;
private State state = State.DISCONNECTED;
private UserState state = UserState.DISCONNECTED;
private transient TcpConnection connection;
public PeerUser(String id, String username) {
@ -28,11 +29,6 @@ public class PeerUser extends User implements Comparable<PeerUser> {
history = new ChatHistory(this);
}
public PeerUser(String id) {
super(id);
history = new ChatHistory(this);
}
public PeerUser() {
super();
history = new ChatHistory(this);
@ -109,7 +105,7 @@ public class PeerUser extends User implements Comparable<PeerUser> {
this.connection = connection;
this.setId(id);
setUsername(username);
setState(State.CONNECTED);
setState(UserState.CONNECTED);
subscribeToMessages((e) -> {
disconnect();
errorCallback.onError(e);
@ -165,7 +161,7 @@ public class PeerUser extends User implements Comparable<PeerUser> {
public void disconnect() {
Log.v(this.getClass().getSimpleName(), "Disconnecting from user: " + getId());
closeConnection();
setState(State.DISCONNECTED);
setState(UserState.DISCONNECTED);
}
@Override
@ -202,7 +198,7 @@ public class PeerUser extends User implements Comparable<PeerUser> {
*
* @param state The new state
*/
protected void setState(State state) {
protected void setState(UserState state) {
getPcs().firePropertyChange("state", this.state, state);
this.state = state;
}
@ -213,7 +209,7 @@ public class PeerUser extends User implements Comparable<PeerUser> {
* @return True id active, false otherwise
*/
public boolean isActive() {
return state == State.CONNECTED;
return state == UserState.CONNECTED;
}
@Override
@ -225,12 +221,4 @@ public class PeerUser extends User implements Comparable<PeerUser> {
}
return getUsername().compareTo(peerUser.getUsername());
}
/**
* The user connection state
*/
public enum State {
CONNECTED,
DISCONNECTED,
}
}

View file

@ -8,20 +8,40 @@ import java.io.Serializable;
public class UserInformation implements Serializable {
public final String id;
private final String username;
private final UserState state;
public UserInformation(String id, String username) {
this.id = id;
this.username = username;
this.state = UserState.CONNECTED;
}
public UserInformation(String id, String username, UserState state) {
this.id = id;
this.username = username;
this.state = state;
}
public UserInformation(User user) {
this.id = user.getId();
this.username = user.getUsername();
this.state = UserState.CONNECTED;
}
public UserInformation(User user, UserState state) {
this.id = user.getId();
this.username = user.getUsername();
this.state = state;
}
public String getUsername() {
return username;
}
public UserState getState() {
return state;
}
@Override
public String toString() {
return "UserInfo " + id + '(' + username + ')';

View file

@ -0,0 +1,6 @@
package fr.insa.clavardator.lib.users;
public enum UserState {
CONNECTED,
DISCONNECTED,
}