Skip to content

Commit

Permalink
feat: override toString() method of custom config types
Browse files Browse the repository at this point in the history
So they will perform like other Java / Kotlin type when calling `toString()`
  • Loading branch information
WhiredPlanck committed Apr 25, 2024
1 parent cd91ded commit d11dcf8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Keyboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Keyboard() {
keyboardConfig!!["keys"]!!.configList
.map {
it!!.configMap.entries.associate { (k, v) ->
k to v!!.configValue.getString()
k to v!!
}
}
horizontalGap =
Expand Down Expand Up @@ -425,7 +425,7 @@ class Keyboard() {
Configuration.ORIENTATION_LANDSCAPE -> keyboardHeightLand.takeIf { it > 0 } ?: keyboardHeight
else -> keyboardHeight
}
return appContext.dp(value).toInt()
return appContext.dp(value)
}

private fun getKeyboardHeightFromKeyboardConfig(keyboardConfig: Map<String, Any?>?): Int {
Expand Down
7 changes: 1 addition & 6 deletions app/src/main/java/com/osfans/trime/util/CollectionUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package com.osfans.trime.util

import com.osfans.trime.util.config.ConfigValue

object CollectionUtils {
@JvmStatic
fun <K, V> getOrDefault(
Expand Down Expand Up @@ -42,10 +40,7 @@ object CollectionUtils {
): String {
if (map.isNullOrEmpty() || key.isEmpty()) return defValue
val v = obtainValue(map, key)
return when (v) {
is ConfigValue? -> v?.getString()
else -> v?.toString()
} ?: defValue
return v?.toString() ?: defValue
}

@JvmStatic
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/com/osfans/trime/util/config/ConfigTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract class ConfigItem(val node: YamlNode) {
item: ConfigItem,
expectedType: String,
): Nothing {
throw IllegalArgumentException("Expected element to be $expectedType bus is ${item::class.simpleName}")
throw IllegalArgumentException("Expected element to be $expectedType but is ${item::class.simpleName}")
}
}

Expand All @@ -75,6 +75,8 @@ class ConfigValue(private val scalar: YamlScalar) : ConfigItem(scalar) {
override fun isEmpty() = scalar.content.isEmpty()

override fun contentToString(): String = scalar.contentToString()

override fun toString(): String = scalar.content
}

/** The wrapper of [YamlList] */
Expand Down Expand Up @@ -111,6 +113,9 @@ class ConfigList(private val list: YamlList) : ConfigItem(list), List<ConfigItem
override fun indexOf(element: ConfigItem?): Int = items.indexOf(element)

fun getValue(index: Int) = get(index)?.configValue

// just like other Java / Kotlin collection type
override fun toString(): String = items.joinToString(prefix = "[", postfix = "]")
}

class ConfigMap(private val map: YamlMap) : ConfigItem(map), Map<String, ConfigItem?> {
Expand Down Expand Up @@ -140,4 +145,7 @@ class ConfigMap(private val map: YamlMap) : ConfigItem(map), Map<String, ConfigI
override operator fun get(key: String): ConfigItem? = entries.firstOrNull { it.key == key }?.value

fun getValue(key: String): ConfigValue? = get(key)?.configValue

// just like other Java / Kotlin map type
override fun toString(): String = entries.joinToString(prefix = "{", postfix = "}") { (key, value) -> "$key=$value" }
}

0 comments on commit d11dcf8

Please sign in to comment.