How to handle auth while showing loading screen?

时间:2018-09-18 19:45:43

标签: kotlin tornadofx

How to show a loading screen and handle authorization at the same time?

Can I switch to LoadingView and return back to AuthView in AuthController or I need to move auth logic to 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))
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您将在同一脉冲中将AuthView替换为LoadingView,然后将LoadingView替换为MainView,这样就无法满足您的需求。通常,在评估身份验证信息之前,您需要在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)
    }
}

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