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.
118 lines
3.4 KiB
118 lines
3.4 KiB
package wow.doge.chatto
|
|
|
|
import javax.enterprise.context.ApplicationScoped
|
|
import javax.enterprise.inject.Produces
|
|
import javax.inject.Named
|
|
import com.sfxcode.sapphire.core.controller.DefaultWindowController
|
|
// import org.asynchttpclient.Dsl._
|
|
import wow.doge.chatto.controller.MainViewController
|
|
import sttp.client.asynchttpclient.future.AsyncHttpClientFutureBackend
|
|
import scala.concurrent.ExecutionContext.Implicits.global
|
|
import scala.async.Async.{async, await}
|
|
// import org.json4s.native.JsonMethods._
|
|
|
|
import org.scalafx.extras._
|
|
import javax.inject._
|
|
import javafx.application.Platform
|
|
@Named
|
|
@ApplicationScoped
|
|
class ApplicationController extends DefaultWindowController {
|
|
// import ApplicationController._
|
|
|
|
lazy val mainViewController = getController[MainViewController]()
|
|
private implicit lazy val serialization = org.json4s.jackson.Serialization
|
|
private implicit lazy val backend = AsyncHttpClientFutureBackend()
|
|
|
|
// override def width: Int = 400
|
|
|
|
// @Inject
|
|
// private var appDataHandler: AppDataHandler = _
|
|
|
|
def applicationDidLaunch() = {
|
|
logger.info("start " + this)
|
|
applicationEnvironment.loadResourceBundle("bundles/application")
|
|
replaceSceneContent(mainViewController.loginController)
|
|
}
|
|
|
|
@Produces
|
|
def applicationName: ApplicationName = {
|
|
ApplicationName(configStringValue("application.name"))
|
|
}
|
|
|
|
@Produces
|
|
def httpBackend = backend
|
|
|
|
// @Produces
|
|
// def encryptionService: EncryptionService = EncryptionServiceImpl()
|
|
|
|
def replacePrimarySceneContent(): Unit = {
|
|
// Styling
|
|
reloadStyles()
|
|
// Resources
|
|
applicationEnvironment.clearResourceBundleCache()
|
|
applicationEnvironment.loadResourceBundle("bundles/application")
|
|
// FXML
|
|
val newMainViewController = getController[MainViewController]()
|
|
replaceSceneContent(newMainViewController)
|
|
}
|
|
|
|
// def testListener(@Observes event: SceneControllerWillChangeEvent) = {
|
|
// logger.info("test success 1")
|
|
// // logger.info(s"${event.newController}")
|
|
// }
|
|
|
|
// def testListener2(@Observes event: SceneControllerDidChangeEvent) = {
|
|
// logger.info("test success 2")
|
|
// // logger.info(s"${event.newController}")
|
|
// }
|
|
|
|
override def applicationWillStop(): Unit = async {
|
|
super.applicationWillStop()
|
|
println("stopping")
|
|
await(httpBackend.close())
|
|
Platform.exit()
|
|
System.exit(0)
|
|
}
|
|
|
|
def showLoginPane() = onFX {
|
|
// offFX { appDataHandler.clearCredentials() }
|
|
replaceSceneContent(mainViewController.loginController)
|
|
}
|
|
|
|
def logout() = onFX {
|
|
val newMainViewController = getController[MainViewController]()
|
|
replaceSceneContent(mainViewController.loginController)
|
|
}
|
|
|
|
def showChatPane(): Unit = onFX {
|
|
replaceSceneContent(mainViewController)
|
|
mainViewController.showChatController()
|
|
}
|
|
}
|
|
|
|
final case class ApplicationName(name: String)
|
|
final case class UserCredentials(
|
|
username: String,
|
|
token: String
|
|
)
|
|
object UserCredentials {
|
|
def empty = UserCredentials("empty", "empty")
|
|
}
|
|
|
|
@ApplicationScoped
|
|
class AppDataHandler {
|
|
private var _appData: AppData = AppData(UserCredentials.empty, "empty")
|
|
|
|
private def appData_=(appData: AppData) = synchronized { _appData = appData }
|
|
|
|
def appData = _appData
|
|
|
|
def updateCredentials(credentials: UserCredentials): Unit = offFX {
|
|
appData = appData.copy(credentials = credentials)
|
|
}
|
|
|
|
def clearCredentials() = {
|
|
appData = appData.copy(credentials = UserCredentials.empty)
|
|
}
|
|
}
|
|
final case class AppData(credentials: UserCredentials, sumth: String)
|