Created
June 25, 2017 16:57
-
-
Save edvin/8157208f7b7f4641c688c4bfb972d1d7 to your computer and use it in GitHub Desktop.
TornadoFX Login App - https://www.youtube.com/watch?v=Jyuf4xn0oy4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loginapp.app | |
import loginapp.views.LoginScreen | |
import tornadofx.App | |
class LoginApp : App(LoginScreen::class) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loginapp.controllers | |
import javafx.beans.property.SimpleStringProperty | |
import loginapp.models.UserModel | |
import loginapp.views.LoginScreen | |
import loginapp.views.WelcomeScreen | |
import tornadofx.* | |
class LoginController : Controller() { | |
val statusProperty = SimpleStringProperty("") | |
var status by statusProperty | |
val api: Rest by inject() | |
val user: UserModel by inject() | |
init { | |
api.baseURI = "https://api.github.com/" | |
} | |
fun login(username: String, password: String) { | |
runLater { status = "" } | |
api.setBasicAuth(username, password) | |
val response = api.get("user") | |
val json = response.one() | |
runLater { | |
if (response.ok()) { | |
user.item = json.toModel() | |
find(LoginScreen::class).replaceWith(WelcomeScreen::class, sizeToScene = true, centerOnScreen = true) | |
} else { | |
status = json.string("message") ?: "Login failed" | |
} | |
} | |
} | |
fun logout() { | |
user.item = null | |
primaryStage.uiComponent<UIComponent>()?.replaceWith(LoginScreen::class, sizeToScene = true, centerOnScreen = true) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loginapp.views | |
import javafx.beans.property.SimpleStringProperty | |
import javafx.geometry.Orientation | |
import javafx.scene.paint.Color | |
import javafx.scene.text.FontWeight | |
import loginapp.controllers.LoginController | |
import tornadofx.* | |
class LoginScreen : View("Login") { | |
val model = ViewModel() | |
val username = model.bind { SimpleStringProperty() } | |
val password = model.bind { SimpleStringProperty() } | |
val loginController: LoginController by inject() | |
override val root = form { | |
fieldset(labelPosition = Orientation.VERTICAL) { | |
field("Username") { | |
textfield(username).required() | |
} | |
field("Password") { | |
passwordfield(password).required() | |
} | |
button("Log in") { | |
enableWhen(model.valid) | |
isDefaultButton = true | |
useMaxWidth = true | |
action { | |
runAsyncWithProgress { | |
loginController.login(username.value, password.value) | |
} | |
} | |
} | |
} | |
label(loginController.statusProperty) { | |
style { | |
paddingTop = 10 | |
textFill = Color.RED | |
fontWeight = FontWeight.BOLD | |
} | |
} | |
} | |
override fun onDock() { | |
username.value = "" | |
password.value = "" | |
model.clearDecorators() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loginapp.models | |
import javafx.beans.property.SimpleStringProperty | |
import tornadofx.* | |
import javax.json.JsonObject | |
class User : JsonModel { | |
val nameProperty = SimpleStringProperty() | |
var name by nameProperty | |
override fun updateModel(json: JsonObject) { | |
with (json) { | |
name = string("name") | |
} | |
} | |
} | |
class UserModel : ItemViewModel<User>() { | |
val name = bind(User::nameProperty) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loginapp.views | |
import javafx.geometry.Pos | |
import javafx.scene.text.FontWeight | |
import loginapp.controllers.LoginController | |
import loginapp.models.UserModel | |
import tornadofx.* | |
class WelcomeScreen : View("Welcome") { | |
val user: UserModel by inject() | |
val loginController: LoginController by inject() | |
override val root = vbox(10) { | |
setPrefSize(800.0, 600.0) | |
alignment = Pos.CENTER | |
label(user.name) { | |
style { | |
fontWeight = FontWeight.BOLD | |
fontSize = 24.px | |
} | |
} | |
button("Logout").action(loginController::logout) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment