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.

102 lines
3.1 KiB

4 years ago
  1. package wow.doge.chatto
  2. import javax.enterprise.context.ApplicationScoped
  3. import javax.enterprise.inject.Produces
  4. import javax.inject.Named
  5. import com.typesafe.config.ConfigFactory
  6. import com.sfxcode.sapphire.core.controller.DefaultWindowController
  7. // import org.asynchttpclient.Dsl._
  8. import wow.doge.chatto.controller.MainViewController
  9. import sttp.client.asynchttpclient.future.AsyncHttpClientFutureBackend
  10. import sttp.client._
  11. import scala.concurrent.ExecutionContext.Implicits.global
  12. import scala.async.Async.{async, await}
  13. import sttp.client.json4s._
  14. import org.json4s._
  15. import org.json4s.native.JsonMethods._
  16. import org.json4s.JsonDSL._
  17. import scala.util.Success
  18. import scala.util.Failure
  19. import com.softwaremill.quicklens._
  20. @Named
  21. @ApplicationScoped
  22. class ApplicationController extends DefaultWindowController {
  23. // import ApplicationController._
  24. lazy val mainViewController = getController[MainViewController]()
  25. private implicit lazy val serialization = org.json4s.native.Serialization
  26. private implicit lazy val backend = AsyncHttpClientFutureBackend()
  27. // override def width: Int = 400
  28. def applicationDidLaunch() = {
  29. logger.info("start " + this)
  30. applicationEnvironment.loadResourceBundle("bundles/application")
  31. replaceSceneContent(mainViewController)
  32. }
  33. @Produces
  34. def applicationName: ApplicationName = {
  35. ApplicationName(configStringValue("application.name"))
  36. }
  37. @Produces var appData: AppData = synchronized {
  38. AppData(User.empty, "")
  39. }
  40. @Produces
  41. def httpBackend = backend
  42. def replacePrimarySceneContent(): Unit = {
  43. // Styling
  44. reloadStyles()
  45. // Resources
  46. applicationEnvironment.clearResourceBundleCache()
  47. applicationEnvironment.loadResourceBundle("bundles/application")
  48. // FXML
  49. val newMainViewController = getController[MainViewController]()
  50. replaceSceneContent(newMainViewController)
  51. }
  52. override def applicationWillStop(): Unit = async {
  53. super.applicationWillStop()
  54. println("stopping")
  55. await(httpBackend.close())
  56. System.exit(0)
  57. }
  58. def showLoginPane() = {
  59. appData = appData.copy(user = User.empty)
  60. appData = appData.modify(_.user).using(_ => User.empty)
  61. appData = appData.modify(_.user.username).using(_ => "")
  62. replaceSceneContent(mainViewController.loginController)
  63. }
  64. def showChatPane(): Unit = {
  65. // import org.scalafx.extras._
  66. replaceSceneContent(mainViewController.chatController, true)
  67. // httpBackend.send(basicRequest.get(uri""))
  68. // val willBeResponse = basicRequest
  69. // .get(uri"https://httpbin.org/get")
  70. // .response(asJson[HttpBinResponse])
  71. // .send()
  72. // async {
  73. // val r = await { willBeResponse }
  74. // r.body.map(println)
  75. // }
  76. // willBeResponse onComplete {
  77. // case Success(x) => { x.body }
  78. // case Failure(x) => {}
  79. // }
  80. // val body = for {
  81. // r <- willBeResponse
  82. // } yield (r.body)
  83. }
  84. }
  85. final case class ApplicationName(name: String)
  86. final case class User(username: String, password: String, token: String)
  87. object User {
  88. def empty = User("", "", "")
  89. }
  90. final case class AppData(user: User, sumth: String)