|
|
@ -22,19 +22,26 @@ import javafx.scene.control.Label |
|
|
|
import javafx.scene.input.KeyCode |
|
|
|
import scala.async.Async.{async, await} |
|
|
|
import wow.doge.chatto.AppData |
|
|
|
import wow.doge.chatto.User |
|
|
|
import wow.doge.chatto.UserCredentials |
|
|
|
import sttp.client._ |
|
|
|
import scala.concurrent.Future |
|
|
|
import sttp.client.asynchttpclient.WebSocketHandler |
|
|
|
import wow.doge.chatto.types.AppTypes.HttpBackend |
|
|
|
import wow.doge.chatto.types.AppTypes |
|
|
|
import org.scalafx.extras._ |
|
|
|
import wow.doge.chatto.AppDataHandler |
|
|
|
import com.sfxcode.sapphire.core.value.BeanConversions |
|
|
|
|
|
|
|
class LoginController @Inject() (userService: UserService, var appData: AppData)( |
|
|
|
class LoginController @Inject() ( |
|
|
|
userService: UserService, |
|
|
|
appDataHandler: AppDataHandler |
|
|
|
)( |
|
|
|
implicit backend: HttpBackend |
|
|
|
) extends AbstractViewController |
|
|
|
with LazyLogging |
|
|
|
with AppTypes { |
|
|
|
@FXML private var submitButton: JFXButton = _ |
|
|
|
with AppTypes |
|
|
|
with BeanConversions { |
|
|
|
@FXML var submitButton: JFXButton = _ |
|
|
|
|
|
|
|
@FXML private var usernameTextField: JFXTextField = _ |
|
|
|
|
|
|
@ -45,12 +52,11 @@ class LoginController @Inject() (userService: UserService, var appData: AppData) |
|
|
|
override def didGainVisibilityFirstTime(): Unit = { |
|
|
|
super.didGainVisibilityFirstTime() |
|
|
|
this.stage.resizable = false |
|
|
|
usernameTextField.requestFocus() |
|
|
|
submitButton.setOnAction(actionLogin) |
|
|
|
// usernameTextField.requestFocus() |
|
|
|
|
|
|
|
// println(something) |
|
|
|
// submitButton.setOnAction(actionLogin) |
|
|
|
|
|
|
|
val bindings = KeyBindings("usernameTextField", "passwordTextField") |
|
|
|
val bindings = KeyBindings("username", "password") |
|
|
|
// Expression Binding Example |
|
|
|
// bindings.add( |
|
|
|
// "usernameTextField", |
|
|
@ -59,26 +65,13 @@ class LoginController @Inject() (userService: UserService, var appData: AppData) |
|
|
|
|
|
|
|
val box = new VBox() |
|
|
|
val adapter = FXBeanAdapter[Person](this) |
|
|
|
// adapter. |
|
|
|
val bean = FXBean[Person](Person("twar", "username", "password")) |
|
|
|
// bean. |
|
|
|
// bean.get |
|
|
|
adapter.addBindings(bindings) |
|
|
|
adapter.set(bean) |
|
|
|
// adapter.addIntConverter("age") |
|
|
|
// adapter.hasBeanProperty |
|
|
|
// adapter.revert() |
|
|
|
|
|
|
|
// usernameTextField.onKeyPressed = (keyEvent) => { |
|
|
|
// if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire() |
|
|
|
// } |
|
|
|
// submitButton.onKeyPressed = (keyEvent) => { |
|
|
|
// if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire() |
|
|
|
// } |
|
|
|
// passwordTextField.onKeyPressed = (keyEvent) => { |
|
|
|
// if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire() |
|
|
|
// } |
|
|
|
|
|
|
|
Array(usernameTextField, passwordTextField, submitButton) |
|
|
|
.foreach(_.onKeyPressed = (keyEvent) => { |
|
|
|
if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire() |
|
|
@ -90,51 +83,43 @@ class LoginController @Inject() (userService: UserService, var appData: AppData) |
|
|
|
usernameTextField.requestFocus() |
|
|
|
} |
|
|
|
|
|
|
|
def actionLogin(e: ActionEvent) = { |
|
|
|
import org.scalafx.extras._ |
|
|
|
def actionLogin() = { |
|
|
|
val inputUserName = usernameTextField.text() |
|
|
|
val inputPassword = passwordTextField.text() |
|
|
|
|
|
|
|
// val authenticated = |
|
|
|
// inputPassword.equals("password") && inputUserName.equals("hmm") |
|
|
|
login(inputUserName, inputPassword) onComplete { |
|
|
|
case Success(value) => { |
|
|
|
value.foreach(println) |
|
|
|
value match { |
|
|
|
case Some(token) => { |
|
|
|
appData = |
|
|
|
appData.copy(user = User(inputUserName, inputPassword, token)) |
|
|
|
case Success(maybeToken) => { |
|
|
|
maybeToken.foreach(println) |
|
|
|
maybeToken match { |
|
|
|
case Some(token) => |
|
|
|
async { |
|
|
|
val credentials = |
|
|
|
UserCredentials(inputUserName, inputPassword, token) |
|
|
|
appDataHandler.updateCredentials(credentials) |
|
|
|
await { |
|
|
|
userService.getUsers(credentials).map(_.body.foreach(println)) |
|
|
|
} |
|
|
|
applicationController.showChatPane() |
|
|
|
} |
|
|
|
case None => { |
|
|
|
onFX(errorLabel.text = |
|
|
|
"Error logging in - please check your password" |
|
|
|
) |
|
|
|
updateErrorLabel("Error logging in - please check your password") |
|
|
|
logger.warn("Login unsuccessful wrong password") |
|
|
|
} |
|
|
|
} |
|
|
|
// Platform.runLater(() => applicationController.showChatPane()) |
|
|
|
onFX(applicationController.showChatPane()) |
|
|
|
// applicationController.showChatPane() |
|
|
|
|
|
|
|
} |
|
|
|
case Failure(exception) => { |
|
|
|
logger.error(s"${exception.getMessage()}") |
|
|
|
logger.warn("Login unsuccessful network problem") |
|
|
|
onFX { |
|
|
|
errorLabel.text = "Error logging in - Please check your network" |
|
|
|
applicationController.showChatPane() |
|
|
|
updateErrorLabel("Error logging in - Please check your network") |
|
|
|
// applicationController.showChatPane() |
|
|
|
} |
|
|
|
// onFX(applicationController.showChatPane()) |
|
|
|
} |
|
|
|
} |
|
|
|
// if (authenticated) { |
|
|
|
// passwordTextField.clear() |
|
|
|
// val res = Result(username = inputUserName, password = inputPassword) |
|
|
|
// println(res) |
|
|
|
// // loginManager. |
|
|
|
|
|
|
|
// applicationController.showChatPane() |
|
|
|
// } else { |
|
|
|
// logger.error("Login Error") |
|
|
|
// } |
|
|
|
def updateErrorLabel(message: String) = onFX { |
|
|
|
errorLabel.text = message |
|
|
|
} |
|
|
|
|
|
|
|
def login(username: String, password: String) = async { |
|
|
@ -153,8 +138,8 @@ class LoginController @Inject() (userService: UserService, var appData: AppData) |
|
|
|
|
|
|
|
final case class Person( |
|
|
|
id: String, |
|
|
|
usernameTextField: String, |
|
|
|
passwordTextField: String |
|
|
|
username: String, |
|
|
|
password: String |
|
|
|
) |
|
|
|
|
|
|
|
} |