显示加载屏幕时如何处理身份验证?



如何同时显示加载屏幕和处理授权?

我可以切换到LoadingView并在AuthController中返回到AuthView还是需要将身份验证逻辑移动到LoadingController

class AuthController : Controller() {
val authView : AuthView by inject()
val loadingView : LoadingView by inject()
fun tryAuth(login: String, password: String) {
runAsync {
login == "admin" && password == "admin"
} ui { successful ->
authView.replaceWith(loadingView, ViewTransition.Fade(0.5.seconds))
if (successful) {
// doesn't work
loadingView.replaceWith(MainView::class, ViewTransition.Metro(0.5.seconds))
} else {
// doesn't work
loadingView.replaceWith(AuthView::class, ViewTransition.Fade(0.5.seconds))
}
}
}
}

你用LoadingView替换AuthView,然后用同一脉冲中的MainView替换LoadingView,所以这不会给你你想要的。通常,在评估身份验证信息之前,需要更改为 UI 线程上的LoadingView。使用此方法,您的代码可以工作,但它可能不是您想要的。

class AuthController : Controller() {
val authView : AuthView by inject()
val loadingView : LoadingView by inject()
fun tryAuth(login: String, password: String) {
authView.replaceWith(loadingView, ViewTransition.Fade(0.5.seconds))
runAsync {
// Simulate db access or http call
Thread.sleep(2000)
login == "admin" && password == "admin"
} ui { successful ->
if (successful) {
// doesn't work
loadingView.replaceWith(MainView::class, ViewTransition.Metro(0.5.seconds))
} else {
// doesn't work
loadingView.replaceWith(AuthView::class, ViewTransition.Fade(0.5.seconds))
}
}
}
}
class AuthView : View("Auth") {
val authController: AuthController by inject()
override val root = stackpane {
button(title).action {
authController.tryAuth("admin", "admin")
}
}
}
class LoadingView : View("Loading...") {
override val root = stackpane {
label(title)
}
}
class MainView : View("Main View") {
override val root = stackpane {
label(title)
}
}

您必须记住,替换视图不会调整窗口大小(尽管您可以访问舞台并要求它调整为当前视图的大小(,因此最好在单独的窗口中打开每个视图。

最新更新