Skip to content

Commit

Permalink
#1274 show dialog to select which assistant type when clicking more o…
Browse files Browse the repository at this point in the history
…ptions on an assistant trigger key
  • Loading branch information
sds100 committed Oct 13, 2024
1 parent 83e3d69 commit 9b7c0a7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ class ConfigKeyMapUseCaseImpl(
)
}

override fun getTriggerKey(uid: String): TriggerKey? {
return mapping.value.dataOrNull()?.trigger?.keys?.find { it.uid == uid }
}

override fun setParallelTriggerMode() = editTrigger { trigger ->
if (trigger.mode is TriggerMode.Parallel) {
return@editTrigger trigger
Expand Down Expand Up @@ -249,27 +253,37 @@ class ConfigKeyMapUseCaseImpl(
}

override fun setTriggerKeyClickType(keyUid: String, clickType: ClickType) {
editTriggerKey(keyUid) {
it.setClickType(clickType = clickType)
editTriggerKey(keyUid) { key ->
key.setClickType(clickType = clickType)
}
}

override fun setTriggerKeyDevice(keyUid: String, device: TriggerKeyDevice) {
editTriggerKey(keyUid) {
if (it is KeyCodeTriggerKey) {
it.copy(device = device)
editTriggerKey(keyUid) { key ->
if (key is KeyCodeTriggerKey) {
key.copy(device = device)
} else {
it
key
}
}
}

override fun setTriggerKeyConsumeKeyEvent(keyUid: String, consumeKeyEvent: Boolean) {
editTriggerKey(keyUid) {
if (it is KeyCodeTriggerKey) {
it.copy(consumeEvent = consumeKeyEvent)
editTriggerKey(keyUid) { key ->
if (key is KeyCodeTriggerKey) {
key.copy(consumeEvent = consumeKeyEvent)
} else {
key
}
}
}

override fun setAssistantTriggerKeyType(keyUid: String, type: AssistantTriggerType) {
editTriggerKey(keyUid) { key ->
if (key is AssistantTriggerKey) {
key.copy(type = type)
} else {
it
key
}
}
}
Expand Down Expand Up @@ -505,6 +519,7 @@ interface ConfigKeyMapUseCase : ConfigMappingUseCase<KeyMapAction, KeyMap> {
fun addKeyCodeTriggerKey(keyCode: Int, device: TriggerKeyDevice)
fun addAssistantTriggerKey(type: AssistantTriggerType)
fun removeTriggerKey(uid: String)
fun getTriggerKey(uid: String): TriggerKey?
fun moveTriggerKey(fromIndex: Int, toIndex: Int)

fun restoreState(keyMap: KeyMap)
Expand All @@ -522,6 +537,7 @@ interface ConfigKeyMapUseCase : ConfigMappingUseCase<KeyMapAction, KeyMap> {
fun setTriggerKeyClickType(keyUid: String, clickType: ClickType)
fun setTriggerKeyDevice(keyUid: String, device: TriggerKeyDevice)
fun setTriggerKeyConsumeKeyEvent(keyUid: String, consumeKeyEvent: Boolean)
fun setAssistantTriggerKeyType(keyUid: String, type: AssistantTriggerType)

fun setVibrateEnabled(enabled: Boolean)
fun setVibrationDuration(duration: Defaultable<Int>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,48 +316,52 @@ abstract class BaseConfigTriggerViewModel(
fun onRemoveKeyClick(uid: String) = config.removeTriggerKey(uid)
fun onMoveTriggerKey(fromIndex: Int, toIndex: Int) = config.moveTriggerKey(fromIndex, toIndex)

fun onTriggerKeyOptionsClick(id: String) {
open fun onTriggerKeyOptionsClick(id: String) {
runBlocking { _openEditOptions.emit(id) }
}

fun onChooseDeviceClick(keyUid: String) {
coroutineScope.launch {
val idAny = "any"
val idInternal = "this_device"
val devices = config.getAvailableTriggerKeyDevices()
val showDeviceDescriptors = displayKeyMap.showDeviceDescriptors.first()

val listItems = devices.map { device: TriggerKeyDevice ->
when (device) {
TriggerKeyDevice.Any -> idAny to getString(R.string.any_device)
TriggerKeyDevice.Internal -> idInternal to getString(R.string.this_device)
is TriggerKeyDevice.External -> {
if (showDeviceDescriptors) {
val name = InputDeviceUtils.appendDeviceDescriptorToName(
device.descriptor,
device.name,
)
device.descriptor to name
} else {
device.descriptor to device.name
}
chooseDeviceForKeyCodeTriggerKey(keyUid)
}
}

private suspend fun chooseDeviceForKeyCodeTriggerKey(keyUid: String) {
val idAny = "any"
val idInternal = "this_device"
val devices = config.getAvailableTriggerKeyDevices()
val showDeviceDescriptors = displayKeyMap.showDeviceDescriptors.first()

val listItems = devices.map { device: TriggerKeyDevice ->
when (device) {
TriggerKeyDevice.Any -> idAny to getString(R.string.any_device)
TriggerKeyDevice.Internal -> idInternal to getString(R.string.this_device)
is TriggerKeyDevice.External -> {
if (showDeviceDescriptors) {
val name = InputDeviceUtils.appendDeviceDescriptorToName(
device.descriptor,
device.name,
)
device.descriptor to name
} else {
device.descriptor to device.name
}
}
}
}

val triggerKeyDeviceId = showPopup(
"pick_trigger_key_device",
PopupUi.SingleChoice(listItems),
) ?: return@launch

val selectedTriggerKeyDevice = when (triggerKeyDeviceId) {
idAny -> TriggerKeyDevice.Any
idInternal -> TriggerKeyDevice.Internal
else -> devices.single { it is TriggerKeyDevice.External && it.descriptor == triggerKeyDeviceId }
}
val triggerKeyDeviceId = showPopup(
"pick_trigger_key_device",
PopupUi.SingleChoice(listItems),
) ?: return

config.setTriggerKeyDevice(keyUid, selectedTriggerKeyDevice)
val selectedTriggerKeyDevice = when (triggerKeyDeviceId) {
idAny -> TriggerKeyDevice.Any
idInternal -> TriggerKeyDevice.Internal
else -> devices.single { it is TriggerKeyDevice.External && it.descriptor == triggerKeyDeviceId }
}

config.setTriggerKeyDevice(keyUid, selectedTriggerKeyDevice)
}

fun onRecordTriggerButtonClick() {
Expand Down Expand Up @@ -443,6 +447,7 @@ abstract class BaseConfigTriggerViewModel(
extraInfo = getTriggerKeyExtraInfo(key, showDeviceDescriptors),
linkType = linkDrawable,
isDragDropEnabled = trigger.keys.size > 1,
isChooseDeviceButtonVisible = key is KeyCodeTriggerKey,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ data class TriggerKeyListItem(
val linkType: TriggerKeyLinkType,

val isDragDropEnabled: Boolean,

/**
* The button for choosing the device is only visible for key code trigger keys.
*/
val isChooseDeviceButtonVisible: Boolean,
)
26 changes: 14 additions & 12 deletions app/src/main/res/layout/list_item_trigger_key.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<data>

<import type="android.view.View" />

<import type="io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerKeyLinkType" />

<variable
Expand Down Expand Up @@ -35,8 +36,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:tint="?attr/colorOnSurface"
android:src="@{model.linkType == TriggerKeyLinkType.ARROW ? @drawable/ic_baseline_arrow_downward_24 : @drawable/ic_baseline_add_24}"
android:tint="?attr/colorOnSurface"
android:visibility="@{model.linkType == TriggerKeyLinkType.HIDDEN ? View.GONE : View.VISIBLE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down Expand Up @@ -73,7 +74,7 @@
android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@+id/buttonDevice"
app:layout_constraintStart_toEndOf="@id/buttonDrag"
app:layout_constraintTop_toTopOf="@+id/buttonDevice">
app:layout_constraintTop_toTopOf="@+id/buttonRemove">

<TextView
android:id="@+id/textViewKeyName"
Expand Down Expand Up @@ -120,28 +121,29 @@
app:srcCompat="@drawable/ic_baseline_drag_handle_24" />

<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/buttonEdit"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:id="@+id/buttonDevice"
style="@style/Widget.Material3.Button.TextButton"
android:layout_width="36dp"
android:layout_height="0dp"
android:onClick="@{onMoreClick}"
android:onClick="@{onDeviceClick}"
android:tint="?attr/colorOnSurface"
android:visibility="@{model.chooseDeviceButtonVisible ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="@+id/buttonRemove"
app:layout_constraintEnd_toStartOf="@+id/buttonRemove"
app:layout_constraintEnd_toStartOf="@+id/buttonEdit"
app:layout_constraintTop_toTopOf="@id/buttonRemove"
app:srcCompat="@drawable/ic_outline_more_vert_24" />
app:srcCompat="@drawable/ic_baseline_devices_other_24" />

<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/buttonDevice"
style="@style/Widget.Material3.Button.TextButton"
android:id="@+id/buttonEdit"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="36dp"
android:layout_height="0dp"
android:onClick="@{onDeviceClick}"
android:onClick="@{onMoreClick}"
android:tint="?attr/colorOnSurface"
app:layout_constraintBottom_toBottomOf="@+id/buttonRemove"
app:layout_constraintEnd_toStartOf="@+id/buttonEdit"
app:layout_constraintEnd_toStartOf="@+id/buttonRemove"
app:layout_constraintTop_toTopOf="@id/buttonRemove"
app:srcCompat="@drawable/ic_baseline_devices_other_24" />
app:srcCompat="@drawable/ic_outline_more_vert_24" />

<io.github.sds100.keymapper.util.ui.SquareImageButton
android:id="@+id/buttonRemove"
Expand Down

0 comments on commit 9b7c0a7

Please sign in to comment.