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.
 
 
 

144 lines
4.4 KiB

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.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
import javafx.scene.layout.StackPane
import com.jfoenix.controls.JFXSpinner
class LoginController @Inject() (
userService: UserService,
appDataHandler: AppDataHandler
)(
implicit backend: HttpBackend
) extends AbstractViewController
with LazyLogging
with AppTypes
with BeanConversions {
@FXML 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)
val bindings = KeyBindings("username", "password")
// Expression Binding Example
// bindings.add(
// "usernameTextField",
// "${sf:i18n('personText', _self.usernameTextField(), _self.passwordTextField())})"
// )
val box = new VBox()
val adapter = FXBeanAdapter[Person](this)
val bean = FXBean[Person](Person("0", "username", "password"))
adapter.addBindings(bindings)
adapter.set(bean)
// adapter.addIntConverter("age")
// adapter.hasBeanProperty
// adapter.revert()
Array(usernameTextField, passwordTextField, submitButton)
.foreach(_.onKeyPressed = (keyEvent) => {
if (keyEvent.getCode() == KeyCode.ENTER) submitButton.fire()
})
}
override def didGainVisibility(): Unit = {
usernameTextField.requestFocus()
}
def actionLogin() = {
val inputUserName = usernameTextField.text()
val inputPassword = passwordTextField.text()
submitButton.disable = true
login(inputUserName, inputPassword) onComplete {
case Success(maybeToken) => {
maybeToken match {
case Some(token) =>
async {
val credentials =
UserCredentials(inputUserName, inputPassword, token)
appDataHandler.updateCredentials(credentials)
updateErrorLabel("")
applicationController.showChatPane()
onFX { submitButton.disable = false }
}
case None => {
updateErrorLabel("Error logging in - please check your password")
logger.warn("Login unsuccessful wrong password")
onFX { submitButton.disable = false }
}
}
}
case Failure(exception) => {
logger.error(s"${exception.getMessage()}")
logger.warn("Login unsuccessful network problem")
updateErrorLabel("Error logging in - Please check your network")
onFX { submitButton.disable = false }
}
}
}
def updateErrorLabel(message: String) = onFX {
errorLabel.text = message
}
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,
username: String,
password: String
)
}