Skip to content

Commit

Permalink
#1274 move TriggerKeyEntity to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
sds100 committed Oct 3, 2024
1 parent be93e77 commit 6ae4cc9
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.github.sds100.keymapper.ServiceLocator
import io.github.sds100.keymapper.data.entities.ActionEntity
import io.github.sds100.keymapper.data.entities.KeyMapEntity
import io.github.sds100.keymapper.data.entities.TriggerEntity
import io.github.sds100.keymapper.data.entities.TriggerKeyEntity
import kotlinx.coroutines.coroutineScope

/**
Expand Down Expand Up @@ -45,25 +46,25 @@ class SeedDatabaseWorker(
private fun createRandomTrigger(): TriggerEntity {
val keys = sequence {
yield(
TriggerEntity.KeyEntity(
TriggerKeyEntity(
KeyEvent.KEYCODE_CTRL_LEFT,
TriggerEntity.KeyEntity.DEVICE_ID_THIS_DEVICE,
TriggerKeyEntity.DEVICE_ID_THIS_DEVICE,
null,
TriggerEntity.SHORT_PRESS,
),
)
yield(
TriggerEntity.KeyEntity(
TriggerKeyEntity(
KeyEvent.KEYCODE_ALT_LEFT,
TriggerEntity.KeyEntity.DEVICE_ID_ANY_DEVICE,
TriggerKeyEntity.DEVICE_ID_ANY_DEVICE,
null,
TriggerEntity.LONG_PRESS,
),
)
yield(
TriggerEntity.KeyEntity(
TriggerKeyEntity(
KeyEvent.KEYCODE_DEL,
TriggerEntity.KeyEntity.DEVICE_ID_THIS_DEVICE,
TriggerKeyEntity.DEVICE_ID_THIS_DEVICE,
null,
TriggerEntity.SHORT_PRESS,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.google.gson.Gson
import com.google.gson.GsonBuilder
import io.github.sds100.keymapper.data.entities.Extra
import io.github.sds100.keymapper.data.entities.TriggerEntity
import io.github.sds100.keymapper.data.entities.TriggerKeyEntity

/**
* Created by sds100 on 05/09/2018.
Expand All @@ -17,7 +18,7 @@ class TriggerTypeConverter {
fun toTrigger(json: String): TriggerEntity {
val gson = GsonBuilder()
.registerTypeAdapter(TriggerEntity.DESERIALIZER)
.registerTypeAdapter(TriggerEntity.KeyEntity.DESERIALIZER)
.registerTypeAdapter(TriggerKeyEntity.DESERIALIZER)
.registerTypeAdapter(Extra.DESERIALIZER).create()

return gson.fromJson(json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import androidx.annotation.IntDef
import com.github.salomonbrys.kotson.byArray
import com.github.salomonbrys.kotson.byInt
import com.github.salomonbrys.kotson.byNullableInt
import com.github.salomonbrys.kotson.byNullableString
import com.github.salomonbrys.kotson.byString
import com.github.salomonbrys.kotson.jsonDeserializer
import com.google.gson.annotations.SerializedName
import kotlinx.android.parcel.Parcelize
import java.util.UUID

/**
* Created by sds100 on 16/07/2018.
Expand All @@ -19,7 +16,7 @@ import java.util.UUID
@Parcelize
data class TriggerEntity(
@SerializedName(NAME_KEYS)
val keys: List<KeyEntity> = listOf(),
val keys: List<TriggerKeyEntity> = listOf(),

@SerializedName(NAME_EXTRAS)
val extras: List<Extra> = listOf(),
Expand Down Expand Up @@ -64,7 +61,7 @@ data class TriggerEntity(

val DESERIALIZER = jsonDeserializer {
val triggerKeysJsonArray by it.json.byArray(NAME_KEYS)
val keys = it.context.deserialize<List<KeyEntity>>(triggerKeysJsonArray)
val keys = it.context.deserialize<List<TriggerKeyEntity>>(triggerKeysJsonArray)

val extrasJsonArray by it.json.byArray(NAME_EXTRAS)
val extraList = it.context.deserialize<List<Extra>>(extrasJsonArray) ?: listOf()
Expand All @@ -77,65 +74,6 @@ data class TriggerEntity(
}
}

// TODO move to separate file
@Parcelize
data class KeyEntity(
@SerializedName(NAME_KEYCODE)
val keyCode: Int,
@SerializedName(NAME_DEVICE_ID)
val deviceId: String = DEVICE_ID_THIS_DEVICE,

@SerializedName(NAME_DEVICE_NAME)
val deviceName: String? = null,

@ClickType
@SerializedName(NAME_CLICK_TYPE)
val clickType: Int = SHORT_PRESS,

@SerializedName(NAME_FLAGS)
val flags: Int = 0,

@SerializedName(NAME_UID)
val uid: String = UUID.randomUUID().toString(),
) : Parcelable {

companion object {
// DON'T CHANGE THESE. Used for JSON serialization and parsing.
const val NAME_KEYCODE = "keyCode"
const val NAME_DEVICE_ID = "deviceId"
const val NAME_DEVICE_NAME = "deviceName"
const val NAME_CLICK_TYPE = "clickType"
const val NAME_FLAGS = "flags"
const val NAME_UID = "uid"

// IDS! DON'T CHANGE
const val DEVICE_ID_THIS_DEVICE = "io.github.sds100.keymapper.THIS_DEVICE"
const val DEVICE_ID_ANY_DEVICE = "io.github.sds100.keymapper.ANY_DEVICE"

const val FLAG_DO_NOT_CONSUME_KEY_EVENT = 1

val DESERIALIZER = jsonDeserializer {
val keycode by it.json.byInt(NAME_KEYCODE)
val deviceId by it.json.byString(NAME_DEVICE_ID)
val deviceName by it.json.byNullableString(NAME_DEVICE_NAME)
val clickType by it.json.byInt(NAME_CLICK_TYPE)

// nullable because this property was added after backup and restore was released.
val flags by it.json.byNullableInt(NAME_FLAGS)
val uid by it.json.byNullableString(NAME_UID)

KeyEntity(
keycode,
deviceId,
deviceName,
clickType,
flags ?: 0,
uid ?: UUID.randomUUID().toString(),
)
}
}
}

@IntDef(value = [PARALLEL, SEQUENCE, UNDEFINED])
annotation class Mode

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.github.sds100.keymapper.data.entities

import android.os.Parcelable
import com.github.salomonbrys.kotson.byInt
import com.github.salomonbrys.kotson.byNullableInt
import com.github.salomonbrys.kotson.byNullableString
import com.github.salomonbrys.kotson.byString
import com.github.salomonbrys.kotson.jsonDeserializer
import com.google.gson.annotations.SerializedName
import kotlinx.android.parcel.Parcelize
import java.util.UUID

@Parcelize
data class TriggerKeyEntity(
@SerializedName(NAME_KEYCODE)
val keyCode: Int,
@SerializedName(NAME_DEVICE_ID)
val deviceId: String = DEVICE_ID_THIS_DEVICE,

@SerializedName(NAME_DEVICE_NAME)
val deviceName: String? = null,

@TriggerEntity.ClickType
@SerializedName(NAME_CLICK_TYPE)
val clickType: Int = TriggerEntity.SHORT_PRESS,

@SerializedName(NAME_FLAGS)
val flags: Int = 0,

@SerializedName(NAME_UID)
val uid: String = UUID.randomUUID().toString(),
) : Parcelable {

companion object {
// DON'T CHANGE THESE. Used for JSON serialization and parsing.
const val NAME_KEYCODE = "keyCode"
const val NAME_DEVICE_ID = "deviceId"
const val NAME_DEVICE_NAME = "deviceName"
const val NAME_CLICK_TYPE = "clickType"
const val NAME_FLAGS = "flags"
const val NAME_UID = "uid"

// IDS! DON'T CHANGE
const val DEVICE_ID_THIS_DEVICE = "io.github.sds100.keymapper.THIS_DEVICE"
const val DEVICE_ID_ANY_DEVICE = "io.github.sds100.keymapper.ANY_DEVICE"

const val FLAG_DO_NOT_CONSUME_KEY_EVENT = 1

val DESERIALIZER = jsonDeserializer {
val keycode by it.json.byInt(NAME_KEYCODE)
val deviceId by it.json.byString(NAME_DEVICE_ID)
val deviceName by it.json.byNullableString(NAME_DEVICE_NAME)
val clickType by it.json.byInt(NAME_CLICK_TYPE)

// nullable because this property was added after backup and restore was released.
val flags by it.json.byNullableInt(NAME_FLAGS)
val uid by it.json.byNullableString(NAME_UID)

TriggerKeyEntity(
keycode,
deviceId,
deviceName,
clickType,
flags ?: 0,
uid ?: UUID.randomUUID().toString(),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import io.github.sds100.keymapper.data.entities.ActionEntity
import io.github.sds100.keymapper.data.entities.TriggerEntity
import io.github.sds100.keymapper.data.entities.TriggerKeyEntity
import splitties.bitflags.hasFlag
import timber.log.Timber

Expand Down Expand Up @@ -107,7 +107,7 @@ object Migration1To2 {

createTriggerKey2(
it.asInt,
TriggerEntity.KeyEntity.DEVICE_ID_ANY_DEVICE,
TriggerKeyEntity.DEVICE_ID_ANY_DEVICE,
clickType,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.github.salomonbrys.kotson.registerTypeAdapter
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import io.github.sds100.keymapper.data.entities.TriggerEntity
import io.github.sds100.keymapper.data.entities.TriggerKeyEntity
import splitties.bitflags.hasFlag
import splitties.bitflags.minusFlag
import splitties.bitflags.withFlag
Expand Down Expand Up @@ -40,7 +41,7 @@ object Migration6To7 {

val newTriggerKeys = trigger.keys.map {
if (trigger.flags.hasFlag(TRIGGER_FLAG_DONT_OVERRIDE_DEFAULT_ACTION)) {
it.copy(flags = it.flags.withFlag(TriggerEntity.KeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT))
it.copy(flags = it.flags.withFlag(TriggerKeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT))
} else {
it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.github.sds100.keymapper.data.db.dao.KeyMapDao
import io.github.sds100.keymapper.data.entities.ActionEntity
import io.github.sds100.keymapper.data.entities.Extra
import io.github.sds100.keymapper.data.entities.KeyMapEntity
import io.github.sds100.keymapper.data.entities.TriggerEntity
import io.github.sds100.keymapper.data.entities.TriggerKeyEntity
import io.github.sds100.keymapper.mappings.keymaps.KeyMapRepository
import io.github.sds100.keymapper.system.devices.DevicesAdapter
import io.github.sds100.keymapper.util.DefaultDispatcherProvider
Expand Down Expand Up @@ -149,8 +149,8 @@ class RoomKeyMapRepository(
var updateKeyMap = false

val newTriggerKeys = keyMap.trigger.keys.map { triggerKey ->
if (triggerKey.deviceId != TriggerEntity.KeyEntity.DEVICE_ID_THIS_DEVICE ||
triggerKey.deviceId != TriggerEntity.KeyEntity.DEVICE_ID_ANY_DEVICE
if (triggerKey.deviceId != TriggerKeyEntity.DEVICE_ID_THIS_DEVICE ||
triggerKey.deviceId != TriggerKeyEntity.DEVICE_ID_ANY_DEVICE
) {
val deviceDescriptor = triggerKey.deviceId

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.sds100.keymapper.mappings.keymaps.trigger

import io.github.sds100.keymapper.data.entities.TriggerEntity
import io.github.sds100.keymapper.data.entities.TriggerKeyEntity
import io.github.sds100.keymapper.mappings.ClickType
import kotlinx.serialization.Serializable
import splitties.bitflags.hasFlag
Expand Down Expand Up @@ -32,13 +33,13 @@ data class TriggerKey(

object TriggerKeyEntityMapper {
fun fromEntity(
entity: TriggerEntity.KeyEntity,
entity: TriggerKeyEntity,
): TriggerKey = TriggerKey(
uid = entity.uid,
keyCode = entity.keyCode,
device = when (entity.deviceId) {
TriggerEntity.KeyEntity.DEVICE_ID_THIS_DEVICE -> TriggerKeyDevice.Internal
TriggerEntity.KeyEntity.DEVICE_ID_ANY_DEVICE -> TriggerKeyDevice.Any
TriggerKeyEntity.DEVICE_ID_THIS_DEVICE -> TriggerKeyDevice.Internal
TriggerKeyEntity.DEVICE_ID_ANY_DEVICE -> TriggerKeyDevice.Any
else -> TriggerKeyDevice.External(
entity.deviceId,
entity.deviceName ?: "",
Expand All @@ -50,14 +51,14 @@ object TriggerKeyEntityMapper {
TriggerEntity.DOUBLE_PRESS -> ClickType.DOUBLE_PRESS
else -> ClickType.SHORT_PRESS
},
consumeKeyEvent = !entity.flags.hasFlag(TriggerEntity.KeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT),
consumeKeyEvent = !entity.flags.hasFlag(TriggerKeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT),
)

fun toEntity(key: TriggerKey): TriggerEntity.KeyEntity {
fun toEntity(key: TriggerKey): TriggerKeyEntity {
val deviceId = when (key.device) {
TriggerKeyDevice.Any -> TriggerEntity.KeyEntity.DEVICE_ID_ANY_DEVICE
TriggerKeyDevice.Any -> TriggerKeyEntity.DEVICE_ID_ANY_DEVICE
is TriggerKeyDevice.External -> key.device.descriptor
TriggerKeyDevice.Internal -> TriggerEntity.KeyEntity.DEVICE_ID_THIS_DEVICE
TriggerKeyDevice.Internal -> TriggerKeyEntity.DEVICE_ID_THIS_DEVICE
}

val deviceName = if (key.device is TriggerKeyDevice.External) {
Expand All @@ -75,10 +76,10 @@ object TriggerKeyEntityMapper {
var flags = 0

if (!key.consumeKeyEvent) {
flags = flags.withFlag(TriggerEntity.KeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT)
flags = flags.withFlag(TriggerKeyEntity.FLAG_DO_NOT_CONSUME_KEY_EVENT)
}

return TriggerEntity.KeyEntity(
return TriggerKeyEntity(
keyCode = key.keyCode,
deviceId = deviceId,
deviceName = deviceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.sds100.keymapper.data.entities.ActionEntity
import io.github.sds100.keymapper.data.entities.Extra
import io.github.sds100.keymapper.data.entities.KeyMapEntity
import io.github.sds100.keymapper.data.entities.TriggerEntity
import io.github.sds100.keymapper.data.entities.TriggerKeyEntity
import io.github.sds100.keymapper.system.devices.FakeDevicesAdapter
import io.github.sds100.keymapper.system.devices.InputDeviceInfo
import io.github.sds100.keymapper.util.State
Expand Down Expand Up @@ -254,7 +255,7 @@ class KeyMapRepositoryTest {
fun `key map with device name for trigger key, if device for trigger key is connected, do not update trigger key device name`() =
runTest(testDispatcher) {
// GIVEN
val triggerKey = TriggerEntity.KeyEntity(
val triggerKey = TriggerKeyEntity(
keyCode = 1,
deviceId = FAKE_KEYBOARD.descriptor,
deviceName = FAKE_KEYBOARD.name,
Expand All @@ -277,7 +278,7 @@ class KeyMapRepositoryTest {
fun `key map with device name for trigger key, if device for trigger key is disconnected, do not update trigger key device name`() =
runTest(testDispatcher) {
// GIVEN
val triggerKey = TriggerEntity.KeyEntity(
val triggerKey = TriggerKeyEntity(
keyCode = 1,
deviceId = FAKE_KEYBOARD.descriptor,
deviceName = FAKE_KEYBOARD.name,
Expand All @@ -298,7 +299,7 @@ class KeyMapRepositoryTest {
fun `key map with no device name for trigger key, if device for trigger key is connected, update trigger key device name`() =
runTest(testDispatcher) {
// GIVEN
val triggerKey = TriggerEntity.KeyEntity(
val triggerKey = TriggerKeyEntity(
keyCode = 1,
deviceId = FAKE_KEYBOARD.descriptor,
deviceName = "",
Expand Down

0 comments on commit 6ae4cc9

Please sign in to comment.