Make theme persistant

This commit is contained in:
Lars Westermann 2019-05-31 22:57:10 +02:00
parent f90a4dddba
commit 0288150c6e
Signed by: lars.westermann
GPG key ID: 9D417FA5BB9D5E1D

View file

@ -62,7 +62,7 @@ class MainTemplate(private val theme: Theme) : Template<HTML> {
div("footer-theme") { div("footer-theme") {
for (it in Theme.values()) { for (it in Theme.values()) {
val name = it.name.toLowerCase() val name = it.name.toLowerCase()
a("?theme=$name", classes = if (theme == it) "selected" else "") { a("?theme=${it.name}", classes = if (theme == it) "selected" else "") {
id = "theme-$name" id = "theme-$name"
+name.capitalize() +name.capitalize()
} }
@ -78,7 +78,7 @@ enum class Theme {
LIGHT, DARK LIGHT, DARK
} }
private fun String?.toTheme() = this?.toUpperCase()?.let { str -> private fun String?.toTheme() = this?.let { str ->
Theme.values().find { str == it.name } Theme.values().find { str == it.name }
} ?: Theme.LIGHT } ?: Theme.LIGHT
@ -86,7 +86,12 @@ suspend fun PipelineContext<Unit, ApplicationCall>.respondMain(body: MainTemplat
val param = call.request.queryParameters["theme"] val param = call.request.queryParameters["theme"]
if (param != null) { if (param != null) {
call.response.cookies.append("theme", param.toTheme().name.toLowerCase()) call.response.cookies.append(
name = "theme",
value = param.toTheme().name,
maxAge = Int.MAX_VALUE,
path = "/"
)
call.respondRedirect(call.request.path()) call.respondRedirect(call.request.path())
} else { } else {
call.respondHtmlTemplate( call.respondHtmlTemplate(