Skip to content

Commit

Permalink
#1274 name KeyMapTrigger to CustomTrigger
Browse files Browse the repository at this point in the history
  • Loading branch information
sds100 committed Oct 3, 2024
1 parent e1c1afc commit be93e77
Show file tree
Hide file tree
Showing 24 changed files with 106 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import java.util.UUID
* Created by sds100 on 16/07/2018.
*/

/**
* @property [keys] The key codes which will trigger the action
*/
@Parcelize
data class TriggerEntity(
@SerializedName(NAME_KEYS)
Expand Down Expand Up @@ -80,6 +77,7 @@ data class TriggerEntity(
}
}

// TODO move to separate file
@Parcelize
data class KeyEntity(
@SerializedName(NAME_KEYCODE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ object Migration11To12 {
val cursor = database.query(keyMapListQuery)

val keyMapIdColumnIndex = cursor.getColumnIndex("id")
val keyMapTriggerColumnIndex = cursor.getColumnIndex("trigger")
val triggerColumnIndex = cursor.getColumnIndex("trigger")
val keyMapActionListColumnIndex = cursor.getColumnIndex("action_list")

while (cursor.moveToNext()) {
val id = cursor.getLong(keyMapIdColumnIndex)
val triggerJson = cursor.getString(keyMapTriggerColumnIndex)
val triggerJson = cursor.getString(triggerColumnIndex)
val triggerJsonObject = parser.parse(triggerJson).asJsonObject

val actionListJson = cursor.getString(keyMapActionListColumnIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.github.sds100.keymapper.data.repositories.PreferenceRepository
import io.github.sds100.keymapper.mappings.BaseConfigMappingUseCase
import io.github.sds100.keymapper.mappings.ClickType
import io.github.sds100.keymapper.mappings.ConfigMappingUseCase
import io.github.sds100.keymapper.mappings.keymaps.trigger.KeyMapTrigger
import io.github.sds100.keymapper.mappings.keymaps.trigger.CustomTrigger
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerKey
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerKeyDevice
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerMode
Expand Down Expand Up @@ -435,7 +435,7 @@ class ConfigKeyMapUseCaseImpl(
}
}

private fun editTrigger(block: (trigger: KeyMapTrigger) -> KeyMapTrigger) {
private fun editTrigger(block: (trigger: CustomTrigger) -> CustomTrigger) {
editKeyMap { keyMap ->
val newTrigger = block(keyMap.trigger)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ConfigKeyMapViewModel(
resourceProvider,
)

val configTriggerViewModel = ConfigKeyMapTriggerViewModel(
val configTriggerViewModel = ConfigTriggerViewModel(
viewModelScope,
onboarding,
config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import kotlinx.coroutines.withContext
/**
* Created by sds100 on 29/11/20.
*/
class ConfigKeyMapTriggerOptionsViewModel(
class ConfigTriggerOptionsViewModel(
private val coroutineScope: CoroutineScope,
private val config: ConfigKeyMapUseCase,
private val createKeyMapShortcut: CreateKeyMapShortcutUseCase,
Expand Down Expand Up @@ -109,7 +109,7 @@ class ConfigKeyMapTriggerOptionsViewModel(

result.onFailure { error ->
val snackBar = PopupUi.SnackBar(
message = error.getFullMessage(this@ConfigKeyMapTriggerOptionsViewModel),
message = error.getFullMessage(this@ConfigTriggerOptionsViewModel),
)

showPopup("create_shortcut_result", snackBar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package io.github.sds100.keymapper.mappings.keymaps

import android.os.Build
import android.view.KeyEvent
import androidx.compose.runtime.getValue
import io.github.sds100.keymapper.R
import io.github.sds100.keymapper.mappings.ClickType
import io.github.sds100.keymapper.mappings.keymaps.trigger.KeyMapTrigger
import io.github.sds100.keymapper.mappings.keymaps.trigger.KeyMapTriggerError
import io.github.sds100.keymapper.mappings.keymaps.trigger.CustomTrigger
import io.github.sds100.keymapper.mappings.keymaps.trigger.CustomTriggerError
import io.github.sds100.keymapper.mappings.keymaps.trigger.RecordTriggerState
import io.github.sds100.keymapper.mappings.keymaps.trigger.RecordTriggerUseCase
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerKeyDevice
Expand Down Expand Up @@ -57,7 +56,7 @@ import kotlinx.coroutines.runBlocking
* Created by sds100 on 24/11/20.
*/

class ConfigKeyMapTriggerViewModel(
class ConfigTriggerViewModel(
private val coroutineScope: CoroutineScope,
private val onboarding: OnboardingUseCase,
private val config: ConfigKeyMapUseCase,
Expand All @@ -69,7 +68,7 @@ class ConfigKeyMapTriggerViewModel(
PopupViewModel by PopupViewModelImpl(),
NavigationViewModel by NavigationViewModelImpl() {

val optionsViewModel = ConfigKeyMapTriggerOptionsViewModel(
val optionsViewModel = ConfigTriggerOptionsViewModel(
coroutineScope,
config,
createKeyMapShortcut,
Expand Down Expand Up @@ -269,20 +268,20 @@ class ConfigKeyMapTriggerViewModel(
}
}

private fun buildTriggerErrorListItems(triggerErrors: List<KeyMapTriggerError>) =
private fun buildTriggerErrorListItems(triggerErrors: List<CustomTriggerError>) =
triggerErrors.map { error ->
when (error) {
KeyMapTriggerError.DND_ACCESS_DENIED -> TextListItem.Error(
CustomTriggerError.DND_ACCESS_DENIED -> TextListItem.Error(
id = error.toString(),
text = getString(R.string.trigger_error_dnd_access_denied),
)

KeyMapTriggerError.SCREEN_OFF_ROOT_DENIED -> TextListItem.Error(
CustomTriggerError.SCREEN_OFF_ROOT_DENIED -> TextListItem.Error(
id = error.toString(),
text = getString(R.string.trigger_error_screen_off_root_permission_denied),
)

KeyMapTriggerError.CANT_DETECT_IN_PHONE_CALL -> TextListItem.Error(
CustomTriggerError.CANT_DETECT_IN_PHONE_CALL -> TextListItem.Error(
id = error.toString(),
text = getString(R.string.trigger_error_cant_detect_in_phone_call),
)
Expand Down Expand Up @@ -367,17 +366,17 @@ class ConfigKeyMapTriggerViewModel(

if (result is Error.AccessibilityServiceDisabled) {
ViewModelHelper.handleAccessibilityServiceStoppedSnackBar(
resourceProvider = this@ConfigKeyMapTriggerViewModel,
popupViewModel = this@ConfigKeyMapTriggerViewModel,
resourceProvider = this@ConfigTriggerViewModel,
popupViewModel = this@ConfigTriggerViewModel,
startService = displayKeyMap::startAccessibilityService,
message = R.string.dialog_message_enable_accessibility_service_to_record_trigger,
)
}

if (result is Error.AccessibilityServiceCrashed) {
ViewModelHelper.handleAccessibilityServiceCrashedSnackBar(
resourceProvider = this@ConfigKeyMapTriggerViewModel,
popupViewModel = this@ConfigKeyMapTriggerViewModel,
resourceProvider = this@ConfigTriggerViewModel,
popupViewModel = this@ConfigTriggerViewModel,
restartService = displayKeyMap::restartAccessibilityService,
message = R.string.dialog_message_restart_accessibility_service_to_record_trigger,
)
Expand All @@ -393,30 +392,30 @@ class ConfigKeyMapTriggerViewModel(

fun onTriggerErrorClick(listItemId: String) {
coroutineScope.launch {
when (KeyMapTriggerError.valueOf(listItemId)) {
KeyMapTriggerError.DND_ACCESS_DENIED -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
when (CustomTriggerError.valueOf(listItemId)) {
CustomTriggerError.DND_ACCESS_DENIED -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ViewModelHelper.showDialogExplainingDndAccessBeingUnavailable(
resourceProvider = this@ConfigKeyMapTriggerViewModel,
popupViewModel = this@ConfigKeyMapTriggerViewModel,
resourceProvider = this@ConfigTriggerViewModel,
popupViewModel = this@ConfigTriggerViewModel,
neverShowDndTriggerErrorAgain = { displayKeyMap.neverShowDndTriggerErrorAgain() },
fixError = { displayKeyMap.fixError(it) },
)
}

KeyMapTriggerError.SCREEN_OFF_ROOT_DENIED -> {
CustomTriggerError.SCREEN_OFF_ROOT_DENIED -> {
val error = Error.PermissionDenied(Permission.ROOT)
displayKeyMap.fixError(error)
}

KeyMapTriggerError.CANT_DETECT_IN_PHONE_CALL -> {
CustomTriggerError.CANT_DETECT_IN_PHONE_CALL -> {
displayKeyMap.fixError(Error.CantDetectKeyEventsInPhoneCall)
}
}
}
}

private fun createListItems(
trigger: KeyMapTrigger,
trigger: CustomTrigger,
showDeviceDescriptors: Boolean,
): List<TriggerKeyListItem> =
trigger.keys.mapIndexed { index, key ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.view.KeyEvent
import io.github.sds100.keymapper.data.Keys
import io.github.sds100.keymapper.data.repositories.PreferenceRepository
import io.github.sds100.keymapper.mappings.DisplaySimpleMappingUseCase
import io.github.sds100.keymapper.mappings.keymaps.trigger.KeyMapTriggerError
import io.github.sds100.keymapper.mappings.keymaps.trigger.CustomTriggerError
import io.github.sds100.keymapper.system.inputmethod.InputMethodAdapter
import io.github.sds100.keymapper.system.inputmethod.KeyMapperImeHelper
import io.github.sds100.keymapper.system.permissions.Permission
Expand Down Expand Up @@ -40,28 +40,28 @@ class DisplayKeyMapUseCaseImpl(
preferenceRepository.get(Keys.neverShowDndError).map { }.drop(1),
)

override suspend fun getTriggerErrors(keyMap: KeyMap): List<KeyMapTriggerError> {
override suspend fun getTriggerErrors(keyMap: KeyMap): List<CustomTriggerError> {
val trigger = keyMap.trigger
val errors = mutableListOf<KeyMapTriggerError>()
val errors = mutableListOf<CustomTriggerError>()

// can only detect volume button presses during a phone call with an input method service
if (!keyMapperImeHelper.isCompatibleImeChosen() && keyMap.requiresImeKeyEventForwarding()) {
errors.add(KeyMapTriggerError.CANT_DETECT_IN_PHONE_CALL)
errors.add(CustomTriggerError.CANT_DETECT_IN_PHONE_CALL)
}

if (trigger.keys.any { it.keyCode in keysThatRequireDndAccess }) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
!permissionAdapter.isGranted(Permission.ACCESS_NOTIFICATION_POLICY)
) {
errors.add(KeyMapTriggerError.DND_ACCESS_DENIED)
errors.add(CustomTriggerError.DND_ACCESS_DENIED)
}
}

if (trigger.screenOffTrigger &&
!permissionAdapter.isGranted(Permission.ROOT) &&
trigger.isDetectingWhenScreenOffAllowed()
) {
errors.add(KeyMapTriggerError.SCREEN_OFF_ROOT_DENIED)
errors.add(CustomTriggerError.SCREEN_OFF_ROOT_DENIED)
}

return errors
Expand All @@ -70,5 +70,5 @@ class DisplayKeyMapUseCaseImpl(

interface DisplayKeyMapUseCase : DisplaySimpleMappingUseCase {
val invalidateTriggerErrors: Flow<Unit>
suspend fun getTriggerErrors(keyMap: KeyMap): List<KeyMapTriggerError>
suspend fun getTriggerErrors(keyMap: KeyMap): List<CustomTriggerError>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import io.github.sds100.keymapper.constraints.ConstraintState
import io.github.sds100.keymapper.data.entities.KeyMapEntity
import io.github.sds100.keymapper.mappings.Mapping
import io.github.sds100.keymapper.mappings.keymaps.detection.KeyMapController
import io.github.sds100.keymapper.mappings.keymaps.trigger.KeyMapTrigger
import io.github.sds100.keymapper.mappings.keymaps.trigger.KeymapTriggerEntityMapper
import io.github.sds100.keymapper.mappings.keymaps.trigger.CustomTrigger
import io.github.sds100.keymapper.mappings.keymaps.trigger.CustomTriggerEntityMapper
import kotlinx.serialization.Serializable
import java.util.UUID

Expand All @@ -22,7 +22,7 @@ import java.util.UUID
data class KeyMap(
val dbId: Long? = null,
val uid: String = UUID.randomUUID().toString(),
val trigger: KeyMapTrigger = KeyMapTrigger(),
val trigger: CustomTrigger = CustomTrigger(),
override val actionList: List<KeyMapAction> = emptyList(),
override val constraintState: ConstraintState = ConstraintState(),
override val isEnabled: Boolean = true,
Expand Down Expand Up @@ -81,7 +81,7 @@ object KeyMapEntityMapper {
return KeyMap(
dbId = entity.id,
uid = entity.uid,
trigger = KeymapTriggerEntityMapper.fromEntity(entity.trigger),
trigger = CustomTriggerEntityMapper.fromEntity(entity.trigger),
actionList = actionList,
constraintState = ConstraintState(constraintList, constraintMode),
isEnabled = entity.isEnabled,
Expand All @@ -93,7 +93,7 @@ object KeyMapEntityMapper {

return KeyMapEntity(
id = dbId,
trigger = KeymapTriggerEntityMapper.toEntity(keyMap.trigger),
trigger = CustomTriggerEntityMapper.toEntity(keyMap.trigger),
actionList = actionEntityList,
constraintList = keyMap.constraintState.constraints.map {
ConstraintEntityMapper.toEntity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package io.github.sds100.keymapper.mappings.keymaps
import io.github.sds100.keymapper.R
import io.github.sds100.keymapper.mappings.BaseMappingListItemCreator
import io.github.sds100.keymapper.mappings.ClickType
import io.github.sds100.keymapper.mappings.keymaps.trigger.KeyMapTrigger
import io.github.sds100.keymapper.mappings.keymaps.trigger.KeyMapTriggerError
import io.github.sds100.keymapper.mappings.keymaps.trigger.CustomTrigger
import io.github.sds100.keymapper.mappings.keymaps.trigger.CustomTriggerError
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerKeyDevice
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerMode
import io.github.sds100.keymapper.system.devices.InputDeviceUtils
Expand Down Expand Up @@ -111,21 +111,21 @@ class KeyMapListItemCreator(

val triggerErrorChips = triggerErrors.map {
when (it) {
KeyMapTriggerError.DND_ACCESS_DENIED ->
CustomTriggerError.DND_ACCESS_DENIED ->
ChipUi.Error(
id = KeyMapTriggerError.DND_ACCESS_DENIED.toString(),
id = CustomTriggerError.DND_ACCESS_DENIED.toString(),
text = getString(R.string.trigger_error_dnd_access_denied_short),
error = Error.PermissionDenied(Permission.ACCESS_NOTIFICATION_POLICY),
)

KeyMapTriggerError.SCREEN_OFF_ROOT_DENIED -> ChipUi.Error(
id = KeyMapTriggerError.SCREEN_OFF_ROOT_DENIED.toString(),
CustomTriggerError.SCREEN_OFF_ROOT_DENIED -> ChipUi.Error(
id = CustomTriggerError.SCREEN_OFF_ROOT_DENIED.toString(),
text = getString(R.string.trigger_error_screen_off_root_permission_denied_short),
error = Error.PermissionDenied(Permission.ROOT),
)

KeyMapTriggerError.CANT_DETECT_IN_PHONE_CALL -> ChipUi.Error(
id = KeyMapTriggerError.SCREEN_OFF_ROOT_DENIED.toString(),
CustomTriggerError.CANT_DETECT_IN_PHONE_CALL -> ChipUi.Error(
id = CustomTriggerError.SCREEN_OFF_ROOT_DENIED.toString(),
text = getString(R.string.trigger_error_cant_detect_in_phone_call),
error = Error.CantDetectKeyEventsInPhoneCall,
)
Expand All @@ -143,7 +143,7 @@ class KeyMapListItemCreator(
)
}

private fun getTriggerOptionLabels(trigger: KeyMapTrigger): List<String> {
private fun getTriggerOptionLabels(trigger: CustomTrigger): List<String> {
val labels = mutableListOf<String>()

if (trigger.isVibrateAllowed() && trigger.vibrate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import io.github.sds100.keymapper.mappings.keymaps.trigger.RecordTriggerState
@Composable
fun RecordTriggerButtonRow(
modifier: Modifier = Modifier,
viewModel: ConfigKeyMapTriggerViewModel,
viewModel: ConfigTriggerViewModel,
) {
val recordTriggerState by viewModel.recordTriggerState.collectAsState()
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
Expand Down
Loading

0 comments on commit be93e77

Please sign in to comment.