WIP desktop client for Chatto reimplemented in ScalaFX and Sapphire Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
5.0 KiB

4 years ago
  1. package wow.doge.chatto.controller
  2. import com.typesafe.scalalogging.LazyLogging
  3. import com.sfxcode.sapphire.core.controller.ViewController
  4. import javafx.fxml.FXML
  5. import com.jfoenix.controls.JFXButton
  6. import com.jfoenix.controls.JFXTextField
  7. import com.jfoenix.controls.JFXPasswordField
  8. import scalafx.Includes._
  9. // import scalafx.application.Platform
  10. import scalafx.event.ActionEvent
  11. import com.sfxcode.sapphire.core.value.KeyBindings
  12. import scalafx.scene.layout.VBox
  13. import com.sfxcode.sapphire.core.value.FXBean
  14. import javax.inject.Inject
  15. import com.sfxcode.sapphire.core.value.FXBeanAdapter
  16. import wow.doge.chatto.service.UserService
  17. import scala.concurrent.ExecutionContext.Implicits.global
  18. import scala.util.Success
  19. import scala.util.Failure
  20. import javafx.scene.control.Label
  21. import javafx.scene.input.KeyCode
  22. import scala.async.Async.{async, await}
  23. import wow.doge.chatto.AppData
  24. import wow.doge.chatto.User
  25. import sttp.client._
  26. import scala.concurrent.Future
  27. import sttp.client.asynchttpclient.WebSocketHandler
  28. import wow.doge.chatto.types.AppTypes.HttpBackend
  29. import wow.doge.chatto.types.AppTypes
  30. class LoginController @Inject() (userService: UserService, var appData: AppData)(
  31. implicit backend: HttpBackend
  32. ) extends AbstractViewController
  33. with LazyLogging
  34. with AppTypes {
  35. @FXML private var submitButton: JFXButton = _
  36. @FXML private var usernameTextField: JFXTextField = _
  37. @FXML private var passwordTextField: JFXPasswordField = _
  38. @FXML private var errorLabel: Label = _
  39. override def didGainVisibilityFirstTime(): Unit = {
  40. super.didGainVisibilityFirstTime()
  41. this.stage.resizable = false
  42. usernameTextField.requestFocus()
  43. submitButton.setOnAction(actionLogin)
  44. // println(something)
  45. val bindings = KeyBindings("usernameTextField", "passwordTextField")
  46. // Expression Binding Example
  47. // bindings.add(
  48. // "usernameTextField",
  49. // "${sf:i18n('personText', _self.usernameTextField(), _self.passwordTextField())})"
  50. // )
  51. val box = new VBox()
  52. val adapter = FXBeanAdapter[Person](this)
  53. // adapter.
  54. val bean = FXBean[Person](Person("twar", "username", "password"))
  55. // bean.
  56. // bean.get
  57. adapter.addBindings(bindings)
  58. adapter.set(bean)
  59. // adapter.addIntConverter("age")
  60. // adapter.hasBeanProperty
  61. // adapter.revert()
  62. // usernameTextField.onKeyPressed = (keyEvent) => {
  63. // if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire()
  64. // }
  65. // submitButton.onKeyPressed = (keyEvent) => {
  66. // if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire()
  67. // }
  68. // passwordTextField.onKeyPressed = (keyEvent) => {
  69. // if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire()
  70. // }
  71. Array(usernameTextField, passwordTextField, submitButton)
  72. .foreach(_.onKeyPressed = (keyEvent) => {
  73. if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire()
  74. })
  75. }
  76. override def didGainVisibility(): Unit = {
  77. usernameTextField.requestFocus()
  78. }
  79. def actionLogin(e: ActionEvent) = {
  80. import org.scalafx.extras._
  81. val inputUserName = usernameTextField.text()
  82. val inputPassword = passwordTextField.text()
  83. // val authenticated =
  84. // inputPassword.equals("password") && inputUserName.equals("hmm")
  85. login(inputUserName, inputPassword) onComplete {
  86. case Success(value) => {
  87. value.foreach(println)
  88. value match {
  89. case Some(token) => {
  90. appData =
  91. appData.copy(user = User(inputUserName, inputPassword, token))
  92. }
  93. case None => {
  94. onFX(errorLabel.text =
  95. "Error logging in - please check your password"
  96. )
  97. logger.warn("Login unsuccessful wrong password")
  98. }
  99. }
  100. // Platform.runLater(() => applicationController.showChatPane())
  101. onFX(applicationController.showChatPane())
  102. }
  103. case Failure(exception) => {
  104. logger.error(s"${exception.getMessage()}")
  105. logger.warn("Login unsuccessful network problem")
  106. onFX {
  107. errorLabel.text = "Error logging in - Please check your network"
  108. applicationController.showChatPane()
  109. }
  110. // onFX(applicationController.showChatPane())
  111. }
  112. }
  113. // if (authenticated) {
  114. // passwordTextField.clear()
  115. // val res = Result(username = inputUserName, password = inputPassword)
  116. // println(res)
  117. // // loginManager.
  118. // applicationController.showChatPane()
  119. // } else {
  120. // logger.error("Login Error")
  121. // }
  122. }
  123. def login(username: String, password: String) = async {
  124. val resp = await(initLogin(username, password))
  125. resp.header("X-AUTH-TOKEN")
  126. }
  127. def initLogin(username: String, password: String) = {
  128. basicRequest.auth
  129. .basic(username, password)
  130. .get(uri"http://localhost:8080/api/chat/get/token")
  131. .send()
  132. }
  133. final case class Result(username: String, password: String)
  134. final case class Person(
  135. id: String,
  136. usernameTextField: String,
  137. passwordTextField: String
  138. )
  139. }