Files
wekeyT9/app/src/main/java/net/mezimmah/wkt9/keypad/KeyCommandResolver.kt

89 lines
3.0 KiB
Kotlin

package net.mezimmah.wkt9.keypad
class KeyCommandResolver (
private val onShort: HashMap<Key, Command> = HashMap(mapOf()),
private val onLong: HashMap<Key, Command> = HashMap(mapOf()),
private val afterShort: HashMap<Key, Command> = HashMap(mapOf()),
private val afterLong: HashMap<Key, Command> = HashMap(mapOf()),
private val onRepeat: HashMap<Key, Command> = HashMap(mapOf()),
private val parent: KeyCommandResolver? = null
) {
fun getCommand(key: Key, longPress: Boolean = false, after: Boolean = false, repeat: Int = 0): Command? {
val command = when {
repeat > 0 -> onRepeat[key]
(longPress && after) -> afterLong[key]
(longPress) -> onLong[key]
(after) -> afterShort[key]
else -> onShort[key]
}
return when (command) {
null -> parent?.getCommand(key, longPress, after)
else -> command
}
}
companion object {
fun getBasic(): KeyCommandResolver {
return KeyCommandResolver(
onShort = HashMap(mapOf(
Key.BACK to Command.BACK,
Key.LEFT to Command.LEFT,
Key.RIGHT to Command.RIGHT,
Key.UP to Command.NAVIGATE,
Key.DOWN to Command.NAVIGATE,
Key.STAR to Command.DELETE,
Key.SELECT to Command.SELECT,
Key.FN to Command.FN
)),
onLong = HashMap(mapOf(
Key.N0 to Command.NUMBER,
Key.N1 to Command.NUMBER,
Key.N2 to Command.NUMBER,
Key.N3 to Command.NUMBER,
Key.N4 to Command.NUMBER,
Key.N5 to Command.NUMBER,
Key.N6 to Command.NUMBER,
Key.N7 to Command.NUMBER,
Key.N8 to Command.NUMBER,
Key.N9 to Command.NUMBER,
Key.POUND to Command.SWITCH_MODE,
Key.SELECT to Command.RECORD
)),
afterShort = HashMap(mapOf(
Key.N0 to Command.SPACE,
Key.N1 to Command.CHARACTER,
Key.N2 to Command.CHARACTER,
Key.N3 to Command.CHARACTER,
Key.N4 to Command.CHARACTER,
Key.N5 to Command.CHARACTER,
Key.N6 to Command.CHARACTER,
Key.N7 to Command.CHARACTER,
Key.N8 to Command.CHARACTER,
Key.N9 to Command.CHARACTER,
Key.BACK to Command.BACK,
Key.POUND to Command.SHIFT_MODE,
Key.FN to Command.FN
)),
afterLong = HashMap(mapOf(
Key.SELECT to Command.TRANSCRIBE,
)),
onRepeat = HashMap(mapOf(
Key.BACK to Command.HOME,
Key.STAR to Command.DELETE,
))
)
}
}
}