Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
onoderis committed Sep 3, 2023
1 parent 94aec5c commit 3970297
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 67 deletions.
15 changes: 8 additions & 7 deletions src/main/kotlin/failchat/ConfigKeys.kt
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/failchat/ConfigUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion src/main/kotlin/failchat/Kodein.kt
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ val kodein = DI.direct {

// Etc
bind<Path>("workingDirectory") with singleton { Paths.get("") }
bind<Path>("homeDirectory") with singleton { getFailchatHomePath() }
bind<Path>("homeDirectory") with singleton { failchatHomePath }
bind<Path>("failchatEmoticonsDirectory") with singleton { instance<Path>("homeDirectory").resolve("failchat-emoticons") }
bind<Path>("emoticonCacheDirectory") with singleton { instance<Path>("workingDirectory").resolve("emoticons") }
bind<Path>("emoticonDbFile") with singleton { instance<Path>("emoticonCacheDirectory").resolve("emoticons.db") }
Expand Down
20 changes: 10 additions & 10 deletions src/main/kotlin/failchat/chat/ChatMessageSender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
74 changes: 37 additions & 37 deletions src/main/kotlin/failchat/gui/SettingsFrame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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())
}
Expand Down
14 changes: 6 additions & 8 deletions src/main/kotlin/failchat/twitch/ConfigurationTokenContainer.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package failchat.twitch

import failchat.ConfigKeys
import mu.KLogging
import org.apache.commons.configuration2.Configuration
import java.time.Instant
Expand All @@ -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
Expand All @@ -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")
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/failchat/Shared.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 3970297

Please sign in to comment.