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

How to handle auth while showing loading screen?

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

我可以在 AuthController 中切换到 LoadingView 和 return 回到 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)
    }
}

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