From 39702972d77a0d9b6aee6a0d6709af9a9ac08087 Mon Sep 17 00:00:00 2001 From: onoderis Date: Sun, 3 Sep 2023 20:31:16 +0600 Subject: [PATCH] Refactor --- src/main/kotlin/failchat/ConfigKeys.kt | 15 ++-- src/main/kotlin/failchat/ConfigUtils.kt | 4 +- src/main/kotlin/failchat/Kodein.kt | 2 +- .../kotlin/failchat/chat/ChatMessageSender.kt | 20 ++--- src/main/kotlin/failchat/gui/SettingsFrame.kt | 74 +++++++++---------- .../twitch/ConfigurationTokenContainer.kt | 14 ++-- src/test/kotlin/failchat/Shared.kt | 2 +- 7 files changed, 64 insertions(+), 67 deletions(-) diff --git a/src/main/kotlin/failchat/ConfigKeys.kt b/src/main/kotlin/failchat/ConfigKeys.kt index 5a8bb861..b7d1b1a9 100644 --- a/src/main/kotlin/failchat/ConfigKeys.kt +++ b/src/main/kotlin/failchat/ConfigKeys.kt @@ -1,31 +1,32 @@ package failchat -@Suppress("ClassName") object ConfigKeys { //todo use properties from here - object peka2tv { + object Peka2tv { const val enabled = "peka2tv.enabled" const val channel = "peka2tv.channel" } - object goodgame { + object Goodgame { const val enabled = "goodgame.enabled" const val channel = "goodgame.channel" } - object twitch { + object Twitch { const val enabled = "twitch.enabled" const val channel = "twitch.channel" + const val expiresAt = "twitch.bearer-token-expires-at" + const val token = "twitch.bearer-token" } - object youtube { + object Youtube { const val enabled = "youtube.enabled" const val channel = "youtube.channel" } - object nativeClient { + object NativeClient { private const val prefix = "native-client" const val backgroundColor = "$prefix.background-color" const val coloredNicknames = "$prefix.colored-nicknames" @@ -34,7 +35,7 @@ object ConfigKeys { const val showStatusMessages = "$prefix.show-status-messages" } - object externalClient { + object ExternalClient { private const val prefix = "external-client" const val backgroundColor = "$prefix.background-color" const val coloredNicknames = "$prefix.colored-nicknames" diff --git a/src/main/kotlin/failchat/ConfigUtils.kt b/src/main/kotlin/failchat/ConfigUtils.kt index 642c7a21..91199f37 100644 --- a/src/main/kotlin/failchat/ConfigUtils.kt +++ b/src/main/kotlin/failchat/ConfigUtils.kt @@ -11,6 +11,4 @@ fun Configuration.resetEmoticonsUpdatedTime() { } } -fun getFailchatHomePath(): Path { - return Paths.get(System.getProperty("user.home")).resolve(".failchat") -} +val failchatHomePath: Path = Paths.get(System.getProperty("user.home")).resolve(".failchat") diff --git a/src/main/kotlin/failchat/Kodein.kt b/src/main/kotlin/failchat/Kodein.kt index d19a7ea3..4721747d 100644 --- a/src/main/kotlin/failchat/Kodein.kt +++ b/src/main/kotlin/failchat/Kodein.kt @@ -307,7 +307,7 @@ val kodein = DI.direct { // Etc bind("workingDirectory") with singleton { Paths.get("") } - bind("homeDirectory") with singleton { getFailchatHomePath() } + bind("homeDirectory") with singleton { failchatHomePath } bind("failchatEmoticonsDirectory") with singleton { instance("homeDirectory").resolve("failchat-emoticons") } bind("emoticonCacheDirectory") with singleton { instance("workingDirectory").resolve("emoticons") } bind("emoticonDbFile") with singleton { instance("emoticonCacheDirectory").resolve("emoticons.db") } diff --git a/src/main/kotlin/failchat/chat/ChatMessageSender.kt b/src/main/kotlin/failchat/chat/ChatMessageSender.kt index f3ac8068..428ba809 100644 --- a/src/main/kotlin/failchat/chat/ChatMessageSender.kt +++ b/src/main/kotlin/failchat/chat/ChatMessageSender.kt @@ -130,18 +130,18 @@ class ChatMessageSender( put("clickTransparency", config.getBoolean(ConfigKeys.clickTransparency)) put("showClickTransparencyIcon", config.getBoolean(ConfigKeys.showClickTransparencyIcon)) putObject("nativeClient").apply { - put("backgroundColor", config.getString(ConfigKeys.nativeClient.backgroundColor)) - put("coloredNicknames", config.getBoolean(ConfigKeys.nativeClient.coloredNicknames)) - put("hideMessages", config.getBoolean(ConfigKeys.nativeClient.hideMessages)) - put("hideMessagesAfter", config.getInt(ConfigKeys.nativeClient.hideMessagesAfter)) - put("showStatusMessages", config.getBoolean(ConfigKeys.nativeClient.showStatusMessages)) + put("backgroundColor", config.getString(ConfigKeys.NativeClient.backgroundColor)) + put("coloredNicknames", config.getBoolean(ConfigKeys.NativeClient.coloredNicknames)) + put("hideMessages", config.getBoolean(ConfigKeys.NativeClient.hideMessages)) + put("hideMessagesAfter", config.getInt(ConfigKeys.NativeClient.hideMessagesAfter)) + put("showStatusMessages", config.getBoolean(ConfigKeys.NativeClient.showStatusMessages)) } putObject("externalClient").apply { - put("backgroundColor", config.getString(ConfigKeys.externalClient.backgroundColor)) - put("coloredNicknames", config.getBoolean(ConfigKeys.externalClient.coloredNicknames)) - put("hideMessages", config.getBoolean(ConfigKeys.externalClient.hideMessages)) - put("hideMessagesAfter", config.getInt(ConfigKeys.externalClient.hideMessagesAfter)) - put("showStatusMessages", config.getBoolean(ConfigKeys.externalClient.showStatusMessages)) + put("backgroundColor", config.getString(ConfigKeys.ExternalClient.backgroundColor)) + put("coloredNicknames", config.getBoolean(ConfigKeys.ExternalClient.coloredNicknames)) + put("hideMessages", config.getBoolean(ConfigKeys.ExternalClient.hideMessages)) + put("hideMessagesAfter", config.getInt(ConfigKeys.ExternalClient.hideMessagesAfter)) + put("showStatusMessages", config.getBoolean(ConfigKeys.ExternalClient.showStatusMessages)) } putObject("enabledOrigins").apply { COUNTABLE_ORIGINS.forEach { origin -> diff --git a/src/main/kotlin/failchat/gui/SettingsFrame.kt b/src/main/kotlin/failchat/gui/SettingsFrame.kt index 21110b0c..13bb76c6 100644 --- a/src/main/kotlin/failchat/gui/SettingsFrame.kt +++ b/src/main/kotlin/failchat/gui/SettingsFrame.kt @@ -217,24 +217,24 @@ class SettingsFrame( } fun updateSettingsValues() { - peka2tvChannel.text = config.getString(ConfigKeys.peka2tv.channel) - goodgameChannel.text = config.getString(ConfigKeys.goodgame.channel) - twitchChannel.text = config.getString(ConfigKeys.twitch.channel) - youtubeChannel.text = config.getString(ConfigKeys.youtube.channel) + peka2tvChannel.text = config.getString(ConfigKeys.Peka2tv.channel) + goodgameChannel.text = config.getString(ConfigKeys.Goodgame.channel) + twitchChannel.text = config.getString(ConfigKeys.Twitch.channel) + youtubeChannel.text = config.getString(ConfigKeys.Youtube.channel) - config.getBoolean(ConfigKeys.peka2tv.enabled).let { + config.getBoolean(ConfigKeys.Peka2tv.enabled).let { peka2tvEnabled.isSelected = it peka2tvChannel.configureChannelField(it) } - config.getBoolean(ConfigKeys.goodgame.enabled).let { + config.getBoolean(ConfigKeys.Goodgame.enabled).let { goodgameEnabled.isSelected = it goodgameChannel.configureChannelField(it) } - config.getBoolean(ConfigKeys.twitch.enabled).let { + config.getBoolean(ConfigKeys.Twitch.enabled).let { twitchEnabled.isSelected = it twitchChannel.configureChannelField(it) } - config.getBoolean(ConfigKeys.youtube.enabled).let { + config.getBoolean(ConfigKeys.Youtube.enabled).let { youtubeEnabled.isSelected = it youtubeChannel.configureChannelField(it) } @@ -256,17 +256,17 @@ class SettingsFrame( zoomPercent.text = config.getInt(ConfigKeys.zoomPercent).toString() deletedMessagePlaceholder.text = config.getString(ConfigKeys.deletedMessagePlaceholder) - nativeBgColorPicker.value = Color.web(config.getString(ConfigKeys.nativeClient.backgroundColor)) - coloredNicknamesNative.isSelected = config.getBoolean(ConfigKeys.nativeClient.coloredNicknames) - hideMessagesNative.isSelected = config.getBoolean(ConfigKeys.nativeClient.hideMessages) - hideMessagesNativeAfter.text = config.getInt(ConfigKeys.nativeClient.hideMessagesAfter).toString() - showStatusMessagesNative.isSelected = config.getBoolean(ConfigKeys.nativeClient.showStatusMessages) + nativeBgColorPicker.value = Color.web(config.getString(ConfigKeys.NativeClient.backgroundColor)) + coloredNicknamesNative.isSelected = config.getBoolean(ConfigKeys.NativeClient.coloredNicknames) + hideMessagesNative.isSelected = config.getBoolean(ConfigKeys.NativeClient.hideMessages) + hideMessagesNativeAfter.text = config.getInt(ConfigKeys.NativeClient.hideMessagesAfter).toString() + showStatusMessagesNative.isSelected = config.getBoolean(ConfigKeys.NativeClient.showStatusMessages) - externalBgColorPicker.value = Color.web(config.getString(ConfigKeys.externalClient.backgroundColor)) - coloredNicknamesExternal.isSelected = config.getBoolean(ConfigKeys.externalClient.coloredNicknames) - hideMessagesExternal.isSelected = config.getBoolean(ConfigKeys.externalClient.hideMessages) - hideMessagesExternalAfter.text = config.getInt(ConfigKeys.externalClient.hideMessagesAfter).toString() - showStatusMessagesExternal.isSelected = config.getBoolean(ConfigKeys.externalClient.showStatusMessages) + externalBgColorPicker.value = Color.web(config.getString(ConfigKeys.ExternalClient.backgroundColor)) + coloredNicknamesExternal.isSelected = config.getBoolean(ConfigKeys.ExternalClient.coloredNicknames) + hideMessagesExternal.isSelected = config.getBoolean(ConfigKeys.ExternalClient.hideMessages) + hideMessagesExternalAfter.text = config.getInt(ConfigKeys.ExternalClient.hideMessagesAfter).toString() + showStatusMessagesExternal.isSelected = config.getBoolean(ConfigKeys.ExternalClient.showStatusMessages) opacitySlider.value = config.getDouble(ConfigKeys.opacity) @@ -280,15 +280,15 @@ class SettingsFrame( private fun saveSettingsValues() { //todo use loop for origins - config.setProperty(ConfigKeys.peka2tv.channel, peka2tvChannel.text) - config.setProperty(ConfigKeys.goodgame.channel, goodgameChannel.text) - config.setProperty(ConfigKeys.twitch.channel, twitchChannel.text) - config.setProperty(ConfigKeys.youtube.channel, youtubeChannel.text) + config.setProperty(ConfigKeys.Peka2tv.channel, peka2tvChannel.text) + config.setProperty(ConfigKeys.Goodgame.channel, goodgameChannel.text) + config.setProperty(ConfigKeys.Twitch.channel, twitchChannel.text) + config.setProperty(ConfigKeys.Youtube.channel, youtubeChannel.text) - config.setProperty(ConfigKeys.peka2tv.enabled, peka2tvEnabled.isSelected) - config.setProperty(ConfigKeys.goodgame.enabled, goodgameEnabled.isSelected) - config.setProperty(ConfigKeys.twitch.enabled, twitchEnabled.isSelected) - config.setProperty(ConfigKeys.youtube.enabled, youtubeEnabled.isSelected) + config.setProperty(ConfigKeys.Peka2tv.enabled, peka2tvEnabled.isSelected) + config.setProperty(ConfigKeys.Goodgame.enabled, goodgameEnabled.isSelected) + config.setProperty(ConfigKeys.Twitch.enabled, twitchEnabled.isSelected) + config.setProperty(ConfigKeys.Youtube.enabled, youtubeEnabled.isSelected) config.setProperty(ConfigKeys.skin, skin.value.name) config.setProperty(ConfigKeys.frame, frame.isSelected) @@ -309,17 +309,17 @@ class SettingsFrame( config.setProperty(ConfigKeys.zoomPercent, parseZoomPercent(zoomPercent.text)) config.setProperty(ConfigKeys.deletedMessagePlaceholder, deletedMessagePlaceholder.text) - config.setProperty(ConfigKeys.nativeClient.backgroundColor, nativeBgColorPicker.value.toHexFormat()) - config.setProperty(ConfigKeys.nativeClient.coloredNicknames, coloredNicknamesNative.isSelected) - config.setProperty(ConfigKeys.nativeClient.hideMessages, hideMessagesNative.isSelected) - config.setProperty(ConfigKeys.nativeClient.hideMessagesAfter, parseHideMessagesAfter(hideMessagesNativeAfter.text)) - config.setProperty(ConfigKeys.nativeClient.showStatusMessages, showStatusMessagesNative.isSelected) - - config.setProperty(ConfigKeys.externalClient.backgroundColor, externalBgColorPicker.value.toHexFormat()) - config.setProperty(ConfigKeys.externalClient.coloredNicknames, coloredNicknamesExternal.isSelected) - config.setProperty(ConfigKeys.externalClient.hideMessages, hideMessagesExternal.isSelected) - config.setProperty(ConfigKeys.externalClient.hideMessagesAfter, parseHideMessagesAfter(hideMessagesExternalAfter.text)) - config.setProperty(ConfigKeys.externalClient.showStatusMessages, showStatusMessagesExternal.isSelected) + config.setProperty(ConfigKeys.NativeClient.backgroundColor, nativeBgColorPicker.value.toHexFormat()) + config.setProperty(ConfigKeys.NativeClient.coloredNicknames, coloredNicknamesNative.isSelected) + config.setProperty(ConfigKeys.NativeClient.hideMessages, hideMessagesNative.isSelected) + config.setProperty(ConfigKeys.NativeClient.hideMessagesAfter, parseHideMessagesAfter(hideMessagesNativeAfter.text)) + config.setProperty(ConfigKeys.NativeClient.showStatusMessages, showStatusMessagesNative.isSelected) + + config.setProperty(ConfigKeys.ExternalClient.backgroundColor, externalBgColorPicker.value.toHexFormat()) + config.setProperty(ConfigKeys.ExternalClient.coloredNicknames, coloredNicknamesExternal.isSelected) + config.setProperty(ConfigKeys.ExternalClient.hideMessages, hideMessagesExternal.isSelected) + config.setProperty(ConfigKeys.ExternalClient.hideMessagesAfter, parseHideMessagesAfter(hideMessagesExternalAfter.text)) + config.setProperty(ConfigKeys.ExternalClient.showStatusMessages, showStatusMessagesExternal.isSelected) config.setProperty(ConfigKeys.ignore, ignoreList.text.split("\n").dropLastWhile { it.isEmpty() }.toTypedArray()) } diff --git a/src/main/kotlin/failchat/twitch/ConfigurationTokenContainer.kt b/src/main/kotlin/failchat/twitch/ConfigurationTokenContainer.kt index 226f278f..26cf335a 100644 --- a/src/main/kotlin/failchat/twitch/ConfigurationTokenContainer.kt +++ b/src/main/kotlin/failchat/twitch/ConfigurationTokenContainer.kt @@ -1,5 +1,6 @@ package failchat.twitch +import failchat.ConfigKeys import mu.KLogging import org.apache.commons.configuration2.Configuration import java.time.Instant @@ -8,13 +9,10 @@ class ConfigurationTokenContainer( private val config: Configuration ) : HelixTokenContainer { - private companion object : KLogging() { - const val expiresAtKey = "twitch.bearer-token-expires-at" - const val tokenKey = "twitch.bearer-token" - } + private companion object : KLogging() override fun getToken(): HelixApiToken? { - val expiresAt = Instant.ofEpochMilli(config.getLong(expiresAtKey, 0)) + val expiresAt = Instant.ofEpochMilli(config.getLong(ConfigKeys.Twitch.expiresAt, 0)) val now = Instant.now() if (now > expiresAt) { return null @@ -24,8 +22,8 @@ class ConfigurationTokenContainer( } override fun setToken(token: HelixApiToken) { - config.setProperty(tokenKey, token.value) - config.setProperty(expiresAtKey, token.ttl.toEpochMilli()) - logger.info("Helix token was saved to configuration at '$tokenKey'") + config.setProperty(ConfigKeys.Twitch.token, token.value) + config.setProperty(ConfigKeys.Twitch.expiresAt, token.ttl.toEpochMilli()) + logger.info("Helix token was saved to configuration") } } diff --git a/src/test/kotlin/failchat/Shared.kt b/src/test/kotlin/failchat/Shared.kt index cbbd0ad9..0b3203a0 100644 --- a/src/test/kotlin/failchat/Shared.kt +++ b/src/test/kotlin/failchat/Shared.kt @@ -18,6 +18,6 @@ val ktorClient = HttpClient(OkHttp) { } -val userHomeConfig: Configuration by lazy { ConfigLoader(getFailchatHomePath()).load() } +val userHomeConfig: Configuration by lazy { ConfigLoader(failchatHomePath).load() } val defaultConfig: Configuration by lazy { loadDefaultConfig() } val testObjectMapper: ObjectMapper = objectMapper()