package wow.doge.chatto.controller import com.typesafe.scalalogging.LazyLogging import com.sfxcode.sapphire.core.controller.ViewController import javafx.fxml.FXML import com.jfoenix.controls.JFXButton import com.jfoenix.controls.JFXTextField import com.jfoenix.controls.JFXPasswordField import scalafx.Includes._ // import scalafx.application.Platform import scalafx.event.ActionEvent import com.sfxcode.sapphire.core.value.KeyBindings import scalafx.scene.layout.VBox import com.sfxcode.sapphire.core.value.FXBean import javax.inject.Inject import com.sfxcode.sapphire.core.value.FXBeanAdapter import wow.doge.chatto.service.UserService import scala.concurrent.ExecutionContext.Implicits.global import scala.util.Success import scala.util.Failure 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 sttp.client._ import scala.concurrent.Future import sttp.client.asynchttpclient.WebSocketHandler import wow.doge.chatto.types.AppTypes.HttpBackend import wow.doge.chatto.types.AppTypes class LoginController @Inject() (userService: UserService, var appData: AppData)( implicit backend: HttpBackend ) extends AbstractViewController with LazyLogging with AppTypes { @FXML private var submitButton: JFXButton = _ @FXML private var usernameTextField: JFXTextField = _ @FXML private var passwordTextField: JFXPasswordField = _ @FXML private var errorLabel: Label = _ override def didGainVisibilityFirstTime(): Unit = { super.didGainVisibilityFirstTime() this.stage.resizable = false usernameTextField.requestFocus() submitButton.setOnAction(actionLogin) // println(something) val bindings = KeyBindings("usernameTextField", "passwordTextField") // Expression Binding Example // bindings.add( // "usernameTextField", // "${sf:i18n('personText', _self.usernameTextField(), _self.passwordTextField())})" // ) 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() }) } override def didGainVisibility(): Unit = { usernameTextField.requestFocus() } def actionLogin(e: ActionEvent) = { import org.scalafx.extras._ 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 None => { onFX(errorLabel.text = "Error logging in - please check your password" ) logger.warn("Login unsuccessful wrong password") } } // Platform.runLater(() => applicationController.showChatPane()) onFX(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() } // 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 login(username: String, password: String) = async { val resp = await(initLogin(username, password)) resp.header("X-AUTH-TOKEN") } def initLogin(username: String, password: String) = { basicRequest.auth .basic(username, password) .get(uri"http://localhost:8080/api/chat/get/token") .send() } final case class Result(username: String, password: String) final case class Person( id: String, usernameTextField: String, passwordTextField: String ) }